Skip to content

Commit 6cc674d

Browse files
committed
Fixed issues with rotate() in 3D
Adjusted PMatrix3D.prototype.rotate(angle, v0, v1, v2): - added normalization for cases when <v0, v1, v2> was not a unit vector - zero vector now does not cause rotation - fixed an incorrect argument check - changed unit tests (!) which reflected different assumptions than standard Processing (weren't normalizing and were accepting zero vector). Current tests now match output for Java mode. Added above rotation functionality to Drawing3D.prototype.rotate (fixes processing-js#89).
1 parent 7c84583 commit 6cc674d

File tree

4 files changed

+323
-294
lines changed

4 files changed

+323
-294
lines changed

processing.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11319,10 +11319,19 @@ module.exports = function setupParser(Processing, options) {
1131911319
* @param {float} angle the angle of rotation in radiants
1132011320
*/
1132111321
rotate: function(angle, v0, v1, v2) {
11322-
if (!v1) {
11322+
if (arguments.length < 4) {
1132311323
this.rotateZ(angle);
1132411324
} else {
11325-
// TODO should make sure this vector is normalized
11325+
var v = new PVector(v0, v1, v2);
11326+
var m = v.mag();
11327+
if (m === 0) {
11328+
return;
11329+
} else if (m != 1) {
11330+
v.normalize();
11331+
v0 = v.x;
11332+
v1 = v.y;
11333+
v2 = v.z;
11334+
}
1132611335
var c = p.cos(angle);
1132711336
var s = p.sin(angle);
1132811337
var t = 1.0 - c;
@@ -13136,7 +13145,12 @@ module.exports = function setupParser(Processing, options) {
1313613145
};
1313713146

1313813147
Drawing3D.prototype.rotate = function(angleInRadians) {
13139-
p.rotateZ(angleInRadians);
13148+
if (arguments.length < 4) {
13149+
p.rotateZ(angleInRadians);
13150+
} else {
13151+
modelView.rotate(angleInRadians, arguments[1], arguments[2], arguments[3]);
13152+
modelViewInv.rotate((-angleInRadians), arguments[1], arguments[2], arguments[3]);
13153+
}
1314013154
};
1314113155

1314213156
/**

0 commit comments

Comments
 (0)