UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Raycasts.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::Raycasts
7{
8 template <typename T, int d>
9 CHAOSCORE_API bool RayAabb(const TVector<FReal, d>& RayStart, const TVector<FReal, d>& RayDir, const FReal RayLength, const FReal RayThickness, const TVector<T, d>& AabbMin, const TVector<T, d>& AabbMax, FReal& OutTime, TVector<FReal, d>& OutPosition, TVector<FReal, d>& OutNormal, int32& OutFaceIndex);
10
11 template <typename T, int d>
13 {
16
17 //For each axis record the start and end time when ray is in the box. If the intervals overlap the ray is inside the box
20
21 for (int Axis = 0; Axis < d; ++Axis)
22 {
23 FReal Time1, Time2;
24 if (bRayParallel[Axis])
25 {
26 if (StartToMin[Axis] > 0 || StartToMax[Axis] < 0)
27 {
28 return false; //parallel and outside
29 }
30 else
31 {
32 Time1 = 0;
34 }
35 }
36 else
37 {
38 Time1 = StartToMin[Axis] * InvRayDir[Axis];
39 Time2 = StartToMax[Axis] * InvRayDir[Axis];
40 }
41
42 if (Time1 > Time2)
43 {
44 //going from max to min direction
45 Swap(Time1, Time2);
46 }
47
48 LatestStartTime = FMath::Max(LatestStartTime, Time1);
49 EarliestEndTime = FMath::Min(EarliestEndTime, Time2);
50
52 {
53 return false; //Outside of slab before entering another
54 }
55 }
56
57 //infinite ray intersects with inflated box
59 {
60 //outside of line segment given
61 return false;
62 }
63
66 return true;
67 }
68
69 CHAOSCORE_API bool RayCapsule(const FVec3& RayStart, const FVec3& RayDir, const FReal RayLength, const FReal RayThickness, FReal CapsuleRadius, FReal CapsuleHeight, const FVec3& CapsuleAxis, const FVec3& CapsuleX1, const FVec3& CapsuleX2, FReal& OutTime, FVec3& OutPosition, FVec3& OutNormal);
70
71 template <typename T, int d>
72 bool RaySphere(const TVector<T, d>& RayStart, const TVector<T, d>& RayDir, const T RayLength, const T RayThickness, const FVec3f& SphereCenter, const FRealSingle SphereRadius, T& OutTime, TVector<T, d>& OutPosition, TVector<T, d>& OutNormal)
73 {
75 ensure(RayLength >= 0);
76
77 const T EffectiveRadius = RayThickness + SphereRadius;
79 const TVector<T, d> Offset = SphereCenter - RayStart;
80 const T OffsetSize2 = Offset.SizeSquared();
82 {
83 //initial overlap
84 OutTime = 0; //no position or normal since initial overlap
85 return true;
86 }
87
88 //(SphereCenter-X) \dot (SphereCenter-X) = EffectiveRadius^2
89 //Let X be on ray, then (SphereCenter - RayStart - t RayDir) \dot (SphereCenter - RayStart - t RayDir) = EffectiveRadius^2
90 //Let Offset = (SphereCenter - RayStart), then reduces to quadratic: t^2 - 2t*(Offset \dot RayDir) + Offset^2 - EffectiveRadius^2 = 0
91 //const T A = 1;
93 const T C = OffsetSize2 - EffectiveRadius2;
94 //time = (-b +- sqrt(b^2 - 4ac)) / 2a
95 //2 from the B cancels because of 2a and 4ac
96 const T QuarterUnderRoot = HalfB * HalfB - C;
97 if (QuarterUnderRoot < 0)
98 {
99 return false;
100 }
101
102 constexpr T Epsilon = 1e-4f;
103 //we early out if starting in sphere, so using first time is always acceptable
104 T FirstTime = QuarterUnderRoot < Epsilon ? -HalfB : -HalfB - FMath::Sqrt(QuarterUnderRoot);
105 if (FirstTime >= 0 && FirstTime <= RayLength)
106 {
107 const TVector<T, d> FinalSpherePosition = RayStart + FirstTime * RayDir;
110
111 OutTime = FirstTime;
112 OutPosition = IntersectionPosition;
113 OutNormal = FinalNormal;
114 return true;
115 }
116
117 return false;
118 }
119} // namespace Chaos::Raycasts
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define ensure( InExpression)
Definition AssertionMacros.h:464
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
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
uint32 Offset
Definition VulkanMemory.cpp:4033
Definition Vector.h:407
Definition Vector.h:41
Definition Raycasts.cpp:10
bool RayAabb(const TVector< FReal, d > &RayStart, const TVector< FReal, d > &RayDir, const FReal RayLength, const FReal RayThickness, const TVector< T, d > &AabbMin, const TVector< T, d > &AabbMax, FReal &OutTime, TVector< FReal, d > &OutPosition, TVector< FReal, d > &OutNormal, int32 &OutFaceIndex)
Definition Raycasts.cpp:12
bool RayCapsule(const FVec3 &RayStart, const FVec3 &RayDir, const FReal RayLength, const FReal RayThickness, FReal CapsuleRadius, FReal CapsuleHeight, const FVec3 &CapsuleAxis, const FVec3 &CapsuleX1, const FVec3 &CapsuleX2, FReal &OutTime, FVec3 &OutPosition, FVec3 &OutNormal)
Definition Raycasts.cpp:181
bool RaySphere(const TVector< T, d > &RayStart, const TVector< T, d > &RayDir, const T RayLength, const T RayThickness, const FVec3f &SphereCenter, const FRealSingle SphereRadius, T &OutTime, TVector< T, d > &OutPosition, TVector< T, d > &OutNormal)
Definition Raycasts.h:72
FRealDouble FReal
Definition Real.h:22
float FRealSingle
Definition Real.h:14
static UE_FORCEINLINE_HINT bool IsNearlyEqual(float A, float B, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:388
Definition NumericLimits.h:41