How to get an angle between 2 Vectors?

Can you put some code, i got confused… sounds easy but … ? … : ) ,

1 Like

None of these helped, the dot product method never worked, so I thought id share my method for anyone like me who was looking:

In: Vector 1 and Vector 2

Out: Angle Between 0 - 360

6 Likes

None of the answers worked for me. One pointed me in the right way, the find quaternion between vectors one.

This is how i did it. Basically i was trying to constraint the horizontal camera input depending on an angle related to the orientation of the character. So he could not look behind or “break” his neck in certain states.

I needed this angle to be > 0 when the vector camera looked too far to the right and < 0 when the camera looked too far to the left. This is where the other answers failed me as Acos returns positive always on both directions because it comes from the dot product.

So i complemented the quaternion solution by checking the Z axis of the vector that FQuat::ToAxisAndAngle returns.

If the angle goes left, axis.Z will be -1.0f and if it goes right axis.Z will be 1.0f. I multiplied axis.Z by my angle and it worked like a charm.

Hopefully this helps somebody.

327770-captura-de-pantalla-2021-01-21-184901.png

Thank you so much for this solution! This works for my use case better then the dot product and seems to be cleaner.

This is the exact solution I am looking for. Thank you very much.

For me I was trying to find the signed angle between two vectors, which meant finding the angle between two vectors that could rotate a full 360 degrees, but be represented as -180 to +180.

This worked for me:

1) Normalize both your vectors. The vector A is “From” and the vector B is “To”. We are getting the angle “From” Vector A “To” vector B. Note that I lowered the tolerance here to the minimum for greater accuracy.

2) Take the dot product of the two normalized vectors and get the arccos of that value. This is our angle

3) Take the cross product of the two normalized vectors. This produces a perpendicular vector to Vector A and Vector B. We need this vector because we need to define the axis on which we test what will be negative rotation and what will be positive. In my example I have used X, because I care about the Roll. But in your case it may be the Pitch or Yaw that you care about. It creates a new axis relative to the A and B which we can use to define Relative Rotation.

4) If the Cross Product’s vector axis ranges from 0 to -1, that is negative rotation, while 0 to +1 means positive rotation. -1 means a full -180 degrees, 0 being 0 rotation on the X axis, and +1 means a full 180 degrees.

340633-vectors.png

3 Likes

For anyone still looking for a clean solution for returning an angle from -180 to +180 deg from 2 vectors using blueprint.

jcdentonunatco’s answer almost worked for me…except I had to check the z axis of the cross product > 0 instead of checking the x axis. My game is a top down shooter where the angle is the ship rotation about the z axis. Given I had to modify jcdentonunatco solution slightly, potentially you might need to adjust which axis of the cross product you check to get it to work for you, don’t know. Here is a full blueprint for the function that works for me, only tested where the angle is in a single plane with the angle around the z axis.

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!

6 Likes

thanks a lot, it does work for me :slight_smile:

Very good. The winning solution.

Thank you it is very useful!

Note: the “solved” answer here will give you an angle in radians, rather than degrees.

FVector Difference = VectorA - VectorB;
float Angle = FMath::RadiansToDegrees(FMath::Atan2(Difference.X, Difference.Y)) + 180.f;

Thank you !!! You’re the best ! <3

Just to save somebodys time function to find the angle between 2 vectors (2D and 3D) | Unreal engine Code Snippet

1 Like

Thank you everyone! got my angle calculation working thanks to this discussion :pray:

Here is the C++ code that might be useful - it basically a conversion of latest solution thats provided here in blueprints

float resultAngleInRadians = 0.0f;
firstVector.Normalize();
secondVector.Normalize();

auto crossProduct = firstVector.Cross(secondVector);
auto dotProduct = firstVector.Dot(secondVector);

if(crossProduct.Z > 0 )
{
    resultAngleInRadians = acosf(dotProduct);
}
else
{
	resultAngleInRadians = -1 * acosf(dotProduct);
}

auto resultAngleInDegrees = FMath::RadiansToDegrees(resultAngleInRadians);
2 Likes

nice one!! I think your snippet has some extra nodes that are not needed…you are getting
the cross vector and evaluating if z > 0 to invert Acos angle and have a positive value when you can just ABS the results :slight_smile:
Anyway your formula was very useful for me because I wanted to get angle in B with A ,B and C points given and here is the results :slight_smile:

Thanks again!

3 Likes

For some reason nothing worked for me. So I added a cube to the actor, set the nodes so that the cube is always looking at the player, and now the RelativeRotation of this cube gives me the angle I need.

2 Likes

Updated the C++ code to be a bit more readable

// Assume you have two vectors, Vector1 and Vector2
FVector Vector1 = FVector(1, 0, 0);
FVector Vector2 = FVector(0, 1, 0);

// Normalize the vectors
Vector1.Normalize();
Vector2.Normalize();

// Calculate the dot product of the two vectors
float DotProduct = FVector::DotProduct(Vector1, Vector2);

// Calculate the angle in radians between the two vectors
float AngleInRadians = FMath::Acos(DotProduct);

// Convert the angle to degrees
float AngleInDegrees = FMath::RadiansToDegrees(AngleInRadians);

// Calculate the cross product of the two vectors
FVector CrossProduct = FVector::CrossProduct(Vector1, Vector2);

// If the Z component of the cross product is less than zero, the second vector is to the left of the first
if (CrossProduct.Z < 0)
{
    AngleInDegrees = -AngleInDegrees;
}
1 Like

It has been mentionned few times in this thread and you can easily (like me) miss them, but first give a shot to FindLookAtRotation function before trying anything else :

I tried the code written above, but the rotation wasn’t clean when leaving flat surface and giving only Yaw. FindLookAtRotation solved all my problems

1 Like

This seems like a helpful function. Could you kindly show how you’ve used it to get the angle between two vectors in blueprint?