intersectRay method

dynamic intersectRay(
  1. dynamic ray,
  2. dynamic result
)
  • Performs a ray/OBB intersection test and stores the intersection point
    • to the given 3D vector. If no intersection is detected, null is returned.

Implementation

intersectRay(ray, result) {
  // the idea is to perform the intersection test in the local space
  // of the OBB.

  this.getSize(size);
  aabb.setFromCenterAndSize(v1.set(0, 0, 0), size);

  // create a 4x4 transformation matrix

  matrix4FromRotationMatrix(obbmatrix, this.rotation);
  obbmatrix.setPositionFromVector3(this.center);

  // transform ray to the local space of the OBB

  inverse.copy(obbmatrix).invert();
  localRay.copy(ray).applyMatrix4(inverse);

  // perform ray <-> AABB intersection test

  if (localRay.intersectBox(aabb, result) != null) {
    // transform the intersection point back to world space

    return result.applyMatrix4(obbmatrix);
  } else {
    return null;
  }
}