Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/ConvexSupport.h
9913 views
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
7
#include <Jolt/Math/Mat44.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// Helper functions to get the support point for a convex object
12
/// Structure that transforms a convex object (supports only uniform scaling)
13
template <typename ConvexObject>
14
struct TransformedConvexObject
15
{
16
/// Create transformed convex object.
17
TransformedConvexObject(Mat44Arg inTransform, const ConvexObject &inObject) :
18
mTransform(inTransform),
19
mObject(inObject)
20
{
21
}
22
23
/// Calculate the support vector for this convex shape.
24
Vec3 GetSupport(Vec3Arg inDirection) const
25
{
26
return mTransform * mObject.GetSupport(mTransform.Multiply3x3Transposed(inDirection));
27
}
28
29
/// Get the vertices of the face that faces inDirection the most
30
template <class VERTEX_ARRAY>
31
void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
32
{
33
mObject.GetSupportingFace(mTransform.Multiply3x3Transposed(inDirection), outVertices);
34
35
for (Vec3 &v : outVertices)
36
v = mTransform * v;
37
}
38
39
Mat44 mTransform;
40
const ConvexObject & mObject;
41
};
42
43
/// Structure that adds a convex radius
44
template <typename ConvexObject>
45
struct AddConvexRadius
46
{
47
AddConvexRadius(const ConvexObject &inObject, float inRadius) :
48
mObject(inObject),
49
mRadius(inRadius)
50
{
51
}
52
53
/// Calculate the support vector for this convex shape.
54
Vec3 GetSupport(Vec3Arg inDirection) const
55
{
56
float length = inDirection.Length();
57
return length > 0.0f ? mObject.GetSupport(inDirection) + (mRadius / length) * inDirection : mObject.GetSupport(inDirection);
58
}
59
60
const ConvexObject & mObject;
61
float mRadius;
62
};
63
64
/// Structure that performs a Minkowski difference A - B
65
template <typename ConvexObjectA, typename ConvexObjectB>
66
struct MinkowskiDifference
67
{
68
MinkowskiDifference(const ConvexObjectA &inObjectA, const ConvexObjectB &inObjectB) :
69
mObjectA(inObjectA),
70
mObjectB(inObjectB)
71
{
72
}
73
74
/// Calculate the support vector for this convex shape.
75
Vec3 GetSupport(Vec3Arg inDirection) const
76
{
77
return mObjectA.GetSupport(inDirection) - mObjectB.GetSupport(-inDirection);
78
}
79
80
const ConvexObjectA & mObjectA;
81
const ConvexObjectB & mObjectB;
82
};
83
84
/// Class that wraps a point so that it can be used with convex collision detection
85
struct PointConvexSupport
86
{
87
/// Calculate the support vector for this convex shape.
88
Vec3 GetSupport([[maybe_unused]] Vec3Arg inDirection) const
89
{
90
return mPoint;
91
}
92
93
Vec3 mPoint;
94
};
95
96
/// Class that wraps a triangle so that it can used with convex collision detection
97
struct TriangleConvexSupport
98
{
99
/// Constructor
100
TriangleConvexSupport(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3) :
101
mV1(inV1),
102
mV2(inV2),
103
mV3(inV3)
104
{
105
}
106
107
/// Calculate the support vector for this convex shape.
108
Vec3 GetSupport(Vec3Arg inDirection) const
109
{
110
// Project vertices on inDirection
111
float d1 = mV1.Dot(inDirection);
112
float d2 = mV2.Dot(inDirection);
113
float d3 = mV3.Dot(inDirection);
114
115
// Return vertex with biggest projection
116
if (d1 > d2)
117
{
118
if (d1 > d3)
119
return mV1;
120
else
121
return mV3;
122
}
123
else
124
{
125
if (d2 > d3)
126
return mV2;
127
else
128
return mV3;
129
}
130
}
131
132
/// Get the vertices of the face that faces inDirection the most
133
template <class VERTEX_ARRAY>
134
void GetSupportingFace([[maybe_unused]] Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
135
{
136
outVertices.push_back(mV1);
137
outVertices.push_back(mV2);
138
outVertices.push_back(mV3);
139
}
140
141
/// The three vertices of the triangle
142
Vec3 mV1;
143
Vec3 mV2;
144
Vec3 mV3;
145
};
146
147
/// Class that wraps a polygon so that it can used with convex collision detection
148
template <class VERTEX_ARRAY>
149
struct PolygonConvexSupport
150
{
151
/// Constructor
152
explicit PolygonConvexSupport(const VERTEX_ARRAY &inVertices) :
153
mVertices(inVertices)
154
{
155
}
156
157
/// Calculate the support vector for this convex shape.
158
Vec3 GetSupport(Vec3Arg inDirection) const
159
{
160
Vec3 support_point = mVertices[0];
161
float best_dot = mVertices[0].Dot(inDirection);
162
163
for (typename VERTEX_ARRAY::const_iterator v = mVertices.begin() + 1; v < mVertices.end(); ++v)
164
{
165
float dot = v->Dot(inDirection);
166
if (dot > best_dot)
167
{
168
best_dot = dot;
169
support_point = *v;
170
}
171
}
172
173
return support_point;
174
}
175
176
/// Get the vertices of the face that faces inDirection the most
177
template <class VERTEX_ARRAY_ARG>
178
void GetSupportingFace([[maybe_unused]] Vec3Arg inDirection, VERTEX_ARRAY_ARG &outVertices) const
179
{
180
for (Vec3 v : mVertices)
181
outVertices.push_back(v);
182
}
183
184
/// The vertices of the polygon
185
const VERTEX_ARRAY & mVertices;
186
};
187
188
JPH_NAMESPACE_END
189
190