Unity Physics

Unity uses PhysX, the physics engine by nVidia.

Using PhysX in Unity.
PhysX is used in Unity by attaching physics components to GameObjects. A simple rigidbody component makes that GameObject come under control of the physics engine.

Rigidbody
The rigidbody component is the basis for most physics in Unity. A rigidbody component causes the GameObject it is attached to come under control of the physics engine. Any child GameObjects retain their relationship and are moved with the parent, unless they themselves have a ridigbody.

Constrants.
Constraint components constrain rigidbodies together. A hingejoint constrains to another rigidbody to simulate a hinge. All Constraint components are based on Configurable Joint, which exposes all of PhysX's joint properties.

Colliders.
Colliders attached to rigidbodies allow the rigidbody to collide and bounce off of other colliders. it also defines the center of mass - a rigidbody without a collider won't spin properly (important when working with constraints). Colliders can be attached to non-rigidbodies, these are static and are often used for building levels.

Common formulas and functions
PhysX provides most functionality, including gravity, applying force etc. Stuff listed here are some extra functionality that may be useful but are not included in PhysX.

Direct Impact
An interesting issue is to calcuate a value representing how hard an impact was directly onto a surface. Think of the difference between shooting a brick wall face on, and shooting a brick wall at an 89 degree angle.

One way of getting a rough value is to get the dot between the impact normal and the direction of the projectile.

float collisionMagnitude = collision.relativeVelocity.magnitude * Vector3.Dot(collision.relativeVelocity.normalised, contactPoint.normal);

Performance considerations
When working with the physics system, it's important to know what the limits of the physics engine is and how to obtain optimal performance.

Islands
A physics engine may seperate rigidbodies into "islands", which are seperate collections of rigidbodies that are solved independently. These islands are usually determined by which objects are colliding/nearly-colliding with each other.

Since the physics engine has to solve for each potential collision, the physics engine may have to make (n-1)^2+n checks. With islands, the number of checks can be significantly reduced by ignoring objects which are definetely not in contact. 10 islands of 2 cubes is much faster than 1 island of 20.

Example: A line of 500 cubes touching would form 1 very long island. A pile of 50 touching cubes would form 1 island. Two seperate groups of 25 cubes touching would form 2 islands. 1 island with 5 cubes may have to perform (n-1)^2+n = 20 collision checks (each cube has to check the other 4 cubes).

Advice: Islands are handled by PhysX automagically. You can have a considerable number of physics objects, but try to avoid having many rigidbodies contacting or constrained to each other.

Sleeping
When a physics object has been idle for a certain span of time, the physics engine can deactivate it. Since in most games, 99% of rigidbodies do nothing 99% of the time, this can allow for a considerable speed improvement. When the object is impacted, it is reawoken.

Advice: Sleeping is handled by PhysX automagically, however if you intend to push or otherwise interact with a rigidbody by code, it is recommended to manually wake the object. Generally, entire islands sleep and awaken, this can lead to sudden crippling lag when the player touches a sleeping pile of rigidbodies.

Colliders
Do not move colliders directly. Move the rigidbody (or the transform that the rigidbody is attached to) that the collider is attached too. Moving colliders causes PhysX to recompute stuff internally (and silently) and can cause severe, difficult to trace performance problems.

If you want to programmatically move around a static collider (i.e. a collider that does not have a rigidbody attached), attach a rigidbody to it and mark it as kinematic. This will cause the collider to function basically the same as a static collider and allow you to efficiently move it around.