src/collisions/src/collision-handler.js
function _resolveCollisionWithStationaryObject(collision, physicsParams) {
  const contactNormal = collision.contactNormal;

  let physicsCollidable;
  if (collision.collidableA.physicsJob) {
    physicsCollidable = collision.collidableA;
  } else {
    physicsCollidable = collision.collidableB;
    vec3.negate(contactNormal, contactNormal);
  }

  const previousState = physicsCollidable.physicsJob.previousState;
  const nextState = physicsCollidable.physicsJob.currentState;
  const center = physicsCollidable.centerOfMass;
  const contactPoint = collision.contactPoint;

  const contactPointOffset = tmpVec3;
  vec3.subtract(contactPointOffset, contactPoint, center);

  // Calculate the relative velocity of the bodies at the point of contact. We use the velocity from
  // the previous state, since it is the velocity that led to the collision.
  const velocity = vec3.create();
  vec3.cross(tmpVec1, previousState.angularVelocity, contactPointOffset);
  vec3.add(velocity, previousState.velocity, tmpVec1);

  if (vec3.dot(velocity, contactNormal) <= 0) {
    // If the relative velocity is not pointing against the normal, then the normal was calculated
    // incorrectly (this is likely due to the time step being too large and the fact that our
    // contact calculations don't consider velocity). So update the contact normal to be in the
    // direction of the relative velocity.

    // TODO: Check that this works as expected.

    console.warn('Non-collision because object is moving away from stationary object.');

    vec3.copy(collision.contactNormal, velocity);
    vec3.normalize(collision.contactNormal, collision.contactNormal);
    vec3.negate(collision.contactNormal, collision.contactNormal);
  }

  _applyImpulseFromCollisionWithStationaryObject(physicsCollidable, collision, velocity,
      contactPointOffset, physicsParams);

  // Revert to the position and orientation from immediately before the collision.
  vec3.copy(nextState.position, previousState.position);
  quat.copy(nextState.orientation, previousState.orientation);

  // Also revert the collidable's position and orientation.
  physicsCollidable.position = previousState.position;
  physicsCollidable.orientation = previousState.orientation;

  nextState.updateDependentFields();
}
function _resolveCollisionWithStationaryObject(collision, physicsParams) {
  const contactNormal = collision.contactNormal;

  let physicsCollidable;
  if (collision.collidableA.physicsJob) {
    physicsCollidable = collision.collidableA;
  } else {
    physicsCollidable = collision.collidableB;
    vec3.negate(contactNormal, contactNormal);
  }

  const previousState = physicsCollidable.physicsJob.previousState;
  const nextState = physicsCollidable.physicsJob.currentState;
  const center = physicsCollidable.centerOfMass;
  const contactPoint = collision.contactPoint;

  const contactPointOffset = tmpVec3;
  vec3.subtract(contactPointOffset, contactPoint, center);

  // Calculate the relative velocity of the bodies at the point of contact. We use the velocity from
  // the previous state, since it is the velocity that led to the collision.
  const velocity = vec3.create();
  vec3.cross(tmpVec1, previousState.angularVelocity, contactPointOffset);
  vec3.add(velocity, previousState.velocity, tmpVec1);

  if (vec3.dot(velocity, contactNormal) <= 0) {
    // If the relative velocity is not pointing against the normal, then the normal was calculated
    // incorrectly (this is likely due to the time step being too large and the fact that our
    // contact calculations don't consider velocity). So update the contact normal to be in the
    // direction of the relative velocity.

    // TODO: Check that this works as expected.

    console.warn('Non-collision because object is moving away from stationary object.');

    vec3.copy(collision.contactNormal, velocity);
    vec3.normalize(collision.contactNormal, collision.contactNormal);
    vec3.negate(collision.contactNormal, collision.contactNormal);
  }

  _applyImpulseFromCollisionWithStationaryObject(physicsCollidable, collision, velocity,
      contactPointOffset, physicsParams);

  // Revert to the position and orientation from immediately before the collision.
  vec3.copy(nextState.position, previousState.position);
  quat.copy(nextState.orientation, previousState.orientation);

  // Also revert the collidable's position and orientation.
  physicsCollidable.position = previousState.position;
  physicsCollidable.orientation = previousState.orientation;

  nextState.updateDependentFields();
}
function _resolveCollisionWithStationaryObject ( collision , physicsParams ) { const contactNormal = collision . contactNormal ; let physicsCollidable ; if ( collision . collidableA . physicsJob ) { physicsCollidable = collision . collidableA ; } else { physicsCollidable = collision . collidableB ; vec3 . negate ( contactNormal , contactNormal ) ; } const previousState = physicsCollidable . physicsJob . previousState ; const nextState = physicsCollidable . physicsJob . currentState ; const center = physicsCollidable . centerOfMass ; const contactPoint = collision . contactPoint ; const contactPointOffset = tmpVec3 ; vec3 . subtract ( contactPointOffset , contactPoint , center ) ; // Calculate the relative velocity of the bodies at the point of contact. We use the velocity from // the previous state, since it is the velocity that led to the collision. const velocity = vec3 . create ( ) ; vec3 . cross ( tmpVec1 , previousState . angularVelocity , contactPointOffset ) ; vec3 . add ( velocity , previousState . velocity , tmpVec1 ) ; if ( vec3 . dot ( velocity , contactNormal ) <= 0 ) { // If the relative velocity is not pointing against the normal, then the normal was calculated // incorrectly (this is likely due to the time step being too large and the fact that our // contact calculations don't consider velocity). So update the contact normal to be in the // direction of the relative velocity. // TODO: Check that this works as expected. console . warn ( 'Non-collision because object is moving away from stationary object.' ) ; vec3 . copy ( collision . contactNormal , velocity ) ; vec3 . normalize ( collision . contactNormal , collision . contactNormal ) ; vec3 . negate ( collision . contactNormal , collision . contactNormal ) ; } _applyImpulseFromCollisionWithStationaryObject ( physicsCollidable , collision , velocity , contactPointOffset , physicsParams ) ; // Revert to the position and orientation from immediately before the collision. vec3 . copy ( nextState . position , previousState . position ) ; quat . copy ( nextState . orientation , previousState . orientation ) ; // Also revert the collidable's position and orientation. physicsCollidable . position = previousState . position ; physicsCollidable . orientation = previousState . orientation ; nextState . updateDependentFields ( ) ; }
Resolve a collision between one moving, physics-based object and one stationary object.

@param {Collision} collision
@param {PhysicsConfig} physicsParams
@private
Resolve a collision between one moving physics - based object and one stationary object .