shapeCast method
ShapeCastHit?
shapeCast(
- Shape shape,
- Matrix4 from,
- Vector3 direction,
- double distance, {
- int layerMask = 0xFFFFFFFF,
- bool includeFixed = true,
- bool includeKinematic = true,
- bool includeDynamic = true,
- bool includeTriggers = false,
override
Implementation
@override
ShapeCastHit? shapeCast(
Shape shape,
Matrix4 from,
Vector3 direction,
double distance, {
int layerMask = 0xFFFFFFFF,
bool includeFixed = true,
bool includeKinematic = true,
bool includeDynamic = true,
bool includeTriggers = false,
}) {
if (shape is! SphereShape) {
throw UnsupportedError(
'BasicPhysicsWorld.shapeCast currently supports SphereShape only.',
);
}
// Sphere cast = expand each collider's AABB by the sphere radius
// and raycast from `from`'s translation along `direction` against
// the inflated volumes. Returns the closest hit collider.
final origin = from.getTranslation();
final ray = Ray.originDirection(origin, direction);
ShapeCastHit? best;
for (final collider in _colliders) {
if (!_passesFilters(
collider,
layerMask: layerMask,
includeFixed: includeFixed,
includeKinematic: includeKinematic,
includeTriggers: includeTriggers,
)) {
continue;
}
final aabb = shapeWorldAabb(collider.shape, collider.worldPose);
final inflated = Aabb3.minMax(
aabb.min - Vector3.all(shape.radius),
aabb.max + Vector3.all(shape.radius),
);
final hit = _aabbRaycast(ray, inflated, distance);
if (hit == null) continue;
if (best == null || hit.distance < best.distance) {
best = ShapeCastHit(
node: collider.node,
collider: collider,
worldPoint: hit.worldPoint,
worldNormal: hit.worldNormal,
distance: hit.distance,
);
}
}
return best;
}