Hi,

I’m looking to get the rotation angle of the camera(universalcamera) to an object back in degrees. So basically if the camera is facing the object the angle would be 0 and if its to the left it would return 90 or 270 etc. I would like to get the value in realtime and then use this value for other thing. I’m at a very basic level so i can’t seem to figure this out. Any help would be appreciated.

# Rotation angle of camera to object

Hey and welcome!

so technically, here is the idea:

- your “0” is the vector (0, 0, 1) in the camera world
- Your “Target” is the vector from your object to your camera: so mesh.position.subtract(camera.position)
- So the angle you are looking for is the angle between “0” and your target
- Thanks to trigonometry, we know that the angle between two vectors (that need to be normalized) is equal to acos(dot(v1, v2))

so in babylon.js terms:

```
let v0 = new BABYLON.Vector3(0, 0, 1);
let v1 = mesh.position.subtract(camera.position);
v1.normalize();
let angle = Math.acos(BABYLON.Vector3.Dot(v0, v1));
let angleInDegree = BABYLON.Tools.ToDegrees(angle)
```

Thank you!!

I tryed it out and i’m getting the angle as i move around the object but not if I rotate my view away from the object. So i’m looking to get the angle the camera is looking to the object. If im looking directly at the object it would be 0 and i rotate the camera 90degress left then if the camera would be say 90 degrees and 180 if im looking the opposite direction. Sorry i’m really new here and that last example helped a ton

Maybe you could repro what you have in the playground? Far easier to discuss with code to repro:)

https://playground.babylonjs.com/#LG0NUQ

Here is an example. So i’m looking to the get cameras axis rotation angle relative to the sphere. So weither it is looking at the object or to what degree away from the object.

Hey Deltakosh,

Thanks so much! This is more in line with what i was thinking. It might be possible to do what i’m thinking with these return values. Although because there is no difference in the values from when I rotate to the left from the centre or to the right from the centre I can’t enough information. I think a 359 degree value return would help out more.I would be able to split the 359 value into 4 sections. 0-90, 90-180,180-270,270-359. This way getting a full rotation value to work with.

Hi again,

So this works for a mono pan but not stereo. Is it possible to get a 360 degree angle value at all and have it based horizontally only. So based only when the camera moves around horizontally and disregard the vertical rotation of the camera or am I out of luck? The most important thing is being able to get different numbers from the rotation to the left and right. Because i have to differentiate the left from the right. Or else i can can’t stereo pan. :L

What if you store the current camera rotation and then compare it with previous value.

This way you will know where you are in your sections?

https://playground.babylonjs.com/#LG0NUQ#4

I modded the example you give me.

the xvalue in the console log gives the correct values that i want for panning but obv does not return correct values when the camera is repositioned in the space by moving around the object. But it’s giving me a full 360 value feedback just to show what i’m talking about.

The sections don’t really matter, I only need them to correct the feedback if its giving large values but numbers between -2 and 2 are good like in this example. Rotating horizontally on the spot.

This doesn’t work as The camera would be moving around the object.

for the sake of it, will that work better:

https://playground.babylonjs.com/#LG0NUQ#5

Using world location

I’m not sure how that works better but thank you. It doesn’t look like i will be able to get this working. The first solution you sent worked much better but it didn’t return different values depending on weither the camera turns left or right. Because we have two ears this information is needed to adjust the volume of the left speaker and the right speaker. If the camera turns90 degrees left then the left speaker(left ear) would turn down because it is now facing away from the sphere but the right speaker(right ear) would be facing it directly and would thus need to remain at its volume level.

But there is no way of doing that if I can’t tell weither the camera has turned to the left away from the sphere or to the right away from the sphere. The first example returns the same angle either from the left or right.

But wait, this is al;ready supported by babylon.js. We do have spatial audio:

https://doc.babylonjs.com/how_to/playing_sounds_and_music#attaching-a-sound-to-a-mesh

Does it work?

yes but i wanted to implement the sound using api from another engine. I’m sure it’s possible to work a simple solution out to the problem but using cos angles is not the way. cos only returns 180 degree angle. I got pretty close just using the position and camera rotation vectors normalised and working off of each other to get the values. But ran into problems which that I don’t have time to solve atm. I’m not particularly great a maths haha. But i’m sure there’s a simple elegent solution to it for some basic stereo panning.

Sorry it took so long to answer but have been busy with other stuff. Hope this works for you.

https://playground.babylonjs.com/#LG0NUQ#6.

If you find that the sign of the angle is wrong for what you need, swap the order of v0 and v1 in line 50

Hi John,

So i took I your example and modified it.

https://playground.babylonjs.com/#LG0NUQ#7

This now shows me if the camera turned left or to the right relative to the sphere. The angle goes negative if i rotate right and positive if i turn left. Which is great because now I can differentiate from rotating left or right. The camera does need to be free as it represents the players view point as they walk around. Is there a way to have the values only apply on the horizonally? So not taking into account weither the camera rotates upwards or downwards?

Thank you for the solution!

Edit:

https://playground.babylonjs.com/#LG0NUQ#10

Returning vectors instead of angles.

https://playground.babylonjs.com/#LG0NUQ#8

I have made the sphere the object you are facing so line 43 sets the base line for your 0 angle.

The cube is your sound source and as I understand it you want the horizontal angle between the object you are facing (the sphere) and the sound source (cube) with you being the origin.

The easiest way to do horizontal angle only is to ignore the height, ie set y component of all vectors to zero.

I you do want to work with the starting position and direction of the camera as 0 rather than an object it is facing then https://playground.babylonjs.com/#LG0NUQ#9

Yes I set that in my edited example so that the angle reflects the soundsource. The soundsource is the sphere and i wanted the angle that the camera is facing the sphere. I needed to differentiate between left and right tho and that was causing the problem. I’m pretty terrible at math so forgive me! I see what you mean now in your first example tho. Thanks! I am entirely new to Babylon js so if you would elaborate on that point of setting the y component of all vectors to zero i would greatly appreciate it.

Yes set y = 0

Blue vector includes upwards or downwards rotation, set Vy = 0 and this is the orange vector which only gives horizontal rotation as you wanted.