UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CollisionShape.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Math/Vector.h"
6
9{
17};
18
21{
23
24 static constexpr float MinBoxExtent() { return UE_KINDA_SMALL_NUMBER; }
25 static constexpr float MinSphereRadius() { return UE_KINDA_SMALL_NUMBER; }
26 static constexpr float MinCapsuleRadius() { return UE_KINDA_SMALL_NUMBER; }
27 static constexpr float MinCapsuleAxisHalfHeight() { return UE_KINDA_SMALL_NUMBER; }
28
30 union
31 {
32 struct
33 {
37 } Box;
38
39 struct
40 {
41 float Radius;
43
44 struct
45 {
46 float Radius;
49 };
50
55
57 bool IsLine() const
58 {
60 }
61
63 bool IsBox() const
64 {
66 }
67
69 bool IsSphere() const
70 {
72 }
73
75 bool IsCapsule() const
76 {
78 }
79
82 {
84 Box.HalfExtentX = HalfExtent.X;
85 Box.HalfExtentY = HalfExtent.Y;
86 Box.HalfExtentZ = HalfExtent.Z;
87 }
88
90 void SetSphere(const float Radius)
91 {
93 Sphere.Radius = Radius;
94 }
95
97 void SetCapsule(const float Radius, const float HalfHeight)
98 {
100 Capsule.Radius = Radius;
101 Capsule.HalfHeight = HalfHeight;
102 }
103
105 void SetCapsule(const FVector3f& Extent)
106 {
108 Capsule.Radius = FMath::Max(Extent.X, Extent.Y);
109 Capsule.HalfHeight = Extent.Z;
110 }
111
114 {
115 switch (InShapeType)
116 {
118 {
119 SetBox(FVector3f(Extent));
120 }
121 break;
123 {
124 SetSphere(static_cast<float>(Extent[0]));
125 }
126 break;
128 {
129 SetCapsule(FVector3f(Extent));
130 }
131 break;
133 default:
135 }
136 }
137
139 bool IsNearlyZero() const
140 {
141 switch (ShapeType)
142 {
144 {
145 return (Box.HalfExtentX <= FCollisionShape::MinBoxExtent() && Box.HalfExtentY <= FCollisionShape::MinBoxExtent() && Box.HalfExtentZ <= FCollisionShape::MinBoxExtent());
146 }
148 {
149 return (Sphere.Radius <= FCollisionShape::MinSphereRadius());
150 }
152 {
153 // @Todo check height? It didn't check before, so I'm keeping this way for time being
154 return (Capsule.Radius <= FCollisionShape::MinCapsuleRadius());
155 }
156 }
157
158 return true;
159 }
160
163 {
164 switch (ShapeType)
165 {
167 {
168 return FVector(Box.HalfExtentX, Box.HalfExtentY, Box.HalfExtentZ);
169 }
171 {
172 return FVector(Sphere.Radius, Sphere.Radius, Sphere.Radius);
173 }
175 {
176 // @Todo check height? It didn't check before, so I'm keeping this way for time being
177 return FVector(Capsule.Radius, Capsule.Radius, Capsule.HalfHeight);
178 }
179 }
180
181 return FVector::ZeroVector;
182 }
183
186 {
188 return FMath::Max<float>(Capsule.HalfHeight - Capsule.Radius, FCollisionShape::FCollisionShape::MinCapsuleAxisHalfHeight());
189 }
190
193 {
194 return FVector(Box.HalfExtentX, Box.HalfExtentY, Box.HalfExtentZ);
195 }
196
198 const float GetSphereRadius() const
199 {
200 return Sphere.Radius;
201 }
202
204 const float GetCapsuleRadius() const
205 {
206 return Capsule.Radius;
207 }
208
210 const float GetCapsuleHalfHeight() const
211 {
212 return Capsule.HalfHeight;
213 }
214
216 {
218
219 if (Inflation == 0.f)
220 {
221 InflatedShape = *this;
222 }
223 else
224 {
225 // Don't shrink below zero size.
226 switch (ShapeType)
227 {
229 {
230 const FVector3f InflatedExtent = FVector3f(Box.HalfExtentX + Inflation, Box.HalfExtentY + Inflation, Box.HalfExtentZ + Inflation).ComponentMax(FVector3f::ZeroVector);
232 }
233 break;
235 {
236 InflatedShape.SetSphere(FMath::Max(Sphere.Radius + Inflation, 0));
237 }
238 break;
240 {
241 InflatedShape.SetCapsule(FMath::Max(Capsule.Radius + Inflation, 0), FMath::Max(Capsule.HalfHeight + Inflation, 0));
242 }
243 break;
245 default:
246 {
247 // do not inflate for unsupported shapes
248 InflatedShape = *this;
249 }
250 }
251 }
252
253 return InflatedShape;
254 }
255
256 inline FString ToString() const
257 {
258 switch (ShapeType)
259 {
261 {
262 return FString::Printf(TEXT("Box=(X=%3.3f Y=%3.3f Z=%3.3f)"), Box.HalfExtentX, Box.HalfExtentY, Box.HalfExtentZ);
263 }
265 {
266 return FString::Printf(TEXT("Sphere=(Radius=%3.3f)"), Sphere.Radius);
267 }
269 {
270 return FString::Printf(TEXT("Capsule=(Radius=%3.3f HalfHeight=%3.3f)"), Capsule.Radius, Capsule.HalfHeight);
271 }
273 default:
274 {
275 return TEXT("Line");
276 }
277 }
278 }
279
282
290
298
300 static FCollisionShape MakeSphere(const float SphereRadius)
301 {
303 SphereShape.SetSphere(SphereRadius);
304 return SphereShape;
305 }
306
308 static FCollisionShape MakeCapsule(const float CapsuleRadius, const float CapsuleHalfHeight)
309 {
311 CapsuleShape.SetCapsule(CapsuleRadius, CapsuleHalfHeight);
312 return CapsuleShape;
313 }
314
316 static FCollisionShape MakeCapsule(const FVector& Extent)
317 {
320 return CapsuleShape;
321 }
322};
#define ensure( InExpression)
Definition AssertionMacros.h:464
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define FVector
Definition IOSSystemIncludes.h:8
UE::Math::TVector< float > FVector3f
Definition MathFwd.h:73
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
Definition CollisionShape.h:9
Type
Definition CollisionShape.h:11
@ Sphere
Definition CollisionShape.h:14
@ Box
Definition CollisionShape.h:13
@ Line
Definition CollisionShape.h:12
@ Capsule
Definition CollisionShape.h:15
Definition CollisionShape.h:21
FVector GetExtent() const
Definition CollisionShape.h:162
const float GetCapsuleHalfHeight() const
Definition CollisionShape.h:210
static constexpr float MinCapsuleRadius()
Definition CollisionShape.h:26
FCollisionShape()
Definition CollisionShape.h:51
float HalfHeight
Definition CollisionShape.h:47
void SetCapsule(const float Radius, const float HalfHeight)
Definition CollisionShape.h:97
struct FCollisionShape::@1660::@1663 Sphere
void SetShape(const ECollisionShape::Type InShapeType, const FVector &Extent)
Definition CollisionShape.h:113
static FCollisionShape MakeCapsule(const FVector &Extent)
Definition CollisionShape.h:316
float GetCapsuleAxisHalfLength() const
Definition CollisionShape.h:185
struct FCollisionShape::@1660::@1662 Box
bool IsSphere() const
Definition CollisionShape.h:69
FString ToString() const
Definition CollisionShape.h:256
static FCollisionShape MakeSphere(const float SphereRadius)
Definition CollisionShape.h:300
static constexpr float MinSphereRadius()
Definition CollisionShape.h:25
static FCollisionShape MakeBox(const FVector &BoxHalfExtent)
Definition CollisionShape.h:284
static constexpr float MinBoxExtent()
Definition CollisionShape.h:24
bool IsNearlyZero() const
Definition CollisionShape.h:139
ECollisionShape::Type ShapeType
Definition CollisionShape.h:22
float HalfExtentX
Definition CollisionShape.h:34
FCollisionShape Inflate(const float Inflation) const
Definition CollisionShape.h:215
struct FCollisionShape::@1660::@1664 Capsule
bool IsLine() const
Definition CollisionShape.h:57
float HalfExtentZ
Definition CollisionShape.h:36
FVector GetBox() const
Definition CollisionShape.h:192
float HalfExtentY
Definition CollisionShape.h:35
bool IsBox() const
Definition CollisionShape.h:63
void SetCapsule(const FVector3f &Extent)
Definition CollisionShape.h:105
static FCollisionShape MakeBox(const FVector3f &BoxHalfExtent)
Definition CollisionShape.h:292
void SetSphere(const float Radius)
Definition CollisionShape.h:90
const float GetCapsuleRadius() const
Definition CollisionShape.h:204
const float GetSphereRadius() const
Definition CollisionShape.h:198
void SetBox(const FVector3f &HalfExtent)
Definition CollisionShape.h:81
float Radius
Definition CollisionShape.h:41
bool IsCapsule() const
Definition CollisionShape.h:75
static constexpr float MinCapsuleAxisHalfHeight()
Definition CollisionShape.h:27
static FCollisionShape MakeCapsule(const float CapsuleRadius, const float CapsuleHalfHeight)
Definition CollisionShape.h:308
static struct FCollisionShape LineShape
Definition CollisionShape.h:281
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
static CORE_API const TVector< double > ZeroVector
Definition Vector.h:79
TVector< T > ComponentMax(const TVector< T > &Other) const
Definition Vector.h:1704
T X
Definition Vector.h:62