Platformer physics help

Hey, was just looking for a little help in setting up the values for the platformer that I'm building.

The basic premise is that I have a character who is constantly running throughout the game level, and will have to go up, down and along platforms, as well as fall off them. Initially I have my character start running with a velocity of 0.03f. This moves him fine when running across a flat platform, however, when he has to start running up a platform, he naturally slows down, and vice versa for running down a platform, as I'd rather he doesn't slow down or speed up. I'm looking for the best way to have him move at a continuous pace, whether going up, down or straight across. I don't think I can just turn off gravity as I need him to be able to fall off.

I also need the character to "stick" to a platform that moves down (or up for that matter). I find if I move a platform the character will be left in the air for a fraction of a second before falling to meet it. I'd rather havet the kind of Mario effect where he stands on a platform that moves. Gravity is currently set to -10.0f, get the feeling that if I set it any stronger, then my other problem of having the character run up the platform will be amplified.

If anyone has any advice, tips or could point me to a tutorial, I'd really appreciate it.
Hi swelsh84.

I wouldnt recommend box2d for a pure platformer game, but if you use hard physics in your game you won't have a better choice. Im doing something alike and set up vars is not so easy. Will be situations where you need a setup, and situations where you need a different setup.

An option for speed is set up damping property, it will slow down your body if you are not applying a force or impulse, so the velocity results can be the same for each direction, but damping reduce gravity effects... About 'stick' your body to a platform, you would need a joint between your player and the platform. The problem is that meanwhile he is stuck to a platform, gravity and other forces won't act in the right way... so if another body hits your player, it wil be still stuck to the platform... It's better if you slow down the platform, and let physics world work as it would be for real. Modifying parameters in real-time depending of the situation of your game would give weird results.

Here some useful tutorials: http://www.iforce2d.net/b2dtut/ , http://www.iforce2d.net/b2dtut/jumpability
As Loren says if you want to mix Box2D with behavior that is not physically realistic, usually you will have to do some kind of kludge or workaround to get the effect you want.

About running up an incline without slowing down, you might find this recent topic interesting: viewtopic.php?f=3&t=8223 I think that one would go in the 'kludge' category...

One workaround for the problem of the character being in the air for a moment when the lift starts moving down, would be just to start the platform moving a little more gradually - this is how the elevator in my building works and I'm really glad not be left in the air when it starts moving. If your gravity is -10m/s/s and your time step length is say 1/60s (for 60fps) then the character would pick up speed at a rate of 10/60 = 0.166m/s every time step if he was simply falling. So if you restricted the platform to pick up speed at the same rate, the character would not separate from it. For example if the target speed of the platform is 4m/s it would take 4/0.166 = 24 time steps or 0.4 seconds to reach the target speed, then you could just let it move normally after that.

In my experience you get the same problem coming back up, if the platform stops suddenly at the top, right? You could do the same thing in reverse to prevent that, by slowing the body by 0.166m/s every time step, starting at the same point where it reached the target velocity on the way down.

I guess it depends on what kind of game you're making whether this kind of speeding-up/slowing-down would fit in, but if it does, I think it would be easier because it goes along with Box2D instead of trying to fight it 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=8260