yet another collision problem...

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

yet another collision problem...

Post by ChebbyShabby »

so far this code can load levels and move a sprite horizontally and make it jump vertically. however, everytime the player collides with the platform in any other side other than the top of it, the player moves right on top of every other sprite; i know this is due to continuously positioning the player on top of every platform, but can anyone provide a quick solution for this?

here's my code: http://www.MailFreeOnline.com/uploader/E7F55D5D.zip
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

when you do a coordinates-check, you can check only the feet of the
player and only the inner area of the top of the platform for collision...

lets say, player is 32x32 and platform is 120x20.
normal sprite collision will return every touch as #true.

but if you check the lower 32x8 area of the player,
and the top-center 100x10 area of the platform,
this could solve your problem.

collisions -> http://www.purebasic.fr/german/viewtopi ... collisions
oh... and have a nice day.
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post by ChebbyShabby »

that was the missing piece of the puzzle :D . thanks a lot.

(unfortunately, however, i couldn't use the link you gave me since i don't understand german/dutch)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

its's german, but the procedure are in PB... ;)

perhaps i will translate the short info in the first posting into english, later.
but you only need the StdCall-Procedure

Code: Select all

Procedure P_StdColl(x1,y1,a1,b1,x2,y2,a2,b2) 
  Coll = #False 
  
  If x1 < x2 
    dx = x2 - (x1 + a1) 
  Else 
    dx = x1 - (x2 + a2) 
  EndIf 
  If y1 < y2 
    dy = y2 - (y1 + b1) 
  Else 
    dy = y1 - (y2 + b2) 
  EndIf 
  
  If dx < 0 And dy < 0 
    Coll = #True 
  EndIf 
  
  ProcedureReturn Coll 

EndProcedure 
x/y is the position, a/b the width/heigth of the two rectangles to test.

the other procedures are
- Bounding-Box Collision working with a center of the colliding boxes,
- Bounding-Circle Collision, working with center and radius

the macros are just translations of the procedures if one wants to work with macros rather than procs.

at the end of the topic there is a Circle-Box-Collision procedure.

PS:
the call with the exaple values in the post before would be

Code: Select all

P_StdColl(PlayerX,PlayerY+24,32,8,PlatformX+10,PlatformY,100,10)
oh... and have a nice day.
ChebbyShabby
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Jun 26, 2006 10:47 am

Post by ChebbyShabby »

heh. a slightly more simple example i made:

Code: Select all

Procedure.b UpperCollision(SPRITE1, x, y, SPRITE2, xX, yY)
  If SpriteCollision(SPRITE1, x, y, SPRITE2, xX, yY)
  If y + SpriteHeight(SPRITE1) => yY: ProcedureReturn 1: EndIf
  EndIf
EndProcedure
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

shorter code does'n mean it's more simpel....
in fact, my code IS extremely simpel.
and way far more universal. it also works with drawn objekts or even non-existing objects.

you are using two extra function-calls. in my eyes its redundant.
its are pure mathematical problem, so it should be solved mathematically.
oh... and have a nice day.
Post Reply