From 58c0bcca59a6a267436ecd0c7da6dcf85ab006e8 Mon Sep 17 00:00:00 2001 From: Renato Cordeiro Ferreira Date: Mon, 11 May 2015 21:18:38 -0300 Subject: [PATCH] Normalize trackball quartenion --- App/quaternion.js | 10 ++++++++++ App/trackball.js | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/App/quaternion.js b/App/quaternion.js index 23d9b10..e068a67 100644 --- a/App/quaternion.js +++ b/App/quaternion.js @@ -62,6 +62,16 @@ Quaternion.prototype.norm = function() { return dot(this.toVec4(), this.toVec4()); } +/** + * @return Normalized version of this quartenion + */ +Quaternion.prototype.normalize = function() { + return new this.constructor( + this.s/this.norm(), + scalar(1/this.norm(), this.v) + ); +} + /** * Operator q()q^-1 * Use quartenion properties to represent a rotation in IRĀ³ diff --git a/App/trackball.js b/App/trackball.js index 4632c29..6fb34a1 100644 --- a/App/trackball.js +++ b/App/trackball.js @@ -38,7 +38,7 @@ Trackball.prototype.rotation = function(x1, y1, x2, y2, mORq) { if (N[0] == 0 && N[1] == 0 && N[2] == 0) theta = 0; if (mORq == 'q') // return a quaternion - return createRotationQuaternionFromAngleAndAxis(theta, N); + return createRotationQuaternionFromAngleAndAxis(theta, N).normalize(); else // return a matrix return rotate(theta, N); }