src/collisions/src/collision-handler.js
function handleCollisionsForJob(job, elapsedTime, physicsParams) {
  const collidable = job.collidable;

  // Clear any previous collision info.
  collidable.previousCollisions = collidable.collisions;
  collidable.collisions = [];

  // Find all colliding collidables.
  const collidingCollidables = findIntersectingCollidablesForCollidable(collidable);

  // Store the time of collision for each collision.
  const collisions = _recordCollisions(collidable, collidingCollidables, elapsedTime);

  // Calculate the points of contact for each collision.
  _calculatePointsOfContact(collisions);

  // Collision resolution.
  _resolveCollisions(collisions, physicsParams);
}
function handleCollisionsForJob(job, elapsedTime, physicsParams) {
  const collidable = job.collidable;

  // Clear any previous collision info.
  collidable.previousCollisions = collidable.collisions;
  collidable.collisions = [];

  // Find all colliding collidables.
  const collidingCollidables = findIntersectingCollidablesForCollidable(collidable);

  // Store the time of collision for each collision.
  const collisions = _recordCollisions(collidable, collidingCollidables, elapsedTime);

  // Calculate the points of contact for each collision.
  _calculatePointsOfContact(collisions);

  // Collision resolution.
  _resolveCollisions(collisions, physicsParams);
}
function handleCollisionsForJob ( job , elapsedTime , physicsParams ) { const collidable = job . collidable ; // Clear any previous collision info. collidable . previousCollisions = collidable . collisions ; collidable . collisions = [ ] ; // Find all colliding collidables. const collidingCollidables = findIntersectingCollidablesForCollidable ( collidable ) ; // Store the time of collision for each collision. const collisions = _recordCollisions ( collidable , collidingCollidables , elapsedTime ) ; // Calculate the points of contact for each collision. _calculatePointsOfContact ( collisions ) ; // Collision resolution. _resolveCollisions ( collisions , physicsParams ) ; }
This module defines a collision pipeline.

These functions will detect collisions between collidable bodies and update their momenta in
response to the collisions.

- Consists of an efficient broad-phase collision detection step followed by a precise
narrow-phase step.
- Calculates the position, surface normal, and time of each contact.
- Calculates the impulse of a collision and updates the bodies' linear and angular momenta in
response.
- Applies Coulomb friction to colliding bodies.
- Sub-divides the time step to more precisely determine when and where a collision occurs.
- Supports multiple collisions with a single body in a single time step.
- Efficiently supports bodies coming to rest against each other.
- Bodies will never penetrate one another.
- This does not address the tunnelling problem. That is, it is possible for two fast-moving
bodies to pass through each other as long as they did not intersect each other during any time
step.
- This only supports collisions between certain types of shapes. Fortunately, this set provides
reasonable approximations for most other shapes. The supported types of shapes are: spheres,
capsules, AABBs, and OBBs.

## Objects that come to rest

An important efficiency improvement is to not process objects through the physics engine pipeline
after they have come to rest. The isAtRest field indicates when a body has come to rest.

isAtRest is set to true after a physics frame is finished if the collisions, forces, position,
and orientation of a job have not changed from the previous to the current state.

isAtRest is set to false from two possible events: after a physics frame is finished if the
collisions have changed from the previous to the current state, or when a force is added to
removed from the job.

## Collision calculations do not consider velocity

Collision detection works by waiting until two bodies intersect. However, because time frames are
not infinitely small, when an intersection is detected, it's already past the exact instance of
collision. To alleviate problems from this, the velocity of each body can be considered when
calculating the collision time, position, and contact normal. However, taking velocity into
account makes the contact calculations much more complex, so we do not consider velocity in our
calculations.

A notable consequence of this is that the calculated contact normals can be incorrect. Consider
the following moving squares. At time t2 they are found to have collided. The calculated contact
point will be somewhere within the intersection of the corners. But the calculated contact normal
will point upwards, while the true contact normal should point to the right. This is because the
contact calculations do not consider velocity and instead only consider the shallowest direction
of overlap.

// Time t1
+------------+
|            |
|            |
<-- |      B     |
|            |
+------------+    |            |
|            |    +------------+
|            |
|      A     | -->
|            |
|            |
+------------+

// Time t2
+------------+
|            |
|            |
|      B     |
|            |
+------------+      |
|      +-----|------+
|            |
|      A     |
|            |
|            |
+------------+
 
Detect and handle any collisions between a given job and all other collidable bodies.

@param {CollidablePhysicsJob} job
@param {DOMHighResTimeStamp} elapsedTime
@param {PhysicsConfig} physicsParams
This module defines a collision pipeline .