Explorar el Código

Quaternion from Euler and apply Quaternion to Vector3

hammeron-art hace 11 años
padre
commit
ce132b6f37
Se han modificado 4 ficheros con 68 adiciones y 0 borrados
  1. 22 0
      gameplay/src/Quaternion.cpp
  2. 16 0
      gameplay/src/Quaternion.h
  3. 20 0
      gameplay/src/Vector3.cpp
  4. 10 0
      gameplay/src/Vector3.h

+ 22 - 0
gameplay/src/Quaternion.cpp

@@ -60,6 +60,23 @@ 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)
+{
+	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 = cos(euler.x * 0.5f);
+	float sy = cos(euler.y * 0.5f);
+	float sz = cos(euler.z * 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;
+}
+
 void Quaternion::createFromRotationMatrix(const Matrix& m, Quaternion* dst)
 void Quaternion::createFromRotationMatrix(const Matrix& m, Quaternion* dst)
 {
 {
     m.getRotation(dst);
     m.getRotation(dst);
@@ -219,6 +236,11 @@ 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;

+ 16 - 0
gameplay/src/Quaternion.h

@@ -139,6 +139,15 @@ public:
      */
      */
     bool isZero() const;
     bool isZero() const;
 
 
+	/**
+	* Creates this quaternion equal to the rotation from the specified euler angles
+	* and stores the result in dst.
+	*
+	* @param euler A vector describing the euler angles (in radians).
+	* @param dst A quaternion to store the conjugate in.
+	*/
+	static void createFromEuler(const Vector3& euler, Quaternion* dst);
+
     /**
     /**
      * Creates a quaternion equal to the rotational part of the specified matrix
      * Creates a quaternion equal to the rotational part of the specified matrix
      * and stores the result in dst.
      * and stores the result in dst.
@@ -263,6 +272,13 @@ public:
      */
      */
     void set(const Vector3& axis, float angle);
     void set(const Vector3& axis, float angle);
 
 
+	/**
+	* Sets the elements of this quaternion to a copy of the specified euler angle.
+	*
+	* @param euler The euler angle (in radians).
+	*/
+	void set(const Vector3& euler);
+
     /**
     /**
      * Sets the elements of this quaternion to a copy of the specified quaternion.
      * Sets the elements of this quaternion to a copy of the specified quaternion.
      *
      *

+ 20 - 0
gameplay/src/Vector3.cpp

@@ -1,5 +1,6 @@
 #include "Base.h"
 #include "Base.h"
 #include "Vector3.h"
 #include "Vector3.h"
+#include "Quaternion.h"
 #include "MathUtil.h"
 #include "MathUtil.h"
 
 
 namespace gameplay
 namespace gameplay
@@ -264,6 +265,25 @@ void Vector3::scale(float scalar)
     z *= scalar;
     z *= scalar;
 }
 }
 
 
+void Vector3::applyQuaternion(Quaternion& q)
+{
+
+	Quaternion vecQuat;
+	Quaternion resQuat;
+	vecQuat.x = this->x;
+	vecQuat.y = this->y;
+	vecQuat.z = this->z;
+	vecQuat.w = 0.0f;
+
+	Quaternion conQuat = q;
+	conQuat.conjugate();
+
+	resQuat = vecQuat * conQuat;
+	resQuat = q * resQuat;
+
+	this->set(resQuat.x, resQuat.y, resQuat.z);
+}
+
 void Vector3::set(float x, float y, float z)
 void Vector3::set(float x, float y, float z)
 {
 {
     this->x = x;
     this->x = x;

+ 10 - 0
gameplay/src/Vector3.h

@@ -301,6 +301,16 @@ public:
      */
      */
     void scale(float scalar);
     void scale(float scalar);
 
 
+	/**
+	* Apply a quaternion rotation to this vector
+	*
+	* Note: This vector must first be normalized.
+	*
+	* @param vec The vector to multiply.
+	* @return The multiplied vector.
+	*/
+	void applyQuaternion(Quaternion& q);
+
     /**
     /**
      * Sets the elements of this vector to the specified values.
      * Sets the elements of this vector to the specified values.
      *
      *