Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have frequently tried to make some sort of a platformer, but when I actually code it, I run into a bit of a snag. How would I be able to make platforms in the air stop objects if they hit the sides or bottom, but allow them to sit on top of them?

I have tried many different methods, and some work slightly, but have many bugs, or some don't even work at all. Like this one I tried made it so that if you hit the side of the platform at the right angle, it forced you up on top of it.

share|improve this question
What do you have right now? What have you tried? It's a simple collision checking problem, but people would like to see how your world is set, etc. – Gustavo Maciel May 25 '12 at 2:48
@Gustavo i just wanted to see suggestions for how to make a platforming collision. right now i have given up on platforming, simply because of this problems. i have tried using mostly rectangles before, but i haven't tried using just the positions before. – Adam Geisweit May 25 '12 at 10:17
I'll also live this little treasure that landed up in my inbox today right here:… – Jonathan Dickinson May 27 '12 at 6:27
up vote 2 down vote accepted

Without knowing much about how your code works it's hard to give an exact answer, but something like this should work:

if (collision(platform) && pos.y - height - vel.y < platform.y && vel.y > 0) {
    // Player collides, was over the platform the last update and is falling
    vel.y = 0;                      // Stop player
    pos.y = platform.y - height;    // Position player on platform
    onGround = true;

If the registration point of the player is centered you would use height/2 instead of height, and you might have to take the platforms height into account as well if it's registration point isn't at the top.

share|improve this answer
thank you. i haven't tried this before. – Adam Geisweit May 25 '12 at 10:14
You're welcome. I noticed a small error in the if statement, I have updated it to take into account the velocity in the second condition. The second condition should check that the player was over the platform in the last frame, not that he is now. – Danik May 25 '12 at 11:26
Wow. I never thought to do that with checking if it was above before. This is before acceleration comes into effect in this frame, right? – Adam Geisweit May 25 '12 at 19:28
The way I wrote the example you would check for collisions after everything else is updated, like velocities and positions. I think that is probably the easiest way. – Danik May 25 '12 at 19:45
Should i include acceleration? – Adam Geisweit May 25 '12 at 20:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.