Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/SoftBody/SoftBodyShape.cpp
9913 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#include <Jolt/Jolt.h>
6
7
#include <Jolt/Physics/SoftBody/SoftBodyShape.h>
8
#include <Jolt/Core/Profiler.h>
9
#include <Jolt/Geometry/RayTriangle.h>
10
#include <Jolt/Physics/Collision/RayCast.h>
11
#include <Jolt/Physics/Collision/CastResult.h>
12
#include <Jolt/Physics/Collision/TransformedShape.h>
13
#include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
14
#include <Jolt/Physics/Collision/CastConvexVsTriangles.h>
15
#include <Jolt/Physics/Collision/CastSphereVsTriangles.h>
16
#include <Jolt/Physics/Collision/CollideConvexVsTriangles.h>
17
#include <Jolt/Physics/Collision/CollideSphereVsTriangles.h>
18
#include <Jolt/Physics/Collision/CollisionDispatch.h>
19
#ifdef JPH_DEBUG_RENDERER
20
#include <Jolt/Renderer/DebugRenderer.h>
21
#endif // JPH_DEBUG_RENDERER
22
23
JPH_NAMESPACE_BEGIN
24
25
uint SoftBodyShape::GetSubShapeIDBits() const
26
{
27
// Ensure we have enough bits to encode our shape [0, n - 1]
28
uint32 n = (uint32)mSoftBodyMotionProperties->GetFaces().size() - 1;
29
return 32 - CountLeadingZeros(n);
30
}
31
32
uint32 SoftBodyShape::GetFaceIndex(const SubShapeID &inSubShapeID) const
33
{
34
SubShapeID remainder;
35
uint32 face_index = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
36
JPH_ASSERT(remainder.IsEmpty());
37
return face_index;
38
}
39
40
AABox SoftBodyShape::GetLocalBounds() const
41
{
42
return mSoftBodyMotionProperties->GetLocalBounds();
43
}
44
45
bool SoftBodyShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
46
{
47
JPH_PROFILE_FUNCTION();
48
49
uint num_triangle_bits = GetSubShapeIDBits();
50
uint triangle_idx = uint(-1);
51
52
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
53
for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
54
{
55
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
56
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
57
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
58
59
float fraction = RayTriangle(inRay.mOrigin, inRay.mDirection, x1, x2, x3);
60
if (fraction < ioHit.mFraction)
61
{
62
// Store fraction
63
ioHit.mFraction = fraction;
64
65
// Store triangle index
66
triangle_idx = uint(&f - mSoftBodyMotionProperties->GetFaces().data());
67
}
68
}
69
70
if (triangle_idx == uint(-1))
71
return false;
72
73
ioHit.mSubShapeID2 = inSubShapeIDCreator.PushID(triangle_idx, num_triangle_bits).GetID();
74
return true;
75
}
76
77
void SoftBodyShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
78
{
79
JPH_PROFILE_FUNCTION();
80
81
// Test shape filter
82
if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
83
return;
84
85
uint num_triangle_bits = GetSubShapeIDBits();
86
87
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
88
for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
89
{
90
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
91
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
92
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
93
94
// Back facing check
95
if (inRayCastSettings.mBackFaceModeTriangles == EBackFaceMode::IgnoreBackFaces && (x2 - x1).Cross(x3 - x1).Dot(inRay.mDirection) > 0.0f)
96
continue;
97
98
// Test ray against triangle
99
float fraction = RayTriangle(inRay.mOrigin, inRay.mDirection, x1, x2, x3);
100
if (fraction < ioCollector.GetEarlyOutFraction())
101
{
102
// Better hit than the current hit
103
RayCastResult hit;
104
hit.mBodyID = TransformedShape::sGetBodyID(ioCollector.GetContext());
105
hit.mFraction = fraction;
106
hit.mSubShapeID2 = inSubShapeIDCreator.PushID(uint(&f - mSoftBodyMotionProperties->GetFaces().data()), num_triangle_bits).GetID();
107
ioCollector.AddHit(hit);
108
}
109
}
110
}
111
112
void SoftBodyShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
113
{
114
sCollidePointUsingRayCast(*this, inPoint, inSubShapeIDCreator, ioCollector, inShapeFilter);
115
}
116
117
void SoftBodyShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const
118
{
119
/* Not implemented */
120
}
121
122
const PhysicsMaterial *SoftBodyShape::GetMaterial(const SubShapeID &inSubShapeID) const
123
{
124
SubShapeID remainder;
125
uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
126
JPH_ASSERT(remainder.IsEmpty());
127
128
const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
129
return mSoftBodyMotionProperties->GetMaterials()[f.mMaterialIndex];
130
}
131
132
Vec3 SoftBodyShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
133
{
134
SubShapeID remainder;
135
uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
136
JPH_ASSERT(remainder.IsEmpty());
137
138
const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
139
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
140
141
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
142
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
143
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
144
145
return (x2 - x1).Cross(x3 - x1).NormalizedOr(Vec3::sAxisY());
146
}
147
148
void SoftBodyShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
149
{
150
SubShapeID remainder;
151
uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
152
JPH_ASSERT(remainder.IsEmpty());
153
154
const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
155
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
156
157
for (uint32 i : f.mVertex)
158
outVertices.push_back(inCenterOfMassTransform * (inScale * vertices[i].mPosition));
159
}
160
161
void SoftBodyShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const
162
{
163
outSubmergedVolume = 0.0f;
164
outTotalVolume = mSoftBodyMotionProperties->GetVolume();
165
outCenterOfBuoyancy = Vec3::sZero();
166
}
167
168
#ifdef JPH_DEBUG_RENDERER
169
170
void SoftBodyShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
171
{
172
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
173
for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
174
{
175
RVec3 x1 = inCenterOfMassTransform * vertices[f.mVertex[0]].mPosition;
176
RVec3 x2 = inCenterOfMassTransform * vertices[f.mVertex[1]].mPosition;
177
RVec3 x3 = inCenterOfMassTransform * vertices[f.mVertex[2]].mPosition;
178
179
inRenderer->DrawTriangle(x1, x2, x3, inColor, DebugRenderer::ECastShadow::On);
180
}
181
}
182
183
#endif // JPH_DEBUG_RENDERER
184
185
struct SoftBodyShape::SBSGetTrianglesContext
186
{
187
Mat44 mCenterOfMassTransform;
188
int mTriangleIndex;
189
};
190
191
void SoftBodyShape::GetTrianglesStart(GetTrianglesContext &ioContext, [[maybe_unused]] const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const
192
{
193
SBSGetTrianglesContext &context = reinterpret_cast<SBSGetTrianglesContext &>(ioContext);
194
context.mCenterOfMassTransform = Mat44::sRotationTranslation(inRotation, inPositionCOM) * Mat44::sScale(inScale);
195
context.mTriangleIndex = 0;
196
}
197
198
int SoftBodyShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
199
{
200
SBSGetTrianglesContext &context = reinterpret_cast<SBSGetTrianglesContext &>(ioContext);
201
202
const Array<SoftBodyMotionProperties::Face> &faces = mSoftBodyMotionProperties->GetFaces();
203
const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
204
const PhysicsMaterialList &materials = mSoftBodyMotionProperties->GetMaterials();
205
206
int num_triangles = min(inMaxTrianglesRequested, (int)faces.size() - context.mTriangleIndex);
207
for (int i = 0; i < num_triangles; ++i)
208
{
209
const SoftBodyMotionProperties::Face &f = faces[context.mTriangleIndex + i];
210
211
Vec3 x1 = context.mCenterOfMassTransform * vertices[f.mVertex[0]].mPosition;
212
Vec3 x2 = context.mCenterOfMassTransform * vertices[f.mVertex[1]].mPosition;
213
Vec3 x3 = context.mCenterOfMassTransform * vertices[f.mVertex[2]].mPosition;
214
215
x1.StoreFloat3(outTriangleVertices++);
216
x2.StoreFloat3(outTriangleVertices++);
217
x3.StoreFloat3(outTriangleVertices++);
218
219
if (outMaterials != nullptr)
220
*outMaterials++ = materials[f.mMaterialIndex];
221
}
222
223
context.mTriangleIndex += num_triangles;
224
return num_triangles;
225
}
226
227
Shape::Stats SoftBodyShape::GetStats() const
228
{
229
return Stats(sizeof(*this), (uint)mSoftBodyMotionProperties->GetFaces().size());
230
}
231
232
float SoftBodyShape::GetVolume() const
233
{
234
return mSoftBodyMotionProperties->GetVolume();
235
}
236
237
void SoftBodyShape::sCollideConvexVsSoftBody(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, [[maybe_unused]] const ShapeFilter &inShapeFilter)
238
{
239
JPH_ASSERT(inShape1->GetType() == EShapeType::Convex);
240
const ConvexShape *shape1 = static_cast<const ConvexShape *>(inShape1);
241
JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::SoftBody);
242
const SoftBodyShape *shape2 = static_cast<const SoftBodyShape *>(inShape2);
243
244
const Array<SoftBodyVertex> &vertices = shape2->mSoftBodyMotionProperties->GetVertices();
245
const Array<SoftBodyMotionProperties::Face> &faces = shape2->mSoftBodyMotionProperties->GetFaces();
246
uint num_triangle_bits = shape2->GetSubShapeIDBits();
247
248
CollideConvexVsTriangles collider(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
249
for (const SoftBodyMotionProperties::Face &f : faces)
250
{
251
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
252
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
253
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
254
255
collider.Collide(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
256
}
257
}
258
259
void SoftBodyShape::sCollideSphereVsSoftBody(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, [[maybe_unused]] const ShapeFilter &inShapeFilter)
260
{
261
JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Sphere);
262
const SphereShape *shape1 = static_cast<const SphereShape *>(inShape1);
263
JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::SoftBody);
264
const SoftBodyShape *shape2 = static_cast<const SoftBodyShape *>(inShape2);
265
266
const Array<SoftBodyVertex> &vertices = shape2->mSoftBodyMotionProperties->GetVertices();
267
const Array<SoftBodyMotionProperties::Face> &faces = shape2->mSoftBodyMotionProperties->GetFaces();
268
uint num_triangle_bits = shape2->GetSubShapeIDBits();
269
270
CollideSphereVsTriangles collider(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
271
for (const SoftBodyMotionProperties::Face &f : faces)
272
{
273
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
274
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
275
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
276
277
collider.Collide(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
278
}
279
}
280
281
void SoftBodyShape::sCastConvexVsSoftBody(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, [[maybe_unused]] const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
282
{
283
JPH_ASSERT(inShape->GetSubType() == EShapeSubType::SoftBody);
284
const SoftBodyShape *shape = static_cast<const SoftBodyShape *>(inShape);
285
286
const Array<SoftBodyVertex> &vertices = shape->mSoftBodyMotionProperties->GetVertices();
287
const Array<SoftBodyMotionProperties::Face> &faces = shape->mSoftBodyMotionProperties->GetFaces();
288
uint num_triangle_bits = shape->GetSubShapeIDBits();
289
290
CastConvexVsTriangles caster(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
291
for (const SoftBodyMotionProperties::Face &f : faces)
292
{
293
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
294
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
295
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
296
297
caster.Cast(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
298
}
299
}
300
301
void SoftBodyShape::sCastSphereVsSoftBody(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, [[maybe_unused]] const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
302
{
303
JPH_ASSERT(inShape->GetSubType() == EShapeSubType::SoftBody);
304
const SoftBodyShape *shape = static_cast<const SoftBodyShape *>(inShape);
305
306
const Array<SoftBodyVertex> &vertices = shape->mSoftBodyMotionProperties->GetVertices();
307
const Array<SoftBodyMotionProperties::Face> &faces = shape->mSoftBodyMotionProperties->GetFaces();
308
uint num_triangle_bits = shape->GetSubShapeIDBits();
309
310
CastSphereVsTriangles caster(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
311
for (const SoftBodyMotionProperties::Face &f : faces)
312
{
313
Vec3 x1 = vertices[f.mVertex[0]].mPosition;
314
Vec3 x2 = vertices[f.mVertex[1]].mPosition;
315
Vec3 x3 = vertices[f.mVertex[2]].mPosition;
316
317
caster.Cast(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
318
}
319
}
320
321
void SoftBodyShape::sRegister()
322
{
323
ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::SoftBody);
324
f.mConstruct = nullptr; // Not supposed to be constructed by users!
325
f.mColor = Color::sDarkGreen;
326
327
for (EShapeSubType s : sConvexSubShapeTypes)
328
{
329
CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::SoftBody, sCollideConvexVsSoftBody);
330
CollisionDispatch::sRegisterCastShape(s, EShapeSubType::SoftBody, sCastConvexVsSoftBody);
331
332
CollisionDispatch::sRegisterCollideShape(EShapeSubType::SoftBody, s, CollisionDispatch::sReversedCollideShape);
333
CollisionDispatch::sRegisterCastShape(EShapeSubType::SoftBody, s, CollisionDispatch::sReversedCastShape);
334
}
335
336
// Specialized collision functions
337
CollisionDispatch::sRegisterCollideShape(EShapeSubType::Sphere, EShapeSubType::SoftBody, sCollideSphereVsSoftBody);
338
CollisionDispatch::sRegisterCastShape(EShapeSubType::Sphere, EShapeSubType::SoftBody, sCastSphereVsSoftBody);
339
}
340
341
JPH_NAMESPACE_END
342
343