Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ShapeCast.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
10
12
14template <class Vec, class Mat, class ShapeCastType>
16{
18
20 ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection, const AABox &inWorldSpaceBounds) :
21 mShape(inShape),
22 mScale(inScale),
23 mCenterOfMassStart(inCenterOfMassStart),
24 mDirection(inDirection),
25 mShapeWorldBounds(inWorldSpaceBounds)
26 {
27 }
28
30 ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection) :
31 ShapeCastT<Vec, Mat, ShapeCastType>(inShape, inScale, inCenterOfMassStart, inDirection, inShape->GetWorldSpaceBounds(inCenterOfMassStart, inScale))
32 {
33 }
34
36 static inline ShapeCastType sFromWorldTransform(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inWorldTransform, Vec3Arg inDirection)
37 {
38 return ShapeCastType(inShape, inScale, inWorldTransform.PreTranslated(inShape->GetCenterOfMass()), inDirection);
39 }
40
42 ShapeCastType PostTransformed(typename Mat::ArgType inTransform) const
43 {
44 Mat44 start = inTransform * mCenterOfMassStart;
45 Vec3 direction = inTransform.Multiply3x3(mDirection);
46 return { mShape, mScale, start, direction };
47 }
48
50 ShapeCastType PostTranslated(typename Vec::ArgType inTranslation) const
51 {
52 return { mShape, mScale, mCenterOfMassStart.PostTranslated(inTranslation), mDirection };
53 }
54
56 inline Vec GetPointOnRay(float inFraction) const
57 {
58 return mCenterOfMassStart.GetTranslation() + inFraction * mDirection;
59 }
60
61 const Shape * mShape;
62 const Vec3 mScale;
66};
67
68struct ShapeCast : public ShapeCastT<Vec3, Mat44, ShapeCast>
69{
71};
72
73struct RShapeCast : public ShapeCastT<RVec3, RMat44, RShapeCast>
74{
76
78 explicit RShapeCast(const ShapeCast &inCast) :
79 RShapeCast(inCast.mShape, inCast.mScale, RMat44(inCast.mCenterOfMassStart), inCast.mDirection, inCast.mShapeWorldBounds)
80 {
81 }
82
84 explicit operator ShapeCast() const
85 {
87 }
88};
89
92{
93public:
95
97 EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
98
100 EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
101
104
107};
108
111{
112public:
114
116 ShapeCastResult() = default;
117
127 ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2) :
128 CollideShapeResult(inContactPoint1, inContactPoint2, inContactNormalOrPenetrationDepth, (inContactPoint2 - inContactPoint1).Length(), inSubShapeID1, inSubShapeID2, inBodyID2),
129 mFraction(inFraction),
130 mIsBackFaceHit(inBackFaceHit)
131 {
132 }
133
135 inline float GetEarlyOutFraction() const { return mFraction > 0.0f? mFraction : -mPenetrationDepth; }
136
139 ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const
140 {
141 // Calculate by how much to shift the contact points
142 Vec3 delta = mFraction * inWorldSpaceCastDirection;
143
144 ShapeCastResult result;
145 result.mContactPointOn2 = mContactPointOn1 - delta;
146 result.mContactPointOn1 = mContactPointOn2 - delta;
149 result.mSubShapeID2 = mSubShapeID1;
150 result.mSubShapeID1 = mSubShapeID2;
151 result.mBodyID2 = mBodyID2;
152 result.mFraction = mFraction;
154
156 for (Face::size_type i = 0; i < mShape1Face.size(); ++i)
157 result.mShape2Face[i] = mShape1Face[i] - delta;
158
160 for (Face::size_type i = 0; i < mShape2Face.size(); ++i)
161 result.mShape1Face[i] = mShape2Face[i] - delta;
162
163 return result;
164 }
165
166 float mFraction;
168};
169
EBackFaceMode
How collision detection functions will treat back facing triangles.
Definition: BackFaceMode.h:11
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
Mat44 RMat44
Definition: Real.h:31
Vec3 RVec3
Definition: Real.h:29
Axis aligned box.
Definition: AABox.h:16
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Settings to be passed with a collision query.
Definition: CollideShape.h:72
Class that contains all information of two colliding shapes.
Definition: CollideShape.h:19
SubShapeID mSubShapeID1
Sub shape ID that identifies the face on shape 1.
Definition: CollideShape.h:63
BodyID mBodyID2
BodyID to which shape 2 belongs to.
Definition: CollideShape.h:65
Vec3 mContactPointOn2
Contact point on the surface of shape 2 (in world space or relative to base offset)....
Definition: CollideShape.h:60
Vec3 mPenetrationAxis
Direction to move shape 2 out of collision along the shortest path (magnitude is meaningless,...
Definition: CollideShape.h:61
Vec3 mContactPointOn1
Contact point on the surface of shape 1 (in world space or relative to base offset)
Definition: CollideShape.h:59
float mPenetrationDepth
Penetration depth (move shape 2 by this distance to resolve the collision)
Definition: CollideShape.h:62
Face mShape1Face
Colliding face on shape 1 (optional result, in world space or relative to base offset)
Definition: CollideShape.h:66
SubShapeID mSubShapeID2
Sub shape ID that identifies the face on shape 2.
Definition: CollideShape.h:64
Face mShape2Face
Colliding face on shape 2 (optional result, in world space or relative to base offset)
Definition: CollideShape.h:67
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
JPH_INLINE Mat44 ToMat44() const
In single precision mode just return the matrix itself.
Definition: Mat44.h:225
Result of a shape cast test.
Definition: ShapeCast.h:111
float GetEarlyOutFraction() const
Function required by the CollisionCollector. A smaller fraction is considered to be a 'better hit'....
Definition: ShapeCast.h:135
bool mIsBackFaceHit
True if the shape was hit from the back side.
Definition: ShapeCast.h:167
JPH_OVERRIDE_NEW_DELETE ShapeCastResult()=default
Default constructor.
float mFraction
This is the fraction where the shape hit the other shape: CenterOfMassOnHit = Start + value * (End - ...
Definition: ShapeCast.h:166
ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const
Definition: ShapeCast.h:139
ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2)
Definition: ShapeCast.h:127
Settings to be passed with a shape cast.
Definition: ShapeCast.h:92
JPH_OVERRIDE_NEW_DELETE EBackFaceMode mBackFaceModeTriangles
How backfacing triangles should be treated (should we report moving out of a triangle?...
Definition: ShapeCast.h:97
bool mReturnDeepestPoint
When true, and the shape is intersecting at the beginning of the cast (fraction = 0) then this will c...
Definition: ShapeCast.h:106
EBackFaceMode mBackFaceModeConvex
How backfacing convex objects should be treated (should we report starting inside an object and movin...
Definition: ShapeCast.h:100
bool mUseShrunkenShapeAndConvexRadius
Indicates if we want to shrink the shape by the convex radius and then expand it again....
Definition: ShapeCast.h:103
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:178
virtual Vec3 GetCenterOfMass() const
All shapes are centered around their center of mass. This function returns the center of mass positio...
Definition: Shape.h:203
uint size_type
Definition: StaticArray.h:16
size_type size() const
Returns amount of elements in the array.
Definition: StaticArray.h:87
void resize(size_type inNewSize)
Resize array to new length.
Definition: StaticArray.h:99
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Definition: Vec3.h:16
Definition: ShapeCast.h:74
RShapeCast(const ShapeCast &inCast)
Convert from ShapeCast, converts single to double precision.
Definition: ShapeCast.h:78
Definition: ShapeCast.h:69
Structure that holds a single shape cast (a shape moving along a linear path in 3d space with no rota...
Definition: ShapeCast.h:16
static ShapeCastType sFromWorldTransform(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inWorldTransform, Vec3Arg inDirection)
Construct a shape cast using a world transform for a shape instead of a center of mass transform.
Definition: ShapeCast.h:36
ShapeCastType PostTranslated(typename Vec::ArgType inTranslation) const
Translate this shape cast by inTranslation.
Definition: ShapeCast.h:50
JPH_OVERRIDE_NEW_DELETE ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection, const AABox &inWorldSpaceBounds)
Constructor.
Definition: ShapeCast.h:20
const Vec3 mDirection
Direction and length of the cast (anything beyond this length will not be reported as a hit)
Definition: ShapeCast.h:64
const Shape * mShape
Shape that's being cast (cannot be mesh shape). Note that this structure does not assume ownership ov...
Definition: ShapeCast.h:61
ShapeCastT(const Shape *inShape, Vec3Arg inScale, typename Mat::ArgType inCenterOfMassStart, Vec3Arg inDirection)
Constructor.
Definition: ShapeCast.h:30
Vec GetPointOnRay(float inFraction) const
Get point with fraction inFraction on ray from mCenterOfMassStart to mCenterOfMassStart + mDirection ...
Definition: ShapeCast.h:56
ShapeCastType PostTransformed(typename Mat::ArgType inTransform) const
Transform this shape cast using inTransform. Multiply transform on the left left hand side.
Definition: ShapeCast.h:42
const Mat mCenterOfMassStart
Start position and orientation of the center of mass of the shape (construct using sFromWorldTransfor...
Definition: ShapeCast.h:63
const Vec3 mScale
Scale in local space of the shape being cast.
Definition: ShapeCast.h:62
const AABox mShapeWorldBounds
Cached shape's world bounds, calculated in constructor.
Definition: ShapeCast.h:65