Skip to content

Commit c61fee3

Browse files
committed
normalize angles in math functions
1 parent d0f0381 commit c61fee3

File tree

3 files changed

+66
-67
lines changed

3 files changed

+66
-67
lines changed

camera.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,18 @@ Camera.prototype.moveAngleHorizontal=function(angleDegrees){
5555
}
5656
/** @private */
5757
Camera.prototype._angleHorizontal=function(angleDegrees){
58-
var change=this.angleY;
59-
this.angleY+=(angleDegrees*GLMath.PiDividedBy180)%(Math.PI*2);
60-
if(this.angleY<0)this.angleY=(Math.PI*2+this.angleY);
61-
change=this.angleY-change;
58+
var change=((angleDegrees>=0 && angleDegrees<360) ? angleDegrees : ((angleDegrees%360)+(angleDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy180;
59+
this.angleY+=change;
60+
this.angleY=((this.angleY>=0 && this.angleY<GLMath.PiTimes2) ? this.angleY : ((this.angleY%GLMath.PiTimes2)+(this.angleY<0 ? GLMath.PiTimes2 : 0)))
6261
this.angleQuat=GLMath.quatMultiply(this.angleQuat,
6362
GLMath.quatFromAxisAngle(change*GLMath.Num180DividedByPi,0,1,0));
6463
GLMath.quatNormInPlace(this.angleQuat);
6564
}
6665
/** @private */
6766
Camera.prototype._angleVertical=function(angleDegrees){
68-
var change=this.angleX;
69-
this.angleX+=(angleDegrees*GLMath.PiDividedBy180)%(Math.PI*2);
70-
if(this.angleX<0)this.angleX=(Math.PI*2+this.angleX);
71-
change=this.angleX-change;
67+
var change=((angleDegrees>=0 && angleDegrees<360) ? angleDegrees : ((angleDegrees%360)+(angleDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy180;
68+
this.angleX+=change;
69+
this.angleX=((this.angleX>=0 && this.angleX<GLMath.PiTimes2) ? this.angleX : ((this.angleX%GLMath.PiTimes2)+(this.angleX<0 ? GLMath.PiTimes2 : 0)))
7270
this.angleQuat=GLMath.quatMultiply(this.angleQuat,
7371
GLMath.quatFromAxisAngle(change*GLMath.Num180DividedByPi,1,0,0));
7472
GLMath.quatNormInPlace(this.angleQuat);

glmath.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ http://creativecommons.org/publicdomain/zero/1.0/
66
If you like this, you should donate to Peter O.
77
at: http://upokecenter.dreamhosters.com/articles/donate-now-2/
88
*/
9+
910
/**
1011
* A collection of math functions for working
1112
* with vectors and matrices.<p>
1213
* See the tutorial "{@tutorial glmath}" for more information.
1314
* @module glmath
1415
*/
1516
(function (g,f) {
16-
if (typeof define=="function" && define["amd"]) {
17-
define([ "exports" ], f);
18-
} else if (typeof exports=="object") {
19-
f(exports);
20-
} else {
21-
f(g);
22-
}
17+
if (typeof define=="function" && define["amd"]) {
18+
define([ "exports" ], f);
19+
} else if (typeof exports=="object") {
20+
f(exports);
21+
} else {
22+
f(g);
23+
}
2324
}(this, function (exports) {
24-
if (exports.GLMath) { return; }
25+
if (exports.GLMath) { return; }
2526

2627
/**
2728
* A collection of math functions for working
@@ -539,18 +540,18 @@ if(typeof vy!="undefined" && typeof vz!="undefined"){
539540
v0=v;
540541
v1=vy;
541542
v2=vz;
542-
ang=angle*GLMath.PiDividedBy360;
543+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy360;
543544
} else if(typeof v=="undefined"){
544545
v0=angle[0];
545546
v1=angle[1];
546547
v2=angle[2];
547548
ang=angle[3];
548-
ang=ang*GLMath.PiDividedBy360;
549+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy360;
549550
} else {
550551
v0=v[0];
551552
v1=v[1];
552553
v2=v[2];
553-
ang=angle*GLMath.PiDividedBy360;
554+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy360;
554555
}
555556
var cost = Math.cos(ang);
556557
var sint = Math.sin(ang);
@@ -585,13 +586,13 @@ quatFromTaitBryan:function(pitchDegrees,yawDegrees,rollDegrees, mode){
585586
if(mode==null)mode=GLMath.RollPitchYaw;
586587
if(mode<0 || mode>=6)throw new Error("invalid mode");
587588
if(pitchDegrees.constructor==Array){
588-
rollRad=pitchDegrees[2]*GLMath.PiDividedBy360;
589-
pitchRad=pitchDegrees[0]*GLMath.PiDividedBy360;
590-
yawRad=pitchDegrees[1]*GLMath.PiDividedBy360;
589+
rollRad=((pitchDegrees[2]>=0 && pitchDegrees[2]<360) ? pitchDegrees[2] : ((pitchDegrees[2]%360)+(pitchDegrees[2]<0 ? 360 : 0)))*GLMath.PiDividedBy360;
590+
pitchRad=((pitchDegrees[0]>=0 && pitchDegrees[0]<360) ? pitchDegrees[0] : ((pitchDegrees[0]%360)+(pitchDegrees[0]<0 ? 360 : 0)))*GLMath.PiDividedBy360;
591+
yawRad=((pitchDegrees[1]>=0 && pitchDegrees[1]<360) ? pitchDegrees[1] : ((pitchDegrees[1]%360)+(pitchDegrees[1]<0 ? 360 : 0)))*GLMath.PiDividedBy360;
591592
} else {
592-
rollRad=rollDegrees*GLMath.PiDividedBy360;
593-
pitchRad=pitchDegrees*GLMath.PiDividedBy360;
594-
yawRad=yawDegrees*GLMath.PiDividedBy360;
593+
rollRad=((rollDegrees>=0 && rollDegrees<360) ? rollDegrees : ((rollDegrees%360)+(rollDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy360;
594+
pitchRad=((pitchDegrees>=0 && pitchDegrees<360) ? pitchDegrees : ((pitchDegrees%360)+(pitchDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy360;
595+
yawRad=((yawDegrees>=0 && yawDegrees<360) ? yawDegrees : ((yawDegrees%360)+(yawDegrees<0 ? 360 : 0)))*GLMath.PiDividedBy360;
595596
}
596597
var px = Math.sin(pitchRad);
597598
var py = Math.cos(pitchRad);
@@ -652,7 +653,7 @@ quatToTaitBryan:function(a,mode){
652653
} else {
653654
c1=a[0]; c2=a[1]; c3=a[2];
654655
}
655-
var sq1=c1*c1;
656+
var sq1=c1*c1;
656657
var sq2=c2*c2;
657658
var sq3=c3*c3;
658659
var e1=Math.atan2(2*(c0*c1-e*c2*c3),1-(sq1+sq2)*2);
@@ -924,12 +925,12 @@ mat4scale:function(mat,v3,v3y,v3z){
924925
scaleY=v3[1];
925926
scaleZ=v3[2];
926927
}
927-
return [
928+
return [
928929
mat[0]*scaleX, mat[1]*scaleX, mat[2]*scaleX, mat[3]*scaleX,
929930
mat[4]*scaleY, mat[5]*scaleY, mat[6]*scaleY, mat[7]*scaleY,
930931
mat[8]*scaleZ, mat[9]*scaleZ, mat[10]*scaleZ, mat[11]*scaleZ,
931932
mat[12], mat[13], mat[14], mat[15]
932-
];
933+
];
933934
},
934935
/**
935936
* Returns a 4x4 matrix representing a scaling transformation.
@@ -1099,7 +1100,7 @@ mat4translate:function(mat,v3,v3y,v3z){
10991100
* @return {Array<number>} The resulting 4x4 matrix.
11001101
*/
11011102
mat4perspective:function(fovY,aspectRatio,near,far){
1102-
var f = 1/Math.tan(fovY*GLMath.PiDividedBy360);
1103+
var f = 1/Math.tan(((fovY>=0 && fovY<360) ? fovY : ((fovY%360)+(fovY<0 ? 360 : 0)))*GLMath.PiDividedBy360);
11031104
var nmf = near-far;
11041105
nmf=1/nmf;
11051106
return [f/aspectRatio, 0, 0, 0, 0, f, 0, 0, 0, 0,
@@ -1286,13 +1287,13 @@ mat4scaleInPlace:function(mat,v3,v3y,v3z){
12861287
*/
12871288
mat4multiply:function(a,b){
12881289
var dst=[];
1289-
for(var i = 0; i < 16; i+= 4){
1290-
for(var j = 0; j < 4; j++){
1291-
dst[i+j] =
1292-
b[i] * a[j] +
1293-
b[i+1] * a[j+4] +
1294-
b[i+2] * a[j+8] +
1295-
b[i+3] * a[j+12];
1290+
for(var i = 0; i < 16; i+= 4){
1291+
for(var j = 0; j < 4; j++){
1292+
dst[i+j] =
1293+
b[i] * a[j] +
1294+
b[i+1] * a[j+4] +
1295+
b[i+2] * a[j+8] +
1296+
b[i+3] * a[j+12];
12961297
}
12971298
}
12981299
return dst;
@@ -1311,10 +1312,10 @@ mat4multiply:function(a,b){
13111312
* @return {Array<number>} The resulting quaternion.
13121313
*/
13131314
quatMultiply:function(a,b){
1314-
return [
1315-
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
1316-
a[3] * b[1] + a[1] * b[3] + a[2] * b[0] - a[0] * b[2],
1317-
a[3] * b[2] + a[2] * b[3] + a[0] * b[1] - a[1] * b[0],
1315+
return [
1316+
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
1317+
a[3] * b[1] + a[1] * b[3] + a[2] * b[0] - a[0] * b[2],
1318+
a[3] * b[2] + a[2] * b[3] + a[0] * b[1] - a[1] * b[0],
13181319
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]]
13191320
},
13201321
/**
@@ -1344,18 +1345,18 @@ if(typeof vy!="undefined" && typeof vz!="undefined"){
13441345
v0=v;
13451346
v1=vy;
13461347
v2=vz;
1347-
ang=angle*GLMath.PiDividedBy180;
1348+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy180;
13481349
} else if(typeof v=="undefined"){
13491350
v0=angle[0];
13501351
v1=angle[1];
13511352
v2=angle[2];
13521353
ang=angle[3];
1353-
ang=ang*GLMath.PiDividedBy180;
1354+
ang=((ang>=0 && ang<360) ? ang : ((ang%360)+(ang<0 ? 360 : 0)))*GLMath.PiDividedBy180;
13541355
} else {
13551356
v0=v[0];
13561357
v1=v[1];
13571358
v2=v[2];
1358-
ang=angle*GLMath.PiDividedBy180;
1359+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy180;
13591360
}
13601361
var cost = Math.cos(ang);
13611362
var sint = Math.sin(ang);
@@ -1441,18 +1442,18 @@ if(typeof vy!="undefined" && typeof vz!="undefined"){
14411442
v0=v;
14421443
v1=vy;
14431444
v2=vz;
1444-
ang=angle*GLMath.PiDividedBy180;
1445+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy180;
14451446
} else if(typeof v=="undefined"){
14461447
v0=angle[0];
14471448
v1=angle[1];
14481449
v2=angle[2];
14491450
ang=angle[3];
1450-
ang=ang*GLMath.PiDividedBy180;
1451+
ang=((ang>=0 && ang<360) ? ang : ((ang%360)+(ang<0 ? 360 : 0)))*GLMath.PiDividedBy180;
14511452
} else {
14521453
v0=v[0];
14531454
v1=v[1];
14541455
v2=v[2];
1455-
ang=angle*GLMath.PiDividedBy180;
1456+
ang=((angle>=0 && angle<360) ? angle : ((angle%360)+(angle<0 ? 360 : 0)))*GLMath.PiDividedBy180;
14561457
}
14571458
var cost = Math.cos(ang);
14581459
var sint = Math.sin(ang);
@@ -1608,5 +1609,5 @@ GLMath.RollYawPitch = 5;
16081609
GLMath.quatToEuler=GLMath.quatToTaitBryan;
16091610
/** @deprecated Renamed to quatFromTaitBryan. */
16101611
GLMath.quatFromEuler=GLMath.quatFromTaitBryan;
1611-
exports["GLMath"]=GLMath;
1612+
exports["GLMath"]=GLMath;
16121613
}));

0 commit comments

Comments
 (0)