CollisionHelper Class
Namespace: Stride.Core.MathematicsAssembly: Stride.Core.Mathematics.dll
Contains static methods to help in determining intersections, containment, etc.
public static class CollisionHelper
Methods
BoxContainsBox(ref BoundingBox, ref BoundingBox)
Determines whether a BoundingBox contains a BoundingBox.
public static ContainmentType BoxContainsBox(ref BoundingBox box1, ref BoundingBox box2)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box1 | The first box to test. |
BoundingBox | box2 | The second box to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
BoxContainsPoint(ref BoundingBox, ref Vector3)
Determines whether a BoundingBox contains a point.
public static ContainmentType BoxContainsPoint(ref BoundingBox box, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | The box to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
BoxContainsSphere(ref BoundingBox, ref BoundingSphere)
Determines whether a BoundingBox contains a BoundingSphere.
public static ContainmentType BoxContainsSphere(ref BoundingBox box, ref BoundingSphere sphere)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | The box to test. |
BoundingSphere | sphere | The sphere to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
BoxIntersectsBox(ref BoundingBox, ref BoundingBox)
Determines whether there is an intersection between a BoundingBox and a BoundingBox.
public static bool BoxIntersectsBox(ref BoundingBox box1, ref BoundingBox box2)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box1 | The first box to test. |
BoundingBox | box2 | The second box to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
BoxIntersectsSphere(ref BoundingBox, ref BoundingSphere)
Determines whether there is an intersection between a BoundingBox and a BoundingSphere.
public static bool BoxIntersectsSphere(ref BoundingBox box, ref BoundingSphere sphere)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | The box to test. |
BoundingSphere | sphere | The sphere to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
ClosestPointBoxPoint(ref BoundingBox, ref Vector3, out Vector3)
Determines the closest point between a BoundingBox and a point.
public static void ClosestPointBoxPoint(ref BoundingBox box, ref Vector3 point, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | The box to test. |
Vector3 | point | The point to test. |
Vector3 | result | When the method completes, contains the closest point between the two objects. |
ClosestPointPlanePoint(ref Plane, ref Vector3, out Vector3)
Determines the closest point between a Plane and a point.
public static void ClosestPointPlanePoint(ref Plane plane, ref Vector3 point, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
Vector3 | point | The point to test. |
Vector3 | result | When the method completes, contains the closest point between the two objects. |
ClosestPointPointTriangle(ref Vector3, ref Vector3, ref Vector3, ref Vector3, out Vector3)
Determines the closest point between a point and a triangle.
public static void ClosestPointPointTriangle(ref Vector3 point, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | The point to test. |
Vector3 | vertex1 | The first vertex to test. |
Vector3 | vertex2 | The second vertex to test. |
Vector3 | vertex3 | The third vertex to test. |
Vector3 | result | When the method completes, contains the closest point between the two objects. |
ClosestPointSpherePoint(ref BoundingSphere, ref Vector3, out Vector3)
Determines the closest point between a BoundingSphere and a point.
public static void ClosestPointSpherePoint(ref BoundingSphere sphere, ref Vector3 point, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The bounding sphere. |
Vector3 | point | The point to test. |
Vector3 | result | When 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 BoundingSphere, ref BoundingSphere, out Vector3)
Determines the closest point between a BoundingSphere and a BoundingSphere.
public static void ClosestPointSphereSphere(ref BoundingSphere sphere1, ref BoundingSphere sphere2, out Vector3 result)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere1 | The first sphere to test. |
BoundingSphere | sphere2 | The second sphere to test. |
Vector3 | result | When 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 BoundingBox, ref BoundingBox)
Determines the distance between a BoundingBox and a BoundingBox.
public static float DistanceBoxBox(ref BoundingBox box1, ref BoundingBox box2)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box1 | The first box to test. |
BoundingBox | box2 | The second box to test. |
Returns
Type | Description |
---|---|
System.Single | The distance between the two objects. |
DistanceBoxPoint(ref BoundingBox, ref Vector3)
Determines the distance between a BoundingBox and a point.
public static float DistanceBoxPoint(ref BoundingBox box, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | The box to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
System.Single | The distance between the two objects. |
DistancePlanePoint(ref Plane, ref Vector3)
Determines the distance between a Plane and a point.
public static float DistancePlanePoint(ref Plane plane, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
System.Single | The distance between the two objects. |
DistanceSpherePoint(ref BoundingSphere, ref Vector3)
Determines the distance between a BoundingSphere and a point.
public static float DistanceSpherePoint(ref BoundingSphere sphere, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The sphere to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
System.Single | The distance between the two objects. |
DistanceSphereSphere(ref BoundingSphere, ref BoundingSphere)
Determines the distance between a BoundingSphere and a BoundingSphere.
public static float DistanceSphereSphere(ref BoundingSphere sphere1, ref BoundingSphere sphere2)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere1 | The first sphere to test. |
BoundingSphere | sphere2 | The second sphere to test. |
Returns
Type | Description |
---|---|
System.Single | The distance between the two objects. |
FrustumContainsBox(ref BoundingFrustum, ref 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 BoundingFrustum frustum, ref BoundingBoxExt boundingBoxExt)
Parameters
Type | Name | Description |
---|---|---|
BoundingFrustum | frustum | The frustum. |
BoundingBoxExt | boundingBoxExt | The bounding box ext. |
Returns
Type | Description |
---|---|
System.Boolean |
|
GetNearestHit<T>(IEnumerable<T>, ref Ray, out T, out Single, 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 Ray ray, out T hitObject, out float distance, out Vector3 point)
where T : IIntersectableWithRay
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | objects | The objects that get tested for a collision with the ray. |
Ray | ray | The ray. |
T | hitObject | The hit object. |
System.Single | distance | The distance from the start of the ray. |
Vector3 | point | The position of the collision. |
Returns
Type | Description |
---|---|
System.Boolean | Whether there was a hit. |
Type Parameters
Name | Description |
---|---|
T |
PlaneIntersectsBox(ref Plane, ref BoundingBox)
Determines whether there is an intersection between a Plane and a BoundingBox.
public static PlaneIntersectionType PlaneIntersectsBox(ref Plane plane, ref BoundingBox box)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
BoundingBox | box | The box to test. |
Returns
Type | Description |
---|---|
PlaneIntersectionType | Whether the two objects intersected. |
PlaneIntersectsPlane(ref Plane, ref Plane)
public static bool PlaneIntersectsPlane(ref Plane plane1, ref Plane plane2)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane1 | The first plane to test. |
Plane | plane2 | The second plane to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
PlaneIntersectsPlane(ref Plane, ref Plane, out Ray)
public static bool PlaneIntersectsPlane(ref Plane plane1, ref Plane plane2, out Ray line)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane1 | The first plane to test. |
Plane | plane2 | The second plane to test. |
Ray | line | When the method completes, contains the line of intersection as a Ray, or a zero ray if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | 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 Plane, ref Vector3)
Determines whether there is an intersection between a Plane and a point.
public static PlaneIntersectionType PlaneIntersectsPoint(ref Plane plane, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
PlaneIntersectionType | Whether the two objects intersected. |
PlaneIntersectsSphere(ref Plane, ref BoundingSphere)
Determines whether there is an intersection between a Plane and a BoundingSphere.
public static PlaneIntersectionType PlaneIntersectsSphere(ref Plane plane, ref BoundingSphere sphere)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
BoundingSphere | sphere | The sphere to test. |
Returns
Type | Description |
---|---|
PlaneIntersectionType | Whether the two objects intersected. |
PlaneIntersectsTriangle(ref Plane, ref Vector3, ref Vector3, ref Vector3)
Determines whether there is an intersection between a Plane and a triangle.
public static PlaneIntersectionType PlaneIntersectsTriangle(ref Plane plane, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Parameters
Type | Name | Description |
---|---|---|
Plane | plane | The plane to test. |
Vector3 | vertex1 | The first vertex of the triangle to test. |
Vector3 | vertex2 | The second vertex of the triagnle to test. |
Vector3 | vertex3 | The third vertex of the triangle to test. |
Returns
Type | Description |
---|---|
PlaneIntersectionType | Whether the two objects intersected. |
RayIntersectsBox(ref Ray, ref BoundingBox, out Vector3)
public static bool RayIntersectsBox(ref Ray ray, ref BoundingBox box, out Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
BoundingBox | box | The box to test. |
Vector3 | point | When the method completes, contains the point of intersection, or Zero if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsBox(ref Ray, ref BoundingBox, out Single)
Determines whether there is an intersection between a Ray and a BoundingBox.
public static bool RayIntersectsBox(ref Ray ray, ref BoundingBox box, out float distance)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
BoundingBox | box | The box to test. |
System.Single | distance | When the method completes, contains the distance of the intersection, or 0 if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsPlane(ref Ray, ref Plane, out Vector3)
public static bool RayIntersectsPlane(ref Ray ray, ref Plane plane, out Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
Plane | plane | The plane to test |
Vector3 | point | When the method completes, contains the point of intersection, or Zero if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsPlane(ref Ray, ref Plane, out Single)
public static bool RayIntersectsPlane(ref Ray ray, ref Plane plane, out float distance)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
Plane | plane | The plane to test. |
System.Single | distance | When the method completes, contains the distance of the intersection, or 0 if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersect. |
RayIntersectsPoint(ref Ray, ref Vector3)
Determines whether there is an intersection between a Ray and a point.
public static bool RayIntersectsPoint(ref Ray ray, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersect. |
RayIntersectsRay(ref Ray, ref Ray, out Vector3)
public static bool RayIntersectsRay(ref Ray ray1, ref Ray ray2, out Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray1 | The first ray to test. |
Ray | ray2 | The second ray to test. |
Vector3 | point | When the method completes, contains the point of intersection, or Zero if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | 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 Ray, ref Matrix, ref Vector3, Int32, out Vector3)
Determines whether there is an intersection between a Ray and a rectangle (2D).
public static bool RayIntersectsRectangle(ref Ray ray, ref Matrix rectangleWorldMatrix, ref Vector3 rectangleSize, int normalAxis, out Vector3 intersectionPoint)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test |
Matrix | rectangleWorldMatrix | The world matrix applied on the rectangle |
Vector3 | rectangleSize | The size of the rectangle in 3D |
System.Int32 | normalAxis | The index of axis defining the normal of the rectangle in the world. This value should be 0, 1 or 2 |
Vector3 | intersectionPoint | The position of the intersection point in the world |
Returns
Type | Description |
---|---|
System.Boolean |
RayIntersectsSphere(ref Ray, ref BoundingSphere, out Vector3)
Determines whether there is an intersection between a Ray and a BoundingSphere.
public static bool RayIntersectsSphere(ref Ray ray, ref BoundingSphere sphere, out Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
BoundingSphere | sphere | The sphere to test. |
Vector3 | point | When the method completes, contains the point of intersection, or Zero if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsSphere(ref Ray, ref BoundingSphere, out Single)
Determines whether there is an intersection between a Ray and a BoundingSphere.
public static bool RayIntersectsSphere(ref Ray ray, ref BoundingSphere sphere, out float distance)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
BoundingSphere | sphere | The sphere to test. |
System.Single | distance | When the method completes, contains the distance of the intersection, or 0 if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsTriangle(ref Ray, ref Vector3, ref Vector3, ref Vector3, out Vector3)
Determines whether there is an intersection between a Ray and a triangle.
public static bool RayIntersectsTriangle(ref Ray ray, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
Vector3 | vertex1 | The first vertex of the triangle to test. |
Vector3 | vertex2 | The second vertex of the triangle to test. |
Vector3 | vertex3 | The third vertex of the triangle to test. |
Vector3 | point | When the method completes, contains the point of intersection, or Zero if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
RayIntersectsTriangle(ref Ray, ref Vector3, ref Vector3, ref Vector3, out Single)
Determines whether there is an intersection between a Ray and a triangle.
public static bool RayIntersectsTriangle(ref Ray ray, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out float distance)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray to test. |
Vector3 | vertex1 | The first vertex of the triangle to test. |
Vector3 | vertex2 | The second vertex of the triagnle to test. |
Vector3 | vertex3 | The third vertex of the triangle to test. |
System.Single | distance | When the method completes, contains the distance of the intersection, or 0 if there was no intersection. |
Returns
Type | Description |
---|---|
System.Boolean | 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 BoundingSphere, ref BoundingBox)
Determines whether a BoundingSphere contains a BoundingBox.
public static ContainmentType SphereContainsBox(ref BoundingSphere sphere, ref BoundingBox box)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The sphere to test. |
BoundingBox | box | The box to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
SphereContainsPoint(ref BoundingSphere, ref Vector3)
Determines whether a BoundingSphere contains a point.
public static ContainmentType SphereContainsPoint(ref BoundingSphere sphere, ref Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The sphere to test. |
Vector3 | point | The point to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
SphereContainsSphere(ref BoundingSphere, ref BoundingSphere)
Determines whether a BoundingSphere contains a BoundingSphere.
public static ContainmentType SphereContainsSphere(ref BoundingSphere sphere1, ref BoundingSphere sphere2)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere1 | The first sphere to test. |
BoundingSphere | sphere2 | The second sphere to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
SphereContainsTriangle(ref BoundingSphere, ref Vector3, ref Vector3, ref Vector3)
Determines whether a BoundingSphere contains a triangle.
public static ContainmentType SphereContainsTriangle(ref BoundingSphere sphere, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The sphere to test. |
Vector3 | vertex1 | The first vertex of the triangle to test. |
Vector3 | vertex2 | The second vertex of the triagnle to test. |
Vector3 | vertex3 | The third vertex of the triangle to test. |
Returns
Type | Description |
---|---|
ContainmentType | The type of containment the two objects have. |
SphereIntersectsSphere(ref BoundingSphere, ref BoundingSphere)
Determines whether there is an intersection between a BoundingSphere and a BoundingSphere.
public static bool SphereIntersectsSphere(ref BoundingSphere sphere1, ref BoundingSphere sphere2)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere1 | First sphere to test. |
BoundingSphere | sphere2 | Second sphere to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |
SphereIntersectsTriangle(ref BoundingSphere, ref Vector3, ref Vector3, ref Vector3)
Determines whether there is an intersection between a BoundingSphere and a triangle.
public static bool SphereIntersectsTriangle(ref BoundingSphere sphere, ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Parameters
Type | Name | Description |
---|---|---|
BoundingSphere | sphere | The sphere to test. |
Vector3 | vertex1 | The first vertex of the triangle to test. |
Vector3 | vertex2 | The second vertex of the triagnle to test. |
Vector3 | vertex3 | The third vertex of the triangle to test. |
Returns
Type | Description |
---|---|
System.Boolean | Whether the two objects intersected. |