UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RotationMatrix.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Math/Vector.h"
7#include "Math/Rotator.h"
8#include "Math/Matrix.h"
11
12namespace UE {
13namespace Math {
14
16template<typename T>
19{
20public:
22
29 : TRotationTranslationMatrix<T>(Rot, TVector<T>::ZeroVector)
30 { }
31
33 [[nodiscard]] static TMatrix<T> Make(TRotator<T> const& Rot)
34 {
35 return TRotationMatrix(Rot);
36 }
37
39 [[nodiscard]] static TMatrix<T> Make(TQuat<T> const& Rot);
40
42 [[nodiscard]] static TMatrix<T> MakeFromX(TVector<T> const& XAxis);
43
45 [[nodiscard]] static TMatrix<T> MakeFromY(TVector<T> const& YAxis);
46
48 [[nodiscard]] static TMatrix<T> MakeFromZ(TVector<T> const& ZAxis);
49
51 [[nodiscard]] static TMatrix<T> MakeFromXY(TVector<T> const& XAxis, TVector<T> const& YAxis);
52
54 [[nodiscard]] static TMatrix<T> MakeFromXZ(TVector<T> const& XAxis, TVector<T> const& ZAxis);
55
57 [[nodiscard]] static TMatrix<T> MakeFromYX(TVector<T> const& YAxis, TVector<T> const& XAxis);
58
60 [[nodiscard]] static TMatrix<T> MakeFromYZ(TVector<T> const& YAxis, TVector<T> const& ZAxis);
61
63 [[nodiscard]] static TMatrix<T> MakeFromZX(TVector<T> const& ZAxis, TVector<T> const& XAxis);
64
66 [[nodiscard]] static TMatrix<T> MakeFromZY(TVector<T> const& ZAxis, TVector<T> const& YAxis);
67};
68
69template<typename T>
74
75template<typename T>
77{
78 TVector<T> const NewX = XAxis.GetSafeNormal();
79
80 // try to use up if possible
81 TVector<T> const UpVector = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
82
83 const TVector<T> NewY = (UpVector ^ NewX).GetSafeNormal();
84 const TVector<T> NewZ = NewX ^ NewY;
85
87}
88
89template<typename T>
91{
92 TVector<T> const NewY = YAxis.GetSafeNormal();
93
94 // try to use up if possible
95 TVector<T> const UpVector = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
96
97 const TVector<T> NewZ = (UpVector ^ NewY).GetSafeNormal();
98 const TVector<T> NewX = NewY ^ NewZ;
99
101}
102
103template<typename T>
105{
106 TVector<T> const NewZ = ZAxis.GetSafeNormal();
107
108 // try to use up if possible
109 TVector<T> const UpVector = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
110
111 const TVector<T> NewX = (UpVector ^ NewZ).GetSafeNormal();
112 const TVector<T> NewY = NewZ ^ NewX;
113
115}
116
117template<typename T>
119{
120 TVector<T> NewX = XAxis.GetSafeNormal();
121 TVector<T> Norm = YAxis.GetSafeNormal();
122
123 // if they're almost same, we need to find arbitrary vector
124 if (FMath::IsNearlyEqual(FMath::Abs(NewX | Norm), T(1.f)))
125 {
126 // make sure we don't ever pick the same as NewX
127 Norm = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
128 }
129
130 const TVector<T> NewZ = (NewX ^ Norm).GetSafeNormal();
131 const TVector<T> NewY = NewZ ^ NewX;
132
134}
135
136template<typename T>
138{
139 TVector<T> const NewX = XAxis.GetSafeNormal();
140 TVector<T> Norm = ZAxis.GetSafeNormal();
141
142 // if they're almost same, we need to find arbitrary vector
143 if (FMath::IsNearlyEqual(FMath::Abs(NewX | Norm), T(1.f)))
144 {
145 // make sure we don't ever pick the same as NewX
146 Norm = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
147 }
148
149 const TVector<T> NewY = (Norm ^ NewX).GetSafeNormal();
150 const TVector<T> NewZ = NewX ^ NewY;
151
153}
154
155template<typename T>
157{
158 TVector<T> const NewY = YAxis.GetSafeNormal();
159 TVector<T> Norm = XAxis.GetSafeNormal();
160
161 // if they're almost same, we need to find arbitrary vector
162 if (FMath::IsNearlyEqual(FMath::Abs(NewY | Norm), T(1.f)))
163 {
164 // make sure we don't ever pick the same as NewX
165 Norm = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
166 }
167
168 const TVector<T> NewZ = (Norm ^ NewY).GetSafeNormal();
169 const TVector<T> NewX = NewY ^ NewZ;
170
172}
173
174template<typename T>
176{
177 TVector<T> const NewY = YAxis.GetSafeNormal();
178 TVector<T> Norm = ZAxis.GetSafeNormal();
179
180 // if they're almost same, we need to find arbitrary vector
181 if (FMath::IsNearlyEqual(FMath::Abs(NewY | Norm), T(1.f)))
182 {
183 // make sure we don't ever pick the same as NewX
184 Norm = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
185 }
186
187 const TVector<T> NewX = (NewY ^ Norm).GetSafeNormal();
188 const TVector<T> NewZ = NewX ^ NewY;
189
191}
192
193template<typename T>
195{
196 TVector<T> const NewZ = ZAxis.GetSafeNormal();
197 TVector<T> Norm = XAxis.GetSafeNormal();
198
199 // if they're almost same, we need to find arbitrary vector
200 if (FMath::IsNearlyEqual(FMath::Abs(NewZ | Norm), T(1.f)))
201 {
202 // make sure we don't ever pick the same as NewX
203 Norm = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
204 }
205
206 const TVector<T> NewY = (NewZ ^ Norm).GetSafeNormal();
207 const TVector<T> NewX = NewY ^ NewZ;
208
210}
211
212template<typename T>
214{
215 TVector<T> const NewZ = ZAxis.GetSafeNormal();
216 TVector<T> Norm = YAxis.GetSafeNormal();
217
218 // if they're almost same, we need to find arbitrary vector
219 if (FMath::IsNearlyEqual(FMath::Abs(NewZ | Norm), T(1.f)))
220 {
221 // make sure we don't ever pick the same as NewX
222 Norm = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
223 }
224
225 const TVector<T> NewX = (Norm ^ NewZ).GetSafeNormal();
226 const TVector<T> NewY = NewZ ^ NewX;
227
229}
230
231} // namespace Math
232} // namespace UE
233
235
236template<> struct TIsUECoreVariant<FRotationMatrix44f> { enum { Value = true }; };
237template<> struct TIsUECoreVariant<FRotationMatrix44d> { enum { Value = true }; };
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_DECLARE_LWC_TYPE(...)
Definition LargeWorldCoordinates.h:27
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
Definition AdvancedWidgetsModule.cpp:13
static UE_FORCEINLINE_HINT bool IsNearlyEqual(float A, float B, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:388
Definition IsUECoreType.h:19
@ Value
Definition IsUECoreType.h:20
Definition Matrix.h:43
T M[4][4]
Definition Matrix.h:49
Definition Quat.h:39
Definition RotationMatrix.h:19
static TMatrix< T > MakeFromZ(TVector< T > const &ZAxis)
Definition RotationMatrix.h:104
static TMatrix< T > MakeFromYX(TVector< T > const &YAxis, TVector< T > const &XAxis)
Definition RotationMatrix.h:156
TRotationMatrix(const TRotator< T > &Rot)
Definition RotationMatrix.h:28
static TMatrix< T > MakeFromYZ(TVector< T > const &YAxis, TVector< T > const &ZAxis)
Definition RotationMatrix.h:175
static TMatrix< T > MakeFromZY(TVector< T > const &ZAxis, TVector< T > const &YAxis)
Definition RotationMatrix.h:213
static TMatrix< T > MakeFromX(TVector< T > const &XAxis)
Definition RotationMatrix.h:76
static TMatrix< T > MakeFromXY(TVector< T > const &XAxis, TVector< T > const &YAxis)
Definition RotationMatrix.h:118
static TMatrix< T > MakeFromY(TVector< T > const &YAxis)
Definition RotationMatrix.h:90
static TMatrix< T > MakeFromXZ(TVector< T > const &XAxis, TVector< T > const &ZAxis)
Definition RotationMatrix.h:137
static TMatrix< T > Make(TRotator< T > const &Rot)
Definition RotationMatrix.h:33
static TMatrix< T > MakeFromZX(TVector< T > const &ZAxis, TVector< T > const &XAxis)
Definition RotationMatrix.h:194
Definition RotationTranslationMatrix.h:17
Definition Rotator.h:37
Definition Vector.h:51
TVector< T > GetSafeNormal(T Tolerance=UE_SMALL_NUMBER, const TVector< T > &ResultIfZero=ZeroVector) const
Definition Vector.h:2060