Skip to content
Snippets Groups Projects
Select Git revision
  • 9b71a236415fe5a92c5f6c8b05b56e65b08be7bf
  • stable default protected
  • MA_Pape_2018
  • MA_2018_Lopatin
  • feature/mesh_viewer
  • feature/#468_access_isosurface_scalar
  • feature/#459_default_primitives
  • master protected
  • feature/#470_Create_a_color_lookup_table
  • feature/#473_resize_companion_window
  • feature/#462_do_not_use_arb_extensions
  • feature/#495_Provide_data_for_larger_isosurfaces
  • feature/#323_default_image
  • feature/#480_Create_a_smaller_test_mesh_for_combustion_demo
  • feature/#236_Get_Integration_tests_running_on_CI
  • feature/#447_Copy_standard_assets_to_build_folder
  • 447-copy-standard-assets-to-build-folder-and-remove-resource-path
  • feature/#445_mesh_render_settings_component
  • feature/#251_Make_sure_tests_cpp_is_compiled_once
  • feature/#455_Remove_navigation_and_improve_interaction_for_combustion_demo
  • feature/446_strange_txt_files
  • v18.06.0
  • v18.05.0
  • #251_bad
  • #251_good
  • v18.03.0
  • v18.02.0
  • v18.01.0
  • v17.12.0
  • v17.11.0
  • v17.10.0
  • v17.09.0
  • v17.07.0
33 results

rotation_helper.cpp

Blame
  • TurnComponent.cpp 3.26 KiB
    // Fill out your copyright notice in the Description page of Project Settings.
    
    
    #include "Pawn/Navigation/TurnComponent.h"
    
    #include "EnhancedInputComponent.h"
    #include "Camera/CameraComponent.h"
    #include "Pawn/RWTHVRPawn.h"
    #include "Utility/RWTHVRUtilities.h"
    
    void UTurnComponent::SetupPlayerInput(UInputComponent* PlayerInputComponent)
    {
    	Super::SetupPlayerInput(PlayerInputComponent);
    
    	if (!VRPawn || !VRPawn->HasLocalNetOwner())
    	{
    		return;
    	}
    
    	// simple way of changing the handedness
    	if (bTurnWithLeftHand)
    	{
    		RotationHand = VRPawn->LeftHand;
    	}
    	else
    	{
    		RotationHand = VRPawn->RightHand;
    	}
    
    	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 && !URWTHVRUtilities::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 (URWTHVRUtilities::IsDesktopMode())
    	{
    		EI->BindAction(DesktopRotation, ETriggerEvent::Started, this, &UTurnComponent::StartDesktopRotation);
    		EI->BindAction(DesktopRotation, ETriggerEvent::Completed, this, &UTurnComponent::EndDesktopRotation);
    	}
    }
    
    // A separate button has to be pressed, for when mouse movement should contribute to turning
    void UTurnComponent::StartDesktopRotation() { bApplyDesktopRotation = true; }
    
    void UTurnComponent::EndDesktopRotation() { bApplyDesktopRotation = false; }
    
    void UTurnComponent::OnBeginTurn(const FInputActionValue& Value)
    {
    	if (URWTHVRUtilities::IsDesktopMode() && !bApplyDesktopRotation)
    		return;
    
    	if (!VRPawn || !VRPawn->Controller)
    		return;
    
    	const FVector2D TurnValue = Value.Get<FVector2D>();
    
    	if (TurnValue.X != 0.f)
    	{
    		if (URWTHVRUtilities::IsDesktopMode() && bApplyDesktopRotation)
    		{
    			VRPawn->AddControllerYawInput(TurnRateFactor * TurnValue.X);
    		}
    		else
    		{
    			RotateCameraAndPawn(TurnRateFactor * TurnValue.X);
    		}
    	}
    
    	if (TurnValue.Y != 0.f)
    	{
    		if (URWTHVRUtilities::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) const
    {
    	const FVector OrigLocation = VRPawn->HeadCameraComponent->GetComponentLocation();
    	
    	const FRotator OrigRotation = VRPawn->GetActorRotation();
    	const FRotator NewRotation = FRotator(0, VRPawn->GetActorRotation().Yaw + Yaw, 0);
    
    	const FVector Offset = VRPawn->GetActorLocation() - OrigLocation;
    	const FVector UntwistedOffset = OrigRotation.GetInverse().RotateVector(Offset);
    	const FVector NewLocation = OrigLocation + NewRotation.RotateVector(UntwistedOffset);
    
    	VRPawn->Controller->SetControlRotation(NewRotation);
    	VRPawn->SetActorLocationAndRotation(NewLocation, NewRotation);
    }