From 8ac4271602e09274e9b232ff00290463a54c108f Mon Sep 17 00:00:00 2001 From: Kevin Poretti Date: Sat, 7 Jan 2023 23:16:39 -0500 Subject: [PATCH] Fix rotations so character faces their previous direction when switching grav direction --- .../Content/Characters/BP_GSCharacter.uasset | 4 +-- .../Character/GSCharacter.cpp | 15 ++++++++ .../GSCharacterMovementComponent.cpp | 35 +++++++++++-------- .../Character/GSCharacterMovementComponent.h | 12 +++++-- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/GravityStomp/Content/Characters/BP_GSCharacter.uasset b/GravityStomp/Content/Characters/BP_GSCharacter.uasset index a86bd25..754dc7a 100644 --- a/GravityStomp/Content/Characters/BP_GSCharacter.uasset +++ b/GravityStomp/Content/Characters/BP_GSCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:562effed96955c4332357dadf266f7924fd822695c5b3214ceb26e6b1ae75772 -size 91237 +oid sha256:72e794b6e5cdf8f2b5df8bb0d49686028b607a37e48b99197235ecf42c4d90a8 +size 35522 diff --git a/GravityStomp/Source/GravityStompGame/Character/GSCharacter.cpp b/GravityStomp/Source/GravityStompGame/Character/GSCharacter.cpp index 910cae0..3b57f64 100644 --- a/GravityStomp/Source/GravityStompGame/Character/GSCharacter.cpp +++ b/GravityStomp/Source/GravityStompGame/Character/GSCharacter.cpp @@ -103,5 +103,20 @@ void AGSCharacter::ChangeGravityDirection(const FInputActionValue& Value) FVector2D GravityDirection = Value.Get(); FVector NewCharacterUpDirection(0.0f, -GravityDirection.X, -GravityDirection.Y); + FRotator CharRot = GetActorRotation(); + if(GetCharacterMovement()->IsCharacterUpAlignedToWorldUp()) + { + CharRot = FRotator(0.0f, CharRot.Yaw, 0.0f); + } + else + { + CharRot = FRotator(CharRot.Pitch, 0.0f, 0.0f); + } + + float NewRoll = GetCharacterMovement()->GetRollFromCharacterUpDir(NewCharacterUpDirection); + FRotator RollRot = UKismetMathLibrary::RotatorFromAxisAndAngle(FVector::ForwardVector, NewRoll); + + SetActorRotation(UKismetMathLibrary::ComposeRotators(RollRot, CharRot)); + GetCharacterMovement()->SetCharacterUpDirection(NewCharacterUpDirection); } diff --git a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp index 8b0834e..b9d4ae2 100644 --- a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp +++ b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.cpp @@ -658,6 +658,25 @@ bool UGSCharacterMovementComponent::IsCharacterUpAlignedToWorldUp() const return FMath::Abs(CharacterUpDirection.Dot(FVector::UpVector)) > UE_KINDA_SMALL_NUMBER; } +float UGSCharacterMovementComponent::GetRollFromCharacterUpDir(const FVector& UpDir) const +{ + float Rot = 0.0f; + if(CharacterUpDirection.Dot(FVector::DownVector) > UE_KINDA_SMALL_NUMBER) + { + Rot = 180.0f; + } + else if (CharacterUpDirection.Dot(FVector::RightVector) > UE_KINDA_SMALL_NUMBER) + { + Rot = 90.0f; + } + else if (CharacterUpDirection.Dot(FVector::LeftVector) > UE_KINDA_SMALL_NUMBER) + { + Rot = 270.0f; + } + + return Rot; +} + void UGSCharacterMovementComponent::SetCharacterUpDirection(FVector NewUpDirection) { @@ -842,21 +861,7 @@ void UGSCharacterMovementComponent::PhysicsRotation(float DeltaTime) DesiredRotation.Pitch = IsCharacterUpAlignedToWorldUp() ? 0.f : FRotator::NormalizeAxis(DesiredRotation.Pitch); DesiredRotation.Yaw = IsCharacterUpAlignedToWorldUp() ? FRotator::NormalizeAxis(DesiredRotation.Yaw) : 0.f; - - float Rot = 0.0f; - if(CharacterUpDirection.Dot(FVector::DownVector) > UE_KINDA_SMALL_NUMBER) - { - Rot = 180.0f; - } - else if (CharacterUpDirection.Dot(FVector::RightVector) > UE_KINDA_SMALL_NUMBER) - { - Rot = 90.0f; - } - else if (CharacterUpDirection.Dot(FVector::LeftVector) > UE_KINDA_SMALL_NUMBER) - { - Rot = 270.0f; - } - DesiredRotation.Roll = Rot; + DesiredRotation.Roll = GetRollFromCharacterUpDir(CharacterUpDirection); DebugDesiredRotation = FString::Printf(TEXT("Desired Rotation after keeping it vertical: %s"), *DesiredRotation.ToString()); GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Cyan, DebugDesiredRotation); diff --git a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h index 8adf187..89b910c 100644 --- a/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h +++ b/GravityStomp/Source/GravityStompGame/Character/GSCharacterMovementComponent.h @@ -19,9 +19,18 @@ class GRAVITYSTOMPGAME_API UGSCharacterMovementComponent : public UCharacterMove public: UGSCharacterMovementComponent(); + UFUNCTION(BlueprintPure) + FORCEINLINE FVector GetCharacterUpDirection() const { return CharacterUpDirection; } + UFUNCTION(BlueprintCallable) void SetCharacterUpDirection(FVector NewUpDirection); + UFUNCTION(BlueprintPure) + float GetRollFromCharacterUpDir(const FVector& UpDir) const; + + UFUNCTION(BlueprintPure) + FORCEINLINE bool IsCharacterUpAlignedToWorldUp() const; + // UCharacterMovementComponent interface virtual FVector ConstrainInputAcceleration(const FVector& InputAcceleration) const override; @@ -59,7 +68,4 @@ public: private: FVector CharacterUpDirection = FVector::UpVector; - - UFUNCTION(BlueprintPure) - FORCEINLINE bool IsCharacterUpAlignedToWorldUp() const; };