Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed several bugs in vec3.rotationTo

This closes #30.
  • Loading branch information...
commit 1a302bceff464008b90ca401d0596c40f0e23269 1 parent 8cd5939
@sinisterchipmunk authored
Showing with 33 additions and 5 deletions.
  1. +6 −5 gl-matrix.js
  2. +27 −0 spec/javascripts/vec3_spec.js
View
11 gl-matrix.js
@@ -467,6 +467,7 @@
var yUnitVec3 = vec3.createFrom(0,1,0);
var zUnitVec3 = vec3.createFrom(0,0,1);
+ var tmpvec3 = vec3.create();
/**
* Generates a quaternion of rotation between two given normalized vectors
*
@@ -478,19 +479,19 @@
*/
vec3.rotationTo = function (a, b, dest) {
if (!dest) { dest = quat4.create(); }
-
+
var d = vec3.dot(a, b);
- var axis = vec3.create();
+ var axis = tmpvec3;
if (d >= 1.0) {
quat4.set(identityQuat4, dest);
} else if (d < (0.000001 - 1.0)) {
vec3.cross(xUnitVec3, a, axis);
- if (axis.length < 0.000001)
+ if (vec3.length(axis) < 0.000001)
vec3.cross(yUnitVec3, a, axis);
- if (axis.length < 0.000001)
+ if (vec3.length(axis) < 0.000001)
vec3.cross(zUnitVec3, a, axis);
vec3.normalize(axis);
- quat4.fromAxisAngle(axis, Math.PI, dest);
+ quat4.fromAngleAxis(Math.PI, axis, dest);
} else {
var s = Math.sqrt((1.0 + d) * 2.0);
var sInv = 1.0 / s;
View
27 spec/javascripts/vec3_spec.js
@@ -1,6 +1,33 @@
describe("vec3", function() {
var vec, vecB, dest;
+ describe("rotationTo", function() {
+ it("where d == 0", function() {
+ var rot = vec3.rotationTo([-1, 0, 0], [0, 1, 0]);
+ expect(quat4.multiplyVec3(rot, [-1, 0, 0])).toBeEqualish([0, 1, 0]);
+ });
+
+ it("where d <= 1 along x axis", function() {
+ var rot = vec3.rotationTo([1, 0, 0], [-1, 0, 0]);
+ expect(quat4.multiplyVec3(rot, [1, 0, 0])).toBeEqualish([-1, 0, 0]);
+ });
+
+ it("where d <= 1 along y axis", function() {
+ var rot = vec3.rotationTo([0, 1, 0], [0, -1, 0]);
+ expect(quat4.multiplyVec3(rot, [0, 1, 0])).toBeEqualish([0, -1, 0]);
+ });
+
+ it("where d <= 1 along z axis", function() {
+ var rot = vec3.rotationTo([0, 0, 1], [0, 0, -1]);
+ expect(quat4.multiplyVec3(rot, [0, 0, 1])).toBeEqualish([0, 0, -1]);
+ });
+
+ it("where d == 1", function() {
+ var rot = vec3.rotationTo([0, 0, 1], [0, 0, 1]);
+ expect(quat4.multiplyVec3(rot, [0, 0, 1])).toBeEqualish([0, 0, 1]);
+ });
+ });
+
describe("when Float32Array is not supported", function() {
beforeEach(function() { setMatrixArrayType(Array); });
Please sign in to comment.
Something went wrong with that request. Please try again.