After performing the challenge, I have both player and AI tanks pointing turret and barrel to their aiming position.
However, while changing the yaw from +0.XXX to -0.XXX the barrel is not performing the “shortest” movement, it turns all the way through the yaw axis.
I am quite sure it has to do with the fact the rotations are working from -180 to +180. My next shot is to add 180 to the calculations and subtract by 180 right after…
Solved with a very patchy workaround
The DeltaRotation after calculating the difference between the current turret normal and the aiming position (cursor) jumps from 0 to +/-360.F in the crossing of +0.XXX to -0.XXX (and other way round).
So I’ve added a condition that in case there is a jump greater than 180.F like this in the DeltaRotation.Yaw, I am inverting the signal of Yaw.
Blockquote
void
UTankAimingComponent::MoveBarrelTowards(FVector AimDirection)
{
// find the difference between the current barrel rotation and the aim direction
FRotator BarrelRotation = TankBarrelComponent->GetForwardVector().Rotation();
FRotator AimAsRotator = AimDirection.Rotation();
FRotator DeltaRotator = AimAsRotator - BarrelRotation;
TankBarrelComponent->Elevate(DeltaRotator.Pitch);
if (FMath::Abs(DeltaRotator.Yaw) > 180.F)
{
DeltaRotator.Yaw *= -1.F;
}
TankTurretComponent->TurnTurret(DeltaRotator.Yaw);
// move the barrel towards to aim direction
// the movement shall be based on elevation speed and frame rate
}
This also solves the issue in case the player moves the cursor too fast - then it will always find the shortest rotation to the target.
That helped, thanks.