Skip to content
Snippets Groups Projects
Commit dce992ee authored by Daniel Rupp's avatar Daniel Rupp
Browse files

refactor(movement): extracts turning logic from MovementCommponentBase to own...

refactor(movement): extracts turning logic from MovementCommponentBase to own component, fix(movement): fixed rotation not being around the users current standing position
parent 1b537526
No related branches found
No related tags found
1 merge request!43refactor(movement): extracts turning logic from MovementCommponentBase to own Component
Showing
with 217 additions and 123 deletions
No preview for this file type
File deleted
File deleted
File added
File added
File added
......@@ -3,12 +3,10 @@
#include "Pawn/Navigation/MovementComponentBase.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Engine/LocalPlayer.h"
#include "GameFramework/PlayerController.h"
#include "Pawn/VirtualRealityPawn.h"
#include "Pawn/VRPawnInputConfig.h"
#include "Utility/VirtualRealityUtilities.h"
void UMovementComponentBase::SetupPlayerInput(UInputComponent* PlayerInputComponent)
......@@ -28,85 +26,4 @@ void UMovementComponentBase::SetupPlayerInput(UInputComponent* PlayerInputCompon
UE_LOG(Toolkit, Error, TEXT("InputSubsystem IS NOT VALID"));
return;
}
// add Input Mapping context
InputSubsystem->AddMappingContext(IMCRotation, 0);
UEnhancedInputComponent* EI = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (!EI)
{
UE_LOG(Toolkit, Error, TEXT("Cannot cast Input Component to Enhanced Inpu Component in VRPawnMovement"));
return;
}
// turning
if (bAllowTurning)
{
// no snap turning for desktop mode
if (bSnapTurn && !UVirtualRealityUtilities::IsDesktopMode())
{
EI->BindAction(Turn, ETriggerEvent::Started, this, &UMovementComponentBase::OnBeginSnapTurn);
}
else
{
EI->BindAction(Turn, ETriggerEvent::Triggered, this, &UMovementComponentBase::OnBeginTurn);
}
}
// bind additional functions for desktop rotations
if (UVirtualRealityUtilities::IsDesktopMode())
{
EI->BindAction(DesktopRotation, ETriggerEvent::Started, this, &UMovementComponentBase::StartDesktopRotation);
EI->BindAction(DesktopRotation, ETriggerEvent::Completed, this, &UMovementComponentBase::EndDesktopRotation);
}
}
void UMovementComponentBase::StartDesktopRotation()
{
bApplyDesktopRotation = true;
}
void UMovementComponentBase::EndDesktopRotation()
{
bApplyDesktopRotation = false;
}
void UMovementComponentBase::OnBeginTurn(const FInputActionValue& Value)
{
if (UVirtualRealityUtilities::IsDesktopMode() && !bApplyDesktopRotation)
return;
if (!VRPawn || !VRPawn->Controller)
return;
const FVector2D TurnValue = Value.Get<FVector2D>();
if (TurnValue.X != 0.f)
{
VRPawn->AddControllerYawInput(TurnRateFactor * TurnValue.X);
}
if (TurnValue.Y != 0.f)
{
if (UVirtualRealityUtilities::IsDesktopMode() && bApplyDesktopRotation)
{
VRPawn->AddControllerPitchInput(TurnRateFactor * -TurnValue.Y);
}
}
}
void UMovementComponentBase::OnBeginSnapTurn(const FInputActionValue& Value)
{
if (!VRPawn || !VRPawn->Controller)
return;
const FVector2D TurnValue = Value.Get<FVector2D>();
if (TurnValue.X > 0.f)
{
VRPawn->AddControllerYawInput(SnapTurnAngle);
}
else if (TurnValue.X < 0.f)
{
VRPawn->AddControllerYawInput(-SnapTurnAngle);
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#include "Pawn/Navigation/TurnComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "MotionControllerComponent.h"
#include "Pawn/VirtualRealityPawn.h"
#include "Utility/VirtualRealityUtilities.h"
void UTurnComponent::SetupPlayerInput(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInput(PlayerInputComponent);
if (!VRPawn || !VRPawn->HasLocalNetOwner() || !InputSubsystem)
{
return;
}
// simple way of changing the handedness
if(bTurnWithLeftHand)
{
RotationHand = VRPawn->LeftHand;
// we use the same IMC for movement and turning
// therefore if we move with the right hand, we turn with the left hand
IMCTurn = IMCMovement_Right;
} else
{
RotationHand = VRPawn->RightHand;
// we use the same IMC for movement and turning
// therefore if we move with the left hand, we turn with the right hand
IMCTurn = IMCMovement_Left;
}
// add Input Mapping context
InputSubsystem->AddMappingContext(IMCTurn, 0);
InputSubsystem->AddMappingContext(IMCDesktopRotation, 0);
UEnhancedInputComponent* EI = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (!EI)
{
UE_LOG(Toolkit, Error, TEXT("Cannot cast Input Component to Enhanced Inpu Component in VRPawnMovement"));
return;
}
// turning
if (bAllowTurning)
{
// no snap turning for desktop mode
if (bSnapTurn && !UVirtualRealityUtilities::IsDesktopMode())
{
EI->BindAction(Turn, ETriggerEvent::Started, this, &UTurnComponent::OnBeginSnapTurn);
}
else
{
EI->BindAction(Turn, ETriggerEvent::Triggered, this, &UTurnComponent::OnBeginTurn);
}
}
// bind additional functions for desktop rotations
if (UVirtualRealityUtilities::IsDesktopMode())
{
EI->BindAction(DesktopRotation, ETriggerEvent::Started, this, &UTurnComponent::StartDesktopRotation);
EI->BindAction(DesktopRotation, ETriggerEvent::Completed, this, &UTurnComponent::EndDesktopRotation);
}
}
void UTurnComponent::StartDesktopRotation()
{
bApplyDesktopRotation = true;
}
void UTurnComponent::EndDesktopRotation()
{
bApplyDesktopRotation = false;
}
void UTurnComponent::OnBeginTurn(const FInputActionValue& Value)
{
if (UVirtualRealityUtilities::IsDesktopMode() && !bApplyDesktopRotation)
return;
if (!VRPawn || !VRPawn->Controller)
return;
const FVector2D TurnValue = Value.Get<FVector2D>();
if (TurnValue.X != 0.f)
{
RotateCameraAndPawn(TurnRateFactor * TurnValue.X);
if (UVirtualRealityUtilities::IsDesktopMode())
{
UpdateRightHandForDesktopInteraction();
}
}
if (TurnValue.Y != 0.f)
{
if (UVirtualRealityUtilities::IsDesktopMode() && bApplyDesktopRotation)
{
VRPawn->AddControllerPitchInput(TurnRateFactor * -TurnValue.Y);
}
}
}
void UTurnComponent::OnBeginSnapTurn(const FInputActionValue& Value)
{
if (!VRPawn || !VRPawn->Controller)
return;
const FVector2D TurnValue = Value.Get<FVector2D>();
if (TurnValue.X > 0.f)
{
RotateCameraAndPawn(SnapTurnAngle);
}
else if (TurnValue.X < 0.f)
{
RotateCameraAndPawn(-SnapTurnAngle);
}
}
void UTurnComponent::RotateCameraAndPawn(float Yaw)
{
FVector NewLocation;
FRotator NewRotation;
FVector OrigLocation = VRPawn->GetActorLocation();
FVector PivotPoint = VRPawn->GetActorTransform().InverseTransformPosition(OrigLocation);
PivotPoint.Z = 0.0f;
FRotator OrigRotation = VRPawn->GetActorRotation();
NewRotation = FRotator(0,VRPawn->GetActorRotation().Yaw+Yaw,0);
NewLocation = OrigLocation + OrigRotation.RotateVector(PivotPoint);
VRPawn->Controller->SetControlRotation(NewRotation);
VRPawn->SetActorLocationAndRotation(NewLocation,NewRotation);
//FVector MovedBy = NewLocation - OrigLocation;
}
......@@ -19,46 +19,7 @@ class RWTHVRTOOLKIT_API UMovementComponentBase : public UActorComponent, public
public:
virtual void SetupPlayerInput(UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement")
bool bAllowTurning = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "bAllowTurning"))
bool bSnapTurn = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "!bSnapTurn && bAllowTurning"))
float TurnRateFactor = 1.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "bSnapTurn && bAllowTurning", ClampMin = 0, ClampMax = 360))
float SnapTurnAngle = 22.5;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
class UInputAction* Turn;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
class UInputAction* DesktopRotation;
/*Movement Input*/
UFUNCTION(BlueprintCallable)
void OnBeginTurn(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable)
void OnBeginSnapTurn(const FInputActionValue& Value);
/*Desktop Testing*/
// the idea is that you have to hold the right mouse button to do rotations
UFUNCTION()
void StartDesktopRotation();
UFUNCTION()
void EndDesktopRotation();
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input")
class UInputMappingContext* IMCRotation;
UPROPERTY()
AVirtualRealityPawn* VRPawn;
......@@ -66,5 +27,4 @@ protected:
UPROPERTY()
UEnhancedInputLocalPlayerSubsystem* InputSubsystem;
bool bApplyDesktopRotation = false;
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "MovementComponentBase.h"
#include "TurnComponent.generated.h"
UCLASS(Blueprintable)
class RWTHVRTOOLKIT_API UTurnComponent : public UMovementComponentBase
{
GENERATED_BODY()
public:
virtual void SetupPlayerInput(UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement")
bool bTurnWithLeftHand = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement")
bool bAllowTurning = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "bAllowTurning"))
bool bSnapTurn = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "!bSnapTurn && bAllowTurning"))
float TurnRateFactor = 1.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement|Turning",
meta = (EditCondition = "bSnapTurn && bAllowTurning", ClampMin = 0, ClampMax = 360))
float SnapTurnAngle = 22.5;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
class UInputAction* Turn;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
class UInputAction* DesktopRotation;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input")
class UInputMappingContext* IMCMovement_Left;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input")
class UInputMappingContext* IMCMovement_Right;
/*Movement Input*/
UFUNCTION(BlueprintCallable)
void OnBeginTurn(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable)
void OnBeginSnapTurn(const FInputActionValue& Value);
/*Desktop Testing*/
// the idea is that you have to hold the right mouse button to do rotations
UFUNCTION()
void StartDesktopRotation();
UFUNCTION()
void EndDesktopRotation();
private:
UPROPERTY()
UMotionControllerComponent* RotationHand;
UPROPERTY()
class UInputMappingContext* IMCTurn;
UPROPERTY()
class UInputMappingContext* IMCDesktopRotation;
void RotateCameraAndPawn(float Yaw);
bool bApplyDesktopRotation;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment