Max Speed/Velocity and Max Force formulas F3.0

Topics: User Forum
Oct 10, 2010 at 5:39 AM
Edited Oct 10, 2010 at 5:43 AM

Ok. Let's say you know the mass of a body and its linear damping. If you wanted to apply a constant linear force (like a rocket thrust) to this body, so that it never exceeds a given velocity, no matter for how long you apply it (max Velocity), how can you calculate the value of that force?

Or, let's say you know the constant force being applied to the mentioned body (max Force) and its linear damping, but you don't know what is the velocity at which the force won't be able to accelerate the body anymore (max velocity)

Or, you know the max velocity you want and the force you will apply to the body, but you still need to calculate the linear damping so that everything fits together...

Well, you can use this class over here:

 

    class PhysicsHelper
    {
        public static float GetMaxLinearVelocity(float mass, float linearDamping, float timeDelta, float maxForceLength)
        {
            float maxLinearVelocity = maxForceLength * (1 - timeDelta * linearDamping) / (mass * linearDamping);
            return maxLinearVelocity;
        }

        public static float GetMaxLinearVelocity(float mass, float deltaTime, float linearDamping, Vector2 maxForce)
        {
            return GetMaxLinearVelocity(mass, deltaTime, linearDamping, maxForce.Length());
        }

        public static float GetMaxForceLength(float mass, float deltaTime, float linearDamping, float maxVelocity)
        {
            float maxForceLength = (maxVelocity * mass * linearDamping) / (1 - deltaTime * linearDamping);
            return maxForceLength;
        }

        public static float GetLinearDamping(float mass, float deltaTime, float maxVelocity, float maxForceLength)
        {
            float linearDamping = (maxForceLength) / (maxVelocity * mass + deltaTime * maxForceLength);
            return linearDamping;
        }

        public static float GetLinearDamping(float mass, float deltaTime, float maxVelocity, Vector2 maxForce)
        {
            return GetLinearDamping(mass, deltaTime, maxVelocity, maxForce.Length());
        }
    }

 

 and save yourself some hand-tweaking headaches.

Use at will.

Remember that deltaTime is the elapsed time in seconds since the last step. It's the value that that you pass to World.Step(float dt). This value should not vary, so once you get it from, let's say, the gameTime parameter of your update function by storing gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f into some variable. If you really, really don't know how to get this value, the standard elapsed time for a fixed timestep game is around 0.01 seconds.

Have fun! Cheers.

-Patricio Marrone

 

P.S.: The formula to get a desired angular velocity is, apparently, the same. You just change linear damping to angular damping, max velocity to max angular velocity, max force to max torque and mass to moment of inertia. I haven't tested this yet, but it should work.