Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HeightFieldShape.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
9#ifdef JPH_DEBUG_RENDERER
11#endif // JPH_DEBUG_RENDERER
12
14
15class ConvexShape;
17class TempAllocator;
18
21{
23 constexpr float cNoCollisionValue = FLT_MAX;
24
26 constexpr int cStackSize = 128;
27
29 constexpr uint cNumBitsXY = 14;
30 constexpr uint cMaskBitsXY = (1 << cNumBitsXY) - 1;
31 constexpr uint cLevelShift = 2 * cNumBitsXY;
32
34 constexpr uint16 cNoCollisionValue16 = 0xffff;
35 constexpr uint16 cMaxHeightValue16 = 0xfffe;
36};
37
40{
42
43public:
46
53 HeightFieldShapeSettings(const float *inSamples, Vec3Arg inOffset, Vec3Arg inScale, uint32 inSampleCount, const uint8 *inMaterialIndices = nullptr, const PhysicsMaterialList &inMaterialList = PhysicsMaterialList());
54
55 // See: ShapeSettings
56 virtual ShapeResult Create() const override;
57
62 void DetermineMinAndMaxSample(float &outMinValue, float &outMaxValue, float &outQuantizationScale) const;
63
67 uint32 CalculateBitsPerSampleForError(float inMaxError) const;
68
71 Vec3 mOffset = Vec3::sZero();
72 Vec3 mScale = Vec3::sReplicate(1.0f);
73 uint32 mSampleCount = 0;
74
76 float mMinHeightValue = FLT_MAX;
77
79 float mMaxHeightValue = -FLT_MAX;
80
83 uint32 mMaterialsCapacity = 0;
84
88 uint32 mBlockSize = 2;
89
93 uint32 mBitsPerSample = 8;
94
97
100
103
107 float mActiveEdgeCosThresholdAngle = 0.996195f; // cos(5 degrees)
108};
109
116{
117public:
119
122 HeightFieldShape(const HeightFieldShapeSettings &inSettings, ShapeResult &outResult);
123 virtual ~HeightFieldShape() override;
124
126 Ref<HeightFieldShape> Clone() const;
127
128 // See Shape::MustBeStatic
129 virtual bool MustBeStatic() const override { return true; }
130
132 inline uint GetSampleCount() const { return mSampleCount; }
133
135 inline uint GetBlockSize() const { return mBlockSize; }
136
137 // See Shape::GetLocalBounds
138 virtual AABox GetLocalBounds() const override;
139
140 // See Shape::GetSubShapeIDBitsRecursive
141 virtual uint GetSubShapeIDBitsRecursive() const override { return GetSubShapeIDBits(); }
142
143 // See Shape::GetInnerRadius
144 virtual float GetInnerRadius() const override { return 0.0f; }
145
146 // See Shape::GetMassProperties
147 virtual MassProperties GetMassProperties() const override;
148
149 // See Shape::GetMaterial
150 virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override;
151
153 const PhysicsMaterial * GetMaterial(uint inX, uint inY) const;
154
155 // See Shape::GetSurfaceNormal
156 virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
157
158 // See Shape::GetSupportingFace
159 virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
160
161 // See Shape::GetSubmergedVolume
162 virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override { JPH_ASSERT(false, "Not supported"); }
163
164#ifdef JPH_DEBUG_RENDERER
165 // See Shape::Draw
166 virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
167#endif // JPH_DEBUG_RENDERER
168
169 // See Shape::CastRay
170 virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
171 virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
172
173 // See: Shape::CollidePoint
174 virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
175
176 // See: Shape::CollideSoftBodyVertices
177 virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;
178
179 // See Shape::GetTrianglesStart
180 virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
181
182 // See Shape::GetTrianglesNext
183 virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
184
187 Vec3 GetPosition(uint inX, uint inY) const;
188
190 bool IsNoCollision(uint inX, uint inY) const;
191
194 bool ProjectOntoSurface(Vec3Arg inLocalPosition, Vec3 &outSurfacePosition, SubShapeID &outSubShapeID) const;
195
201 void GetSubShapeCoordinates(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangleIndex) const;
202
204 float GetMinHeightValue() const { return mOffset.GetY(); }
205 float GetMaxHeightValue() const { return mOffset.GetY() + mScale.GetY() * HeightFieldShapeConstants::cMaxHeightValue16; }
206
215 void GetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY, float *outHeights, intptr_t inHeightsStride) const;
216
228 void SetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY, const float *inHeights, intptr_t inHeightsStride, TempAllocator &inAllocator, float inActiveEdgeCosThresholdAngle = 0.996195f);
229
231 const PhysicsMaterialList & GetMaterialList() const { return mMaterials; }
232
240 void GetMaterials(uint inX, uint inY, uint inSizeX, uint inSizeY, uint8 *outMaterials, intptr_t inMaterialsStride) const;
241
253 bool SetMaterials(uint inX, uint inY, uint inSizeX, uint inSizeY, const uint8 *inMaterials, intptr_t inMaterialsStride, const PhysicsMaterialList *inMaterialList, TempAllocator &inAllocator);
254
255 // See Shape
256 virtual void SaveBinaryState(StreamOut &inStream) const override;
257 virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
258 virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
259
260 // See Shape::GetStats
261 virtual Stats GetStats() const override;
262
263 // See Shape::GetVolume
264 virtual float GetVolume() const override { return 0; }
265
266#ifdef JPH_DEBUG_RENDERER
267 // Settings
269#endif // JPH_DEBUG_RENDERER
270
271 // Register shape functions with the registry
272 static void sRegister();
273
274protected:
275 // See: Shape::RestoreBinaryState
276 virtual void RestoreBinaryState(StreamIn &inStream) override;
277
278private:
279 class DecodingContext;
280 struct HSGetTrianglesContext;
281
283 void CacheValues();
284
286 void AllocateBuffers();
287
289 void CalculateActiveEdges(uint inX, uint inY, uint inSizeX, uint inSizeY, const float *inHeights, uint inHeightsStartX, uint inHeightsStartY, intptr_t inHeightsStride, float inHeightsScale, float inActiveEdgeCosThresholdAngle, TempAllocator &inAllocator);
290
292 void CalculateActiveEdges(const HeightFieldShapeSettings &inSettings);
293
295 void StoreMaterialIndices(const HeightFieldShapeSettings &inSettings);
296
298 inline uint GetNumBlocks() const { return mSampleCount / mBlockSize; }
299
301 static inline uint sGetMaxLevel(uint inNumBlocks) { return 32 - CountLeadingZeros(inNumBlocks - 1); }
302
304 static inline void sGetRangeBlockOffsetAndStride(uint inNumBlocks, uint inMaxLevel, uint &outRangeBlockOffset, uint &outRangeBlockStride);
305
307 inline void GetBlockOffsetAndScale(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, float &outBlockOffset, float &outBlockScale) const;
308
310 inline uint8 GetHeightSample(uint inX, uint inY) const;
311
313 inline Vec3 GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale, bool &outNoCollision) const;
314
316 uint GetSubShapeIDBits() const;
317
319 inline SubShapeID EncodeSubShapeID(const SubShapeIDCreator &inCreator, uint inX, uint inY, uint inTriangle) const;
320 inline void DecodeSubShapeID(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangle) const;
321
323 inline uint8 GetEdgeFlags(uint inX, uint inY, uint inTriangle) const;
324
325 // Helper functions called by CollisionDispatch
326 static void sCollideConvexVsHeightField(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter);
327 static void sCollideSphereVsHeightField(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter);
328 static void sCastConvexVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
329 static void sCastSphereVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
330
333 template <class Visitor>
334 void WalkHeightField(Visitor &ioVisitor) const;
335
337 struct alignas(16) RangeBlock
338 {
339 uint16 mMin[4];
340 uint16 mMax[4];
341 };
342
344 inline void GetRangeBlock(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, RangeBlock *&outBlock, uint &outIndexInBlock);
345
347 static const uint sGridOffsets[];
348
351 Vec3 mOffset = Vec3::sZero();
352 Vec3 mScale = Vec3::sReplicate(1.0f);
353
355 uint32 mSampleCount = 0;
356 uint32 mBlockSize = 2;
357 uint32 mHeightSamplesSize = 0;
358 uint32 mRangeBlocksSize = 0;
359 uint32 mActiveEdgesSize = 0;
360 uint8 mBitsPerSample = 8;
361 uint8 mSampleMask = 0xff;
364 RangeBlock * mRangeBlocks = nullptr;
365 uint8 * mHeightSamples = nullptr;
366 uint8 * mActiveEdges = nullptr;
367
369 PhysicsMaterialList mMaterials;
370 Array<uint8> mMaterialIndices;
371 uint32 mNumBitsPerMaterialIndex = 0;
372
373#ifdef JPH_DEBUG_RENDERER
375 mutable Array<DebugRenderer::GeometryRef> mGeometry;
376 mutable bool mCachedUseMaterialColors = false;
377#endif // JPH_DEBUG_RENDERER
378};
379
std::uint8_t uint8
Definition: Core.h:453
#define JPH_EXPORT
Definition: Core.h:236
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_IF_DEBUG_RENDERER(...)
Definition: Core.h:536
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
std::uint16_t uint16
Definition: Core.h:454
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
uint CountLeadingZeros(uint32 inValue)
Compute the number of leading zero bits (how many high bits are zero)
Definition: Math.h:131
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition: SerializableObject.h:109
EShapeSubType
This enumerates all shape types, each shape can return its type through Shape::GetSubType.
Definition: Shape.h:77
EShapeType
Shapes are categorized in groups, each shape can return which group it belongs to through its Shape::...
Definition: Shape.h:57
@ HeightField
Used by HeightFieldShape.
Array< PhysicsMaterialRefC > PhysicsMaterialList
Definition: Shape.h:53
Axis aligned box.
Definition: AABox.h:16
Settings to be passed with a collision query.
Definition: CollideShape.h:94
Definition: CollideSoftBodyVertexIterator.h:15
Virtual interface that allows collecting multiple collision results.
Definition: CollisionCollector.h:45
Class that holds an RGBA color with 8-bits per component.
Definition: Color.h:16
Base class for all convex shapes. Defines a virtual interface.
Definition: ConvexShape.h:36
Definition: DebugRenderer.h:47
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Definition: HeightFieldShape.cpp:1708
Definition: HeightFieldShape.h:116
const PhysicsMaterialList & GetMaterialList() const
Get the current list of materials, the indices returned by GetMaterials() will index into this list.
Definition: HeightFieldShape.h:231
uint GetSampleCount() const
Get the size of the height field. Note that this will always be rounded up to the nearest multiple of...
Definition: HeightFieldShape.h:132
virtual float GetInnerRadius() const override
Definition: HeightFieldShape.h:144
float GetMaxHeightValue() const
Definition: HeightFieldShape.h:205
float GetMinHeightValue() const
Get the range of height values that this height field can encode. Can be used to determine the allowe...
Definition: HeightFieldShape.h:204
JPH_OVERRIDE_NEW_DELETE HeightFieldShape()
Constructor.
Definition: HeightFieldShape.h:121
static bool sDrawTriangleOutlines
Definition: HeightFieldShape.h:268
virtual uint GetSubShapeIDBitsRecursive() const override
Get the max number of sub shape ID bits that are needed to be able to address any leaf shape in this ...
Definition: HeightFieldShape.h:141
virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override
Definition: HeightFieldShape.h:162
virtual bool MustBeStatic() const override
Check if this shape can only be used to create a static body or if it can also be dynamic/kinematic.
Definition: HeightFieldShape.h:129
uint GetBlockSize() const
Get the size of a block.
Definition: HeightFieldShape.h:135
virtual float GetVolume() const override
Definition: HeightFieldShape.h:264
Class that constructs a HeightFieldShape.
Definition: HeightFieldShape.h:40
HeightFieldShapeSettings()=default
Default constructor for deserialization.
Array< float > mHeightSamples
An array of mSampleCount^2 height samples. Samples are stored in row major order, so the sample at (x...
Definition: HeightFieldShape.h:96
Array< uint8 > mMaterialIndices
An array of (mSampleCount - 1)^2 material indices.
Definition: HeightFieldShape.h:99
PhysicsMaterialList mMaterials
The materials of square at (x, y) is: mMaterials[mMaterialIndices[x + y * (mSampleCount - 1)]].
Definition: HeightFieldShape.h:102
Describes the mass and inertia properties of a body. Used during body construction only.
Definition: MassProperties.h:16
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
Definition: PhysicsMaterial.h:23
An infinite plane described by the formula X . Normal + Constant = 0.
Definition: Plane.h:11
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:107
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:186
Definition: Shape.h:147
virtual ShapeResult Create() const =0
Create a shape according to the settings specified by this object.
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
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
Definition: TempAllocator.h:16
Definition: Vec3.h:17
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
static JPH_INLINE Vec3 sReplicate(float inV)
Replicate inV across all components.
Definition: Vec3.inl:118
Constants for HeightFieldShape, this was moved out of the HeightFieldShape because of a linker bug.
Definition: HeightFieldShape.h:21
constexpr uint cNumBitsXY
A position in the hierarchical grid is defined by a level (which grid), x and y position....
Definition: HeightFieldShape.h:29
constexpr uint cMaskBitsXY
Definition: HeightFieldShape.h:30
constexpr int cStackSize
Stack size to use during WalkHeightField.
Definition: HeightFieldShape.h:26
constexpr uint16 cMaxHeightValue16
This is the maximum allowed height value.
Definition: HeightFieldShape.h:35
constexpr float cNoCollisionValue
Value used to create gaps in the height field.
Definition: HeightFieldShape.h:23
constexpr uint16 cNoCollisionValue16
When height samples are converted to 16 bit:
Definition: HeightFieldShape.h:34
constexpr uint cLevelShift
Definition: HeightFieldShape.h:31
Definition: HeightFieldShape.cpp:2386
Definition: RayCast.h:47
Definition: ShapeCast.h:69