# Getting angle between two vectors - how?

I suck at vector math (but trying to refresh it in my mind), sorry :o

I have player (FPS) looking around and I need to get an angle between forward vector and view vector. So if player look straight forward, the angle will be 0 deg. If player looks straight up, it will be 90 deg. regardless which way player is facing in XY plane.

How do I get that angle ?

Thanks

Do you need the exact angle in degrees/radians or does an approximated -1 to 1 value system work? If so, you can do a dotproduct(up, view). It should give you the values 1 when looking up, 0 when forward and -1 for down. I assume the vectors are already normalized.

Otherwise I am pretty certain that if you just drag a line from a vector and type angle you should get some helper functions to get it in radians or degrees.

Normalize both vectors
calculate DOT product. Dot product is cosinus of those vectors.
Calculate arcus cos of that value.

However this does not tell (because it cant) if rotation is to the left or right. It is always angle between vectors, so 0 to 180.

Also from reading your description you do not need angle beetwen 2 vectors, you want to find rotation from vector A to B.
For that “look at” nodes are best, check those.

I only need to be able to find if player is looking at between ~25 degrees and ~70 degrees up to horizon. If that’s the case, I can do something. If not (player looks down to the ground, even if it’s just slightly below horizon; or straight up) player would do nothing.

To make it more clear let me illustrate it (using Blender :o ):

Red is view vector, green is forward vector (I wonder if it’s forward in relation to the world or player’s character?!).

Then its not dot product, you should instead get camera rotation, break it and check pictch value (i think its pitch).

Sorry, I can’t find anything related to “break camera rotation” Why wouldn’t dot product work ?

Call “Get World Rotation” from your Camera component variable, then drag out the purple rotator pin and select “Break Rotator”, which breaks the struct into 3 floats (pitch/yaw/roll).

Thanks, I’ll mess with it.

No, you don’t. Angle is almost always the wrong value in 3D graphics, except when inputting values in art tools.
Instead, you want to calculate the dot product between the normalized forward vector and the normalized up vector.
When looking straight up, the value will be 1.0.
When looking straight forward, the value will be 0.0.
When looking straight down, the value will be -1.0.
If you now need to do something when the user looks “upwards a bit” then you can compare the dot product to some range of values, say between 0.3 and 0.8.
(You should implement sliders to adjust these values, for tuning purposes.)

In general, if you’re going to be doing graphics, physics, or gameplay programming, reading up on the dot product (and the cousins “cross product” and “transformation matrix”) will help you out a lot.

2 Likes

Thanks folks, I finally got it working!

Ima take a shot at making it easier. This is for any two directions you may want to find the angle for

your two vectors, A & B, (gotta be normalized)
take the cross product of them
take the Dot of them too
the asin of the length of the cross product is your angle when the dot is >=0
in degrees, 90-asin + 90, when dot is < 0
you’ll have the right rotation btw if you feed the cross product and the angle value into a “Rotator from Axis Angle” block,
*just pay attention to who is A vs B to start with in the Cross product.

2 Likes

… since you pointlessly resurrected a 5 year old a topic, let’s add that ArcCosone is a thing in 2020. Probably was already 5 years ago, but it wasn’t necessarily a node of the material system or the blueprint system. It is now.
acosd(A dot B)

I had help from google, it kept sending me here while trying to solve “find angle between two vectors”. I am thinking, there wasn’t help enough since 2016, but google thinks its important, maybe it’ll benefit others.

I am working in the blueprint event graph. Maybe that’s important.

I tried “ArcCosone” did you mean ArcCosine?..neither are a node or a thing in my 2021 version, was 2020 important? Also, acosd(A dot B), doesn’t return a value when dot is negative.

In mine, I have to handle edge cases with asin too. Dot’s return value can’t be >1 or <-1. Clamp those or asine won’t return a value. Also if 180 degrees apart, the cross product will be 0 length. might cause problems.

All the best

2 Likes

This post was flagged by the community and is temporarily hidden.

It’s actually unthinkable that someone wanting to code a 3D game doesn’t know basic 3D mathematics.
If you ended here from google, Study the math. Not engine specific things.

Math is math.
It won’t change all that much across time.
AS SUCH, any post past the 3rd is rather unnecessary.
Knowing math, means you can write formulas long form in any engine without resorting to pre-made (kismet) functions.

The first thing you should know is the basics of trigonometry: SOHCAHTOA.
Sine, Cosine, and Tangent.

Then you should absolutely know what the Unit Circle is, and why it’s important:

After that, you have Inverse functions:

Once you know that, The C++ (or even U4 specific) stuff is rather simple:
As of now, Jul 2022, According to the documentation, the engine offers this list of functions:

Acos (Degrees) Returns the inverse cos (arccos) of A (result is in Degrees)

Acos (Radians) Returns the inverse cosine (arccos) of A (result is in Radians)

Asin (Degrees) Returns the inverse sin (arcsin) of A (result is in Degrees)

Asin (Radians) Returns the inverse sine (arcsin) of A (result is in Radians)

Atan (Degrees) Returns the inverse tan (atan) (result is in Degrees)

Atan2 (Degrees) Returns the inverse tan (atan2) of A/B (result is in Degrees)

Atan2 (Radians) Returns the inverse tan (atan2) of A/B (result is in Radians)

Cos (Degrees) Returns the cos of A (expects Degrees)

Get PI Returns the value of PI

Get TAU Returns the value of TAU (= 2 * PI)

Sin (Degrees) Returns the sin of A (expects Degrees)