mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-02-12 07:47:12 +01:00
Implement Quaternion to Euler conversion as a member method to Vector3 type.
This commit is contained in:
parent
624606e482
commit
6e7c14eafb
@ -387,17 +387,35 @@ void Vector3::SetVector4Ex(Value nx, Value ny, Value nz, Value /*nw*/)
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
void Vector3::SetQuaternion(const Quaternion & q)
|
void Vector3::SetQuaternion(const Quaternion & q)
|
||||||
{
|
{
|
||||||
x = q.x;
|
SetQuaternionEx(q.x, q.y, q.z, q.w);
|
||||||
y = q.y;
|
|
||||||
z = q.z;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
void Vector3::SetQuaternionEx(Value nx, Value ny, Value nz, Value /*nw*/)
|
void Vector3::SetQuaternionEx(Value qx, Value qy, Value qz, Value qw)
|
||||||
{
|
{
|
||||||
x = nx;
|
// Quick conversion to Euler angles to give tilt to user
|
||||||
y = ny;
|
const Value sqx = (qx * qx), sqy = (qy * qy), sqz = (qz * qz), sqw = (qw * qw);
|
||||||
z = nz;
|
|
||||||
|
y = std::asin(STOVAL(2.0) * ((qw * qy) - (qx * qz)));
|
||||||
|
|
||||||
|
if (EpsGt((SQMOD_PI * STOVAL(0.5)) - std::abs(y), STOVAL(1e-10)))
|
||||||
|
{
|
||||||
|
z = std::atan2(STOVAL(2.0) * ((qx * qy) + (qw * qz)), sqx - sqy - sqz + sqw);
|
||||||
|
x = std::atan2(STOVAL(2.0) * ((qw * qx) + (qy * qz)), sqw - sqx - sqy + sqz);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Compute heading from local 'down' vector
|
||||||
|
z = std::atan2((STOVAL(2.0) * qy * qz) - (STOVAL(2.0) * qx * qw),
|
||||||
|
(STOVAL(2.0) * qx * qz) + (STOVAL(2.0) * qy * qw));
|
||||||
|
x = STOVAL(0.0);
|
||||||
|
|
||||||
|
// If facing down, reverse yaw
|
||||||
|
if (EpsLt(y, STOVAL(0.0)))
|
||||||
|
{
|
||||||
|
z -= SQMOD_PI;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user