Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
TransformedShape.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
13
15
16struct RRayCast;
17struct RShapeCast;
19class RayCastResult;
20
26{
27public:
29
31 TransformedShape() = default;
32 TransformedShape(RVec3Arg inPositionCOM, QuatArg inRotation, const Shape *inShape, const BodyID &inBodyID, const SubShapeIDCreator &inSubShapeIDCreator = SubShapeIDCreator()) : mShapePositionCOM(inPositionCOM), mShapeRotation(inRotation), mShape(inShape), mBodyID(inBodyID), mSubShapeIDCreator(inSubShapeIDCreator) { }
33
37 bool CastRay(const RRayCast &inRay, RayCastResult &ioHit) const;
38
41 void CastRay(const RRayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const;
42
46 void CollidePoint(RVec3Arg inPoint, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const;
47
56 void CollideShape(const Shape *inShape, Vec3Arg inShapeScale, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const;
57
64 void CastShape(const RShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const;
65
68 void CollectTransformedShapes(const AABox &inBox, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const;
69
72
78 void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, RVec3Arg inBaseOffset) const;
79
86 int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const;
87
89 inline Vec3 GetShapeScale() const { return Vec3::sLoadFloat3Unsafe(mShapeScale); }
90 inline void SetShapeScale(Vec3Arg inScale) { inScale.StoreFloat3(&mShapeScale); }
91
93 inline RMat44 GetCenterOfMassTransform() const { return RMat44::sRotationTranslation(mShapeRotation, mShapePositionCOM); }
94
96 inline RMat44 GetInverseCenterOfMassTransform() const { return RMat44::sInverseRotationTranslation(mShapeRotation, mShapePositionCOM); }
97
99 inline void SetWorldTransform(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inScale)
100 {
101 mShapePositionCOM = inPosition + inRotation * (inScale * mShape->GetCenterOfMass());
102 mShapeRotation = inRotation;
103 SetShapeScale(inScale);
104 }
105
107 inline void SetWorldTransform(RMat44Arg inTransform)
108 {
109 Vec3 scale;
110 RMat44 rot_trans = inTransform.Decompose(scale);
111 SetWorldTransform(rot_trans.GetTranslation(), rot_trans.GetQuaternion(), scale);
112 }
113
116 {
117 RMat44 transform = RMat44::sRotation(mShapeRotation).PreScaled(GetShapeScale());
118 transform.SetTranslation(mShapePositionCOM - transform.Multiply3x3(mShape->GetCenterOfMass()));
119 return transform;
120 }
121
123 AABox GetWorldSpaceBounds() const { return mShape != nullptr? mShape->GetWorldSpaceBounds(GetCenterOfMassTransform(), GetShapeScale()) : AABox(); }
124
126 inline SubShapeID MakeSubShapeIDRelativeToShape(const SubShapeID &inSubShapeID) const
127 {
128 // Take off the sub shape ID part that comes from mSubShapeIDCreator and validate that it is the same
129 SubShapeID sub_shape_id;
130 uint num_bits_written = mSubShapeIDCreator.GetNumBitsWritten();
131 JPH_IF_ENABLE_ASSERTS(uint32 root_id =) inSubShapeID.PopID(num_bits_written, sub_shape_id);
132 JPH_ASSERT(root_id == (mSubShapeIDCreator.GetID().GetValue() & ((1 << num_bits_written) - 1)));
133 return sub_shape_id;
134 }
135
138 inline Vec3 GetWorldSpaceSurfaceNormal(const SubShapeID &inSubShapeID, RVec3Arg inPosition) const
139 {
140 RMat44 inv_com = GetInverseCenterOfMassTransform();
141 Vec3 scale = GetShapeScale(); // See comment at ScaledShape::GetSurfaceNormal for the math behind the scaling of the normal
142 return inv_com.Multiply3x3Transposed(mShape->GetSurfaceNormal(MakeSubShapeIDRelativeToShape(inSubShapeID), Vec3(inv_com * inPosition) / scale) / scale).Normalized();
143 }
144
151 void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, RVec3Arg inBaseOffset, Shape::SupportingFace &outVertices) const
152 {
153 Mat44 com = GetCenterOfMassTransform().PostTranslated(-inBaseOffset).ToMat44();
154 mShape->GetSupportingFace(MakeSubShapeIDRelativeToShape(inSubShapeID), com.Multiply3x3Transposed(inDirection), GetShapeScale(), com, outVertices);
155 }
156
158 inline const PhysicsMaterial *GetMaterial(const SubShapeID &inSubShapeID) const
159 {
160 return mShape->GetMaterial(MakeSubShapeIDRelativeToShape(inSubShapeID));
161 }
162
164 inline uint64 GetSubShapeUserData(const SubShapeID &inSubShapeID) const
165 {
166 return mShape->GetSubShapeUserData(MakeSubShapeIDRelativeToShape(inSubShapeID));
167 }
168
173 TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, SubShapeID &outRemainder) const
174 {
175 TransformedShape ts = mShape->GetSubShapeTransformedShape(inSubShapeID, Vec3::sZero(), mShapeRotation, GetShapeScale(), outRemainder);
176 ts.mShapePositionCOM += mShapePositionCOM;
177 return ts;
178 }
179
181 inline static BodyID sGetBodyID(const TransformedShape *inTS) { return inTS != nullptr? inTS->mBodyID : BodyID(); }
182
186 Float3 mShapeScale { 1, 1, 1 };
189};
190
191static_assert(JPH_CPU_ADDRESS_BITS != 64 || sizeof(TransformedShape) == JPH_IF_SINGLE_PRECISION_ELSE(64, 96), "Not properly packed");
192static_assert(alignof(TransformedShape) == JPH_RVECTOR_ALIGNMENT, "Not properly aligned");
193
#define JPH_EXPORT
Definition: Core.h:227
std::uint64_t uint64
Definition: Core.h:443
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_IF_SINGLE_PRECISION_ELSE(s, d)
Definition: Core.h:504
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_IF_ENABLE_ASSERTS(...)
Definition: IssueReporting.h:35
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
#define JPH_RVECTOR_ALIGNMENT
Definition: Real.h:34
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:94
Virtual interface that allows collecting multiple collision results.
Definition: CollisionCollector.h:45
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
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 PostTranslated(Vec3Arg inTranslation) const
Post multiply by translation matrix: result = Mat44::sTranslation(inTranslation) * this (i....
Definition: Mat44.inl:903
JPH_INLINE Quat GetQuaternion() const
Convert to quaternion.
Definition: Mat44.inl:783
JPH_INLINE Mat44 ToMat44() const
In single precision mode just return the matrix itself.
Definition: Mat44.h:225
JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const
Multiply vector by only 3x3 part of the transpose of the matrix ( )
Definition: Mat44.inl:336
JPH_INLINE Mat44 Decompose(Vec3 &outScale) const
Definition: Mat44.inl:919
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:316
static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT)
Get matrix that rotates and translates.
Definition: Mat44.inl:149
JPH_INLINE Vec3 GetTranslation() const
Definition: Mat44.h:152
static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle)
Rotate around arbitrary axis.
Definition: Mat44.inl:139
static JPH_INLINE Mat44 sInverseRotationTranslation(QuatArg inR, Vec3Arg inT)
Get inverse matrix of sRotationTranslation.
Definition: Mat44.inl:156
JPH_INLINE void SetTranslation(Vec3Arg inV)
Definition: Mat44.h:153
JPH_INLINE Mat44 PreScaled(Vec3Arg inScale) const
Scale a matrix: result = this * Mat44::sScale(inScale)
Definition: Mat44.inl:908
Definition: PhysicsMaterial.h:23
Definition: Quat.h:33
Specialization of cast result against a shape.
Definition: CastResult.h:30
Settings to be passed with a ray cast.
Definition: RayCast.h:70
Definition: Reference.h:151
Settings to be passed with a shape cast.
Definition: ShapeCast.h:92
Filter class.
Definition: ShapeFilter.h:17
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:178
Definition: SubShapeID.h:108
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Type PopID(uint inBits, SubShapeID &outRemainder) const
Get the next id in the chain of ids (pops parents before children)
Definition: SubShapeID.h:42
Definition: TransformedShape.h:26
void SetShapeScale(Vec3Arg inScale)
Definition: TransformedShape.h:90
SubShapeID MakeSubShapeIDRelativeToShape(const SubShapeID &inSubShapeID) const
Make inSubShapeID relative to mShape. When mSubShapeIDCreator is not empty, this is needed in order t...
Definition: TransformedShape.h:126
static BodyID sGetBodyID(const TransformedShape *inTS)
Helper function to return the body id from a transformed shape. If the transformed shape is null an i...
Definition: TransformedShape.h:181
Quat mShapeRotation
Rotation of the shape.
Definition: TransformedShape.h:184
AABox GetWorldSpaceBounds() const
Get the world space bounding box for this transformed shape.
Definition: TransformedShape.h:123
RVec3 mShapePositionCOM
Center of mass world position of the shape.
Definition: TransformedShape.h:183
Vec3 GetWorldSpaceSurfaceNormal(const SubShapeID &inSubShapeID, RVec3Arg inPosition) const
Definition: TransformedShape.h:138
SubShapeIDCreator mSubShapeIDCreator
Optional sub shape ID creator for the shape (can be used when expanding compound shapes into multiple...
Definition: TransformedShape.h:188
void SetWorldTransform(RMat44Arg inTransform)
Sets the world transform (including scale) of this transformed shape (not from the center of mass but...
Definition: TransformedShape.h:107
void SetWorldTransform(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inScale)
Sets the world transform (including scale) of this transformed shape (not from the center of mass but...
Definition: TransformedShape.h:99
const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const
Get material of a particular sub shape.
Definition: TransformedShape.h:158
TransformedShape(RVec3Arg inPositionCOM, QuatArg inRotation, const Shape *inShape, const BodyID &inBodyID, const SubShapeIDCreator &inSubShapeIDCreator=SubShapeIDCreator())
Definition: TransformedShape.h:32
TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, SubShapeID &outRemainder) const
Definition: TransformedShape.h:173
RMat44 GetInverseCenterOfMassTransform() const
Calculates the inverse of the transform for this shape's center of mass (excluding scale)
Definition: TransformedShape.h:96
uint64 GetSubShapeUserData(const SubShapeID &inSubShapeID) const
Get the user data of a particular sub shape.
Definition: TransformedShape.h:164
Vec3 GetShapeScale() const
Get/set the scale of the shape as a Vec3.
Definition: TransformedShape.h:89
JPH_OVERRIDE_NEW_DELETE TransformedShape()=default
Constructor.
void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, RVec3Arg inBaseOffset, Shape::SupportingFace &outVertices) const
Definition: TransformedShape.h:151
RefConst< Shape > mShape
The shape itself.
Definition: TransformedShape.h:185
BodyID mBodyID
Optional body ID from which this shape comes.
Definition: TransformedShape.h:187
RMat44 GetWorldTransform() const
Calculates the world transform including scale of this shape (not from the center of mass but in the ...
Definition: TransformedShape.h:115
RMat44 GetCenterOfMassTransform() const
Calculates the transform for this shapes's center of mass (excluding scale)
Definition: TransformedShape.h:93
Definition: Vec3.h:16
JPH_INLINE Vec3 Normalized() const
Normalize vector.
Definition: Vec3.inl:694
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition: Vec3.inl:757
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition: Vec3.inl:134
Definition: RayCast.h:52
Definition: ShapeCast.h:74
An opaque buffer that holds shape specific information during GetTrianglesStart/Next.
Definition: Shape.h:336