Rigid Body Physics
#
What is it?ChromeEngine contains a powerful 3D rigid body physics engine. It is a simplified 3D version of Randy Gaul's ImpulseEngine. Critically this engine operates fully under the assumption that all forces during a collision are the same as an objects impulse value during a collision.
Physics
pseudo code#
- Update the players velocity
- Iterate over the
@player_forces
list and determine the element wise sum of all the forces, then add this to the Players physical velocity. - Add the force vector as set by the first three items of the
@player_forces_controlled
list to the Players controlled velocity. - Update the player velocity to be a combination of it's physical and controlled velocity. The physical velocity is multiplied by
delta_t
to account for frame rate. - Dampen both controlled and physical velocity
- Iterate over the
- Update player position using it's velocity
- Detect all collisions (discussed in detail in the Collision Tests section)
- Resolve the collision with earliest time of impact.
- Resolve remaining collisions in random order
Collision resolution
pseudo code#
Collision resolution involves the following steps:
- Retrieve the collision information
- If the collision penetrated deeper than
@penetration_thresh
, move the player backwards along the collision normal so it is no longer penetrating - Add the collision forces associated with the collision.
- If the object is moving then we find the Angular and tangential velocity of the object's moving/spinning bounding sphere at the players location.
- We calculate the magnitude of the collision force as the dot product of the player's velocity minus the objects velocity and the collision normal.
- We add a force with this magnitude in the direction of the collision normal multiplied 1 + the
_restitution
value of the materia to the@player_forces
list- since we want it to apply a force which cancels out all the forwards velocity of the player and then additional force (
_restitution
) which bounces it backwards.
- since we want it to apply a force which cancels out all the forwards velocity of the player and then additional force (
- We calculate the magnitude of the collision force again like before but using the players physical velocity only
- We calculate the tagential component of the collision force by subtracting the main collision force from the players physical velocity minus the objects velocity. We add this tangential force to the
@player_forces
list.- We also prefix it's label with a "t:" to make it clear that it's tagential force.