
So I am using the code from the samples to make an object (userAgent) move, but I don't know how to add maximum speed.
Right now I have something like this:
if (input.KeyboardState.IsKeyDown(Keys.A)) {
force.X += forceAmount;
}
if (input.KeyboardState.IsKeyDown(Keys.D)) {
force.X += forceAmount;
}
_userAgent.ApplyForce(force);
Which works fine but I can't seem to be able to figure out how to add a cap to the speed?


Dec 13, 2012 at 7:57 PM
Edited Dec 13, 2012 at 8:02 PM

Don't apply the force if _userAgent.LinearVelocity>=maxVelocityCap? By Newton's first law your body should keep moving at the current velocity (assuming no friction or other forces or in other words if the net force is 0).
Alternatively v=v0+at where v is the velocity after the time step, t is the timeStep, v0 is the velocity before the time step, a (acceleration) can be obtained from Newton's Second Law F=ma where F is the force you will be
applying and m is the mass of the body. So you need to solve an inequality to find out which F will bring the velocity to the max velocity allowed and cap your forceAmount instead if it would cause excess velocity.
First method would be better with sufficiently small time steps if other forces act on the body that you don't know about (or can't be bothered to calculate)  the velocity could oscillate a bit around the limit but it shouldn't be visible. Second
method is, of course, the more exact mathematical solution if you know for a fact that F (the net force) is only the one you are applying.



Wow I totally missed .LinearVelocity variable, even though I checked the list dozens of times..
Thanks for your help

