0
\$\begingroup\$

I am pretty new to using physics engine and would like to know what is best practice for movement of playercharacters.

Currently I have a class called Hero that has a CircleShape and when the user presses left/right/up/down it gets an linear impulse in the appropiate direction. All this works fine. The problem happens when the player does not produce any input, the shape is going further in the last direction for a long time, but I would like it if the shape stops faster. The players sees the world top down, so the worlds gravity is set to (0;0).

Maybe the whole idea of using impulses to move the character around is flawed, I would just like to know what works.

\$\endgroup\$
1
  • \$\begingroup\$ Platformer? If yes, this is sort of a poor question, as the best answer to your question about using Box2D is far from the best answer about how to do movement in a platformer (for which you either should not use Box2D or you should only use it for interactive objects and treat the player object as a very special beast with its own physical logic): google.com/… \$\endgroup\$ Aug 18, 2013 at 3:37

1 Answer 1

1
\$\begingroup\$

Newtons first law of motion, simply put:

Every object in motion will stay in motion until acted upon by an outside force.

If you want to change the motion of an object, you need to add an additional force. In this case, a stopping force. This force is in the opposite direction of your velocity, with the sole purpose of arresting that velocity.

So, you have the following criteria.

  • The force can't be strong enough to reverse the velocity.
  • The force is only applied when the object is in motion (or when you want to stop).

There are a few "named" forces that fit here.

  • Friction. The resisting force from two solid objects moving against each other.
  • Drag. Essentially friction, but with respect to an object moving through a fluid/gas.

Box2D has support for friction built into its simulation. When defining a fixture, you simply set the friction value:

fixtureDef.friction = 0.3f;

See the Box2D manual for more information (unfortunately, their manual is not really linkable to specific sections, so just search for "Friction is used to make objects slide along each other realistically").

Some things you'll want to think about when implementing a stopping force.

  • Friction will be the easiest to "turn on" and start using, however, you'll need to increase your movement forces to overcome friction.
  • Implementing your own stopping force will allow you better control, but will mean you'll need to add more of your own code.

Basically, the way you're doing things now is fine, and the results are expected. You just need to add some additional functionality to get the results you want.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the answer. I expected to put more work in it, it was just where exactly I have to do it ;) I will go with the "own" force approach, friction doesn't seem to have much of an impact. \$\endgroup\$
    – Jackels
    Aug 18, 2013 at 0:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .