Page 1 of 1
simple collision detection
Posted: Fri Mar 13, 2009 6:06 pm
by ChebbyShabby
hi. i'm trying to detect whether a collision has occured from the left or right of an object (both objects are 16x16 in dimension). i have the following code:
Code: Select all
If (*me\y + 16) > *box\y And *me\y < (*box\y + 16) And (*me\x + 15) > *box\x And (*me\x + 15) < (*box\x + 16)
*me\x = *box\x - 16
EndIf
it doesn't seem to work, however. does anyone have a better way of doing this?
Posted: Fri Mar 13, 2009 6:17 pm
by Kaeru Gaman
http://www.purebasic.fr/german/viewtopi ... 363#227363
over the whole thread there are several collision algorithms.
just ignore the German text, the codes are more or less self-explaning.
Posted: Fri Mar 13, 2009 7:44 pm
by ChebbyShabby
thanks kaeru. i used your function directly and put it in my procedure. for some reason, all it does it empty out my *box pointer.
Code: Select all
Procedure CheckCollisions(*me.Player, *box.Point, *sys.Options)
;*me.Player= AllocateMemory(SizeOf(Player))
;*box.Point= AllocateMemory(SizeOf(Point))
;*sys.Options= AllocateMemory(SizeOf(Options))
If SectorCollision(*me\x, *me\y, 16, 16, *box\x, *box\y, 16, 16) = 1
*me\y = *box\y - 16
*me\Jumps = *sys\Jumps
*me\JumpTimer = *sys\JumpTime
*me\v = 0
EndIf
If SectorCollision(*me\x, *me\y, 16, 16, *box\x, *box\y, 16, 16) = 3 ;Collision with BOTTOM of platform
*me\v = 0
*me\y = *box\y + 16
EndIf
;If (*box\x - 16) < *me\x And *me\x < (*box\x + 16) And (*box\y - 16) < *me\y And *me\y < (*box\y + 16) ;Collision with RIGHT of platform
If SectorCollision(*me\x, *me\y, 16, 16, *box\x, *box\y, 16, 16) = 2
*me\x = *box\x - 16
EndIf
If SectorCollision(*me\x, *me\y, 16, 16, *box\x, *box\y, 16, 16) = 4 ;Collision with LEFT of platform
*me\x = *box\x + plWidth
End
EndIf
EndProcedure