Walking/Stopping

Topics: User Forum
Jul 30, 2009 at 4:23 PM
Edited Jul 30, 2009 at 4:25 PM

Hey everyone,

I've had some problems with getting the physics right for a 2D side-scroller game I am working on. I have been trying to get a simple rectangle geometry (representing a player) to "walk" (move to the left or right in this case). One of the problems is that the walking does not happen at the right speed. When hitting the right arrow, for example, I apply a force to the player:

body.ApplyForce(new Vector2(500, 0));

It takes a second for the player to start "walking", but it eventually works. (I've been trying to get a Super Mario Bros.-ish feel for the game, where you press the right arrow and start walking somewhat instantaneously.)

Then, when I release the key, I run the following code:

body.ClearImpulse();

body.ClearForce();

This slows down the player, but does not stop it. So, I have the following question: How should I model the walking and stopping of a player to be a little more instantaneous and accurate? Is the "ApplyForce" method a good way to do it, or would there be another good method? It doesn't seem that ClearForce() works like it should (or does it, and I just don't understand?)

Thanks for looking into this,

-Zach

Coordinator
Jul 30, 2009 at 7:24 PM
Edited Jul 30, 2009 at 7:25 PM

Try with a higher force value.

To make your player stop, set the friction value of the player (and ground) higher.

ClearForce() removes the force applied to the player. That will make it stop eventually, but it still maintain a velocity. To make it stop instantly, you can set the Velocity to 0.

Aug 3, 2009 at 1:31 PM

Thanks genbox; That worked well. I'm just working out the small bugs now :)