From 36d1407a8a758269a5fea3bdd848862ceae09480 Mon Sep 17 00:00:00 2001 From: Sixze <16257871+Sixze@users.noreply.github.com> Date: Sat, 24 Aug 2024 15:42:19 +0300 Subject: [PATCH] Use the FMath::UnwindDegrees() function instead of FRotator::NormalizeAxis() as it is faster for small angles --- Source/ALS/Private/AlsAnimationInstance.cpp | 28 +++++++++--------- Source/ALS/Private/AlsCharacter.cpp | 32 ++++++++++----------- Source/ALS/Private/AlsCharacter_Actions.cpp | 12 ++++---- Source/ALS/Public/Utility/AlsRotation.h | 8 +++--- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Source/ALS/Private/AlsAnimationInstance.cpp b/Source/ALS/Private/AlsAnimationInstance.cpp index 8cfec3856..4b5c41107 100644 --- a/Source/ALS/Private/AlsAnimationInstance.cpp +++ b/Source/ALS/Private/AlsAnimationInstance.cpp @@ -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; } @@ -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 + @@ -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; @@ -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; @@ -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) { @@ -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 @@ -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; @@ -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); @@ -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)) }; diff --git a/Source/ALS/Private/AlsCharacter.cpp b/Source/ALS/Private/AlsCharacter.cpp index 00818430d..db2cf37b4 100644 --- a/Source/ALS/Private/AlsCharacter.cpp +++ b/Source/ALS/Private/AlsCharacter.cpp @@ -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 { @@ -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; @@ -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()}; @@ -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); @@ -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) { @@ -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. @@ -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 @@ -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); @@ -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); @@ -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); @@ -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; @@ -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); @@ -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)); } diff --git a/Source/ALS/Private/AlsCharacter_Actions.cpp b/Source/ALS/Private/AlsCharacter_Actions.cpp index 4c72cb1d8..7d30d0aff 100644 --- a/Source/ALS/Private/AlsCharacter_Actions.cpp +++ b/Source/ALS/Private/AlsCharacter_Actions.cpp @@ -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))); } } @@ -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) { @@ -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); @@ -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) @@ -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; @@ -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; diff --git a/Source/ALS/Public/Utility/AlsRotation.h b/Source/ALS/Public/Utility/AlsRotation.h index afb262d25..db16f9c3c 100644 --- a/Source/ALS/Public/Utility/AlsRotation.h +++ b/Source/ALS/Public/Utility/AlsRotation.h @@ -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) @@ -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)