Issue with Dot Product

Hey, I’m trying to get the angle between the AI’s forward vector and the player location.
Came across some posts about Dot Products. I’m not sure what is wrong

(The values are in the top left corner of the screenshots)
0 is both directly in front of and behind the AI
150 and -150 is 90 degrees from the AI
180 and -180 back to 0 occurs in around a 45 degree window behind the AI

The other side is positive so the cross product seems to be working, idk what i did wrong with the dot product.
Any help is greatly appreciated :slight_smile:





Hello! Your calculation seems to be working fine, the Dot product is an operation that returns a value in radians instead of degrees.
3.14 radians are equal to 180 degrees. Half of that, 1.57 radians equal 90 degrees, which is similar to the 1.54 you’re getting in your second screenshot.

To transform a value from radian to degrees you can use FMath::RadiansToDegrees(rad);

Depending on what you’re trying to implement, doing it this way may be enough, but you will be unable to determine if the angle is clockwise or counterclockwise because it returns the same value for both cases. If you need to determine the angle direction, you’'ll have to use Atan2

2 Likes

Great thank you, I need the negative rotation to determine the direction for certain animations.
I’m not familiar with Atan2, how can i keep it relative to the AI forward vector/rotation?

dot products work on vectors where each element is a scalar. It does not work on angles at all. If it’s 1, it means they are both identical. If it’s 0, they are at 90 degrees from each other. If it’s -1, they are 180 degrees away from each other.

This is why you’re using Acos.

cos of an angle of 90 degrees is 0.
cos of an angle of 0 degrees is 1.
cos of an angle of 180 degrees is -1.

Note how this is exactly similar to what you have with the dot product.

So doing Acos will find the angle between the vectors using a dot product for this reason.

Note that your DotProd variable is not the dot product, it is the angle of the dot product. It is in radians.

The value you’re displaying is an angle multiplied by the Z of the cross product. I have no idea what you’re trying to do here. Could you explain what you’re trying to do? My guess right now is that FMath::Acos is not needed.

What you have right now is a vector from AI forward. And a vector from AI to the player. You take a cross product meaning the vector will point straight up. You then scale the Z axis of this straight up vector by the dot product. Assuming you’re not using Acos, it would be zero when the AI is facing the player.

Later you say you need the negative rotation to determine the direction for certain animations. If you want the angle, just use UKismetMathLibrary::FindLookAtRotation(AILocation, PlayerLocation).Yaw and subtract AI->GetActorRotation().Yaw

No need for dot products or any of that.

1 Like

Yes thank you :slight_smile: I tried FindLookAtRotation() earlier but forgot to subtract the AI Rotation from it and just over complicated it.
I have the same thing set up to play different animations based on characters rotations to each other, just blanked on it yesterday
Thanks again

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.