# Simple(?) camera alignment (ArcRotateCamera)

Hi There,

I’m trying to get a camera to rotate and align with an object.
In this example I’m just trying to accomplish this with a simple box.
First I had it rotated randomly, but because I couldn’t get it aligned I’m now trying with a 45° rotated box, which also doesn’t work.

I filtered out what I needed for this on a PG

If you check the logs you’ll see the rotation (is this the local one?) and the euler converted angles.
But I can’t seem to find the original 45° angle anywhere.

Thanks a lot.
Pieter

1 Like

By “align”, do you mean the camera should be looking at the “front” of the box always?

1 Like

Well it’s intended for future geometry that will be loaded dynamically, so that when clicking on it, we can align the camera under a certain angle (to be determined) to the object, but each time in the same way.

1 Like

So I’m having a bit of trouble understanding what’s going on here. What I’m seeing in the code is that, in the CamSetupC function, you’re creating a list of ArcRotateCameras, each with different alpha, beta, and radius values but it looks like the list of cameras being creating isn’t being stored or used anywhere else (Line 58).

For the scene, it’s going to use the first created camera as its active camera. From what I’m seeing, the first camera’s alpha (horizontal) is set for 20 degrees and its beta (vertical) is set for 45 degrees. Is this correct?

Hi there, indeed you’re correct about everything.
The code is part of a bigger picture which would make the example to complex here, that’s why I created this subsection about it.

My main issue here is that if I have a box rotated under 45°, and I try to retreive that rotation angle by reading the obj properties of the box, I can’t seem to get that angle anywhere.

So I would like to be able to:

1. Read the object properties (getMeshByName…)
2. Retreive the rotation (around the vertical axis in this case)
3. Using that value to rotate the camera (alpha) (in an animation transition)

Hope you can help me with that.
Thanks
Pieter

1 Like

The rotate function is setting the rotationQuaternion and once this is set, the rotation property is now ignored by Babylon. So to retrieve the rotation value of 45 degrees, you can use rotationQuaternion.toEulerAngles() to get the rotation about each axis in radians, then call Tools.ToDegrees to convert the y component to degrees, to arrive back at the 45 degree value.

Here I log both the vector of radian values and the y value converted to degrees (45) and then I use @labris’s spinTo function to animate the camera to face the mesh by animating its alpha value to 45 degrees, the same as the mesh.

``````// _x: -0
// _y: 0.7853981633974484
// _z: 0
Log(scene.getMeshById("DynBox0").rotationQuaternion.toEulerAngles());

// 45.00000000000001
Log(BABYLON.Tools.ToDegrees(scene.getMeshById("DynBox0").rotationQuaternion.toEulerAngles().y));

scene.activeCamera.spinTo("alpha", scene.getMeshById("DynBox0").rotationQuaternion.toEulerAngles().y, 50);
``````

https://playground.babylonjs.com/#EJB1M7#3

3 Likes

Yes yes, thanks for the help.
I forgot to also apply toEulerAngles after the rotationQuaternion, before ToDegrees hehe.

Thanks again!
Pieter

1 Like