simple collision detection

Advanced game related topics
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

simple collision detection

Post 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?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post 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
Post Reply