Class CollisionHelper
 Namespace
 Stride.Core.Mathematics
 Assembly
 Stride.Core.Mathematics.dll
Contains static methods to help in determining intersections, containment, etc.
public static class CollisionHelper
 Inheritance

CollisionHelper
Methods
BoxContainsBox(ref readonly BoundingBox, ref readonly BoundingBox)
Determines whether a BoundingBox contains a BoundingBox.
public static ContainmentType BoxContainsBox(ref readonly BoundingBox box1, ref readonly BoundingBox box2)
Parameters
box1
BoundingBoxThe first box to test.
box2
BoundingBoxThe second box to test.
Returns
 ContainmentType
The type of containment the two objects have.
BoxContainsPoint(ref readonly BoundingBox, ref readonly Vector3)
Determines whether a BoundingBox contains a point.
public static ContainmentType BoxContainsPoint(ref readonly BoundingBox box, ref readonly Vector3 point)
Parameters
box
BoundingBoxThe box to test.
point
Vector3The point to test.
Returns
 ContainmentType
The type of containment the two objects have.
BoxContainsSphere(ref readonly BoundingBox, ref readonly BoundingSphere)
Determines whether a BoundingBox contains a BoundingSphere.
public static ContainmentType BoxContainsSphere(ref readonly BoundingBox box, ref readonly BoundingSphere sphere)
Parameters
box
BoundingBoxThe box to test.
sphere
BoundingSphereThe sphere to test.
Returns
 ContainmentType
The type of containment the two objects have.
BoxIntersectsBox(ref readonly BoundingBox, ref readonly BoundingBox)
Determines whether there is an intersection between a BoundingBox and a BoundingBox.
public static bool BoxIntersectsBox(ref readonly BoundingBox box1, ref readonly BoundingBox box2)
Parameters
box1
BoundingBoxThe first box to test.
box2
BoundingBoxThe second box to test.
Returns
 bool
Whether the two objects intersected.
BoxIntersectsSphere(ref readonly BoundingBox, ref readonly BoundingSphere)
Determines whether there is an intersection between a BoundingBox and a BoundingSphere.
public static bool BoxIntersectsSphere(ref readonly BoundingBox box, ref readonly BoundingSphere sphere)
Parameters
box
BoundingBoxThe box to test.
sphere
BoundingSphereThe sphere to test.
Returns
 bool
Whether the two objects intersected.
ClosestPointBoxPoint(ref readonly BoundingBox, ref readonly Vector3, out Vector3)
Determines the closest point between a BoundingBox and a point.
public static void ClosestPointBoxPoint(ref readonly BoundingBox box, ref readonly Vector3 point, out Vector3 result)
Parameters
box
BoundingBoxThe box to test.
point
Vector3The point to test.
result
Vector3When the method completes, contains the closest point between the two objects.
ClosestPointPlanePoint(ref readonly Plane, ref readonly Vector3, out Vector3)
Determines the closest point between a Plane and a point.
public static void ClosestPointPlanePoint(ref readonly Plane plane, ref readonly Vector3 point, out Vector3 result)
Parameters
plane
PlaneThe plane to test.
point
Vector3The point to test.
result
Vector3When the method completes, contains the closest point between the two objects.
ClosestPointPointTriangle(ref readonly Vector3, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3, out Vector3)
Determines the closest point between a point and a triangle.
public static void ClosestPointPointTriangle(ref readonly Vector3 point, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3, out Vector3 result)
Parameters
point
Vector3The point to test.
vertex1
Vector3The first vertex to test.
vertex2
Vector3The second vertex to test.
vertex3
Vector3The third vertex to test.
result
Vector3When the method completes, contains the closest point between the two objects.
ClosestPointSpherePoint(ref readonly BoundingSphere, ref readonly Vector3, out Vector3)
Determines the closest point between a BoundingSphere and a point.
public static void ClosestPointSpherePoint(ref readonly BoundingSphere sphere, ref readonly Vector3 point, out Vector3 result)
Parameters
sphere
BoundingSphereThe bounding sphere.
point
Vector3The point to test.
result
Vector3When the method completes, contains the closest point between the two objects; or, if the point is directly in the center of the sphere, contains Zero.
ClosestPointSphereSphere(ref readonly BoundingSphere, ref readonly BoundingSphere, out Vector3)
Determines the closest point between a BoundingSphere and a BoundingSphere.
public static void ClosestPointSphereSphere(ref readonly BoundingSphere sphere1, ref readonly BoundingSphere sphere2, out Vector3 result)
Parameters
sphere1
BoundingSphereThe first sphere to test.
sphere2
BoundingSphereThe second sphere to test.
result
Vector3When the method completes, contains the closest point between the two objects; or, if the point is directly in the center of the sphere, contains Zero.
Remarks
If the two spheres are overlapping, but not directly ontop of each other, the closest point is the 'closest' point of intersection. This can also be considered is the deepest point of intersection.
DistanceBoxBox(ref readonly BoundingBox, ref readonly BoundingBox)
Determines the distance between a BoundingBox and a BoundingBox.
public static float DistanceBoxBox(ref readonly BoundingBox box1, ref readonly BoundingBox box2)
Parameters
box1
BoundingBoxThe first box to test.
box2
BoundingBoxThe second box to test.
Returns
 float
The distance between the two objects.
DistanceBoxPoint(ref readonly BoundingBox, ref readonly Vector3)
Determines the distance between a BoundingBox and a point.
public static float DistanceBoxPoint(ref readonly BoundingBox box, ref readonly Vector3 point)
Parameters
box
BoundingBoxThe box to test.
point
Vector3The point to test.
Returns
 float
The distance between the two objects.
DistancePlanePoint(ref readonly Plane, ref readonly Vector3)
Determines the distance between a Plane and a point.
public static float DistancePlanePoint(ref readonly Plane plane, ref readonly Vector3 point)
Parameters
Returns
 float
The distance between the two objects.
DistanceSpherePoint(ref readonly BoundingSphere, ref readonly Vector3)
Determines the distance between a BoundingSphere and a point.
public static float DistanceSpherePoint(ref readonly BoundingSphere sphere, ref readonly Vector3 point)
Parameters
sphere
BoundingSphereThe sphere to test.
point
Vector3The point to test.
Returns
 float
The distance between the two objects.
DistanceSphereSphere(ref readonly BoundingSphere, ref readonly BoundingSphere)
Determines the distance between a BoundingSphere and a BoundingSphere.
public static float DistanceSphereSphere(ref readonly BoundingSphere sphere1, ref readonly BoundingSphere sphere2)
Parameters
sphere1
BoundingSphereThe first sphere to test.
sphere2
BoundingSphereThe second sphere to test.
Returns
 float
The distance between the two objects.
FrustumContainsBox(ref readonly BoundingFrustum, ref readonly BoundingBoxExt)
Determines whether a BoundingFrustum intersects or contains an AABB determined by its center and extent. Faster variant specific for frustum culling.
public static bool FrustumContainsBox(ref readonly BoundingFrustum frustum, ref readonly BoundingBoxExt boundingBoxExt)
Parameters
frustum
BoundingFrustumThe frustum.
boundingBoxExt
BoundingBoxExtThe bounding box ext.
Returns
 bool
true
if XXXX,false
otherwise.
GetNearestHit<T>(IEnumerable<T>, ref readonly Ray, out T, out float, out Vector3)
Retrieves the nearest hit object starting from the position of the ray in the direction of the ray.
public static bool GetNearestHit<T>(IEnumerable<T> objects, ref readonly Ray ray, out T hitObject, out float distance, out Vector3 point) where T : IIntersectableWithRay
Parameters
objects
IEnumerable<T>The objects that get tested for a collision with the ray.
ray
RayThe ray.
hitObject
TThe hit object.
distance
floatThe distance from the start of the ray.
point
Vector3The position of the collision.
Returns
 bool
Whether there was a hit.
Type Parameters
T
PlaneIntersectsBox(ref readonly Plane, ref readonly BoundingBox)
Determines whether there is an intersection between a Plane and a BoundingBox.
public static PlaneIntersectionType PlaneIntersectsBox(ref readonly Plane plane, ref readonly BoundingBox box)
Parameters
plane
PlaneThe plane to test.
box
BoundingBoxThe box to test.
Returns
 PlaneIntersectionType
Whether the two objects intersected.
PlaneIntersectsPlane(ref readonly Plane, ref readonly Plane)
public static bool PlaneIntersectsPlane(ref readonly Plane plane1, ref readonly Plane plane2)
Parameters
Returns
 bool
Whether the two objects intersected.
PlaneIntersectsPlane(ref readonly Plane, ref readonly Plane, out Ray)
public static bool PlaneIntersectsPlane(ref readonly Plane plane1, ref readonly Plane plane2, out Ray line)
Parameters
plane1
PlaneThe first plane to test.
plane2
PlaneThe second plane to test.
line
RayWhen the method completes, contains the line of intersection as a Ray, or a zero ray if there was no intersection.
Returns
 bool
Whether the two objects intersected.
Remarks
Although a ray is set to have an origin, the ray returned by this method is really a line in three dimensions which has no real origin. The ray is considered valid when both the positive direction is used and when the negative direction is used.
PlaneIntersectsPoint(ref readonly Plane, ref readonly Vector3)
Determines whether there is an intersection between a Plane and a point.
public static PlaneIntersectionType PlaneIntersectsPoint(ref readonly Plane plane, ref readonly Vector3 point)
Parameters
Returns
 PlaneIntersectionType
Whether the two objects intersected.
PlaneIntersectsSphere(ref readonly Plane, ref readonly BoundingSphere)
Determines whether there is an intersection between a Plane and a BoundingSphere.
public static PlaneIntersectionType PlaneIntersectsSphere(ref readonly Plane plane, ref readonly BoundingSphere sphere)
Parameters
plane
PlaneThe plane to test.
sphere
BoundingSphereThe sphere to test.
Returns
 PlaneIntersectionType
Whether the two objects intersected.
PlaneIntersectsTriangle(ref readonly Plane, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3)
Determines whether there is an intersection between a Plane and a triangle.
public static PlaneIntersectionType PlaneIntersectsTriangle(ref readonly Plane plane, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3)
Parameters
plane
PlaneThe plane to test.
vertex1
Vector3The first vertex of the triangle to test.
vertex2
Vector3The second vertex of the triagnle to test.
vertex3
Vector3The third vertex of the triangle to test.
Returns
 PlaneIntersectionType
Whether the two objects intersected.
RayIntersectsBox(ref readonly Ray, ref readonly BoundingBox, out Vector3)
Determines whether there is an intersection between a Ray and a BoundingBox.
public static bool RayIntersectsBox(ref readonly Ray ray, ref readonly BoundingBox box, out Vector3 point)
Parameters
ray
RayThe ray to test.
box
BoundingBoxThe box to test.
point
Vector3When the method completes, contains the point of intersection, or Zero if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsBox(ref readonly Ray, ref readonly BoundingBox, out float)
Determines whether there is an intersection between a Ray and a BoundingBox.
public static bool RayIntersectsBox(ref readonly Ray ray, ref readonly BoundingBox box, out float distance)
Parameters
ray
RayThe ray to test.
box
BoundingBoxThe box to test.
distance
floatWhen the method completes, contains the distance of the intersection, or 0 if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsPlane(ref readonly Ray, ref readonly Plane, out Vector3)
public static bool RayIntersectsPlane(ref readonly Ray ray, ref readonly Plane plane, out Vector3 point)
Parameters
ray
RayThe ray to test.
plane
PlaneThe plane to test
point
Vector3When the method completes, contains the point of intersection, or Zero if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsPlane(ref readonly Ray, ref readonly Plane, out float)
public static bool RayIntersectsPlane(ref readonly Ray ray, ref readonly Plane plane, out float distance)
Parameters
ray
RayThe ray to test.
plane
PlaneThe plane to test.
distance
floatWhen the method completes, contains the distance of the intersection, or 0 if there was no intersection.
Returns
 bool
Whether the two objects intersect.
RayIntersectsPoint(ref readonly Ray, ref readonly Vector3)
Determines whether there is an intersection between a Ray and a point.
public static bool RayIntersectsPoint(ref readonly Ray ray, ref readonly Vector3 point)
Parameters
Returns
 bool
Whether the two objects intersect.
RayIntersectsRay(ref readonly Ray, ref readonly Ray, out Vector3)
public static bool RayIntersectsRay(ref readonly Ray ray1, ref readonly Ray ray2, out Vector3 point)
Parameters
ray1
RayThe first ray to test.
ray2
RayThe second ray to test.
point
Vector3When the method completes, contains the point of intersection, or Zero if there was no intersection.
Returns
 bool
Whether the two objects intersect.
Remarks
This method performs a ray vs ray intersection test based on the following formula from Goldman.
s = det([o_2  o_1, d_2, d_1 x d_2]) / d_1 x d_2^2
t = det([o_2  o_1, d_1, d_1 x d_2]) / d_1 x d_2^2
Where o_1 is the position of the first ray, o_2 is the position of the second ray, d_1 is the normalized direction of the first ray, d_2 is the normalized direction of the second ray, det denotes the determinant of a matrix, x denotes the cross product, [ ] denotes a matrix, and   denotes the length or magnitude of a vector.
RayIntersectsRectangle(ref readonly Ray, ref readonly Matrix, ref readonly Vector3, int, out Vector3)
Determines whether there is an intersection between a Ray and a rectangle (2D).
public static bool RayIntersectsRectangle(ref readonly Ray ray, ref readonly Matrix rectangleWorldMatrix, ref readonly Vector3 rectangleSize, int normalAxis, out Vector3 intersectionPoint)
Parameters
ray
RayThe ray to test
rectangleWorldMatrix
MatrixThe world matrix applied on the rectangle
rectangleSize
Vector3The size of the rectangle in 3D
normalAxis
intThe index of axis defining the normal of the rectangle in the world. This value should be 0, 1 or 2
intersectionPoint
Vector3The position of the intersection point in the world
Returns
 bool
true if the ray and rectangle intersects.
RayIntersectsSphere(ref readonly Ray, ref readonly BoundingSphere, out Vector3)
Determines whether there is an intersection between a Ray and a BoundingSphere.
public static bool RayIntersectsSphere(ref readonly Ray ray, ref readonly BoundingSphere sphere, out Vector3 point)
Parameters
ray
RayThe ray to test.
sphere
BoundingSphereThe sphere to test.
point
Vector3When the method completes, contains the point of intersection, or Zero if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsSphere(ref readonly Ray, ref readonly BoundingSphere, out float)
Determines whether there is an intersection between a Ray and a BoundingSphere.
public static bool RayIntersectsSphere(ref readonly Ray ray, ref readonly BoundingSphere sphere, out float distance)
Parameters
ray
RayThe ray to test.
sphere
BoundingSphereThe sphere to test.
distance
floatWhen the method completes, contains the distance of the intersection, or 0 if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsTriangle(ref readonly Ray, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3, out Vector3)
Determines whether there is an intersection between a Ray and a triangle.
public static bool RayIntersectsTriangle(ref readonly Ray ray, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3, out Vector3 point)
Parameters
ray
RayThe ray to test.
vertex1
Vector3The first vertex of the triangle to test.
vertex2
Vector3The second vertex of the triangle to test.
vertex3
Vector3The third vertex of the triangle to test.
point
Vector3When the method completes, contains the point of intersection, or Zero if there was no intersection.
Returns
 bool
Whether the two objects intersected.
RayIntersectsTriangle(ref readonly Ray, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3, out float)
Determines whether there is an intersection between a Ray and a triangle.
public static bool RayIntersectsTriangle(ref readonly Ray ray, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3, out float distance)
Parameters
ray
RayThe ray to test.
vertex1
Vector3The first vertex of the triangle to test.
vertex2
Vector3The second vertex of the triagnle to test.
vertex3
Vector3The third vertex of the triangle to test.
distance
floatWhen the method completes, contains the distance of the intersection, or 0 if there was no intersection.
Returns
 bool
Whether the two objects intersected.
Remarks
This method tests if the ray intersects either the front or back of the triangle. If the ray is parallel to the triangle's plane, no intersection is assumed to have happened. If the intersection of the ray and the triangle is behind the origin of the ray, no intersection is assumed to have happened. In both cases of assumptions, this method returns false.
SphereContainsBox(ref readonly BoundingSphere, ref readonly BoundingBox)
Determines whether a BoundingSphere contains a BoundingBox.
public static ContainmentType SphereContainsBox(ref readonly BoundingSphere sphere, ref readonly BoundingBox box)
Parameters
sphere
BoundingSphereThe sphere to test.
box
BoundingBoxThe box to test.
Returns
 ContainmentType
The type of containment the two objects have.
SphereContainsPoint(ref readonly BoundingSphere, ref readonly Vector3)
Determines whether a BoundingSphere contains a point.
public static ContainmentType SphereContainsPoint(ref readonly BoundingSphere sphere, ref readonly Vector3 point)
Parameters
sphere
BoundingSphereThe sphere to test.
point
Vector3The point to test.
Returns
 ContainmentType
The type of containment the two objects have.
SphereContainsSphere(ref readonly BoundingSphere, ref readonly BoundingSphere)
Determines whether a BoundingSphere contains a BoundingSphere.
public static ContainmentType SphereContainsSphere(ref readonly BoundingSphere sphere1, ref readonly BoundingSphere sphere2)
Parameters
sphere1
BoundingSphereThe first sphere to test.
sphere2
BoundingSphereThe second sphere to test.
Returns
 ContainmentType
The type of containment the two objects have.
SphereContainsTriangle(ref readonly BoundingSphere, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3)
Determines whether a BoundingSphere contains a triangle.
public static ContainmentType SphereContainsTriangle(ref readonly BoundingSphere sphere, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3)
Parameters
sphere
BoundingSphereThe sphere to test.
vertex1
Vector3The first vertex of the triangle to test.
vertex2
Vector3The second vertex of the triagnle to test.
vertex3
Vector3The third vertex of the triangle to test.
Returns
 ContainmentType
The type of containment the two objects have.
SphereIntersectsSphere(ref readonly BoundingSphere, ref readonly BoundingSphere)
Determines whether there is an intersection between a BoundingSphere and a BoundingSphere.
public static bool SphereIntersectsSphere(ref readonly BoundingSphere sphere1, ref readonly BoundingSphere sphere2)
Parameters
sphere1
BoundingSphereFirst sphere to test.
sphere2
BoundingSphereSecond sphere to test.
Returns
 bool
Whether the two objects intersected.
SphereIntersectsTriangle(ref readonly BoundingSphere, ref readonly Vector3, ref readonly Vector3, ref readonly Vector3)
Determines whether there is an intersection between a BoundingSphere and a triangle.
public static bool SphereIntersectsTriangle(ref readonly BoundingSphere sphere, ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3)
Parameters
sphere
BoundingSphereThe sphere to test.
vertex1
Vector3The first vertex of the triangle to test.
vertex2
Vector3The second vertex of the triagnle to test.
vertex3
Vector3The third vertex of the triangle to test.
Returns
 bool
Whether the two objects intersected.