I'm working on a 2D platformer side scroller game. I am giving high impulse to player, the player starts moving and if it hits a slope on terrain, it gets a vertical movement and starts to fly over (Box2D physics).

What I need is that the player should always be on the terrain and move along the ground.

I would like to know if it's possible using Box2D engine.


If you want perfect terrain contact at all times then you need to stop applying Box2D collision response to the player, and apply your own physics rules, because what you describe is not compatible with newtonian physics rules, which are the rules that Box2D follows.

However, you can get pretty close, while still using newtonian physics.

Increase the mass of the player to something large, and apply some downward force to the player.

Do not make the player's mass too large relative to other objects though, as large mass differences can result in large floating point errors and poor collision response.

Start with a small amount of mass, and gradually increase it until you find something that works while the simulation remains stable.

As you increase the mass of the player, increase the amount of downward force applied to the player at each frame. I'm not very familiar with Box2D, but this can probably be done with an impulse that is applied to the player every physics update.

Keep in mind that this means that all objects interacting with the player are not going to move the player very much unless they are also heavy, like your player is.

Note that you're going to have to increase the size of the impulse you apply to the player to make him move, as you increase the mass of the player, if you want him to continue at the same speed.

Keep in mind that the hero will have higher inertia with higher mass, so you have to decide if you want the hero to keep moving after the player moves him to the right a little. If not, try increasing friction forces on the player.

  • \$\begingroup\$ Thanks.. i have tried increasing mass and thus increasing the gravity (M*g), but doesn't solve the problem. :( \$\endgroup\$ – akc May 25 '11 at 12:51
  • \$\begingroup\$ Try reducing the hero's restitution. Describe to me the problem that you are still having with high gravity and high mass. \$\endgroup\$ – Olhovsky May 25 '11 at 15:10

Two possible solutions I can think of:

  1. Don't apply physics to the player character. Unless they jump or fall, I guess - not sure if this is possible in your game.

  2. Add a lot of "fake gravity" (a force pointing down) to the player character only. It won't make him move along the terrain perfectly, but it might be close enough. Again, this force shouldn't be applied when the character is supposed to be jumping or falling.

  • \$\begingroup\$ Thanks.. Unfortunately removing physics for player is not an option at this point of time, and regarding fake gravity, i have tried it but fails at certain times results in weird behaviors. \$\endgroup\$ – akc May 24 '11 at 12:06

I'm not sure if i perfectly get the problem here, nor have i used Box2d. Actually... if its for flash i might have...

But anyway, try calculating the direction the player is to the terrain (if the terrain is within 10 pixels say), and move it closer to the terrain if possible.

Of course this isn't a full proof method, and you might want to only do this if you have first checked that the player was actually running in a recent step.

Sorry if i didn't answer the question 100%. More detail could be good.

  • \$\begingroup\$ Thanks.. i would like to go your way if i was not using any physics engine for the game. Apparently i am extensively using Box2D. \$\endgroup\$ – akc May 24 '11 at 12:11

I'd add an invisible object just after the slope that uses gravity to pull down your character to avoid the character flying off like you said.

Yes this is possible in box2d but I am unable to tell you how without more information on your project.

(You'll need to learn box2d engine and look at what you can change to the players characters physics when they come off the slope or what you can do to prevent the actions occuring).


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.