0
votes

Even after hours of googling, I can't really get my head around this. Maybe somebody here can help me a bit with this.

I basically want to determine the Y-rotation of an object (that is always in the viewport's center), relative to my camera. Imagine the object standing on the center of a record player/turntable, that slowly rotates around its Y axis, and my camera always facing the center of that object while using OrbitControls to change the cam's position around the object. Imagine the camera not moving, but the turntable turning, one revolution equals this Y rotation to be between and 360°.

For example, this Y rotation would be:

  • when cam's position is [x=0, y=0, z=100], or [x=0, y=100, z=200] (the cam's y position doesn't matter, it always looks down/up to the group's center),

  • 45° when cam's position is [x=100, y=0, z=100] or [x=100, y=200, z=100],

  • 90° when cam's position is [x=100, y=0, z=0] or [x=200, y=100, z=0], etc.

Thing is, both of these can have some pretty random positions & rotations in the world coordinate system, so it's not given that the object's position is [x=0, y=0, z=0].

Any ideas? Thanks a lot!

1

1 Answers

0
votes

I'm not sure if I'm being helpful, but perhaps Object3D.getWorldQuaternion and Quaternion.angleTo() might help?

something like :

const cameraQuaternion = new THREE.Quaternion();
camera.getWorldQuaternion(cameraQuaternion);

const targetQuaternion = new THREE.Quaternion();
target.getWorldQuaternion(targetQuaternion);

const delta = cameraQuaternion.angleTo(targetQuaternion);
const euler = new THREE.Euler().setFromQuaternion(delta);

console.log(euler.y / Math.PI * 180);