From d49f2d328104bc22d900ffd24f794999fd88ac16 Mon Sep 17 00:00:00 2001 From: Marcel Jackwerth Date: Fri, 14 Oct 2011 18:54:44 +0300 Subject: [PATCH 1/2] Always use shortest path for Quaternion.slerp (also see: http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ Other Issues, Inverting Quaternions) --- src/core/Quaternion.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core/Quaternion.js b/src/core/Quaternion.js index 67e22799..de17ad3c 100644 --- a/src/core/Quaternion.js +++ b/src/core/Quaternion.js @@ -207,8 +207,17 @@ THREE.Quaternion.prototype = { THREE.Quaternion.slerp = function ( qa, qb, qm, t ) { + // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ + var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z; + if (cosHalfTheta < 0) { + qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = qb.z; + cosHalfTheta = -cosHalfTheta; + } else { + qm.copy(qb); + } + if ( Math.abs( cosHalfTheta ) >= 1.0 ) { qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z; @@ -233,10 +242,10 @@ THREE.Quaternion.slerp = function ( qa, qb, qm, t ) { var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta, ratioB = Math.sin( t * halfTheta ) / sinHalfTheta; - qm.w = ( qa.w * ratioA + qb.w * ratioB ); - qm.x = ( qa.x * ratioA + qb.x * ratioB ); - qm.y = ( qa.y * ratioA + qb.y * ratioB ); - qm.z = ( qa.z * ratioA + qb.z * ratioB ); + qm.w = ( qa.w * ratioA + qm.w * ratioB ); + qm.x = ( qa.x * ratioA + qm.x * ratioB ); + qm.y = ( qa.y * ratioA + qm.y * ratioB ); + qm.z = ( qa.z * ratioA + qm.z * ratioB ); return qm; From b6e6624a1d93dbc7a2956045d6b7bc470dd49fa8 Mon Sep 17 00:00:00 2001 From: Marcel Jackwerth Date: Fri, 14 Oct 2011 19:46:29 +0300 Subject: [PATCH 2/2] Added missing minus sign. --- src/core/Quaternion.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Quaternion.js b/src/core/Quaternion.js index de17ad3c..eb22b32e 100644 --- a/src/core/Quaternion.js +++ b/src/core/Quaternion.js @@ -212,7 +212,7 @@ THREE.Quaternion.slerp = function ( qa, qb, qm, t ) { var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z; if (cosHalfTheta < 0) { - qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = qb.z; + qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z; cosHalfTheta = -cosHalfTheta; } else { qm.copy(qb);