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 BoundingBox, ref BoundingBox)
Determines whether a BoundingBox contains a BoundingBox.
public static ContainmentType BoxContainsBox(ref BoundingBox box1, ref 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 BoundingBox, ref Vector3)
Determines whether a BoundingBox contains a point.
public static ContainmentType BoxContainsPoint(ref BoundingBox box, ref 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 BoundingBox, ref BoundingSphere)
Determines whether a BoundingBox contains a BoundingSphere.
public static ContainmentType BoxContainsSphere(ref BoundingBox box, ref 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 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
box1
BoundingBoxThe first box to test.
box2
BoundingBoxThe second box to test.
Returns
- bool
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
box
BoundingBoxThe box to test.
sphere
BoundingSphereThe sphere to test.
Returns
- bool
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
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 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
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 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
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 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
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 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
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 BoundingBox, ref BoundingBox)
Determines the distance between a BoundingBox and a BoundingBox.
public static float DistanceBoxBox(ref BoundingBox box1, ref 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 BoundingBox, ref Vector3)
Determines the distance between a BoundingBox and a point.
public static float DistanceBoxPoint(ref BoundingBox box, ref Vector3 point)
Parameters
box
BoundingBoxThe box to test.
point
Vector3The point to test.
Returns
- float
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
Returns
- float
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
sphere
BoundingSphereThe sphere to test.
point
Vector3The point to test.
Returns
- float
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
sphere1
BoundingSphereThe first sphere to test.
sphere2
BoundingSphereThe second sphere to test.
Returns
- float
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
frustum
BoundingFrustumThe frustum.
boundingBoxExt
BoundingBoxExtThe bounding box ext.
Returns
- bool
true
if XXXX,false
otherwise.
GetNearestHit<T>(IEnumerable<T>, ref 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 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 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
plane
PlaneThe plane to test.
box
BoundingBoxThe box to test.
Returns
- PlaneIntersectionType
Whether the two objects intersected.
PlaneIntersectsPlane(ref Plane, ref Plane)
public static bool PlaneIntersectsPlane(ref Plane plane1, ref Plane plane2)
Parameters
Returns
- bool
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
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 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
Returns
- 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
plane
PlaneThe plane to test.
sphere
BoundingSphereThe sphere to test.
Returns
- 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
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 Ray, ref BoundingBox, out Vector3)
public static bool RayIntersectsBox(ref Ray ray, ref 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 Ray, ref BoundingBox, out float)
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
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 Ray, ref Plane, out Vector3)
public static bool RayIntersectsPlane(ref Ray ray, ref 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 Ray, ref Plane, out float)
public static bool RayIntersectsPlane(ref Ray ray, ref 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 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
Returns
- bool
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
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 Ray, ref Matrix, ref Vector3, int, 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
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 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
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 Ray, ref BoundingSphere, out float)
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
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 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
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 Ray, ref Vector3, ref Vector3, ref Vector3, out float)
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
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 BoundingSphere, ref BoundingBox)
Determines whether a BoundingSphere contains a BoundingBox.
public static ContainmentType SphereContainsBox(ref BoundingSphere sphere, ref 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 BoundingSphere, ref Vector3)
Determines whether a BoundingSphere contains a point.
public static ContainmentType SphereContainsPoint(ref BoundingSphere sphere, ref 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 BoundingSphere, ref BoundingSphere)
Determines whether a BoundingSphere contains a BoundingSphere.
public static ContainmentType SphereContainsSphere(ref BoundingSphere sphere1, ref 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 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
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 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
sphere1
BoundingSphereFirst sphere to test.
sphere2
BoundingSphereSecond sphere to test.
Returns
- bool
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
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.