diff --git a/src/webgl/p5.Camera.js b/src/webgl/p5.Camera.js index 11a051f32f..c69c44a9ad 100644 --- a/src/webgl/p5.Camera.js +++ b/src/webgl/p5.Camera.js @@ -1618,6 +1618,24 @@ class Camera { rotatedCenter[1] += this.eyeY; rotatedCenter[2] += this.eyeZ; + // Rotate the up vector to keep the correct camera orientation + /* eslint-disable max-len */ + const rotatedUpX = this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8]; + const rotatedUpY = this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9]; + const rotatedUpZ = this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10]; + /* eslint-enable max-len */ + + //Normalize the up vector + const upLength = Math.sqrt( + rotatedUpX * rotatedUpX + + rotatedUpY * rotatedUpY + + rotatedUpZ * rotatedUpZ + ); + + const normalizedUpX = rotatedUpX / upLength; + const normalizedUpY = rotatedUpY / upLength; + const normalizedUpZ = rotatedUpZ / upLength; + this.camera( this.eyeX, this.eyeY, @@ -1625,9 +1643,9 @@ class Camera { rotatedCenter[0], rotatedCenter[1], rotatedCenter[2], - this.upX, - this.upY, - this.upZ + normalizedUpX, + normalizedUpY, + normalizedUpZ ); }