Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/patches/0005-backport-upstream-commit-449b645.patch
45998 views
1
diff --git a/thirdparty/jolt_physics/Jolt/Physics/Collision/CollideShape.h b/thirdparty/jolt_physics/Jolt/Physics/Collision/CollideShape.h
2
index cfec8139..134f300d 100644
3
--- a/thirdparty/jolt_physics/Jolt/Physics/Collision/CollideShape.h
4
+++ b/thirdparty/jolt_physics/Jolt/Physics/Collision/CollideShape.h
5
@@ -101,6 +101,9 @@ public:
6
7
/// How backfacing triangles should be treated
8
EBackFaceMode mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
9
+
10
+ /// Max squared distance to consider a vertex to be the same as another vertex, used by the internal edge removal algorithm to determine if two edges are shared. (unit: meter^2)
11
+ float mInternalEdgeRemovalVertexToleranceSq = cDefaultInternalEdgeRemovalVertexToleranceSq;
12
};
13
14
JPH_NAMESPACE_END
15
diff --git a/thirdparty/jolt_physics/Jolt/Physics/Collision/InternalEdgeRemovingCollector.h b/thirdparty/jolt_physics/Jolt/Physics/Collision/InternalEdgeRemovingCollector.h
16
index cec7d7be..eee55022 100644
17
--- a/thirdparty/jolt_physics/Jolt/Physics/Collision/InternalEdgeRemovingCollector.h
18
+++ b/thirdparty/jolt_physics/Jolt/Physics/Collision/InternalEdgeRemovingCollector.h
19
@@ -31,7 +31,7 @@ class InternalEdgeRemovingCollector : public CollideShapeCollector
20
{
21
for (const Voided &vf : mVoidedFeatures)
22
if (vf.mSubShapeID == inSubShapeID
23
- && inV.IsClose(Vec3::sLoadFloat3Unsafe(vf.mFeature), 1.0e-8f))
24
+ && inV.IsClose(Vec3::sLoadFloat3Unsafe(vf.mFeature), mVertexToleranceSq))
25
return true;
26
return false;
27
}
28
@@ -76,13 +76,15 @@ class InternalEdgeRemovingCollector : public CollideShapeCollector
29
30
public:
31
/// Constructor, configures a collector to be called with all the results that do not hit internal edges
32
- explicit InternalEdgeRemovingCollector(CollideShapeCollector &inChainedCollector
33
+ explicit InternalEdgeRemovingCollector(CollideShapeCollector &inChainedCollector,
34
+ float inVertexToleranceSq
35
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
36
, RVec3Arg inBaseOffset
37
#endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
38
) :
39
CollideShapeCollector(inChainedCollector),
40
- mChainedCollector(inChainedCollector)
41
+ mChainedCollector(inChainedCollector),
42
+ mVertexToleranceSq(inVertexToleranceSq)
43
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
44
, mBaseOffset(inBaseOffset)
45
#endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
46
@@ -249,7 +251,7 @@ public:
47
JPH_ASSERT(inCollideShapeSettings.mActiveEdgeMode == EActiveEdgeMode::CollideWithAll); // Won't work without colliding with all edges
48
JPH_ASSERT(inCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces); // Won't work without collecting faces
49
50
- InternalEdgeRemovingCollector wrapper(ioCollector
51
+ InternalEdgeRemovingCollector wrapper(ioCollector, inCollideShapeSettings.mInternalEdgeRemovalVertexToleranceSq
52
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
53
, inBaseOffset
54
#endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
55
@@ -271,6 +273,7 @@ private:
56
CollideShapeCollector & mChainedCollector;
57
Array<Voided, STLLocalAllocator<Voided, cMaxLocalVoidedFeatures>> mVoidedFeatures;
58
Array<CollideShapeResult, STLLocalAllocator<CollideShapeResult, cMaxLocalDelayedResults>> mDelayedResults;
59
+ float mVertexToleranceSq; // Max squared distance to consider a vertex to be the same as another vertex, used to determine if a feature is voided
60
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
61
RVec3 mBaseOffset; // Base offset for the query, used to draw the results in the right place
62
#endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
63
diff --git a/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseQuery.cpp b/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseQuery.cpp
64
index 670cee33..1a8c2c78 100644
65
--- a/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseQuery.cpp
66
+++ b/thirdparty/jolt_physics/Jolt/Physics/Collision/NarrowPhaseQuery.cpp
67
@@ -301,7 +301,7 @@ void NarrowPhaseQuery::CollideShapeWithInternalEdgeRemoval(const Shape *inShape,
68
settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
69
settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
70
71
- InternalEdgeRemovingCollector wrapper(ioCollector
72
+ InternalEdgeRemovingCollector wrapper(ioCollector, settings.mInternalEdgeRemovalVertexToleranceSq
73
#ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
74
, inBaseOffset
75
#endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
76
diff --git a/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.cpp b/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.cpp
77
index ff55f4ba..d859b22e 100644
78
--- a/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.cpp
79
+++ b/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.cpp
80
@@ -185,9 +185,9 @@ uint32 HeightFieldShapeSettings::CalculateBitsPerSampleForError(float inMaxError
81
// Not accurate enough, increase bits per sample
82
bits_per_sample++;
83
84
- // Don't go above 8 bits per sample
85
- if (bits_per_sample == 8)
86
- return bits_per_sample;
87
+ // Don't go above cMaxBitsPerSample bits per sample
88
+ if (bits_per_sample == cMaxBitsPerSample)
89
+ return cMaxBitsPerSample;
90
}
91
}
92
}
93
@@ -416,7 +416,7 @@ void HeightFieldShape::StoreMaterialIndices(const HeightFieldShapeSettings &inSe
94
95
void HeightFieldShape::CacheValues()
96
{
97
- mSampleMask = uint8((uint32(1) << mBitsPerSample) - 1);
98
+ mSampleMask = uint16((uint32(1) << mBitsPerSample) - 1);
99
}
100
101
void HeightFieldShape::AllocateBuffers()
102
@@ -424,7 +424,7 @@ void HeightFieldShape::AllocateBuffers()
103
uint num_blocks = GetNumBlocks();
104
uint max_stride = (num_blocks + 1) >> 1;
105
mRangeBlocksSize = sGridOffsets[sGetMaxLevel(num_blocks) - 1] + Square(max_stride);
106
- mHeightSamplesSize = (mSampleCount * mSampleCount * mBitsPerSample + 7) / 8 + 1;
107
+ mHeightSamplesSize = (mSampleCount * mSampleCount * mBitsPerSample + 7) / 8 + 2; // Since we read 3 bytes per sample, we need 2 extra bytes of padding
108
mActiveEdgesSize = (Square(mSampleCount - 1) * 3 + 7) / 8 + 1; // See explanation at HeightFieldShape::CalculateActiveEdges
109
110
JPH_ASSERT(mRangeBlocks == nullptr && mHeightSamples == nullptr && mActiveEdges == nullptr);
111
@@ -457,9 +457,9 @@ HeightFieldShape::HeightFieldShape(const HeightFieldShapeSettings &inSettings, S
112
}
113
114
// Check bits per sample
115
- if (inSettings.mBitsPerSample < 1 || inSettings.mBitsPerSample > 8)
116
+ if (inSettings.mBitsPerSample < 1 || inSettings.mBitsPerSample > HeightFieldShapeConstants::cMaxBitsPerSample)
117
{
118
- outResult.SetError("HeightFieldShape: Bits per sample must be in the range [1, 8]!");
119
+ outResult.SetError("HeightFieldShape: Bits per sample must be in the range [1, 16]!");
120
return;
121
}
122
123
@@ -727,9 +727,10 @@ HeightFieldShape::HeightFieldShape(const HeightFieldShapeSettings &inSettings, S
124
uint byte_pos = sample >> 3;
125
uint bit_pos = sample & 0b111;
126
output_value <<= bit_pos;
127
- JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
128
+ JPH_ASSERT(byte_pos + 2 < mHeightSamplesSize); // We read max 16 bits which could be spread out over 3 bytes
129
mHeightSamples[byte_pos] |= uint8(output_value);
130
mHeightSamples[byte_pos + 1] |= uint8(output_value >> 8);
131
+ mHeightSamples[byte_pos + 2] |= uint8(output_value >> 16);
132
sample += inSettings.mBitsPerSample;
133
}
134
135
@@ -816,7 +817,7 @@ inline void HeightFieldShape::GetBlockOffsetAndScale(uint inBlockX, uint inBlock
136
outBlockScale = float(block.mMax[n] - block.mMin[n]) / float(mSampleMask);
137
}
138
139
-inline uint8 HeightFieldShape::GetHeightSample(uint inX, uint inY) const
140
+inline uint16 HeightFieldShape::GetHeightSample(uint inX, uint inY) const
141
{
142
JPH_ASSERT(inX < mSampleCount);
143
JPH_ASSERT(inY < mSampleCount);
144
@@ -827,16 +828,16 @@ inline uint8 HeightFieldShape::GetHeightSample(uint inX, uint inY) const
145
uint bit_pos = sample & 0b111;
146
147
// Fetch the height sample value
148
- JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
149
+ JPH_ASSERT(byte_pos + 2 < mHeightSamplesSize); // We read max 16 bits which could be spread out over 3 bytes
150
const uint8 *height_samples = mHeightSamples + byte_pos;
151
- uint16 height_sample = uint16(height_samples[0]) | uint16(uint16(height_samples[1]) << 8);
152
- return uint8(height_sample >> bit_pos) & mSampleMask;
153
+ uint32 height_sample = uint32(height_samples[0]) | (uint32(height_samples[1]) << 8) | (uint32(height_samples[2]) << 16);
154
+ return uint16(height_sample >> bit_pos) & mSampleMask;
155
}
156
157
inline Vec3 HeightFieldShape::GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale, bool &outNoCollision) const
158
{
159
// Get quantized value
160
- uint8 height_sample = GetHeightSample(inX, inY);
161
+ uint16 height_sample = GetHeightSample(inX, inY);
162
outNoCollision = height_sample == mSampleMask;
163
164
// Add 0.5 to the quantized value to minimize the error (see constructor)
165
@@ -980,7 +981,7 @@ void HeightFieldShape::GetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY
166
uint output_y = block_y * mBlockSize + sample_y;
167
168
// Get quantized value
169
- uint8 height_sample = GetHeightSample(inX + output_x, inY + output_y);
170
+ uint16 height_sample = GetHeightSample(inX + output_x, inY + output_y);
171
172
// Dequantize
173
float h = height_sample != mSampleMask? offset + height_sample * scale : cNoCollisionValue;
174
@@ -1129,7 +1130,7 @@ void HeightFieldShape::SetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY
175
{
176
// Quantize height
177
float h = heights[sample_y * heights_stride + sample_x];
178
- uint8 quantized_height = h != cNoCollisionValue? uint8(Clamp((int)floor((h - offset) / scale), 0, int(mSampleMask) - 1)) : mSampleMask;
179
+ uint16 quantized_height = h != cNoCollisionValue? uint16(Clamp((int)floor((h - offset) / scale), 0, int(mSampleMask) - 1)) : mSampleMask;
180
181
// Determine bit position of sample
182
uint sample = ((affected_y + sample_y) * mSampleCount + affected_x + sample_x) * uint(mBitsPerSample);
183
@@ -1137,13 +1138,14 @@ void HeightFieldShape::SetHeights(uint inX, uint inY, uint inSizeX, uint inSizeY
184
uint bit_pos = sample & 0b111;
185
186
// Update the height value sample
187
- JPH_ASSERT(byte_pos + 1 < mHeightSamplesSize);
188
+ JPH_ASSERT(byte_pos + 2 < mHeightSamplesSize); // We read max 16 bits which could be spread out over 3 bytes
189
uint8 *height_samples = mHeightSamples + byte_pos;
190
- uint16 height_sample = uint16(height_samples[0]) | uint16(uint16(height_samples[1]) << 8);
191
- height_sample &= ~(uint16(mSampleMask) << bit_pos);
192
- height_sample |= uint16(quantized_height) << bit_pos;
193
+ uint32 height_sample = uint32(height_samples[0]) | (uint32(height_samples[1]) << 8) | (uint32(height_samples[2]) << 16);
194
+ height_sample &= ~(uint32(mSampleMask) << bit_pos);
195
+ height_sample |= uint32(quantized_height) << bit_pos;
196
height_samples[0] = uint8(height_sample);
197
height_samples[1] = uint8(height_sample >> 8);
198
+ height_samples[2] = uint8(height_sample >> 16);
199
}
200
}
201
202
diff --git a/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.h b/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.h
203
index 5aa6de88..c9bdc605 100644
204
--- a/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.h
205
+++ b/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/HeightFieldShape.h
206
@@ -33,6 +33,9 @@ namespace HeightFieldShapeConstants
207
/// When height samples are converted to 16 bit:
208
constexpr uint16 cNoCollisionValue16 = 0xffff; ///< This is the magic value for 'no collision'
209
constexpr uint16 cMaxHeightValue16 = 0xfffe; ///< This is the maximum allowed height value
210
+
211
+ /// Maximum value for HeightFieldShapeSettings::mBitsPerSample
212
+ constexpr uint32 cMaxBitsPerSample = 16;
213
};
214
215
/// Class that constructs a HeightFieldShape
216
@@ -63,7 +66,7 @@ public:
217
218
/// Given mBlockSize, mSampleCount and mHeightSamples, calculate the amount of bits needed to stay below absolute error inMaxError
219
/// @param inMaxError Maximum allowed error in mHeightSamples after compression (note that this does not take mScale.Y into account)
220
- /// @return Needed bits per sample in the range [1, 8].
221
+ /// @return Needed bits per sample in the range [1, 16].
222
uint32 CalculateBitsPerSampleForError(float inMaxError) const;
223
224
/// The height field is a surface defined by: mOffset + mScale * (x, mHeightSamples[y * mSampleCount + x], y).
225
@@ -83,12 +86,12 @@ public:
226
uint32 mMaterialsCapacity = 0;
227
228
/// The heightfield is divided in blocks of mBlockSize * mBlockSize * 2 triangles and the acceleration structure culls blocks only,
229
- /// bigger block sizes reduce memory consumption but also reduce query performance. Sensible values are [2, 8], does not need to be
230
+ /// bigger block sizes reduce memory consumption but also reduce query performance. Valid values are [2, 8], does not need to be
231
/// a power of 2. Note that at run-time we'll perform one more grid subdivision, so the effective block size is half of what is provided here.
232
uint32 mBlockSize = 2;
233
234
- /// How many bits per sample to use to compress the height field. Can be in the range [1, 8].
235
- /// Note that each sample is compressed relative to the min/max value of its block of mBlockSize * mBlockSize pixels so the effective precision is higher.
236
+ /// How many bits per sample to use to compress the height field. Can be in the range [1, 16].
237
+ /// Note that each sample is compressed relative to the min/max value of its block of mBlockSize * mBlockSize samples so the effective precision is higher.
238
/// Also note that increasing mBlockSize saves more memory than reducing the amount of bits per sample.
239
uint32 mBitsPerSample = 8;
240
241
@@ -307,7 +310,7 @@ private:
242
inline void GetBlockOffsetAndScale(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, float &outBlockOffset, float &outBlockScale) const;
243
244
/// Get the height sample at position (inX, inY)
245
- inline uint8 GetHeightSample(uint inX, uint inY) const;
246
+ inline uint16 GetHeightSample(uint inX, uint inY) const;
247
248
/// Faster version of GetPosition when block offset and scale are already known
249
inline Vec3 GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale, bool &outNoCollision) const;
250
@@ -358,7 +361,7 @@ private:
251
uint32 mRangeBlocksSize = 0; ///< Size of mRangeBlocks in elements
252
uint32 mActiveEdgesSize = 0; ///< Size of mActiveEdges in bytes
253
uint8 mBitsPerSample = 8; ///< See HeightFieldShapeSettings::mBitsPerSample
254
- uint8 mSampleMask = 0xff; ///< All bits set for a sample: (1 << mBitsPerSample) - 1, used to indicate that there's no collision
255
+ uint16 mSampleMask = 0xff; ///< All bits set for a sample: (1 << mBitsPerSample) - 1, used to indicate that there's no collision
256
uint16 mMinSample = HeightFieldShapeConstants::cNoCollisionValue16; ///< Min and max value in mHeightSamples quantized to 16 bit, for calculating bounding box
257
uint16 mMaxSample = HeightFieldShapeConstants::cNoCollisionValue16;
258
RangeBlock * mRangeBlocks = nullptr; ///< Hierarchical grid of range data describing the height variations within 1 block. The grid for level <level> starts at offset sGridOffsets[<level>]
259
diff --git a/thirdparty/jolt_physics/Jolt/Physics/PhysicsSettings.h b/thirdparty/jolt_physics/Jolt/Physics/PhysicsSettings.h
260
index 1109e253..cbd88d1a 100644
261
--- a/thirdparty/jolt_physics/Jolt/Physics/PhysicsSettings.h
262
+++ b/thirdparty/jolt_physics/Jolt/Physics/PhysicsSettings.h
263
@@ -16,7 +16,10 @@ constexpr float cDefaultPenetrationTolerance = 1.0e-4f; ///< Stop when there's l
264
constexpr float cDefaultConvexRadius = 0.05f;
265
266
/// Used by (Tapered)CapsuleShape to determine when supporting face is an edge rather than a point (unit: meter)
267
-static constexpr float cCapsuleProjectionSlop = 0.02f;
268
+constexpr float cCapsuleProjectionSlop = 0.02f;
269
+
270
+/// Max squared distance to consider a vertex to be the same as another vertex, used by the internal edge removal algorithm to determine if two edges are shared (unit: meter^2)
271
+constexpr float cDefaultInternalEdgeRemovalVertexToleranceSq = 1.0e-8f;
272
273
/// Maximum amount of jobs to allow
274
constexpr int cMaxPhysicsJobs = 2048;
275
@@ -73,6 +76,9 @@ struct PhysicsSettings
276
/// Maximum allowed distance between old and new contact point to preserve contact forces for warm start (units: meter^2)
277
float mContactPointPreserveLambdaMaxDistSq = Square(0.01f); ///< 1 cm
278
279
+ /// Max squared distance to consider a vertex to be the same as another vertex, used by the internal edge removal algorithm to determine if two edges are shared. (unit: meter^2)
280
+ float mInternalEdgeRemovalVertexToleranceSq = cDefaultInternalEdgeRemovalVertexToleranceSq;
281
+
282
/// Number of solver velocity iterations to run
283
/// Note that this needs to be >= 2 in order for friction to work (friction is applied using the non-penetration impulse from the previous iteration)
284
uint mNumVelocitySteps = 10;
285
diff --git a/thirdparty/jolt_physics/Jolt/Physics/PhysicsSystem.cpp b/thirdparty/jolt_physics/Jolt/Physics/PhysicsSystem.cpp
286
index 7b4babfc..c2ead513 100644
287
--- a/thirdparty/jolt_physics/Jolt/Physics/PhysicsSystem.cpp
288
+++ b/thirdparty/jolt_physics/Jolt/Physics/PhysicsSystem.cpp
289
@@ -1096,6 +1096,7 @@ void PhysicsSystem::ProcessBodyPair(ContactAllocator &ioContactAllocator, const
290
settings.mActiveEdgeMode = mPhysicsSettings.mCheckActiveEdges? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
291
settings.mMaxSeparationDistance = body1->IsSensor() || body2->IsSensor()? 0.0f : mPhysicsSettings.mSpeculativeContactDistance;
292
settings.mActiveEdgeMovementDirection = body1->GetLinearVelocity() - body2->GetLinearVelocity();
293
+ settings.mInternalEdgeRemovalVertexToleranceSq = mPhysicsSettings.mInternalEdgeRemovalVertexToleranceSq;
294
295
// Create shape filter
296
SimShapeFilterWrapper shape_filter(mSimShapeFilter, body1);
297
2.52.0.windows.1
298
299