[API Noob] How to calculate a relative angle in CPP/UE4

Yes, you would have to normalize the LineOfSight vector. A Dot product will only return -1 to 1 if both vectors are normalized. All your movement vectors should be normalized anyway, but that one (since you created it) would have to be normalized. I caught that after I posted the reply last night but figured you’d figure it out on your own. :smiley:

If you re-arrange the code just a little bit, it should work



float UBPFuncLib_TDChelper::AngleOnBow(APawn* TargetShip, APawn* PlyrShip)
{
	FVector LineSight = (PlyrShip->GetActorLocation() - TargetShip->GetActorLocation());
	LineSight.Normalize();
 	float BowAngleInRadians= FGenericPlatformMath::Acos(FVector::DotProduct(TargetShip->GetActorForwardVector(), LineSight));
	
	if (FVector::DotProduct(TargetShip->GetActorRightVector(), LineSight) < 0.0f) // We need one more dot product to actually figure out our winding
  	{
		BowAngleInRadians = TWO_PI - BowAngleInRadians;
	}
        
        BowAngleInRadians = FMath::Clamp(BowAngleInRadians - PI, -PI, PI); // Subtract PI from our radians and clamp us in the -PI, PI space (-180, 180)

        return FMath::RadiansToDegrees(BowAngleInRadians);
}


Radians are your friend and the Unit Circle makes stuff like this pretty easy to figure out (once you wrap your head around it). I’m doing all this in my head, but I think that should give you what you want (or at least give you a push in the right direction). :slight_smile: