Getting +180 / -180 degrees between two vectors

Hello everyone. Potentially basic math question from someone who’s not really good at math at all.

I need to calculate the angle between my Player camera’s Forward Vector and a point in the world (for this test, 0,0,0). I’m at this point right now thanks to some google searching and my knowledge of math:

And this is an illustration of what I expect:

125982-screen02.jpg

Hope this makes sense. Thanks for the help!

Edit: To clear up a couple things, this is for a 2D radar UI I have. The “objective arrow” is my UI element that rotates based on the rotation angle. Also, because this is a 2D radar, I’m flattening everything to 0 height, that’s why I’m making vectors with the Z set to 0 (I could also compare the Z values to add an upper or lower value later on to tell the player whether the objective is higher or lower than the player, but don’t need that for now).

Use FindLookAtRotation, plug your camera world location and your target world location then subtract your camera world Z-rot to the FindLookAtRotation result Z-rot.

Example:

Camera location (0,0,0) target location (0,1,0). FindLookAtRotation will output 90 deg Z because the target is directly on your right side. If your camera is looking at Rot(0,0,-90) then it is looking on opposite side which is 90-(-90) = 180 deg.

I think you will be interested in this talk about using dot products WTF Is? Get DOT Product To in Unreal Engine 4 ( UE4 ) - YouTube

Thanks for the link. That helps understand dot a lot, and now understand why that wasn’t enough for my math. Thanks!

That did it! Thank you very much, kurosu! Now to understand how all this works! :slight_smile:

■■■■ yeah! I’ve been looking all day! Thank you!

Thanks a lot, I was Going in circles for 3 hours until I found this, THANK YOU!

Using Cross Product Z Axis will tell you if the angle is after or before (Clock or anti-clockwise)
The figure shows the angle between forward vector of player and the vector of camera.

1 Like

Answer to the original question on getting -180 / +180 deg angle from 2 vectors for anyone still looking. This is a function you can add then just call when needed. Only tested where the angle is in a single plane with the angle around the z axis. May need a tweak to the cross product axis you check >0 if the angle is in a different axis, don’t know.

And a slightly more complex version with a boolean to switch between returning -180 to 180 deg to returning 0 to 360 deg:

Hopefully helps!

1 Like

Thank you so much, after looking for days your solution was exactly what i was looking for! It accounts for direction - and + so it was very useful in for a turn in place without snapping.

Thanks for sharing this.
But this only works around 0,0,0 coordinates.