|
@@ -60,21 +60,25 @@ bool Quaternion::isZero() const
|
|
|
return x == 0.0f && y == 0.0f && z == 0.0f && w == 0.0f;
|
|
return x == 0.0f && y == 0.0f && z == 0.0f && w == 0.0f;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Quaternion::createFromEuler(const Vector3& euler, Quaternion* dst)
|
|
|
|
|
|
|
+void Quaternion::createFromEuler(float yaw, float pitch, float roll, Quaternion* dst)
|
|
|
{
|
|
{
|
|
|
GP_ASSERT(dst);
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
- float cx = cos(euler.x * 0.5f);
|
|
|
|
|
- float cy = cos(euler.y * 0.5f);
|
|
|
|
|
- float cz = cos(euler.z * 0.5f);
|
|
|
|
|
- float sx = sin(euler.x * 0.5f);
|
|
|
|
|
- float sy = sin(euler.y * 0.5f);
|
|
|
|
|
- float sz = sin(euler.z * 0.5f);
|
|
|
|
|
|
|
+ pitch *= 0.5f;
|
|
|
|
|
+ yaw *= 0.5f;
|
|
|
|
|
+ roll *= 0.5f;
|
|
|
|
|
|
|
|
- dst->x = sx * cy * cz - cx * sy * sz;
|
|
|
|
|
- dst->y = cx * sy * cz + sx * cy * sz;
|
|
|
|
|
- dst->z = cx * cy * sz - sx * sy * cz;
|
|
|
|
|
- dst->w = cx * cy * cz + sx * sy * sz;
|
|
|
|
|
|
|
+ float sinp = sin(pitch);
|
|
|
|
|
+ float siny = sin(yaw);
|
|
|
|
|
+ float sinr = sin(roll);
|
|
|
|
|
+ float cosp = cos(pitch);
|
|
|
|
|
+ float cosy = cos(yaw);
|
|
|
|
|
+ float cosr = cos(roll);
|
|
|
|
|
+
|
|
|
|
|
+ dst->w = cosp * cosy * cosr + sinp * siny * sinr;
|
|
|
|
|
+ dst->x = sinp * cosy * cosr - cosp * siny * sinr;
|
|
|
|
|
+ dst->y = cosp * siny * cosr + sinp * cosy * sinr;
|
|
|
|
|
+ dst->z = cosp * cosy * sinr - sinp * siny * cosr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Quaternion::createFromRotationMatrix(const Matrix& m, Quaternion* dst)
|
|
void Quaternion::createFromRotationMatrix(const Matrix& m, Quaternion* dst)
|
|
@@ -97,6 +101,17 @@ void Quaternion::createFromAxisAngle(const Vector3& axis, float angle, Quaternio
|
|
|
dst->w = cosf(halfAngle);
|
|
dst->w = cosf(halfAngle);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void Quaternion::computeEuler(float* yaw, float* pitch, float* roll)
|
|
|
|
|
+{
|
|
|
|
|
+ GP_ASSERT(yaw);
|
|
|
|
|
+ GP_ASSERT(pitch);
|
|
|
|
|
+ GP_ASSERT(roll);
|
|
|
|
|
+
|
|
|
|
|
+ *pitch = std::atan2(2 * (w*x + y*z), 1 - 2 * (x*x + y*y));
|
|
|
|
|
+ *yaw = std::asin(2 * (w*y - z*x));
|
|
|
|
|
+ *roll = atan2(2 * (w*z + x*y), 1 - 2 * (y*y + z*z));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void Quaternion::conjugate()
|
|
void Quaternion::conjugate()
|
|
|
{
|
|
{
|
|
|
conjugate(this);
|
|
conjugate(this);
|
|
@@ -200,6 +215,24 @@ void Quaternion::normalize(Quaternion* dst) const
|
|
|
dst->w *= n;
|
|
dst->w *= n;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void Quaternion::rotatePoint(Vector3& point, Vector3* dst) const
|
|
|
|
|
+{
|
|
|
|
|
+ Quaternion vecQuat;
|
|
|
|
|
+ Quaternion resQuat;
|
|
|
|
|
+ vecQuat.x = point.x;
|
|
|
|
|
+ vecQuat.y = point.y;
|
|
|
|
|
+ vecQuat.z = point.z;
|
|
|
|
|
+ vecQuat.w = 0.0f;
|
|
|
|
|
+
|
|
|
|
|
+ Quaternion conQuat;
|
|
|
|
|
+ this->conjugate(&conQuat);
|
|
|
|
|
+
|
|
|
|
|
+ resQuat = vecQuat * conQuat;
|
|
|
|
|
+ resQuat = (*this) * resQuat;
|
|
|
|
|
+
|
|
|
|
|
+ dst->set(resQuat.x, resQuat.y, resQuat.z);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void Quaternion::set(float x, float y, float z, float w)
|
|
void Quaternion::set(float x, float y, float z, float w)
|
|
|
{
|
|
{
|
|
|
this->x = x;
|
|
this->x = x;
|
|
@@ -236,11 +269,6 @@ void Quaternion::set(const Quaternion& q)
|
|
|
this->w = q.w;
|
|
this->w = q.w;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Quaternion::set(const Vector3& euler)
|
|
|
|
|
-{
|
|
|
|
|
- Quaternion::createFromEuler(euler, this);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void Quaternion::setIdentity()
|
|
void Quaternion::setIdentity()
|
|
|
{
|
|
{
|
|
|
x = 0.0f;
|
|
x = 0.0f;
|