# Platforms collide-able on one side only

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.

• 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: higherorderfun.com/blog/2012/05/20/… – Jonathan Dickinson May 27 '12 at 6:27

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.

• 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
• 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. – Danik May 26 '12 at 9:27