Download

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” :confused:

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.

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.

… 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