sphereIntersect method

OctreeData? sphereIntersect(
  1. Sphere sphere
)

Implementation

OctreeData? sphereIntersect(Sphere sphere) {
  _sphere.copy(sphere);

  List<Triangle> triangles = getSphereTriangles(_sphere, []);
  bool hit = false;

  for (int i = 0; i < triangles.length; i++) {
    OctreeData? result = triangleSphereIntersect(_sphere, triangles[i]);
    if (result != null) {
      hit = true;
      _sphere.center.add(result.normal.multiplyScalar(result.depth));
    }
  }

  if (hit) {
    Vector3 collisionVector = _sphere.center.clone().sub(sphere.center);
    num depth = collisionVector.length();
    return OctreeData(normal: collisionVector.normalize(), depth: depth);
  }

  return null;
}