rayHitsShape function Physics

RayShapeHit? rayHitsShape(
  1. Ray ray,
  2. Shape shape,
  3. Matrix4 worldXform,
  4. double maxDistance,
)

Closest hit of ray against shape under worldXform, or null.

maxDistance is in world units along the normalized ray direction.

Implementation

RayShapeHit? rayHitsShape(
  Ray ray,
  Shape shape,
  Matrix4 worldXform,
  double maxDistance,
) {
  switch (shape) {
    case SphereShape():
      return _raySphere(ray, shape, worldXform, maxDistance);
    case BoxShape():
      return _rayBox(ray, shape, worldXform, maxDistance);
    case CapsuleShape():
      return _rayCapsule(ray, shape, worldXform, maxDistance);
    case CompoundShape(:final children):
      RayShapeHit? best;
      for (final child in children) {
        final childWorld = worldXform.multiplied(child.localPose);
        final hit = rayHitsShape(ray, child.shape, childWorld, maxDistance);
        if (hit != null && (best == null || hit.distance < best.distance)) {
          best = hit;
        }
      }
      return best;
    case CylinderShape() ||
        ConvexHullShape() ||
        TriMeshShape() ||
        HeightFieldShape():
      return _rayAabb(ray, shapeWorldAabb(shape, worldXform), maxDistance);
  }
}