mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-12-28 15:44:33 +00:00
Add HMM_RotateQV3(), for rotating a vec3 by a quaternion
This commit is contained in:
@@ -2528,6 +2528,19 @@ static inline HMM_Quat HMM_QFromAxisAngle_LH(HMM_Vec3 Axis, float AngleOfRotatio
|
||||
}
|
||||
|
||||
|
||||
// implementation from
|
||||
// https://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
|
||||
COVERAGE(HMM_RotateQV3, 1)
|
||||
static inline HMM_Vec3 HMM_RotateQV3(HMM_Quat Q, HMM_Vec3 V)
|
||||
{
|
||||
ASSERT_COVERED(HMM_RotateQV3);
|
||||
|
||||
HMM_Vec3 t = HMM_MulV3F(HMM_Cross(Q.XYZ, V), 2);
|
||||
return HMM_AddV3(V, HMM_AddV3(HMM_MulV3F(t, Q.W), HMM_Cross(Q.XYZ, t)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -274,3 +274,25 @@ TEST(QuaternionOps, FromAxisAngle)
|
||||
EXPECT_NEAR(result.W, 0.707107f, 0.001f);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(QuaternionOps, RotateVector)
|
||||
{
|
||||
{
|
||||
HMM_Vec3 axis = HMM_V3(0.0f, 1.0f, 0.0f);
|
||||
float angle = -HMM_PI32 / 2.0f;
|
||||
HMM_Quat q = HMM_QFromAxisAngle_RH(axis, angle);
|
||||
HMM_Vec3 result = HMM_RotateQV3(q, HMM_V3(1.0f, 0.0f, 0.0f));
|
||||
EXPECT_NEAR(result.X, 0.0f, 0.001f);
|
||||
EXPECT_NEAR(result.Y, 0.0f, 0.001f);
|
||||
EXPECT_NEAR(result.Z, 1.0f, 0.001f);
|
||||
}
|
||||
{
|
||||
HMM_Vec3 axis = HMM_V3(1.0f, 0.0f, 0.0f);
|
||||
float angle = HMM_PI32 / 4.0f;
|
||||
HMM_Quat q = HMM_QFromAxisAngle_RH(axis, angle);
|
||||
HMM_Vec3 result = HMM_RotateQV3(q, HMM_V3(0.0f, 0.0f, 1.0f));
|
||||
EXPECT_NEAR(result.X, 0.0f, 0.001f);
|
||||
EXPECT_NEAR(result.Y, -0.707170f, 0.001f);
|
||||
EXPECT_NEAR(result.Z, 0.707170f, 0.001f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user