Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
4  
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
add comment

1 Answer 1

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
show 1 more comment

Your Answer

 
discard

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.