[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] My take on platform collision detection



Hi, I have mucked around quite a bit with making various playform games

First off, the method of collision detection where you are always checking if you overlap a platform is faulty-
Because when you are moving fast eg. falling, you can move many pixels in one frame, meaning that in one frame you are above a platform and the next you are below it,  When I was making my first platform I had this problem, so If I was falling from a height the guy could build up enough speed to fall through the floor.


You can work around this problem by capping the maximum moving speed, but it can look like you are floating when you are falling
and still leaves room for error (jumping on to a moving platform) BTW all console platform games I have seen cap the fall speed and some even the jump speed.

The Answer?... well an answer is to check weather you will pass through any platforms between now and where I am going. In a bit more detain...

If myRect.bottom is =< platformRect.top and\ # I am above the platform
  myRect.bottom + myXmovement > platformRect.top and\ # I will be below the platform
  myRect.left < platformRect.right and myRect.right > platformRect.left: # We are horizontally over the playform
  
  # You have collided with this platfrom so...
  pyRect.bottom = platformRect.top # If nothing else you will need to do this.
  ... whatever else you want to do

This is what I use in pyPlatform and It seems to work quite well, however its not 100% and would not be good in the situation where you are moving verry fast horizontally, However it could be modified to work for that also.





-- 
______________________________________________
http://www.linuxmail.org/
Now with POP3/IMAP access for only US$19.95/yr

Powered by Outblaze
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org