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.

  • 4
    \$\begingroup\$ 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. \$\endgroup\$ Commented May 25, 2012 at 2:48
  • \$\begingroup\$ @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. \$\endgroup\$ Commented May 25, 2012 at 10:17
  • \$\begingroup\$ I'll also live this little treasure that landed up in my inbox today right here: higherorderfun.com/blog/2012/05/20/… \$\endgroup\$ Commented May 27, 2012 at 6:27

1 Answer 1


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.

  • \$\begingroup\$ 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. \$\endgroup\$
    – Danik
    Commented May 25, 2012 at 11:26
  • \$\begingroup\$ 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? \$\endgroup\$ Commented May 25, 2012 at 19:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Danik
    Commented May 25, 2012 at 19:45
  • \$\begingroup\$ Should i include acceleration? \$\endgroup\$ Commented May 25, 2012 at 20:18
  • \$\begingroup\$ Not in the code example above, it only cares about speed and position in order to figure out if the player collided from above in the current frame. \$\endgroup\$
    – Danik
    Commented May 26, 2012 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .