OrbitControls - Converting camera.rotation values to Euler values

In OrbitControls, it appears that the camera.rotation values are in Quaternions (even though I cannot read a camera.rotation.w value).

I would like to convert those values to Euler values (where x = Lat and y = Lon) and save them to a Vec3 variable or to a mesh (mesh.rotation), where I can extract them. I have been searching for an answer for several days and everything I have tried have either generated errors or more Quaternion values.

I first tried creating a mesh and using these instructions:

let quaternion = new THREE.Quaternion();
camera.getWorldQuaternion(quaternion); // what does this even do?
Mesh.setRotationFromQuaternion(quaternion);

But that just saved the Quaternion values to the Mesh.

I have tried saving the values to a Vec3 variable using Euler function (which has a .setFromQuaternion subfunction). I have tried all kinds of combinations of instructions, but nothing seems to work.

Help!

Hm. I think the camera rotation contains Euler angles. It is possible to transfer camera’s rotation to a mesh rotation, so that you can rotate an object interactively. The following code uses two approaches: (1) with Euler angles and opposite-angle-rotation; and (2) with Quaternions and same-angle-rotation – just comment/uncomment the lines 57 and 60:

https://codepen.io/boytchev/pen/abgBwqW

As for the pure angles, note that Euler angles may have confusing (non-intuitive) values. This is their nature. To get the horizontal and vertical angles of a camera, it is better to use its getPolarAngle() and getAzimuthalAngle().

2 Likes

Perhaps I need help converting them to Lat/Lon values?

For example, I start pointing the camera in the direction north (lat = 0/lon = 0). Here, the camera xyz values are 0.0.0. And if I move vertically up and down, only the x value changes - which indicates that it represents the latitude. And, if I rotate horizontally, the y value changes - which indicates that it represents the longitude. But as I rotate further, both the x and z numbers also start to change. By the time I am l looking south, they are both +/- 180 degrees. But they should not be changing.

I seem to recall having a problem with the LookAt function. It would add a bank value where there should have been none.

So, if those are Euler values, perhaps I need something to convert them to equivalent lat/lon values where z = 0?

Euler angle are not intuitive. When you change one angle, some of the others may change too. When you write one value and read it back, it might be another value. It depends on the order of rotations and which angle is changed.

To get the angles try getPolarAngle() and getAzimuthalAngle().

And some additional info: after creating the camera, run:

camera.rotation.reorder( 'YXZ');

This may make the camera more convenient to you. Still the Z value might be non-zero, but most likely this is due to numerical precisions (e.g. 1E-20 is practically 0).

1 Like

Yes, when creating my own camera rotator (or almost any other mesh or object), I use YXZ.

It looks like getPolarAngle() and getAzimuthalAngle() are exactly what I needed!

I was so focused on trying to get a “global answer” that I didn’t remember that OrbitControls had those functions.

Thanks!

1 Like