Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ConvexSupport.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
7#include <Jolt/Math/Mat44.h>
8
10
13template <typename ConvexObject>
15{
17 TransformedConvexObject(Mat44Arg inTransform, const ConvexObject &inObject) :
18 mTransform(inTransform),
19 mObject(inObject)
20 {
21 }
22
24 Vec3 GetSupport(Vec3Arg inDirection) const
25 {
26 return mTransform * mObject.GetSupport(mTransform.Multiply3x3Transposed(inDirection));
27 }
28
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
40 const ConvexObject & mObject;
41};
42
44template <typename ConvexObject>
46{
47 AddConvexRadius(const ConvexObject &inObject, float inRadius) :
48 mObject(inObject),
49 mRadius(inRadius)
50 {
51 }
52
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
65template <typename ConvexObjectA, typename ConvexObjectB>
67{
68 MinkowskiDifference(const ConvexObjectA &inObjectA, const ConvexObjectB &inObjectB) :
69 mObjectA(inObjectA),
70 mObjectB(inObjectB)
71 {
72 }
73
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
86{
88 Vec3 GetSupport([[maybe_unused]] Vec3Arg inDirection) const
89 {
90 return mPoint;
91 }
92
94};
95
98{
101 mV1(inV1),
102 mV2(inV2),
103 mV3(inV3)
104 {
105 }
106
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
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
145};
146
148template <class VERTEX_ARRAY>
150{
152 explicit PolygonConvexSupport(const VERTEX_ARRAY &inVertices) :
153 mVertices(inVertices)
154 {
155 }
156
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
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
185 const VERTEX_ARRAY & mVertices;
186};
187
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const
Multiply vector by only 3x3 part of the transpose of the matrix ( )
Definition Mat44.inl:336
Definition Vec3.h:17
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:645
JPH_INLINE float Length() const
Length of vector.
Definition Vec3.inl:677
Structure that adds a convex radius.
Definition ConvexSupport.h:46
const ConvexObject & mObject
Definition ConvexSupport.h:60
AddConvexRadius(const ConvexObject &inObject, float inRadius)
Definition ConvexSupport.h:47
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:54
float mRadius
Definition ConvexSupport.h:61
Structure that performs a Minkowski difference A - B.
Definition ConvexSupport.h:67
MinkowskiDifference(const ConvexObjectA &inObjectA, const ConvexObjectB &inObjectB)
Definition ConvexSupport.h:68
const ConvexObjectA & mObjectA
Definition ConvexSupport.h:80
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:75
const ConvexObjectB & mObjectB
Definition ConvexSupport.h:81
Class that wraps a point so that it can be used with convex collision detection.
Definition ConvexSupport.h:86
Vec3 mPoint
Definition ConvexSupport.h:93
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:88
Class that wraps a polygon so that it can used with convex collision detection.
Definition ConvexSupport.h:150
void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY_ARG &outVertices) const
Get the vertices of the face that faces inDirection the most.
Definition ConvexSupport.h:178
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:158
PolygonConvexSupport(const VERTEX_ARRAY &inVertices)
Constructor.
Definition ConvexSupport.h:152
const VERTEX_ARRAY & mVertices
The vertices of the polygon.
Definition ConvexSupport.h:185
Definition ConvexSupport.h:15
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:24
Mat44 mTransform
Definition ConvexSupport.h:39
const ConvexObject & mObject
Definition ConvexSupport.h:40
void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
Get the vertices of the face that faces inDirection the most.
Definition ConvexSupport.h:31
TransformedConvexObject(Mat44Arg inTransform, const ConvexObject &inObject)
Create transformed convex object.
Definition ConvexSupport.h:17
Class that wraps a triangle so that it can used with convex collision detection.
Definition ConvexSupport.h:98
Vec3 mV1
The three vertices of the triangle.
Definition ConvexSupport.h:142
Vec3 mV3
Definition ConvexSupport.h:144
void GetSupportingFace(Vec3Arg inDirection, VERTEX_ARRAY &outVertices) const
Get the vertices of the face that faces inDirection the most.
Definition ConvexSupport.h:134
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:108
Vec3 mV2
Definition ConvexSupport.h:143
TriangleConvexSupport(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3)
Constructor.
Definition ConvexSupport.h:100