Skip to content

Commit

Permalink
Use the FMath::UnwindDegrees() function instead of FRotator::Normaliz…
Browse files Browse the repository at this point in the history
…eAxis() as it is faster for small angles
  • Loading branch information
Sixze committed Aug 24, 2024
1 parent 07eb219 commit 36d1407
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
28 changes: 14 additions & 14 deletions Source/ALS/Private/AlsAnimationInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ void UAlsAnimationInstance::RefreshView(const float DeltaTime)
{
if (!LocomotionAction.IsValid())
{
ViewState.YawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - LocomotionState.Rotation.Yaw));
ViewState.PitchAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(ViewState.Rotation.Pitch - LocomotionState.Rotation.Pitch));
ViewState.YawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - LocomotionState.Rotation.Yaw));
ViewState.PitchAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(ViewState.Rotation.Pitch - LocomotionState.Rotation.Pitch));

ViewState.PitchAmount = 0.5f - ViewState.PitchAngle / 180.0f;
}
Expand Down Expand Up @@ -444,20 +444,20 @@ void UAlsAnimationInstance::RefreshSpine(const float SpineBlendAmount, const flo
if (MovementBase.bHasRelativeRotation)
{
// Offset the angle to keep it relative to the movement base.
SpineState.LastActorYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
SpineState.LastActorYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
SpineState.LastActorYawAngle + MovementBase.DeltaRotation.Yaw));
}

// Offset the spine rotation to keep it unchanged in world space to achieve a smoother spine rotation when aiming stops.

auto YawAngleOffset{FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(SpineState.LastActorYawAngle - LocomotionState.Rotation.Yaw))};
auto YawAngleOffset{FMath::UnwindDegrees(UE_REAL_TO_FLOAT(SpineState.LastActorYawAngle - LocomotionState.Rotation.Yaw))};

// Keep the offset within 30 degrees, otherwise the spine rotation may lag too much behind the actor rotation.

static constexpr auto MaxYawAngleOffset{30.0f};
YawAngleOffset = FMath::Clamp(YawAngleOffset, -MaxYawAngleOffset, MaxYawAngleOffset);

SpineState.LastActorYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(YawAngleOffset + LocomotionState.Rotation.Yaw));
SpineState.LastActorYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(YawAngleOffset + LocomotionState.Rotation.Yaw));

SpineState.CurrentYawAngle = UAlsRotation::LerpAngle(0.0f, SpineState.LastYawAngle + YawAngleOffset,
SpineState.SpineAmount * SpineState.SpineAmountScale +
Expand Down Expand Up @@ -495,7 +495,7 @@ void UAlsAnimationInstance::RefreshLook()
if (MovementBase.bHasRelativeRotation)
{
// Offset the angle to keep it relative to the movement base.
LookState.WorldYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(LookState.WorldYawAngle + MovementBase.DeltaRotation.Yaw));
LookState.WorldYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(LookState.WorldYawAngle + MovementBase.DeltaRotation.Yaw));
}

float TargetYawAngle;
Expand All @@ -506,7 +506,7 @@ void UAlsAnimationInstance::RefreshLook()
{
// Look towards input direction.

TargetYawAngle = FRotator3f::NormalizeAxis(
TargetYawAngle = FMath::UnwindDegrees(
(LocomotionState.bHasInput ? LocomotionState.InputYawAngle : LocomotionState.TargetYawAngle) - ActorYawAngle);

TargetPitchAngle = 0.0f;
Expand All @@ -530,8 +530,8 @@ void UAlsAnimationInstance::RefreshLook()
}
else
{
const auto YawAngle{FRotator3f::NormalizeAxis(LookState.WorldYawAngle - ActorYawAngle)};
auto DeltaYawAngle{FRotator3f::NormalizeAxis(TargetYawAngle - YawAngle)};
const auto YawAngle{FMath::UnwindDegrees(LookState.WorldYawAngle - ActorYawAngle)};
auto DeltaYawAngle{FMath::UnwindDegrees(TargetYawAngle - YawAngle)};

if (DeltaYawAngle > 180.0f - UAlsRotation::CounterClockwiseRotationAngleThreshold)
{
Expand All @@ -547,11 +547,11 @@ void UAlsAnimationInstance::RefreshLook()

const auto InterpolationAmount{UAlsMath::ExponentialDecay(GetDeltaSeconds(), InterpolationSpeed)};

LookState.YawAngle = FRotator3f::NormalizeAxis(YawAngle + DeltaYawAngle * InterpolationAmount);
LookState.YawAngle = FMath::UnwindDegrees(YawAngle + DeltaYawAngle * InterpolationAmount);
LookState.PitchAngle = UAlsRotation::LerpAngle(LookState.PitchAngle, TargetPitchAngle, InterpolationAmount);
}

LookState.WorldYawAngle = FRotator3f::NormalizeAxis(ActorYawAngle + LookState.YawAngle);
LookState.WorldYawAngle = FMath::UnwindDegrees(ActorYawAngle + LookState.YawAngle);

// Separate the yaw angle into 3 separate values. These 3 values are used to improve the
// blending of the view when rotating completely around the character. This allows to
Expand Down Expand Up @@ -599,7 +599,7 @@ void UAlsAnimationInstance::RefreshLocomotionOnGameThread()
LocomotionState.RotationQuaternion = Locomotion.Rotation.Quaternion();

LocomotionState.YawSpeed = ActorDeltaTime > UE_SMALL_NUMBER
? FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
? FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
Locomotion.Rotation.Yaw - Locomotion.PreviousYawAngle)) / ActorDeltaTime
: 0.0f;

Expand Down Expand Up @@ -761,7 +761,7 @@ void UAlsAnimationInstance::RefreshGroundedMovement()
GroundedState.HipsDirectionLockAmount = FMath::Clamp(GetCurveValue(UAlsConstants::HipsDirectionLockCurveName()), -1.0f, 1.0f);

const auto ViewRelativeVelocityYawAngle{
FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(LocomotionState.VelocityYawAngle - ViewState.Rotation.Yaw))
FMath::UnwindDegrees(UE_REAL_TO_FLOAT(LocomotionState.VelocityYawAngle - ViewState.Rotation.Yaw))
};

RefreshMovementDirection(ViewRelativeVelocityYawAngle);
Expand Down Expand Up @@ -1331,7 +1331,7 @@ void UAlsAnimationInstance::PlayQuickStopAnimation()
}

auto RemainingYawAngle{
FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
(LocomotionState.bHasInput ? LocomotionState.InputYawAngle : LocomotionState.TargetYawAngle) - LocomotionState.Rotation.Yaw))
};

Expand Down
32 changes: 16 additions & 16 deletions Source/ALS/Private/AlsCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void AAlsCharacter::NotifyLocomotionModeChanged(const FGameplayTag& PreviousLoco

StartRolling(PlayRate, LocomotionState.bHasVelocity
? LocomotionState.VelocityYawAngle
: UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(GetActorRotation().Yaw)));
: UE_REAL_TO_FLOAT(FMath::UnwindDegrees(GetActorRotation().Yaw)));
}
else
{
Expand Down Expand Up @@ -1030,7 +1030,7 @@ bool AAlsCharacter::CanSprint() const

static constexpr auto ViewRelativeAngleThreshold{50.0f};

if (FMath::Abs(FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
if (FMath::Abs(FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
LocomotionState.InputYawAngle - ViewState.Rotation.Yaw))) < ViewRelativeAngleThreshold)
{
return true;
Expand Down Expand Up @@ -1377,13 +1377,13 @@ void AAlsCharacter::RefreshLocomotionEarly()
{
// Offset the rotations (the actor's rotation too) to keep them relative to the movement base.

LocomotionState.TargetYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
LocomotionState.TargetYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
LocomotionState.TargetYawAngle + MovementBase.DeltaRotation.Yaw));

LocomotionState.ViewRelativeTargetYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
LocomotionState.ViewRelativeTargetYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
LocomotionState.ViewRelativeTargetYawAngle + MovementBase.DeltaRotation.Yaw));

LocomotionState.SmoothTargetYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
LocomotionState.SmoothTargetYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
LocomotionState.SmoothTargetYawAngle + MovementBase.DeltaRotation.Yaw));

auto NewRotation{GetActorRotation()};
Expand Down Expand Up @@ -1692,7 +1692,7 @@ void AAlsCharacter::RefreshGroundedAimingRotation(const float DeltaTime)

SetTargetYawAngleSmooth(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw), DeltaTime, TargetYawAngleRotationSpeed);

NewActorRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(NewActorRotation.Yaw)),
NewActorRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FMath::UnwindDegrees(NewActorRotation.Yaw)),
LocomotionState.SmoothTargetYawAngle,
DeltaTime, RotationInterpolationSpeed);

Expand Down Expand Up @@ -1720,7 +1720,7 @@ bool AAlsCharacter::ConstrainAimingRotation(FRotator& ActorRotation, const float
LocomotionState.AimingYawAngleLimit = 180.0f;
}

auto ViewRelativeAngle{FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - ActorRotation.Yaw))};
auto ViewRelativeAngle{FMath::UnwindDegrees(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - ActorRotation.Yaw))};

if (FMath::Abs(ViewRelativeAngle) <= AlsCharacterConstants::MinAimingYawAngleLimit + UE_KINDA_SMALL_NUMBER)
{
Expand All @@ -1742,10 +1742,10 @@ bool AAlsCharacter::ConstrainAimingRotation(FRotator& ActorRotation, const float
FMath::Clamp(ViewRelativeAngle, -AlsCharacterConstants::MinAimingYawAngleLimit, AlsCharacterConstants::MinAimingYawAngleLimit)
};

const auto DeltaAngle{FRotator3f::NormalizeAxis(TargetViewRelativeAngle - ViewRelativeAngle)};
const auto DeltaAngle{FMath::UnwindDegrees(TargetViewRelativeAngle - ViewRelativeAngle)};
const auto InterpolationAmount{UAlsMath::ExponentialDecay(DeltaTime, RotationInterpolationSpeed)};

ViewRelativeAngle = FRotator3f::NormalizeAxis(ViewRelativeAngle + DeltaAngle * InterpolationAmount);
ViewRelativeAngle = FMath::UnwindDegrees(ViewRelativeAngle + DeltaAngle * InterpolationAmount);
}

// Primary constraint. Prevents the actor from rotating beyond a certain angle relative to the camera.
Expand All @@ -1761,7 +1761,7 @@ bool AAlsCharacter::ConstrainAimingRotation(FRotator& ActorRotation, const float

const auto PreviousActorYawAngle{ActorRotation.Yaw};

ActorRotation.Yaw = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - ViewRelativeAngle));
ActorRotation.Yaw = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw - ViewRelativeAngle));

// We use UE_KINDA_SMALL_NUMBER here because even if ViewRelativeAngle hasn't
// changed, converting it back to ActorRotation.Yaw may introduce a rounding
Expand Down Expand Up @@ -1869,7 +1869,7 @@ void AAlsCharacter::RefreshInAirAimingRotation(const float DeltaTime)
SetTargetYawAngle(UE_REAL_TO_FLOAT(ViewState.Rotation.Yaw));

auto NewRotation{GetActorRotation()};
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(NewRotation.Yaw)),
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FMath::UnwindDegrees(NewRotation.Yaw)),
LocomotionState.SmoothTargetYawAngle, DeltaTime, RotationInterpolationSpeed);

ConstrainAimingRotation(NewRotation, DeltaTime);
Expand All @@ -1884,7 +1884,7 @@ void AAlsCharacter::SetRotationSmooth(const float TargetYawAngle, const float De
SetTargetYawAngle(TargetYawAngle);

auto NewRotation{GetActorRotation()};
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(NewRotation.Yaw)),
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FMath::UnwindDegrees(NewRotation.Yaw)),
LocomotionState.SmoothTargetYawAngle, DeltaTime, InterpolationSpeed);

SetActorRotation(NewRotation);
Expand All @@ -1898,7 +1898,7 @@ void AAlsCharacter::SetRotationExtraSmooth(const float TargetYawAngle, const flo
SetTargetYawAngleSmooth(TargetYawAngle, DeltaTime, TargetYawAngleRotationSpeed);

auto NewRotation{GetActorRotation()};
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(NewRotation.Yaw)),
NewRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FMath::UnwindDegrees(NewRotation.Yaw)),
LocomotionState.SmoothTargetYawAngle, DeltaTime, InterpolationSpeed);

SetActorRotation(NewRotation);
Expand All @@ -1925,7 +1925,7 @@ void AAlsCharacter::RefreshTargetYawAngleUsingLocomotionRotation()

void AAlsCharacter::SetTargetYawAngle(const float TargetYawAngle)
{
LocomotionState.TargetYawAngle = FRotator3f::NormalizeAxis(TargetYawAngle);
LocomotionState.TargetYawAngle = FMath::UnwindDegrees(TargetYawAngle);

LocomotionState.SmoothTargetYawAngle = LocomotionState.TargetYawAngle;

Expand All @@ -1934,7 +1934,7 @@ void AAlsCharacter::SetTargetYawAngle(const float TargetYawAngle)

void AAlsCharacter::SetTargetYawAngleSmooth(const float TargetYawAngle, const float DeltaTime, const float RotationSpeed)
{
LocomotionState.TargetYawAngle = FRotator3f::NormalizeAxis(TargetYawAngle);
LocomotionState.TargetYawAngle = FMath::UnwindDegrees(TargetYawAngle);

LocomotionState.SmoothTargetYawAngle = UAlsRotation::InterpolateAngleConstant(
LocomotionState.SmoothTargetYawAngle, LocomotionState.TargetYawAngle, DeltaTime, RotationSpeed);
Expand All @@ -1944,6 +1944,6 @@ void AAlsCharacter::SetTargetYawAngleSmooth(const float TargetYawAngle, const fl

void AAlsCharacter::RefreshViewRelativeTargetYawAngle()
{
LocomotionState.ViewRelativeTargetYawAngle = FRotator3f::NormalizeAxis(UE_REAL_TO_FLOAT(
LocomotionState.ViewRelativeTargetYawAngle = FMath::UnwindDegrees(UE_REAL_TO_FLOAT(
ViewState.Rotation.Yaw - LocomotionState.TargetYawAngle));
}
12 changes: 6 additions & 6 deletions Source/ALS/Private/AlsCharacter_Actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void AAlsCharacter::StartRolling(const float PlayRate)
{
StartRolling(PlayRate, Settings->Rolling.bRotateToInputOnStart && LocomotionState.bHasInput
? LocomotionState.InputYawAngle
: UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(GetActorRotation().Yaw)));
: UE_REAL_TO_FLOAT(FMath::UnwindDegrees(GetActorRotation().Yaw)));
}
}

Expand All @@ -50,7 +50,7 @@ void AAlsCharacter::StartRolling(const float PlayRate, const float TargetYawAngl
return;
}

const auto InitialYawAngle{UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(GetActorRotation().Yaw))};
const auto InitialYawAngle{UE_REAL_TO_FLOAT(FMath::UnwindDegrees(GetActorRotation().Yaw))};

if (GetLocalRole() >= ROLE_Authority)
{
Expand Down Expand Up @@ -129,7 +129,7 @@ void AAlsCharacter::RefreshRollingPhysics(const float DeltaTime)
}
else
{
TargetRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(TargetRotation.Yaw)),
TargetRotation.Yaw = UAlsRotation::ExponentialDecayAngle(UE_REAL_TO_FLOAT(FMath::UnwindDegrees(TargetRotation.Yaw)),
RollingState.TargetYawAngle, DeltaTime,
Settings->Rolling.RotationInterpolationSpeed);

Expand Down Expand Up @@ -162,7 +162,7 @@ bool AAlsCharacter::StartMantling(const FAlsMantlingTraceSettings& TraceSettings
}

const auto ActorLocation{GetActorLocation()};
const auto ActorYawAngle{UE_REAL_TO_FLOAT(FRotator::NormalizeAxis(GetActorRotation().Yaw))};
const auto ActorYawAngle{UE_REAL_TO_FLOAT(FMath::UnwindDegrees(GetActorRotation().Yaw))};

float ForwardTraceAngle;
if (LocomotionState.bHasVelocity)
Expand All @@ -178,7 +178,7 @@ bool AAlsCharacter::StartMantling(const FAlsMantlingTraceSettings& TraceSettings
ForwardTraceAngle = LocomotionState.bHasInput ? LocomotionState.InputYawAngle : ActorYawAngle;
}

const auto ForwardTraceDeltaAngle{FRotator3f::NormalizeAxis(ForwardTraceAngle - ActorYawAngle)};
const auto ForwardTraceDeltaAngle{FMath::UnwindDegrees(ForwardTraceAngle - ActorYawAngle)};
if (FMath::Abs(ForwardTraceDeltaAngle) > Settings->Mantling.TraceAngleThreshold)
{
return false;
Expand Down Expand Up @@ -1050,7 +1050,7 @@ void AAlsCharacter::StopRagdollingImplementation()

// Determine whether the ragdoll is facing upward or downward and set the actor rotation accordingly.

const auto bRagdollFacingUpward{FRotator::NormalizeAxis(PelvisRotation.Roll) <= 0.0f};
const auto bRagdollFacingUpward{FMath::UnwindDegrees(PelvisRotation.Roll) <= 0.0f};

auto NewActorRotation{GetActorRotation()};
NewActorRotation.Yaw = bRagdollFacingUpward ? PelvisRotation.Yaw - 180.0f : PelvisRotation.Yaw;
Expand Down
8 changes: 4 additions & 4 deletions Source/ALS/Public/Utility/AlsRotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ inline float UAlsRotation::RemapAngleForCounterClockwiseRotation(const float Ang

inline float UAlsRotation::LerpAngle(const float From, const float To, const float Ratio)
{
auto Delta{FRotator3f::NormalizeAxis(To - From)};
auto Delta{FMath::UnwindDegrees(To - From)};
Delta = RemapAngleForCounterClockwiseRotation(Delta);

return FRotator3f::NormalizeAxis(From + Delta * Ratio);
return FMath::UnwindDegrees(From + Delta * Ratio);
}

inline FRotator UAlsRotation::LerpRotation(const FRotator& From, const FRotator& To, const float Ratio)
Expand All @@ -98,12 +98,12 @@ inline float UAlsRotation::InterpolateAngleConstant(const float Current, const f
return Target;
}

auto Delta{FRotator3f::NormalizeAxis(Target - Current)};
auto Delta{FMath::UnwindDegrees(Target - Current)};
Delta = RemapAngleForCounterClockwiseRotation(Delta);

const auto MaxDelta{Speed * DeltaTime};

return FRotator3f::NormalizeAxis(Current + FMath::Clamp(Delta, -MaxDelta, MaxDelta));
return FMath::UnwindDegrees(Current + FMath::Clamp(Delta, -MaxDelta, MaxDelta));
}

inline float UAlsRotation::DampAngle(const float Current, const float Target, const float DeltaTime, const float Smoothing)
Expand Down

0 comments on commit 36d1407

Please sign in to comment.