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