# How to Calculate Caridnal Direction from Unit Vector

I have unit Vector of acceleration in local space of object. Now I want to calculate Cardinal Direction from (North, East, South, West).

So Far I tried to:

FQuat QRot = LocalAcceleration.ToOrientationQuat();
FRotator Rot = FRotator(QRot);
float Angle = Rot.Yaw;
float Angle2 = (FMath::RoundToInt(Angle) + 360) % 360;


(Another version was calulcating atan2).

Then:

if (FMath::IsWithin(IntAngle, 0, 46)
&& FMath::IsWithin(IntAngle, 314, 0))
{
NewDir = EFourCardinalDirection::N;
}
if (FMath::IsWithin(IntAngle, 134, 228))
{
NewDir = EFourCardinalDirection::S;
}
if (FMath::IsWithin(IntAngle, 46, 136))
{
NewDir = EFourCardinalDirection::E;
}
if (FMath::IsWithin(IntAngle, 229, 313))
{
NewDir = EFourCardinalDirection::W;
}


I also tried to calculate eighth directions and then just assign the four based on the previous eight.

Neither way provided stable results. There are some jumps between directions depending on object movement.

• I actually need to calculate it only for 2 dimensions. The intended use is to calculate cardinal direction in which character is moving and the select animation based on the cardinal direction. – Łukasz Baran Feb 25 '18 at 19:12
• This previous question includes some methods for solving this with 16 directions. For 4 or 8 you can simplify a bit further. – DMGregory Feb 25 '18 at 19:31
• Feel free to post an answer then showing how you solved it. The other question isn't an exact duplicate, I don't think. – DMGregory Feb 25 '18 at 21:08

float Atan2Angle = FMath::Atan2(LocalVelocity.Y, LocalVelocity.X);