Class NarrowPhase
Provides collision detection algorithms for convex shapes defined by support functions.
public static class NarrowPhase
- Inheritance
-
NarrowPhase
- Inherited Members
Remarks
Implements GJK (Gilbert-Johnson-Keerthi) for distance queries, MPR (Minkowski Portal Refinement) for fast overlap detection, and EPA (Expanding Polytope Algorithm) for penetration depth.
All methods are thread-safe when used with thread-local solver state.
Methods
Collision<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector, out JVector, out JVector, out JVector, out float)
Determines whether two convex shapes overlap, providing detailed information for both overlapping and separated cases. Internally, the method employs the Expanding Polytope Algorithm (EPA) to gather collision information.
public static bool Collision<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float penetration) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationAJQuaternionThe orientation of shape A in world space.
orientationBJQuaternionThe orientation of shape B in world space.
positionAJVectorThe position of shape A in world space.
positionBJVectorThe position of shape B in world space.
pointAJVectorFor the overlapping case: the deepest point on shape A inside shape B; for the separated case: the closest point on shape A to shape B.
pointBJVectorFor the overlapping case: the deepest point on shape B inside shape A; for the separated case: the closest point on shape B to shape A.
normalJVectorThe normalized collision normal pointing from pointB to pointA. This normal remains defined even if pointA and pointB coincide. It denotes the direction in which the shapes should be moved by the minimum distance (defined by the penetration depth) to either separate them in the overlapping case or bring them into contact in the separated case.
penetrationfloatThe penetration depth.
Returns
- bool
Returns true if the algorithm completes successfully, false otherwise. In case of algorithm convergence failure, collision information reverts to the type's default values.
Type Parameters
TaTb
Collision<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JVector, out JVector, out JVector, out JVector, out float)
Determines whether two convex shapes overlap, providing detailed information for both overlapping and separated cases. It assumes that support shape A is at position zero and not rotated. Internally, the method employs the Expanding Polytope Algorithm (EPA) to gather collision information.
public static bool Collision<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationB, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float penetration) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationBJQuaternionThe orientation of shape B.
positionBJVectorThe position of shape B.
pointAJVectorFor the overlapping case: the deepest point on shape A inside shape B; for the separated case: the closest point on shape A to shape B.
pointBJVectorFor the overlapping case: the deepest point on shape B inside shape A; for the separated case: the closest point on shape B to shape A.
normalJVectorThe normalized collision normal pointing from pointB to pointA. This normal remains defined even if pointA and pointB coincide. It denotes the direction in which the shapes should be moved by the minimum distance (defined by the penetration depth) to either separate them in the overlapping case or bring them into contact in the separated case.
penetrationfloatThe penetration depth.
Returns
- bool
Returns true if the algorithm completes successfully, false otherwise. In case of algorithm convergence failure, collision information reverts to the type's default values.
Type Parameters
TaTb
Distance<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector, out JVector, out JVector, out JVector, out float)
Provides the distance and closest points for non-overlapping shapes.
public static bool Distance<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float distance) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationAJQuaternionThe orientation of shape A in world space.
orientationBJQuaternionThe orientation of shape B in world space.
positionAJVectorThe position of shape A in world space.
positionBJVectorThe position of shape B in world space.
pointAJVectorClosest point on shape A. Undefined for the overlapping case.
pointBJVectorClosest point on shape B. Undefined for the overlapping case.
normalJVectorUnit direction from shape A toward shape B. Undefined for the overlapping case.
distancefloatThe distance between the separating shapes. Zero if shapes overlap.
Returns
- bool
Returns true if the shapes do not overlap and distance information can be provided.
Type Parameters
TaTb
Distance<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JVector, out JVector, out JVector, out JVector, out float)
Provides the distance and closest points for non-overlapping shapes. It assumes that support shape A is located at position zero and not rotated.
public static bool Distance<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationB, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float distance) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationBJQuaternionThe orientation of shape B in world space.
positionBJVectorThe position of shape B in world space.
pointAJVectorClosest point on shape A. Undefined for the overlapping case.
pointBJVectorClosest point on shape B. Undefined for the overlapping case.
normalJVectorUnit direction from shape A toward shape B. Undefined for the overlapping case.
distancefloatThe distance between the separating shapes. Zero if shapes overlap.
Returns
- bool
Returns true if the shapes do not overlap and distance information can be provided.
Type Parameters
TaTb
MprEpa<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector, out JVector, out JVector, out JVector, out float, float)
Detects whether two convex shapes overlap and provides detailed collision information for overlapping shapes. Internally, this method utilizes the Minkowski Portal Refinement (MPR) to obtain the collision information. Although MPR is not exact, it delivers a strict upper bound for the penetration depth. If the upper bound exceeds a predefined threshold, the results are further refined using the Expanding Polytope Algorithm (EPA).
public static bool MprEpa<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float penetration, float epaThreshold = 0.02) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationAJQuaternionThe orientation of shape A in world space.
orientationBJQuaternionThe orientation of shape B in world space.
positionAJVectorThe position of shape A in world space.
positionBJVectorThe position of shape B in world space.
pointAJVectorThe deepest point on shape A that is inside shape B.
pointBJVectorThe deepest point on shape B that is inside shape A.
normalJVectorThe normalized collision normal pointing from pointB to pointA. This normal remains defined even if pointA and pointB coincide, representing the direction in which the shapes must be separated by the minimal distance (determined by the penetration depth) to avoid overlap.
penetrationfloatThe penetration depth.
epaThresholdfloatPenetration depth threshold above which MPR results are refined with EPA.
Returns
- bool
Returns true if the shapes overlap (collide), and false otherwise.
Type Parameters
TaTb
MprEpa<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JVector, out JVector, out JVector, out JVector, out float, float)
Detects whether two convex shapes overlap and provides detailed collision information for overlapping shapes. It assumes that support shape A is at position zero and not rotated. Internally, this method utilizes the Minkowski Portal Refinement (MPR) to obtain the collision information. Although MPR is not exact, it delivers a strict upper bound for the penetration depth. If the upper bound exceeds a predefined threshold, the results are further refined using the Expanding Polytope Algorithm (EPA).
public static bool MprEpa<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationB, in JVector positionB, out JVector pointA, out JVector pointB, out JVector normal, out float penetration, float epaThreshold = 0.02) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationBJQuaternionThe orientation of shape B.
positionBJVectorThe position of shape B.
pointAJVectorThe deepest point on shape A that is inside shape B.
pointBJVectorThe deepest point on shape B that is inside shape A.
normalJVectorThe normalized collision normal pointing from pointB to pointA. This normal remains defined even if pointA and pointB coincide, representing the direction in which the shapes must be separated by the minimal distance (determined by the penetration depth) to avoid overlap.
penetrationfloatThe penetration depth.
epaThresholdfloatPenetration depth threshold above which MPR results are refined with EPA.
Returns
- bool
Returns true if the shapes overlap (collide), and false otherwise.
Type Parameters
TaTb
Overlap<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector)
Performs an overlap test.
public static bool Overlap<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationAJQuaternionThe orientation of shape A in world space.
orientationBJQuaternionThe orientation of shape B in world space.
positionAJVectorThe position of shape A in world space.
positionBJVectorThe position of shape B in world space.
Returns
- bool
Returns true of the shapes overlap, and false otherwise.
Type Parameters
TaTb
Overlap<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JVector)
Performs an overlap test. It assumes that support shape A is located at position zero and not rotated.
public static bool Overlap<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationB, in JVector positionB) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaThe support function of shape A.
supportBTbThe support function of shape B.
orientationBJQuaternionThe orientation of shape B in world space.
positionBJVectorThe position of shape B in world space.
Returns
- bool
Returns true of the shapes overlap, and false otherwise.
Type Parameters
TaTb
PointTest<Ta>(in Ta, in JMatrix, in JVector, in JVector)
Check if a point is inside a shape.
public static bool PointTest<Ta>(in Ta support, in JMatrix orientation, in JVector position, in JVector point) where Ta : ISupportMappable
Parameters
supportTaSupport map representing the shape.
orientationJMatrixOrientation of the shape.
positionJVectorPosition of the shape.
pointJVectorPoint to check.
Returns
- bool
Returns true if the point is contained within the shape, false otherwise.
Type Parameters
Ta
PointTest<Ta>(in Ta, in JVector)
Check if a point is inside a shape.
public static bool PointTest<Ta>(in Ta support, in JVector point) where Ta : ISupportMappable
Parameters
supportTaSupport map representing the shape.
pointJVectorPoint to check.
Returns
- bool
Returns true if the point is contained within the shape, false otherwise.
Type Parameters
Ta
RayCast<Ta>(in Ta, in JQuaternion, in JVector, in JVector, in JVector, out float, out JVector)
Performs a ray cast against a shape.
public static bool RayCast<Ta>(in Ta support, in JQuaternion orientation, in JVector position, in JVector origin, in JVector direction, out float lambda, out JVector normal) where Ta : ISupportMappable
Parameters
supportTaThe support function of the shape.
orientationJQuaternionThe orientation of the shape in world space.
positionJVectorThe position of the shape in world space.
originJVectorThe origin of the ray.
directionJVectorThe direction of the ray; normalization is not necessary.
lambdafloatSpecifies the hit point of the ray, calculated as 'origin + lambda * direction'. Zero if the origin is inside the shape, PositiveInfinity if the ray does not hit.
normalJVectorThe normalized normal vector perpendicular to the surface, pointing outwards. Zero if the ray does not hit or the ray origin overlaps with the shape.
Returns
- bool
Returns true if the ray intersects with the shape; otherwise, false.
Type Parameters
Ta
RayCast<Ta>(in Ta, in JVector, in JVector, out float, out JVector)
Performs a ray cast against a shape.
public static bool RayCast<Ta>(in Ta support, in JVector origin, in JVector direction, out float lambda, out JVector normal) where Ta : ISupportMappable
Parameters
supportTaThe support function of the shape.
originJVectorThe origin of the ray.
directionJVectorThe direction of the ray; normalization is not necessary.
lambdafloatSpecifies the hit point of the ray, calculated as 'origin + lambda * direction'. Zero if the origin is inside the shape, PositiveInfinity if the ray does not hit.
normalJVectorThe normalized normal vector perpendicular to the surface, pointing outwards. Zero if the ray does not hit or the origin overlaps with the shape.
Returns
- bool
Returns true if the ray intersects with the shape; otherwise, false.
Type Parameters
Ta
Sweep<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector, in JVector, in JVector, out JVector, out JVector, out JVector, out float)
Calculates the time of impact and the collision points in world space for two shapes with linear velocities
sweepA and sweepB.
public static bool Sweep<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB, in JVector sweepA, in JVector sweepB, out JVector pointA, out JVector pointB, out JVector normal, out float lambda) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaShape A.
supportBTbShape B.
orientationAJQuaternionOrientation of shape A in world space.
orientationBJQuaternionOrientation of shape B in world space.
positionAJVectorPosition of shape A in world space.
positionBJVectorPosition of shape B in world space.
sweepAJVectorLinear velocity of shape A.
sweepBJVectorLinear velocity of shape B.
pointAJVectorCollision point on shape A in world space at the sweep origin.
pointBJVectorCollision point on shape B in world space at the sweep origin.
normalJVectorNormalized collision normal in world space (points from A to B). Zero if the shapes already overlap or do not hit.
lambdafloatTime of impact. PositiveInfinity if no hit is detected, zero if shapes overlap.
Returns
- bool
True if the shapes will hit or already overlap, false otherwise.
Type Parameters
TaTb
Sweep<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JQuaternion, in JVector, in JVector, in JVector, in JVector, in JVector, in JVector, in float, in float, out JVector, out JVector, out JVector, out float)
Calculates the time of impact (TOI) and the collision points in world space for two shapes with linear
velocities sweepA and sweepB and angular velocities
sweepAngularA and sweepAngularB.
public static bool Sweep<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationA, in JQuaternion orientationB, in JVector positionA, in JVector positionB, in JVector sweepA, in JVector sweepB, in JVector sweepAngularA, in JVector sweepAngularB, in float extentA, in float extentB, out JVector pointA, out JVector pointB, out JVector normal, out float lambda) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaShape A.
supportBTbShape B.
orientationAJQuaternionOrientation of shape A in world space.
orientationBJQuaternionOrientation of shape B in world space.
positionAJVectorPosition of shape A in world space.
positionBJVectorPosition of shape B in world space.
sweepAJVectorLinear velocity of shape A.
sweepBJVectorLinear velocity of shape B.
sweepAngularAJVectorAngular velocity of shape A.
sweepAngularBJVectorAngular velocity of shape B.
extentAfloatBounding sphere radius of shape A. Used to bound the angular displacement contribution.
extentBfloatBounding sphere radius of shape B. Used to bound the angular displacement contribution.
pointAJVectorCollision point on shape A in world space at the sweep origin.
pointBJVectorCollision point on shape B in world space at the sweep origin.
normalJVectorNormalized collision normal in world space at time of impact (points from A to B). Zero if the shapes already overlap or do not hit.
lambdafloatTime of impact. PositiveInfinity if no hit is detected. Zero if shapes overlap.
Returns
- bool
True if the shapes hit or already overlap, false otherwise.
Type Parameters
TaTb
Remarks
Uses conservative advancement for continuous collision detection. May fail to converge to the correct TOI and collision points in certain edge cases due to limitations in linear motion approximation and distance gradient estimation.
Sweep<Ta, Tb>(in Ta, in Tb, in JQuaternion, in JVector, in JVector, out JVector, out JVector, out JVector, out float)
Performs a sweep test where shape A is fixed at the origin with identity orientation and zero velocity.
public static bool Sweep<Ta, Tb>(in Ta supportA, in Tb supportB, in JQuaternion orientationB, in JVector positionB, in JVector sweepB, out JVector pointA, out JVector pointB, out JVector normal, out float lambda) where Ta : ISupportMappable where Tb : ISupportMappable
Parameters
supportATaShape A (fixed at the origin, identity orientation).
supportBTbShape B.
orientationBJQuaternionOrientation of shape B in world space.
positionBJVectorPosition of shape B in world space.
sweepBJVectorLinear velocity of shape B.
pointAJVectorCollision point on shape A in world space at the sweep origin.
pointBJVectorCollision point on shape B in world space at the sweep origin.
normalJVectorNormalized collision normal in world space (points from A to B). Zero if the shapes already overlap or do not hit.
lambdafloatTime of impact. PositiveInfinity if no hit is detected, zero if shapes overlap.
Returns
- bool
True if the shapes hit or already overlap, false otherwise.
Type Parameters
TaTb