shapeCast method
SimShapeCastHit?
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
SimShapeCastHit? 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(
'BasicSimulation.shapeCast currently supports SphereShape only.',
);
}
// Sphere cast = raycast against each collider's AABB inflated by the
// sphere radius; closest hit wins.
final origin = from.getTranslation();
final ray = Ray.originDirection(origin, direction);
SimShapeCastHit? best;
_colliders.forEach((handle, collider) {
if (!_passesFilters(
collider,
layerMask: layerMask,
includeFixed: includeFixed,
includeKinematic: includeKinematic,
includeTriggers: includeTriggers,
)) {
return;
}
final aabb = shapeWorldAabb(collider.shape, _colliderWorldPose(collider));
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) return;
if (best == null || hit.distance < best!.distance) {
best = SimShapeCastHit(
colliderHandle: handle,
worldPoint: hit.worldPoint,
worldNormal: hit.worldNormal,
distance: hit.distance,
);
}
});
return best;
}