From f7407f35f2862eff2f8620f9863ba8b43f7840b4 Mon Sep 17 00:00:00 2001 From: Pawel Kolodziejski Date: Sat, 21 Feb 2015 12:35:55 +0100 Subject: [PATCH] MATH: Add a vector rotation method to the Quaternion class --- math/quat.cpp | 5 +++++ math/quat.h | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/math/quat.cpp b/math/quat.cpp index 37cff9d7401..ed61d88b33a 100644 --- a/math/quat.cpp +++ b/math/quat.cpp @@ -114,6 +114,11 @@ Quaternion& Quaternion::normalize() { return *this; } +void Quaternion::transform(Vector3d &v) const { + const Vector3d im = Vector3d(x(), y(), z()); + v += 2.0 * Vector3d::crossProduct(im, Vector3d::crossProduct(im, v) + w() * v); +} + void Quaternion::fromMatrix(const Matrix3 &m) { float qx, qy, qz, qw; float tr = m.getValue(0, 0) + m.getValue(1, 1) + m.getValue(2, 2); diff --git a/math/quat.h b/math/quat.h index 3706bc41ab5..4042f87f1c0 100644 --- a/math/quat.h +++ b/math/quat.h @@ -159,6 +159,12 @@ public: */ Quaternion &normalize(); + /** + * Rotate a vector by a Quaternion + * @param v The Vector to be rotated + */ + void transform(Vector3d &v) const; + /** * Converts from this Quaternion to a Matrix4 representation * @return The resulting matrix