0
\$\begingroup\$

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.

\$\endgroup\$
  • 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\$ – Gustavo Maciel May 25 '12 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\$ – Adam Geisweit May 25 '12 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\$ – Jonathan Dickinson May 27 '12 at 6:27
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 May 25 '12 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\$ – Adam Geisweit May 25 '12 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 May 25 '12 at 19:45
  • \$\begingroup\$ Should i include acceleration? \$\endgroup\$ – Adam Geisweit May 25 '12 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 May 26 '12 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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