HI,
I’m attempting to manipulate some controls based on the camera roll (z).
The camera is controlled by device motion and a Quaternion. Anything I do to access this Quaternion, copying it, cloning it etc seems to change how it’s applied to the camera, so I decided to read the data from the camera itself.
At first just getting camera.rotationZ seemed OK, however it soon transpired when the camera is rotated around the Y axis the X and Z flip. So I did some crazy hacks to get around that:
var z = camera.rotationZ;
var x = camera.rotationX;
var x = camera.rotationY;
z = z > 90
? 0 - (z - 180)
: z;
z = z < - 90
? 0 - (z + 180)
: z;
x = x > 90
? x - 180
: x;
x = x < - 90
? x + 180
: x;
Which is kind of working.
Now I’m stuck with what I’m assuming is a Gimble Lock, where if Y is near 90 or -90, X and Z go nuts and trigger my functions.
I assume it would be better to use the camera matrix, however converting that to Eulers seems to have the same affect.
I saw another post where someone created a matrix and applied each rotation in order with Y as the upVector, and this didn’t seem to help with this issue either.
Basically I want to catch if the camera has rolled 45 degrees either way, or is pointing straight down.
Any better way of achieving this?