- Home /
Where can I find details on the physics engine?
I have just one question, everything else I write is just to satisfy your curiosity. My question is: How can I find out exactly how Unity evaluates forces, velocity, friction and drag to calculate the next velocity and object position?
I'm having trouble with object movement. The variables at play are drag, friction via PhysicMaterials and using a lot of rigidbody.AddForce(dir, ForceMode.VelocityChange)
. The biggest problem is that doing this work in the Update loop yields different results for different framerates, but in theory it should not, because logic like this is as simple as it gets:
velocity += deltaTime * acceleration
The above is a great example because it's easy to debug. The problem is that the velocity
getter is not updated until the FixedUpdate
loop, so adding several different values to velocity
will just continually use the old value.
rigidbody.velocity += explosionA;
rigidbody.velocity += explosionB;
The above does not yield the expected result, because velocity is precalculated. It comes out to be the same as the following:
Vector3 cachedVelocity = rigidbody.velocity;
rigidbody.velocity = cachedVelocity + explosionA;
rigidbody.velocity = cachedVelocity + explosionB;
My bigger problem is that it's hard to get movement speed and acceleration quite right, since the game is using a mish-mash of built in physics and manual controls to get everything "just right".
well, I never had the same problem, try looking at The Execution Order
Answer by tanoshimi · Dec 10, 2014 at 07:42 AM
Unity uses the PhysX engine. I don't think you'll find a simple one-line "this is how we resolve forces", but you could try looking at the developer documentation there: http://www.geforce.com/hardware/technology/physx
Also worth noting that the exact implementation of PhysX is propriety. NVIDIA makes a lot of money off having the most efficient, widely implemented engine. They are hardly going to give it away.
Answer by Bunny83 · Dec 10, 2014 at 08:03 AM
Physics is a complex topic an a lot variables can't be predicted (like most collision based stuff). If you want to know how (manual) forces and drag are applied see my answer over here (and the linked one). For information how the bouncing values in your physics material are combined see this question.
However things like friction and complex collisions (which involve angularVelocity as well as linear velocity) are way more complex and can't be predicted / calculated that easy.
Also you should do everything physics related in FixedUpdate. Update in combination with Time.deltaTime only works for linear equations. Everything more complex can't be "corrected" with a linear scalar like deltaTime. A quadratic equation "fixed" with deltaTime is almost correct. However the slower your Update runs the larger is the error. Using FixedUpdate ensures at least a contant behaviour since you have a constant framerate.
Your answer
Follow this Question
Related Questions
Convert rigidbody.drag to acceleration(m/s²)? 0 Answers
Realistic methods of propulsion without friction involved 0 Answers
Overcome drag 1 Answer
arrows in unity 3d? 0 Answers
Help with DragRigidbody.js ? 1 Answer