OpenBarnyard
 
Loading...
Searching...
No Matches
TQuaternion.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TQuaternion.h"
3#include "TMatrix44.h"
4
6
7const TQuaternion TQuaternion::IDENTITY = { 0.0f, 0.0f, 0.0f, 1.0f };
8
10{
11 TFLOAT f1;
12 TFLOAT f2;
13
14 TMath::SinCos( fVal[ 0 ] * 0.5f, f1, f2 );
15 TQuaternion quat = TQuaternion( f1, 0, 0, f2 );
16 TMath::SinCos( fVal[ 1 ] * 0.5f, f1, f2 );
17 Set( 0, f1, 0, f2 );
18 *this *= quat;
19}
20
22{
23 TFLOAT fCosRoll = cos( a_fRoll * 0.5f );
24 TFLOAT fSinRoll = sin( a_fRoll * 0.5f );
25 TFLOAT fCosPitch = cos( a_fPitch * 0.5f );
26 TFLOAT fSinPitch = sin( a_fPitch * 0.5f );
27 TFLOAT fCosYaw = cos( a_fYaw * 0.5f );
28 TFLOAT fSinYaw = sin( a_fYaw * 0.5f );
29
30 TFLOAT fX = fCosYaw * fCosPitch * fSinRoll - fSinYaw * fSinPitch * fCosRoll;
31 TFLOAT fY = fCosPitch * fSinRoll * fSinYaw + fSinPitch * fCosRoll * fCosYaw;
32 TFLOAT fZ = fCosRoll * fCosPitch * fSinYaw - fCosYaw * fSinPitch * fSinRoll;
33 TFLOAT fW = fSinYaw * fSinPitch * fSinRoll + fCosYaw * fCosPitch * fCosRoll;
34
35 Set( fX, fY, fZ, fW );
36}
37
38// $Barnyard: FUNCTION 006c93b0
39void TQuaternion::SetRotation( const TVector3& a_rVec3, TFLOAT a_fVal )
40{
41 TFLOAT fVal;
42 TFLOAT fVal2;
43
44 TMath::SinCos( a_fVal * 0.5f, fVal, fVal2 );
45 Set( a_rVec3.x * fVal, a_rVec3.y * fVal, a_rVec3.z * fVal, fVal2 );
46}
47
48// $Barnyard: FUNCTION 006c9840
49void TQuaternion::SetVectorDirection( const TVector3& a_rVec3, const TVector3& a_rVec3_2 )
50{
51 TVector3 vec3;
52 vec3.CrossProduct( a_rVec3_2, a_rVec3 );
53
54 TFLOAT mag = vec3.Magnitude();
55
56 if ( mag > TMath::TFLOAT_EPSILON )
57 {
58 vec3.Divide( mag );
59 TFLOAT dotProduct = TVector3::DotProduct( a_rVec3_2, a_rVec3 );
60 TFLOAT rot = 0.0f;
61 if ( ( dotProduct <= 1.0f - TMath::TFLOAT_EPSILON ) && ( rot = TMath::PI, -1.0f + TMath::TFLOAT_EPSILON <= dotProduct ) )
62 {
63 rot = TMath::ACos( dotProduct );
64 }
65 SetRotation( vec3, rot );
66 return;
67 }
68
69 TFLOAT dotProduct = TVector3::DotProduct( a_rVec3_2, a_rVec3 );
70
71 if ( dotProduct < 0.0f )
72 {
73 TVector3 vec = a_rVec3_2;
74 if ( TMath::Abs( a_rVec3_2.y ) < 0.95f )
75 {
76 vec.RotateY( TMath::PI * 0.0055555557f * 90.0f );
77 }
78 else
79 {
80 vec.RotateX( TMath::PI * 0.0055555557f * 90.0f );
81 }
83 RotateAroundAxis( vec, TMath::PI * 0.0055555557f * 180.0f );
84 }
85}
86
87void TQuaternion::RotateVector( TVector3& param_1, const TQuaternion& param_2, const TVector3 param_3 )
88{
89 TQuaternion quat = param_2;
90 quat.Negate();
91 TQuaternion quat2 = TQuaternion( param_3.x, param_3.y, param_3.z, 0.0f );
92 TQuaternion quat3 = param_2;
93 quat3 *= quat2;
94 quat3 *= quat;
95
96 param_1.Set( quat3.x, quat3.y, quat3.z );
97}
98
99// $Barnyard: FUNCTION 006c9340
100void TQuaternion::RotateAroundAxis( const TVector3& param_1, TFLOAT param_2 )
101{
102 TFLOAT fVal;
103 TFLOAT fVal2;
104
105 TMath::SinCos( param_2 * 0.5f, fVal, fVal2 );
106 *this *= TQuaternion( param_1.x * fVal, param_1.y * fVal, param_1.z * fVal, fVal2 );
107}
108
110{
111 TMatrix44 matrix;
112 matrix.SetFromQuaternion( *this );
113
114 matrix.GetEulerXYZ( outVec );
115}
116
117// $Barnyard: FUNCTION 006c9190
119{
120 TFLOAT fX = ( ( x * a_Quat.w + a_Quat.z * y ) - a_Quat.y * z ) + a_Quat.x * w;
121 TFLOAT fY = a_Quat.x * z + ( y * a_Quat.w - a_Quat.z * x ) + a_Quat.y * w;
122 TFLOAT fZ = w * a_Quat.z + ( a_Quat.y * x - a_Quat.x * y ) + z * a_Quat.w;
123 TFLOAT fW = ( -( a_Quat.y * y + a_Quat.x * x ) - a_Quat.z * z ) + a_Quat.w * w;
124 Set( fX, fY, fZ, fW );
125 return *this;
126}
127
128// $Barnyard: FUNCTION 006c9ae0
129void TQuaternion::Nlerp( TQuaternion& a_rOut, const TQuaternion& a_rStart, const TQuaternion& a_rEnd, TFLOAT a_fProgress )
130{
131 TFLOAT fDot = TVector4::DotProduct4( a_rStart.AsVector4(), a_rEnd.AsVector4() );
132
133 if ( 0.0f <= fDot )
134 {
135 TVector4 vec = a_rEnd.AsVector4();
136
137 vec.Substract4( a_rStart.AsVector4() );
138 vec.Multiply4( vec, a_fProgress );
139
140 a_rOut.x = vec.x + a_rStart.x;
141 a_rOut.y = vec.y + a_rStart.y;
142 a_rOut.z = vec.z + a_rStart.z;
143 a_rOut.w = vec.w + a_rStart.w;
144
145 a_rOut.AsVector4().Normalise4();
146 }
147 else
148 {
149 TVector4 vec = a_rEnd.AsVector4();
150 vec.Add4( a_rStart.AsVector4() );
151 vec.Multiply4( vec, a_fProgress );
152
153 a_rOut.x = vec.x - a_rStart.x;
154 a_rOut.y = vec.y - a_rStart.y;
155 a_rOut.z = vec.z - a_rStart.z;
156 a_rOut.w = vec.w - a_rStart.w;
157
158 a_rOut.AsVector4().Normalise4();
159 }
160}
161
162// $Barnyard: FUNCTION 006c9400
163void TQuaternion::Slerp( TQuaternion& a_rOut, const TQuaternion& a_rStart, const TQuaternion& a_rEnd, TFLOAT a_fProgress )
164{
165 if ( a_fProgress == 0.0f )
166 {
167 a_rOut = a_rStart;
168 return;
169 }
170
171 if ( a_fProgress == 1.0f )
172 {
173 a_rOut = a_rEnd;
174 return;
175 }
176
177 TFLOAT fDot = TVector4::DotProduct4( a_rStart.AsVector4(), a_rEnd.AsVector4() );
178 TBOOL bNegativeDot = fDot < 0.0f;
179
180 if ( bNegativeDot )
181 fDot *= -1;
182
183 TFLOAT fProg1 = a_fProgress;
184 TFLOAT fProg2;
185
186 if ( 1.0f - fDot <= 0.001f )
187 {
188 fProg2 = 1.0f - a_fProgress;
189 }
190 else
191 {
192 TFLOAT fAcos = TMath::ACos( fDot );
193 TFLOAT fSin = TMath::Sin( fAcos );
194
195 fProg1 = TMath::Sin( fAcos * a_fProgress ) * ( 1.0f / fSin );
196 fProg2 = TMath::Sin( ( 1.0f - a_fProgress ) * fAcos ) * ( 1.0f / fSin );
197 }
198
199 if ( bNegativeDot )
200 {
201 a_rOut.x = a_rStart.x * fProg2 - a_rEnd.x * fProg1;
202 a_rOut.y = a_rStart.y * fProg2 - a_rEnd.y * fProg1;
203 a_rOut.z = a_rStart.z * fProg2 - a_rEnd.z * fProg1;
204 a_rOut.w = a_rStart.w * fProg2 - a_rEnd.w * fProg1;
205 }
206 else
207 {
208 a_rOut.x = a_rStart.x * fProg2 + a_rEnd.x * fProg1;
209 a_rOut.y = a_rStart.y * fProg2 + a_rEnd.y * fProg1;
210 a_rOut.z = a_rStart.z * fProg2 + a_rEnd.z * fProg1;
211 a_rOut.w = a_rStart.w * fProg2 + a_rEnd.w * fProg1;
212 }
213}
214
4x4 matrix implementation for the Toshi engine
Quaternion implementation for the Toshi engine.
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
float TFLOAT
Definition Typedefs.h:4
bool TBOOL
Definition Typedefs.h:6
TFORCEINLINE void SinCos(TFLOAT fVal, TFLOAT &a_rSin, TFLOAT &a_rCos)
TFORCEINLINE TFLOAT Abs(TFLOAT fVal)
Definition TMathInline.h:63
constexpr TFLOAT TFLOAT_EPSILON
Definition TMathInline.h:32
constexpr TFLOAT PI
Definition TMathInline.h:35
TFORCEINLINE TFLOAT Sin(TFLOAT fVal)
Definition TMathInline.h:42
TFORCEINLINE TFLOAT ACos(TFLOAT fVal)
Definition TMathInline.h:50
void GetEulerXYZ(TVector3 &a_rOutVec) const
TMatrix44 & SetFromQuaternion(const TQuaternion &a_rQuaternion)
void GetEulerXYZ(TVector3 &outVec) const
void Set(TFLOAT a_fX, TFLOAT a_fY, TFLOAT a_fZ, TFLOAT a_fW)
Definition TQuaternion.h:31
static const TQuaternion IDENTITY
Definition TQuaternion.h:7
void SetVectorDirection(const TVector3 &a_rVec3, const TVector3 &a_rVec3_2)
void SetRotation(const TVector3 &a_rVec3, TFLOAT a_fVal)
static void RotateVector(TVector3 &param_1, const TQuaternion &param_2, const TVector3 param_3)
void RotateAroundAxis(const TVector3 &param_1, TFLOAT param_2)
static void Nlerp(TQuaternion &a_rOut, const TQuaternion &a_rStart, const TQuaternion &a_rEnd, float a_fProgress)
void SetFromEulerYX(const TFLOAT *fVal)
void SetIdentity()
Definition TQuaternion.h:39
static void Slerp(TQuaternion &a_rOut, const TQuaternion &a_rStart, const TQuaternion &a_rEnd, float a_fProgress)
void SetFromEulerRollPitchYaw(TFLOAT a_fRoll, TFLOAT a_fPitch, TFLOAT a_fYaw)
TQuaternion & operator*=(const TQuaternion &a_rQuat)
const TVector4 & AsVector4() const
Definition TQuaternion.h:66
void Negate()
Definition TQuaternion.h:60
constexpr void Divide(const TVector3 &vec)
Definition TVector3.h:77
void CrossProduct(const TVector3 &, const TVector3 &)
Definition TVector3.cpp:25
TFLOAT y
Definition TVector3.h:163
void RotateX(TFLOAT a_fRotation)
Definition TVector3.cpp:34
constexpr void Set(const TVector3 &vec)
Definition TVector3.h:33
TFLOAT z
Definition TVector3.h:163
TFLOAT x
Definition TVector3.h:163
void RotateY(TFLOAT a_fRotation)
Definition TVector3.cpp:47
static constexpr TFLOAT DotProduct(const TVector3 &vec1, const TVector3 &vec2)
Definition TVector3.h:147
TFLOAT Magnitude() const
Definition TVector3.h:132
constexpr void Add4(const TVector4 &vec)
Definition TVector4.h:81
TFLOAT constexpr DotProduct4(const TVector4 &vec) const
Definition TVector4.h:344
void Normalise4()
Definition TVector4.cpp:64
constexpr void Substract4(const TVector4 &vec)
Definition TVector4.h:102
TFLOAT w
Definition TVector4.h:367
TFLOAT x
Definition TVector4.h:367
TFLOAT y
Definition TVector4.h:367
constexpr void Multiply4(const TVector4 &vec)
Definition TVector4.h:192
TFLOAT z
Definition TVector4.h:367