Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/ManifoldBetweenTwoFaces.cpp
21406 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#include <Jolt/Jolt.h>
6
7
#include <Jolt/Physics/Collision/ManifoldBetweenTwoFaces.h>
8
#include <Jolt/Physics/Constraints/ContactConstraintManager.h>
9
#include <Jolt/Geometry/ClipPoly.h>
10
#ifdef JPH_DEBUG_RENDERER
11
#include <Jolt/Renderer/DebugRenderer.h>
12
#endif // JPH_DEBUG_RENDERER
13
14
JPH_NAMESPACE_BEGIN
15
16
void PruneContactPoints(Vec3Arg inPenetrationAxis, ContactPoints &ioContactPointsOn1, ContactPoints &ioContactPointsOn2 JPH_IF_DEBUG_RENDERER(, RVec3Arg inCenterOfMass))
17
{
18
// Makes no sense to call this with 4 or less points
19
JPH_ASSERT(ioContactPointsOn1.size() > 4);
20
21
// Both arrays should have the same size
22
JPH_ASSERT(ioContactPointsOn1.size() == ioContactPointsOn2.size());
23
24
// Penetration axis must be normalized
25
JPH_ASSERT(inPenetrationAxis.IsNormalized());
26
27
// We use a heuristic of (distance to center of mass) * (penetration depth) to find the contact point that we should keep
28
// Neither of those two terms should ever become zero, so we clamp against this minimum value
29
constexpr float cMinDistanceSq = 1.0e-6f; // 1 mm
30
31
ContactPoints projected;
32
StaticArray<float, 64> penetration_depth_sq;
33
for (ContactPoints::size_type i = 0; i < ioContactPointsOn1.size(); ++i)
34
{
35
// Project contact points on the plane through inCenterOfMass with normal inPenetrationAxis and center around the center of mass of body 1
36
// (note that since all points are relative to inCenterOfMass we can project onto the plane through the origin)
37
Vec3 v1 = ioContactPointsOn1[i];
38
projected.push_back(v1 - v1.Dot(inPenetrationAxis) * inPenetrationAxis);
39
40
// Calculate penetration depth^2 of each point and clamp against the minimal distance
41
Vec3 v2 = ioContactPointsOn2[i];
42
penetration_depth_sq.push_back(max(cMinDistanceSq, (v2 - v1).LengthSq()));
43
}
44
45
// Find the point that is furthest away from the center of mass (its torque will have the biggest influence)
46
// and the point that has the deepest penetration depth. Use the heuristic (distance to center of mass) * (penetration depth) for this.
47
uint point1 = 0;
48
float val = max(cMinDistanceSq, projected[0].LengthSq()) * penetration_depth_sq[0];
49
for (uint i = 0; i < projected.size(); ++i)
50
{
51
float v = max(cMinDistanceSq, projected[i].LengthSq()) * penetration_depth_sq[i];
52
if (v > val)
53
{
54
val = v;
55
point1 = i;
56
}
57
}
58
Vec3 point1v = projected[point1];
59
60
// Find point furthest from the first point forming a line segment with point1. Again combine this with the heuristic
61
// for deepest point as per above.
62
uint point2 = uint(-1);
63
val = -FLT_MAX;
64
for (uint i = 0; i < projected.size(); ++i)
65
if (i != point1)
66
{
67
float v = max(cMinDistanceSq, (projected[i] - point1v).LengthSq()) * penetration_depth_sq[i];
68
if (v > val)
69
{
70
val = v;
71
point2 = i;
72
}
73
}
74
JPH_ASSERT(point2 != uint(-1));
75
Vec3 point2v = projected[point2];
76
77
// Find furthest points on both sides of the line segment in order to maximize the area
78
uint point3 = uint(-1);
79
uint point4 = uint(-1);
80
float min_val = 0.0f;
81
float max_val = 0.0f;
82
Vec3 perp = (point2v - point1v).Cross(inPenetrationAxis);
83
for (uint i = 0; i < projected.size(); ++i)
84
if (i != point1 && i != point2)
85
{
86
float v = perp.Dot(projected[i] - point1v);
87
if (v < min_val)
88
{
89
min_val = v;
90
point3 = i;
91
}
92
else if (v > max_val)
93
{
94
max_val = v;
95
point4 = i;
96
}
97
}
98
99
// Add points to array (in order so they form a polygon)
100
StaticArray<Vec3, 4> points_to_keep_on_1, points_to_keep_on_2;
101
points_to_keep_on_1.push_back(ioContactPointsOn1[point1]);
102
points_to_keep_on_2.push_back(ioContactPointsOn2[point1]);
103
if (point3 != uint(-1))
104
{
105
points_to_keep_on_1.push_back(ioContactPointsOn1[point3]);
106
points_to_keep_on_2.push_back(ioContactPointsOn2[point3]);
107
}
108
points_to_keep_on_1.push_back(ioContactPointsOn1[point2]);
109
points_to_keep_on_2.push_back(ioContactPointsOn2[point2]);
110
if (point4 != uint(-1))
111
{
112
JPH_ASSERT(point3 != point4);
113
points_to_keep_on_1.push_back(ioContactPointsOn1[point4]);
114
points_to_keep_on_2.push_back(ioContactPointsOn2[point4]);
115
}
116
117
#ifdef JPH_DEBUG_RENDERER
118
if (ContactConstraintManager::sDrawContactPointReduction)
119
{
120
// Draw input polygon
121
DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inCenterOfMass), ioContactPointsOn1, Color::sOrange, 0.05f);
122
123
// Draw primary axis
124
DebugRenderer::sInstance->DrawArrow(inCenterOfMass + ioContactPointsOn1[point1], inCenterOfMass + ioContactPointsOn1[point2], Color::sRed, 0.05f);
125
126
// Draw contact points we kept
127
for (Vec3 p : points_to_keep_on_1)
128
DebugRenderer::sInstance->DrawMarker(inCenterOfMass + p, Color::sGreen, 0.1f);
129
}
130
#endif // JPH_DEBUG_RENDERER
131
132
// Copy the points back to the input buffer
133
ioContactPointsOn1 = points_to_keep_on_1;
134
ioContactPointsOn2 = points_to_keep_on_2;
135
}
136
137
void ManifoldBetweenTwoFaces(Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inPenetrationAxis, float inMaxContactDistance, const ConvexShape::SupportingFace &inShape1Face, const ConvexShape::SupportingFace &inShape2Face, ContactPoints &outContactPoints1, ContactPoints &outContactPoints2 JPH_IF_DEBUG_RENDERER(, RVec3Arg inCenterOfMass))
138
{
139
JPH_ASSERT(inMaxContactDistance > 0.0f);
140
141
#ifdef JPH_DEBUG_RENDERER
142
if (ContactConstraintManager::sDrawContactPoint)
143
{
144
RVec3 cp1 = inCenterOfMass + inContactPoint1;
145
RVec3 cp2 = inCenterOfMass + inContactPoint2;
146
147
// Draw contact points
148
DebugRenderer::sInstance->DrawMarker(cp1, Color::sRed, 0.1f);
149
DebugRenderer::sInstance->DrawMarker(cp2, Color::sGreen, 0.1f);
150
151
// Draw contact normal
152
DebugRenderer::sInstance->DrawArrow(cp1, cp1 + inPenetrationAxis.Normalized(), Color::sRed, 0.05f);
153
}
154
#endif // JPH_DEBUG_RENDERER
155
156
// Remember size before adding new points, to check at the end if we added some
157
ContactPoints::size_type old_size = outContactPoints1.size();
158
159
// Both faces need to have at least 2 points or else there can never be more than 1 contact point
160
// At least one face needs to have at least 3 points (in the case that it has 2 points only if the edges match exactly you can have 2 contact points, but this situation is unstable anyhow)
161
if (min(inShape1Face.size(), inShape2Face.size()) >= 2
162
&& max(inShape1Face.size(), inShape2Face.size()) >= 3)
163
{
164
// Swap the shapes if the 2nd face doesn't have enough vertices
165
const ConvexShape::SupportingFace *shape1_face, *shape2_face;
166
ContactPoints *contact_points1, *contact_points2;
167
Vec3 penetration_axis;
168
if (inShape2Face.size() >= 3)
169
{
170
shape1_face = &inShape1Face;
171
shape2_face = &inShape2Face;
172
contact_points1 = &outContactPoints1;
173
contact_points2 = &outContactPoints2;
174
penetration_axis = inPenetrationAxis;
175
}
176
else
177
{
178
shape1_face = &inShape2Face;
179
shape2_face = &inShape1Face;
180
contact_points1 = &outContactPoints2;
181
contact_points2 = &outContactPoints1;
182
penetration_axis = -inPenetrationAxis;
183
}
184
185
// Determine plane origin and first edge direction
186
Vec3 plane_origin = shape1_face->at(0);
187
Vec3 first_edge = shape1_face->at(1) - plane_origin;
188
189
Vec3 plane_normal;
190
ConvexShape::SupportingFace clipped_face;
191
if (shape1_face->size() >= 3)
192
{
193
// Clip the polygon of face 2 against that of 1
194
ClipPolyVsPoly(*shape2_face, *shape1_face, penetration_axis, clipped_face);
195
196
// Three vertices, can just calculate the normal
197
plane_normal = first_edge.Cross(shape1_face->at(2) - plane_origin);
198
}
199
else
200
{
201
// Clip the polygon of face 2 against edge of 1
202
ClipPolyVsEdge(*shape2_face, shape1_face->at(0), shape1_face->at(1), penetration_axis, clipped_face);
203
204
// Two vertices, first find a perpendicular to the edge and penetration axis and then use the perpendicular together with the edge to form a normal
205
plane_normal = first_edge.Cross(penetration_axis).Cross(first_edge);
206
}
207
208
// If penetration axis and plane normal are perpendicular, fall back to the contact points
209
float penetration_axis_dot_plane_normal = penetration_axis.Dot(plane_normal);
210
if (penetration_axis_dot_plane_normal != 0.0f)
211
{
212
float penetration_axis_len = penetration_axis.Length();
213
214
for (Vec3 p2 : clipped_face)
215
{
216
// Project clipped face back onto the plane of face 1, we do this by solving:
217
// p1 = p2 + distance * penetration_axis / |penetration_axis|
218
// (p1 - plane_origin) . plane_normal = 0
219
// This gives us:
220
// distance = -|penetration_axis| * (p2 - plane_origin) . plane_normal / penetration_axis . plane_normal
221
float distance = (p2 - plane_origin).Dot(plane_normal) / penetration_axis_dot_plane_normal; // note left out -|penetration_axis| term
222
223
// If the point is less than inMaxContactDistance in front of the plane of face 2, add it as a contact point
224
if (distance * penetration_axis_len < inMaxContactDistance)
225
{
226
Vec3 p1 = p2 - distance * penetration_axis;
227
contact_points1->push_back(p1);
228
contact_points2->push_back(p2);
229
}
230
}
231
}
232
233
#ifdef JPH_DEBUG_RENDERER
234
if (ContactConstraintManager::sDrawSupportingFaces)
235
{
236
RMat44 com = RMat44::sTranslation(inCenterOfMass);
237
238
// Draw clipped poly
239
DebugRenderer::sInstance->DrawWirePolygon(com, clipped_face, Color::sOrange);
240
241
// Draw supporting faces
242
DebugRenderer::sInstance->DrawWirePolygon(com, inShape1Face, Color::sRed, 0.05f);
243
DebugRenderer::sInstance->DrawWirePolygon(com, inShape2Face, Color::sGreen, 0.05f);
244
245
// Draw normal
246
float plane_normal_len = plane_normal.Length();
247
if (plane_normal_len > 0.0f)
248
{
249
RVec3 plane_origin_ws = inCenterOfMass + plane_origin;
250
DebugRenderer::sInstance->DrawArrow(plane_origin_ws, plane_origin_ws + plane_normal / plane_normal_len, Color::sYellow, 0.05f);
251
}
252
253
// Draw contact points that remain after distance check
254
for (ContactPoints::size_type p = old_size; p < outContactPoints1.size(); ++p)
255
{
256
DebugRenderer::sInstance->DrawMarker(inCenterOfMass + outContactPoints1[p], Color::sYellow, 0.1f);
257
DebugRenderer::sInstance->DrawMarker(inCenterOfMass + outContactPoints2[p], Color::sOrange, 0.1f);
258
}
259
}
260
#endif // JPH_DEBUG_RENDERER
261
}
262
263
// If the clipping result is empty, use the contact point itself
264
if (outContactPoints1.size() == old_size)
265
{
266
outContactPoints1.push_back(inContactPoint1);
267
outContactPoints2.push_back(inContactPoint2);
268
}
269
}
270
271
JPH_NAMESPACE_END
272
273