I am struggling with some quaternion math. I am trying to limit the camera’s rotation to be within a variable angle offset. When the script is enabled, I want to take the camera’s current rotation, and prevent it from rotating too far up, down, left and right.
I created a public test project: https://playcanvas.com/project/703297/overview/camera-rotation-clamp
The code is functional, but the math is wrong. It’s set up with 2 different camera clamps. The 2nd one is supposed to allow +/- 45 degress of x-rotation (vertical). But, clearly it allows for much more than that.
Here’s the relevant code from the postUpdate function:
var curRot = this.cameraEntity.getRotation();
rotationDifference(this.initialRotation,curRot,CameraClampRotation.uQ);
var uE = CameraClampRotation.uQ.getEulerAngles();
if( Abs(uE.x) > this.eulerX || Abs(uE.y) > this.eulerY ) {
// Do not allow this rotation. Use the value from the previous frame.
this.cameraEntity.setRotation(this.validRotation);
} else {
// Store this rotation as valid.
this.validRotation.copy(curRot);
}
Can anyone assist with the quats and converting that into Eulers, here?
Oh, and here is that ‘rotationDifference’ function:
function rotationDifference(q1, q2, retQ) {
retQ.copy(q2).invert();
retQ.mul2(q1,retQ);
}