Browse Source

Add methods to get rotation axis and angle from Quaternion.

Eugene Kozlov 8 years ago
parent
commit
06901b3eb2

+ 2 - 0
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -640,6 +640,8 @@ static void RegisterQuaternion(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Quaternion", "float get_yaw() const", asMETHOD(Quaternion, YawAngle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_yaw() const", asMETHOD(Quaternion, YawAngle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_pitch() const", asMETHOD(Quaternion, PitchAngle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_pitch() const", asMETHOD(Quaternion, PitchAngle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_roll() const", asMETHOD(Quaternion, RollAngle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "float get_roll() const", asMETHOD(Quaternion, RollAngle), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Quaternion", "Vector3 get_axis() const", asMETHOD(Quaternion, Axis), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Quaternion", "float get_angle() const", asMETHOD(Quaternion, Angle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Matrix3 get_rotationMatrix() const", asMETHOD(Quaternion, RotationMatrix), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Matrix3 get_rotationMatrix() const", asMETHOD(Quaternion, RotationMatrix), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Slerp(const Quaternion&in, float) const", asMETHOD(Quaternion, Slerp), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Slerp(const Quaternion&in, float) const", asMETHOD(Quaternion, Slerp), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Nlerp(const Quaternion&in, float, bool) const", asMETHOD(Quaternion, Nlerp), asCALL_THISCALL);
     engine->RegisterObjectMethod("Quaternion", "Quaternion Nlerp(const Quaternion&in, float, bool) const", asMETHOD(Quaternion, Nlerp), asCALL_THISCALL);

+ 2 - 0
Source/Urho3D/LuaScript/pkgs/Math/Quaternion.pkg

@@ -47,6 +47,8 @@ class Quaternion
     float YawAngle() const;
     float YawAngle() const;
     float PitchAngle() const;
     float PitchAngle() const;
     float RollAngle() const;
     float RollAngle() const;
+    Vector3 Axis() const;
+    float Angle() const;
     Matrix3 RotationMatrix() const;
     Matrix3 RotationMatrix() const;
     Quaternion Slerp(const Quaternion& rhs, float t) const;
     Quaternion Slerp(const Quaternion& rhs, float t) const;
     Quaternion Nlerp(const Quaternion& rhs, float t, bool shortestPath) const;
     Quaternion Nlerp(const Quaternion& rhs, float t, bool shortestPath) const;

+ 10 - 0
Source/Urho3D/Math/Quaternion.cpp

@@ -221,6 +221,16 @@ float Quaternion::RollAngle() const
     return EulerAngles().z_;
     return EulerAngles().z_;
 }
 }
 
 
+Urho3D::Vector3 Quaternion::Axis() const
+{
+    return Vector3(x_, y_, z_) / sqrt(1 - w_ * w_);
+}
+
+float Quaternion::Angle() const
+{
+    return 2 * Acos(w_);
+}
+
 Matrix3 Quaternion::RotationMatrix() const
 Matrix3 Quaternion::RotationMatrix() const
 {
 {
     return Matrix3(
     return Matrix3(

+ 4 - 0
Source/Urho3D/Math/Quaternion.h

@@ -431,6 +431,10 @@ public:
     float PitchAngle() const;
     float PitchAngle() const;
     /// Return roll angle in degrees.
     /// Return roll angle in degrees.
     float RollAngle() const;
     float RollAngle() const;
+    /// Return rotation axis.
+    Vector3 Axis() const;
+    /// Return rotation angle.
+    float Angle() const;
     /// Return the rotation matrix that corresponds to this quaternion.
     /// Return the rotation matrix that corresponds to this quaternion.
     Matrix3 RotationMatrix() const;
     Matrix3 RotationMatrix() const;
     /// Spherical interpolation with another quaternion.
     /// Spherical interpolation with another quaternion.