I've just written my first update and background collision routine for a new project I am working on. I have never done a platformer before. I've gone over the logic several times, but I get the impression that it is sub-optimal. Could you all help me think this through? I can't seem to find any discussion of this on the forums.
Note that this routine handles applying gravity, applying velocity to position, background collision and response to those collisions.
My current implementation resolves all edge-cases, but there is a lot of processing here. My current implementation is taking a full 6 scan lines worst-case. I could probably trim this down a bit, but I can't shake the feeling that my logic is not optimal.
Thoughts? How do you all implement platformer-style collision detection?
Thanks!
Note that this routine handles applying gravity, applying velocity to position, background collision and response to those collisions.
Code:
For Object in All Objects
Apply gravity to Object Y velocity
Apply Y velocity to Y position
Calculate bounds
Check Y collision with background
If a collision occured
Correct Y position
Recalculate top and bottom bounds
Apply X velocity to X position
Recalculate left and right bounds
Check X collision with background
If a collision occured
Correct X position
Recalculate left and right bounds
Apply gravity to Object Y velocity
Apply Y velocity to Y position
Calculate bounds
Check Y collision with background
If a collision occured
Correct Y position
Recalculate top and bottom bounds
Apply X velocity to X position
Recalculate left and right bounds
Check X collision with background
If a collision occured
Correct X position
Recalculate left and right bounds
My current implementation resolves all edge-cases, but there is a lot of processing here. My current implementation is taking a full 6 scan lines worst-case. I could probably trim this down a bit, but I can't shake the feeling that my logic is not optimal.
Thoughts? How do you all implement platformer-style collision detection?
Thanks!