2015-08-21 02:18:13 +00:00
|
|
|
#include "CQuaternion.h"
|
2015-07-26 21:39:49 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <math.h>
|
2015-08-21 02:18:13 +00:00
|
|
|
#include <Common/Math.h>
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
CQuaternion::CQuaternion()
|
|
|
|
{
|
2015-08-21 02:18:13 +00:00
|
|
|
w = 0.f;
|
|
|
|
x = 0.f;
|
|
|
|
y = 0.f;
|
|
|
|
z = 0.f;
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 02:18:13 +00:00
|
|
|
CQuaternion::CQuaternion(float _w, float _x, float _y, float _z)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-08-21 02:18:13 +00:00
|
|
|
w = _w;
|
2015-07-26 21:39:49 +00:00
|
|
|
x = _x;
|
|
|
|
y = _y;
|
|
|
|
z = _z;
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:01:58 +00:00
|
|
|
CVector3f CQuaternion::XAxis()
|
|
|
|
{
|
|
|
|
return (*this * CVector3f::skUnitX);
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector3f CQuaternion::YAxis()
|
|
|
|
{
|
|
|
|
return (*this * CVector3f::skUnitY);
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector3f CQuaternion::ZAxis()
|
|
|
|
{
|
|
|
|
return (*this * CVector3f::skUnitZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
CQuaternion CQuaternion::Inverse()
|
|
|
|
{
|
|
|
|
float fNorm = (w * w) + (x * x) + (y * y) + (z * z);
|
|
|
|
|
|
|
|
if (fNorm > 0.f)
|
|
|
|
{
|
|
|
|
float fInvNorm = 1.f / fNorm;
|
2015-08-21 02:18:13 +00:00
|
|
|
return CQuaternion( w * fInvNorm, -x * fInvNorm, -y * fInvNorm, -z * fInvNorm);
|
2015-08-20 01:01:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return CQuaternion::skZero;
|
|
|
|
}
|
|
|
|
|
2015-08-21 02:18:13 +00:00
|
|
|
CVector3f CQuaternion::ToEuler()
|
|
|
|
{
|
|
|
|
// There is more than one way to do this conversion, based on rotation order.
|
|
|
|
// But since we only care about the rotation order used in Retro games, which is consistent,
|
|
|
|
// we can just have a single conversion function. Handy!
|
|
|
|
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
|
|
|
|
|
|
|
|
float ex = atan2f(2 * (w*x + y*z), 1 - (2 * (Math::Pow(x,2) + Math::Pow(y,2))));
|
|
|
|
float ey = asinf(2 * (w*y - z*x));
|
|
|
|
float ez = atan2f(2 * (w*z + x*y), 1 - (2 * (Math::Pow(y,2) + Math::Pow(z,2))));
|
|
|
|
return CVector3f(Math::RadiansToDegrees(ex), Math::RadiansToDegrees(ey), Math::RadiansToDegrees(ez));
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:01:58 +00:00
|
|
|
// ************ OPERATORS ************
|
|
|
|
CVector3f CQuaternion::operator*(const CVector3f& vec) const
|
|
|
|
{
|
|
|
|
CVector3f uv, uuv;
|
|
|
|
CVector3f qvec(x, y, z);
|
|
|
|
uv = qvec.Cross(vec);
|
|
|
|
uuv = qvec.Cross(uv);
|
|
|
|
uv *= (2.0f * w);
|
|
|
|
uuv *= 2.0f;
|
|
|
|
|
|
|
|
return vec + uv + uuv;
|
|
|
|
}
|
|
|
|
|
2015-07-26 21:39:49 +00:00
|
|
|
CQuaternion CQuaternion::operator*(const CQuaternion& other) const
|
|
|
|
{
|
|
|
|
CQuaternion out;
|
2015-08-21 02:18:13 +00:00
|
|
|
out.w = (-x * other.x) - (y * other.y) - (z * other.z) + (w * other.w);
|
2015-07-26 21:39:49 +00:00
|
|
|
out.x = ( x * other.w) + (y * other.z) - (z * other.y) + (w * other.x);
|
|
|
|
out.y = (-x * other.z) + (y * other.w) + (z * other.x) + (w * other.y);
|
|
|
|
out.z = ( x * other.y) - (y * other.x) + (z * other.w) + (w * other.z);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CQuaternion::operator *= (const CQuaternion& other)
|
|
|
|
{
|
|
|
|
*this = *this * other;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ************ STATIC ************
|
|
|
|
CQuaternion CQuaternion::FromEuler(CVector3f euler)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The commented-out code below might be faster but the conversion isn't completely correct
|
|
|
|
* So in lieu of fixing it I'm using axis angles to convert from Eulers instead
|
|
|
|
* I'm not sure what the difference is performance-wise but the result is 100% accurate
|
|
|
|
*/
|
|
|
|
/*CQuaternion quat;
|
|
|
|
|
|
|
|
// Convert from degrees to radians
|
|
|
|
float pi = 3.14159265358979323846f;
|
|
|
|
euler = euler * pi / 180;
|
|
|
|
|
|
|
|
// Convert to quaternion
|
|
|
|
float c1 = cos(euler.x / 2);
|
|
|
|
float c2 = cos(euler.y / 2);
|
|
|
|
float c3 = cos(euler.z / 2);
|
|
|
|
float s1 = sin(euler.x / 2);
|
|
|
|
float s2 = sin(euler.y / 2);
|
|
|
|
float s3 = sin(euler.z / 2);
|
|
|
|
|
|
|
|
quat.w = (c1 * c2 * c3) - (s1 * s2 * s3);
|
|
|
|
quat.x = -((s1 * c2 * c3) + (c1 * s2 * s3));
|
|
|
|
quat.y = ((c1 * s2 * c3) - (s1 * c2 * s3));
|
|
|
|
quat.z = ((s1 * s2 * c3) + (c1 * c2 * s3));*/
|
|
|
|
|
|
|
|
CQuaternion x = CQuaternion::FromAxisAngle(euler.x, CVector3f(1,0,0));
|
|
|
|
CQuaternion y = CQuaternion::FromAxisAngle(euler.y, CVector3f(0,1,0));
|
|
|
|
CQuaternion z = CQuaternion::FromAxisAngle(euler.z, CVector3f(0,0,1));
|
|
|
|
CQuaternion quat = z * y * x;
|
|
|
|
|
|
|
|
return quat;
|
|
|
|
}
|
|
|
|
|
|
|
|
CQuaternion CQuaternion::FromAxisAngle(float angle, CVector3f axis)
|
|
|
|
{
|
|
|
|
CQuaternion quat;
|
|
|
|
axis = axis.Normalized();
|
2015-08-21 02:18:13 +00:00
|
|
|
angle = Math::DegreesToRadians(angle);
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-08-21 02:18:13 +00:00
|
|
|
float sa = sinf(angle / 2);
|
|
|
|
quat.w = cosf(angle / 2);
|
2015-07-26 21:39:49 +00:00
|
|
|
quat.x = axis.x * sa;
|
|
|
|
quat.y = axis.y * sa;
|
|
|
|
quat.z = axis.z * sa;
|
|
|
|
return quat;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 02:18:13 +00:00
|
|
|
CQuaternion CQuaternion::skIdentity = CQuaternion(1.f, 0.f, 0.f, 0.f);
|
2015-08-20 01:01:58 +00:00
|
|
|
CQuaternion CQuaternion::skZero = CQuaternion(0.f, 0.f, 0.f, 0.f);
|