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
98
100 EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
101
103 EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
104
107
110};
111
114{
115public:
117
119 ShapeCastResult() = default;
120
130 ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2) :
131 CollideShapeResult(inContactPoint1, inContactPoint2, inContactNormalOrPenetrationDepth, (inContactPoint2 - inContactPoint1).Length(), inSubShapeID1, inSubShapeID2, inBodyID2),
132 mFraction(inFraction),
133 mIsBackFaceHit(inBackFaceHit)
134 {
135 }
136
138 inline float GetEarlyOutFraction() const { return mFraction > 0.0f? mFraction : -mPenetrationDepth; }
139
142 ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const
143 {
144 // Calculate by how much to shift the contact points
145 Vec3 delta = mFraction * inWorldSpaceCastDirection;
146
147 ShapeCastResult result;
148 result.mContactPointOn2 = mContactPointOn1 - delta;
149 result.mContactPointOn1 = mContactPointOn2 - delta;
152 result.mSubShapeID2 = mSubShapeID1;
153 result.mSubShapeID1 = mSubShapeID2;
154 result.mBodyID2 = mBodyID2;
155 result.mFraction = mFraction;
157
159 for (Face::size_type i = 0; i < mShape1Face.size(); ++i)
160 result.mShape2Face[i] = mShape1Face[i] - delta;
161
163 for (Face::size_type i = 0; i < mShape2Face.size(); ++i)
164 result.mShape1Face[i] = mShape2Face[i] - delta;
165
166 return result;
167 }
168
169 float mFraction;
171};
172
EBackFaceMode
How collision detection functions will treat back facing triangles.
Definition: BackFaceMode.h:11
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
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:114
float GetEarlyOutFraction() const
Function required by the CollisionCollector. A smaller fraction is considered to be a 'better hit'....
Definition: ShapeCast.h:138
bool mIsBackFaceHit
True if the shape was hit from the back side.
Definition: ShapeCast.h:170
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:169
ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const
Definition: ShapeCast.h:142
ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2)
Definition: ShapeCast.h:130
Settings to be passed with a shape cast.
Definition: ShapeCast.h:92
EBackFaceMode mBackFaceModeTriangles
How backfacing triangles should be treated (should we report moving from back to front for triangle b...
Definition: ShapeCast.h:100
bool mReturnDeepestPoint
When true, and the shape is intersecting at the beginning of the cast (fraction = 0) then this will c...
Definition: ShapeCast.h:109
EBackFaceMode mBackFaceModeConvex
How backfacing convex objects should be treated (should we report starting inside an object and movin...
Definition: ShapeCast.h:103
JPH_OVERRIDE_NEW_DELETE void SetBackFaceMode(EBackFaceMode inMode)
Set the backfacing mode for all shapes.
Definition: ShapeCast.h:97
bool mUseShrunkenShapeAndConvexRadius
Indicates if we want to shrink the shape by the convex radius and then expand it again....
Definition: ShapeCast.h:106
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:186
virtual Vec3 GetCenterOfMass() const
All shapes are centered around their center of mass. This function returns the center of mass positio...
Definition: Shape.h:211
uint size_type
Definition: StaticArray.h:18
size_type size() const
Returns amount of elements in the array.
Definition: StaticArray.h:89
void resize(size_type inNewSize)
Resize array to new length.
Definition: StaticArray.h:101
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:17
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 (scales relative to its center of mass)
Definition: ShapeCast.h:62
const AABox mShapeWorldBounds
Cached shape's world bounds, calculated in constructor.
Definition: ShapeCast.h:65