Author Topic: Collisions  (Read 2308 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Collisions
« on: 2011-Nov-03 »
Hi,

I have a problem with my collision detections...

I have code (not at hand right now...) written that looks for box collision item against an array of items ... by iterating through screen section in blocks of 16 pxls

When it finds the item the collision should revert back to pre position and then validate that the 32 pixels of either side of the 32 px box is collision free before continuing....

Hope i made myself clear...

I expect that using array objects is the only collision method and that useasbmp and current pixel colouring cannot be checked to provide an alternative?

Checking the colour of pixels as test would be a nice way based on the useasbmp so that 100s of arrays do not need to be examined...

G.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: Collisions
« Reply #1 on: 2011-Nov-03 »
It is very difficult to explain code. I'm sorry but I did not understand your situation. However, here are some general ideas about collision detection, just in case they are useful:-

There are commands for converting or loading sprites to a memory array: SPRITE2MEM & LOADSPRITEMEM. That may be useful to you. Checking an array is much faster than GETPIXEL and is more reliable too.

Using the BOXCOLL command on array objects is fast. If you need more precision an alternative is the SPRCOLL & ANINCOLL command. This checks if the pixels of two sprites or animations overlap.


Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Collisions
« Reply #2 on: 2011-Nov-03 »
Hi,

thanks, in that case i shall post a bit of code...
What I am trying to do place an object next to a body of objects providing the 32x32 sections on either side of the object are free from other objects...
I think this is simple enough but am getting my approach wrong...

Code: (glbasic) [Select]
LOADSPRITE "castle1.png", 11
result% = 0


FOR f=1 TO 512 STEP 16

FOR n = 1 TO 800 STEP 16

//FOREACH test IN waters[]
FOR i = 0 TO BOUNDS(waters[],0)-1


// IF BOXCOLL (test.wx, test.wy,test.width,32, n,f,32,32)
IF BOXCOLL (waters[i].wx, waters[i].wy,waters[i].width,32, n,f,32,32)


// TEST SIDE SECTIONS
FOREACH leftside IN waters[]

IF BOXCOLL (leftside.wx, leftside.wy,leftside.width,32, n-32,f-16,32,32)=FALSE AND BOXCOLL (leftside.wx, leftside.wy,leftside.width,32, n+64,f-16,32,32)=FALSE



result = 1

ELSE
result = 0
ENDIF

NEXT

IF result = 1

DRAWSPRITE 11, n, f-16


GOTO scr1

ENDIF

ENDIF

NEXT

NEXT

NEXT
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Collisions
« Reply #3 on: 2011-Nov-04 »
Bump!
Next help!
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Collisions
« Reply #4 on: 2011-Nov-04 »
Usually you have a map of your blocks:
LOCAL map%[]
DIM map%[100][100] // playfield

then you put blocks in:
map[0][0] = 1 // stone
map[0][1] = 2 // water ...

and check with:
FOR x = 0 TO BOUNDS(map[], 0)-1
   FOR y = 0 TO BOUNDS(map[], 1)-1
      IF x>0 AND map[x-1][y] = 2 and map
  • [y]=2 // is water, and left is water ....




Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1810
    • View Profile
Re: Collisions
« Reply #5 on: 2011-Nov-04 »
Thanks Kitty,

But I am still a bit confused because...
I kinda do that with the current array except the block are multiples of the 16 x 16 sizes.

So a block could be 64 x 32, or 128 x 32 etc...

I then step through the screen in 16 x 16 blocks
which is like the [100][100] as x,y are divided by 16px simulating the screen as a 16x16 grided area.

When I collide I find that I need to loop through the whole array once again to test against the position before the collision and to the left and right of the 32x32 block (2 x 16px blocks)


I will try to explain in a diagram

B = My Sprite Block 32 x 32
W = Water sections in arrays with height of 32 but width varies in mulples of 16

B moves down 16px then accross from far left in blocks of 16 until it collides

START:
                             B

                     WWW
                WWWWWWWWWWWWWWWWW

 COLLISION DETECTED:           

                     BWWW
                WWWWWWWWWWWWWWWWW

 COLLISION ITEM IS MOVED UP AND PLACED IN POSITION:           
                      B
                     WWW
                WWWWWWWWWWWWWWWWW

HOWEVER THE IDEAL POSITION IN ABOVE IS:

                     
                     WWW     B
                WWWWWWWWWWWWWWWWW

This is due to having adequate water below and space above in equal measure...
So the collision is reversed and areas around need to be checked for collisions and empty squares...

Its finding the solution to this problem that I am struggling with...

 :noggin:
G

« Last Edit: 2011-Nov-04 by MrPlow »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs