How to determine velocity from force

Hi all,

I recently started a project with the intention to master the physics aspect of Unity. Although I have a good knowledge of Unity, I never had the opportunity to work on game that makes extensive use of the physics engine. For that reason I’m trying to figure things out step by step;

So my first question is how to determine the final velocity of a rigidbody given a force over a stipulated time? My current setup is just a flat ground plane with a box collider and a cube with another box collider and a rigidbody. Both colliders have a custom physics material which has dynamic (i.e. kinetic) and static friction set to 0. The rigidbody has 0 drag and 0 angular drag. The cube has this script attached to it;

public class CubeController : MonoBehaviour {

    public float force = 10;
    public float time = 5;

    void Update()
    {
        if (Input.GetKeyDown (KeyCode.Space))
        {
            StartCoroutine(PhysicsCoroutine());
        }
    }

    // Use this for initialization
    IEnumerator PhysicsCoroutine () {
        Debug.Log ("Simulating");
        float t = 0f;
        while(t < time)
        {
            t += Time.fixedDeltaTime;
            rigidbody.AddForce(Vector3.forward * force);
            yield return new WaitForFixedUpdate();
        }

        float dynamicFrictionForce = collider.material.dynamicFriction * Mathf.Abs (Physics.gravity.y) * rigidbody.mass;
        float acceleration = (force - dynamicFrictionForce) / rigidbody.mass;
        float velocity = acceleration * t;
        Debug.Log (string.Format("[ACTUAL] Speed - {0}, Time - {1}", rigidbody.velocity.magnitude, t));
        Debug.Log (string.Format("[CALCULATE] Speed - {0}, Time - {1}", velocity, t));
    }
}

From basic physics formulas one can determine the following;

  • Fn = M * G * cos(O) where Fn is the normal force, M is mass, G is gravity and O is the angle of inclined surface

  • Fk = Fn * Uk where Fk is the kinetic friction force, Fn is the normal force and Uk is the coefficient of the kinetic friction

  • F - Fk = M * A where F is force, Fk is the kinetic friction force, M is mass and A is acceleration

  • V = U + AT where V is the final velocity, U is the initial velocity, A is acceleration and T is time

Therefore given the following values we can find the final velocity;

  • M = 5

  • F = 20

  • T = 5

  • Uk = 0

  • G = 9.81

  • O = 0

Fn = 5 * 9.81 * cos(0)
= 49.05

Fk = 49.05 * 0
= 0

From equation 3 we can find the acceleration by (F - Fk)/M;

A = (20 - 0)/5
= 4

V = 4 * 5
= 20m/s

Therefore the velocity is 20m/s, which matches the magnitude of the rigidbody’s velocity. However when I change just the dynamic friction in the custom physics material, I get incorrect results. Assuming all values remain the same except Uk which is now 0.1

Fn = 5 * 9.81 * cos(0)
= 49.05

Fk = 49.05 * 0.1
= 4.905

A = (20 - 4.905) / 5
= 3.019

V = 3.019 * 5
= 15.095

But the rigidbody.velocity.magnitude is 10.24. Why there is this difference? Is there anything wrong in my formulas? How can I accurately predict an object velocity?

Your help is greatly appreciated,

Yours truly,
A physics noob :slight_smile:

I’m not a huge expert on the innards of physics engines, but given the way they operate, i’m actually a bit surprised that even the first test gives accurate results. There’s so much room for floating point precision errors and such when you calculate stuff a few hundred times during those 5 seconds you are testing.

Physics engines like the ones in Unity aren’t designed to give scientifically accurate results, but rather to offer believable results within constraints and demands placed by stability and performance requirements.

To answer your question however, I’d say the problem lies in the way friction is simulated. In conventional mechanics the friction coefficient (Uk) is determined by the pair of materials associated. In physics engines you cannot just set a single Uk. Both materials have a “friction value”. Otherwise you’d be forced to fill in a matrix with (n*n) values for every physics game you make (where n is the amount of physics materials in your world. Imagine having 20 materials…).

I remember reading somewhere that in a certain 2D physics engine, the friction coefficient is calculated by taking the Geometric mean of the friction values of the colliding materials. I don’t have a clue how Unity does this but i guess you could find out either by Googling or by plotting the values you are getting and determining it from there.