UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GJKShape.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Core.h"
5
6namespace Chaos
7{
14 // Wraps an shape object (could be FImplicitObject or some other type with the required API)
15 // and provides the API required for GJK, treating the shape as if it has zero margin.
16 // This means spheres will be spheres (not points with padding), convexes will be the outer hull, etc.
17 //
18 // See also TGJKShapeTransformed, TGJKCoreShape
19 //
20 // E.g., to use GJK between two Convex implicit objects
21 // GJKDistance(TGJKShape(ConvexA), TGJKShapeTransformed(ConvexB, BToATransform), ...);
22 //
23 // E.g., to use GJK between a convex and a sphere treated as a point with padding
24 // GJKDistance(TGJKShape(ConvexA), TGJKCoreShapeTransformed(SphereB, BToATransform), ...);
25 //
26 template<typename T_SHAPE>
27 struct TGJKShape
28 {
30
32
34 {
35 return V;
36 }
37
39 {
40 return 0.0;
41 }
42
44 {
45 return 0.0f;
46 }
47
48 FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal* OutSupportDelta, int32& VertexIndex) const
49 {
50 return Shape.Support(Dir, InMargin, VertexIndex);
51 }
52
53 bool IsConvex() const
54 {
55 return Shape.IsConvex();
56 }
57
58 FString ToString() const
59 {
60 return FString::Printf(TEXT("TGJKShape: %s"), *Shape.ToString());
61 }
62
64 };
65
66 // Like TGJKShape but for the second shape if it has a transform relative to the first.
67 //
68 // @param InTransform The shape transform (relative to the first shape when used with GJK)
69 //
70 // See also TGJKShape, TGJKCoreShape
71 //
72 template<typename T_SHAPE>
74 {
76
81
83 {
84 return Transform;
85 }
86
88 {
89 return Transform.InverseTransformPositionNoScale(V);
90 }
91
93 {
94 return 0.0f;
95 }
96
97 FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal* OutSupportDelta, int32& VertexIndex) const
98 {
99 const FVec3 LocalDir = Transform.InverseTransformVectorNoScale(Dir);
100 const FVec3 LocalSupport = Shape.Support(LocalDir, InMargin, VertexIndex);
101 return Transform.TransformPositionNoScale(LocalSupport);
102 }
103
104 bool IsConvex() const
105 {
106 return Shape.IsConvex();
107 }
108
109 FString ToString() const
110 {
111 return FString::Printf(TEXT("TGJKShapeTransformed: %s"), *Shape.ToString());
112 }
113
116 };
117
118
119
120 // Like TGJKShape, but treats the shape as if it has a reduced "core" shape with a
121 // margin suitable for collision detection where significant overlaps are likely.
122 // This means spheres will be points, convexes will be rounded shrunken hulls, etc.
123 //
124 // See also TGJKShape
125 //
126 // E.g., to use GJK of a sphere as a point against a marginless convex:
127 // GJKDistance(TGJKCoreShape(MySphere), TGJKShape(MyConvex), ...);
128 //
129 template<typename T_SHAPE>
131 {
133
138
143
148
150 {
151 return V;
152 }
153
155 {
156 return Margin;
157 }
158
160 {
161 return Margin;
162 }
163
164 FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal* OutSupportDelta, int32& VertexIndex) const
165 {
166 return Shape.SupportCore(Dir, InMargin, OutSupportDelta, VertexIndex);
167 }
168
169 bool IsConvex() const
170 {
171 return Shape.IsConvex();
172 }
173
174 FString ToString() const
175 {
176 return FString::Printf(TEXT("TGJKCoreShape: %s, Margin: %f"), *Shape.ToString(), GetMargin());
177 }
178
181 };
182
183 // Like TGJKCoreShape but for the second shape if it has a transform relative to the first.
184 //
185 // @param InTransform The shape transform (relative to the first shape when used with GJK)
186 //
187 // See also TGJKShapeTransformed, TGJKCoreShape
188 //
189 template<typename T_SHAPE>
191 {
193
199
205
207 {
208 return Transform;
209 }
210
212 {
213 return Transform.InverseTransformPositionNoScale(V);
214 }
215
217 {
218 return FReal(Margin);
219 }
220
222 {
223 return Margin;
224 }
225
226 FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal* OutSupportDelta, int32& VertexIndex) const
227 {
228 const FVec3 LocalDir = Transform.InverseTransformVectorNoScale(Dir);
229 const FVec3 LocalSupport = Shape.SupportCore(LocalDir, InMargin, OutSupportDelta, VertexIndex);
230 return Transform.TransformPositionNoScale(LocalSupport);
231 }
232
233 bool IsConvex() const
234 {
235 return Shape.IsConvex();
236 }
237
238 FString ToString() const
239 {
240 return FString::Printf(TEXT("TGJKCoreShapeTransformed: %s"), *Shape.ToString());
241 }
242
246 };
247
253 {
254 public:
256 : Center(InCenter), Radius(InRadius)
257 {
258 }
259
260 inline const FVec3& GetCenter() const
261 {
262 return Center;
263 }
264
265 inline FReal GetRadius() const
266 {
267 return Radius;
268 }
269
271 {
272 return V;
273 }
274
275 inline const FVec3& SupportCore(const FVec3& Direction, const FReal InMargin, FReal* MaxMarginDelta, int32& VertexIndex) const
276 {
277 VertexIndex = 0;
278 return Center;
279 }
280
281 inline FReal GetMargin() const
282 {
283 return Radius;
284 }
285
286 FString ToString() const
287 {
288 return FString::Printf(TEXT("FGJKSphere: Center: [%f, %f, %f], Radius: %f"), Center.X, Center.Y, Center.Z, Radius);
289 }
290
291 private:
292 FVec3 Center;
293 FReal Radius;
294 };
295
297 {
298 public:
300 : Radius(InRadius)
301 {
302 Center = MakeVectorRegisterFloat(InCenter.X, InCenter.Y, InCenter.Z, 0.0f);
303 }
304
306 {
307 return Center;
308 }
309
311 {
312 return Radius;
313 }
314
316 {
317 return Radius;
318 }
319
321 {
322 return Radius;
323 }
324
326 {
327 return Radius;
328 }
329
330 private:
332 FRealSingle Radius;
333 };
334
336 {
337 public:
339 : Radius(InRadius)
340 {
341 Axis = MakeVectorRegisterFloat(InAxis.X, InAxis.Y, InAxis.Z, 0.0f);
342 PointA = MakeVectorRegisterFloat(InPointA.X, InPointA.Y, InPointA.Z, 0.0f);
343 PointB = MakeVectorRegisterFloat(InPointB.X, InPointB.Y, InPointB.Z, 0.0f);
344 }
345
347 {
348 const VectorRegister4Float Dot = VectorDot3(V, Axis);
349 // const bool bIsSecond = Dot >= 0;
351 return VectorSelect(Mask, PointB, PointA);
352 }
353
355 {
356 return Radius;
357 }
358
360 {
361 return Radius;
362 }
363
365 {
366 return Radius;
367 }
368
370 {
371 return Radius;
372 }
373
374 private:
378 FRealSingle Radius;
379 };
380
381 // Utility for creating TGJKShape objects using template parameter deduction
382 template<typename T_SHAPE>
387
388 // Utility for creating TGJKCoreShape objects using template parameter deduction
389 template<typename T_SHAPE>
394}
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FORCEINLINE VectorRegister4Float VectorDot3(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:880
FORCEINLINE VectorRegister4Float VectorSelect(const VectorRegister4Float &Mask, const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:1105
FORCEINLINE VectorRegister4Float VectorCompareGE(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:1000
FORCEINLINE VectorRegister4Float MakeVectorRegisterFloat(uint32 X, uint32 Y, uint32 Z, uint32 W)
Definition UnrealMathFPU.h:175
Definition GJKShape.h:336
FRealSingle GetRadiusf() const
Definition GJKShape.h:359
FGJKCapsuleSingleSIMD(const FVec3f &InPointA, const FVec3f &InPointB, const FVec3f &InAxis, const FRealSingle InRadius)
Definition GJKShape.h:338
FRealSingle GetMargin() const
Definition GJKShape.h:364
VectorRegister4Float SupportCoreSimd(const VectorRegister4Float V, FRealSingle InMargin) const
Definition GJKShape.h:346
FRealSingle GetMarginf() const
Definition GJKShape.h:369
FRealSingle GetRadius() const
Definition GJKShape.h:354
Definition GJKShape.h:297
FRealSingle GetMargin() const
Definition GJKShape.h:320
FRealSingle GetRadius() const
Definition GJKShape.h:310
FRealSingle GetRadiusf() const
Definition GJKShape.h:315
FGJKSphereSingleSIMD(const FVec3f &InCenter, FRealSingle InRadius)
Definition GJKShape.h:299
FRealSingle GetMarginf() const
Definition GJKShape.h:325
VectorRegister4Float SupportCoreSimd(const VectorRegister4Float V, const FRealSingle InMargin) const
Definition GJKShape.h:305
Definition GJKShape.h:253
FReal GetRadius() const
Definition GJKShape.h:265
FVec3 InverseTransformPositionNoScale(const FVec3 &V) const
Definition GJKShape.h:270
FString ToString() const
Definition GJKShape.h:286
FReal GetMargin() const
Definition GJKShape.h:281
FGJKSphere(const FVec3 InCenter, const FReal InRadius)
Definition GJKShape.h:255
const FVec3 & GetCenter() const
Definition GJKShape.h:260
const FVec3 & SupportCore(const FVec3 &Direction, const FReal InMargin, FReal *MaxMarginDelta, int32 &VertexIndex) const
Definition GJKShape.h:275
Definition Vector.h:407
Definition SkeletalMeshComponent.h:307
TGJKShape< T_SHAPE > MakeGJKShape(const T_SHAPE &InShape)
Definition GJKShape.h:383
FRealDouble FReal
Definition Real.h:22
float FRealSingle
Definition Real.h:14
TGJKCoreShape< T_SHAPE > MakeGJKCoreShape(const T_SHAPE &InShape)
Definition GJKShape.h:390
constexpr VectorRegister4Float FloatZero
Definition UnrealMathVectorConstants.h.inl:41
Definition GJKShape.h:191
TGJKCoreShapeTransformed(const FShapeType &InShape, const FRigidTransform3 &InTransform)
Definition GJKShape.h:194
const FShapeType & Shape
Definition GJKShape.h:244
FRealSingle GetMarginf() const
Definition GJKShape.h:221
FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal *OutSupportDelta, int32 &VertexIndex) const
Definition GJKShape.h:226
FString ToString() const
Definition GJKShape.h:238
T_SHAPE FShapeType
Definition GJKShape.h:192
const FRealSingle Margin
Definition GJKShape.h:245
FVec3 InverseTransformPositionNoScale(const FVec3 &V) const
Definition GJKShape.h:211
FRigidTransform3 Transform
Definition GJKShape.h:243
TGJKCoreShapeTransformed(const FShapeType &InShape, const FReal InMargin, const FRigidTransform3 &InTransform)
Definition GJKShape.h:200
bool IsConvex() const
Definition GJKShape.h:233
FReal GetMargin() const
Definition GJKShape.h:216
const FRigidTransform3 & GetTransform() const
Definition GJKShape.h:206
Definition GJKShape.h:131
FVec3 InverseTransformPositionNoScale(const FVec3 &V) const
Definition GJKShape.h:149
TGJKCoreShape(const FShapeType &InShape, const FRealSingle InMargin)
Definition GJKShape.h:144
bool IsConvex() const
Definition GJKShape.h:169
const FRealSingle Margin
Definition GJKShape.h:180
TGJKCoreShape(const FShapeType &InShape, const FReal InMargin)
Definition GJKShape.h:139
FReal GetMargin() const
Definition GJKShape.h:154
const FShapeType & Shape
Definition GJKShape.h:179
T_SHAPE FShapeType
Definition GJKShape.h:132
FRealSingle GetMarginf() const
Definition GJKShape.h:159
TGJKCoreShape(const FShapeType &InShape)
Definition GJKShape.h:134
FString ToString() const
Definition GJKShape.h:174
FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal *OutSupportDelta, int32 &VertexIndex) const
Definition GJKShape.h:164
Definition GJKShape.h:74
const FShapeType & Shape
Definition GJKShape.h:115
bool IsConvex() const
Definition GJKShape.h:104
FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal *OutSupportDelta, int32 &VertexIndex) const
Definition GJKShape.h:97
FVec3 InverseTransformPositionNoScale(const FVec3 &V) const
Definition GJKShape.h:87
TGJKShapeTransformed(const FShapeType &InShape, const FRigidTransform3 &InTransform)
Definition GJKShape.h:77
const FRigidTransform3 & GetTransform() const
Definition GJKShape.h:82
T_SHAPE FShapeType
Definition GJKShape.h:75
FReal GetMargin() const
Definition GJKShape.h:92
FString ToString() const
Definition GJKShape.h:109
FRigidTransform3 Transform
Definition GJKShape.h:114
Definition GJKShape.h:28
FVec3 InverseTransformPositionNoScale(const FVec3 &V) const
Definition GJKShape.h:33
TGJKShape(const FShapeType &InShape)
Definition GJKShape.h:31
FRealSingle GetMarginf() const
Definition GJKShape.h:43
FString ToString() const
Definition GJKShape.h:58
const FShapeType & Shape
Definition GJKShape.h:63
bool IsConvex() const
Definition GJKShape.h:53
T_SHAPE FShapeType
Definition GJKShape.h:29
FVec3 SupportCore(const FVec3 Dir, const FReal InMargin, FReal *OutSupportDelta, int32 &VertexIndex) const
Definition GJKShape.h:48
FReal GetMargin() const
Definition GJKShape.h:38
Definition UnrealMathFPU.h:20