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

added scale teleport classes, added buttons to IMC

parent f5f19b34
No related branches found
No related tags found
No related merge requests found
Pipeline #406161 failed
Showing
with 459 additions and 4 deletions
File added
File added
File added
File added
No preview for this file type
No preview for this file type
// Fill out your copyright notice in the Description page of Project Settings.
#include "Pawn/Navigation/HeightTeleportationComponent.h"
// Fill out your copyright notice in the Description page of Project Settings.
#include "Pawn/Navigation/ScalingComponent.h"
#include "EnhancedInputComponent.h"
#include "HeadMountedDisplayFunctionLibrary.h"
#include "MotionControllerComponent.h"
#include "EntitySystem/MovieSceneComponentDebug.h"
#include "Pawn/RWTHVRPawn.h"
#include "Utility/RWTHVRUtilities.h"
void UScalingComponent::BeginPlay()
{
Super::BeginPlay();
MotionController = VRPawn->RightHand;
ScalingDelegate.AddDynamic(this, &UScalingComponent::AdjustFeedbackMesh);
FloatingPawnMovement = Cast<UFloatingPawnMovement>(VRPawn->GetMovementComponent());
if(!FloatingPawnMovement)
{
UE_LOG(Toolkit,Error,TEXT("[ScalingComponent]: Cannot find FloatingPawnMovement at VRPawn"))
}
DefaultAcceleration = FloatingPawnMovement->Acceleration;
DefaultDeceleration = FloatingPawnMovement->Deceleration;
DefaultMaxSpeed = FloatingPawnMovement->MaxSpeed;
// Spawn the feedback meshes that indicate the current size in relation to the original size
FActorSpawnParameters Params;
OriginalSizeMesh = GetWorld()->SpawnActor<AStaticMeshActor>(Params);
OriginalSizeMesh->GetStaticMeshComponent()->Mobility = EComponentMobility::Movable;
OriginalSizeMesh->SetMobility(EComponentMobility::Movable);
FVector LocationAboveController = MotionController->GetComponentLocation();
LocationAboveController += MotionController->GetUpVector() * 10;
LocationAboveController += MotionController->GetForwardVector() * 15;
LocationAboveController += MotionController->GetRightVector() * FeedbackMeshDistance / 2.f;
OriginalSizeMesh->SetActorLocation(LocationAboveController);
OriginalSizeMesh->GetStaticMeshComponent()->SetStaticMesh(FeedbackMesh);
OriginalSizeMesh->SetActorScale3D(FVector(0.1, 0.1, 0.1));
OriginalSizeMesh->GetStaticMeshComponent()->AttachToComponent(MotionController,
FAttachmentTransformRules(
EAttachmentRule::KeepWorld,
EAttachmentRule::KeepRelative,
EAttachmentRule::KeepRelative, false));
CurrentSizeMesh = GetWorld()->SpawnActor<AStaticMeshActor>(Params);
CurrentSizeMesh->GetStaticMeshComponent()->Mobility = EComponentMobility::Movable;
CurrentSizeMesh->SetMobility(EComponentMobility::Movable);
LocationAboveController = MotionController->GetComponentLocation();
LocationAboveController += MotionController->GetUpVector() * 10;
LocationAboveController += MotionController->GetForwardVector() * 15;
LocationAboveController -= MotionController->GetRightVector() * FeedbackMeshDistance / 2.f;
CurrentSizeMesh->SetActorLocation(LocationAboveController);
CurrentSizeMesh->GetStaticMeshComponent()->SetStaticMesh(FeedbackMesh);
CurrentSizeMesh->SetActorScale3D(FVector(0.1, 0.1, 0.1));
CurrentSizeMesh->GetStaticMeshComponent()->AttachToComponent(MotionController,
FAttachmentTransformRules(
EAttachmentRule::KeepWorld,
EAttachmentRule::KeepRelative,
EAttachmentRule::KeepWorld, false));
// Disable collisions of preview meshes
OriginalSizeMesh->SetActorEnableCollision(false);
CurrentSizeMesh->SetActorEnableCollision(false);
// Correct rotation of meshes
FRotator Corrector = FRotator(0, 90, 0);
OriginalSizeMesh->AddActorLocalRotation(Corrector);
CurrentSizeMesh->AddActorLocalRotation(Corrector);
// Add controllers as equal sized components so thei scale accordingly
AddEqualSizedComponent(VRPawn->LeftHand);
AddEqualSizedComponent(VRPawn->RightHand);
}
void UScalingComponent::SetupPlayerInput(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInput(PlayerInputComponent);
if (!VRPawn || !VRPawn->HasLocalNetOwner())
{
return;
}
UEnhancedInputComponent* EI = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (!EI)
{
UE_LOG(Toolkit, Error, TEXT("Cannot cast Input Component to Enhanced Input Component in ScalingComponent"));
return;
}
// teleporting
EI->BindAction(Grow, ETriggerEvent::Completed, this, &UScalingComponent::OnGrow);
EI->BindAction(Shrink, ETriggerEvent::Completed, this, &UScalingComponent::OnShrink);
EI->BindAction(ResetScale, ETriggerEvent::Completed, this, &UScalingComponent::OnResetScale);
// turning is defined in MovementComponentBase
}
void UScalingComponent::OnGrow(const FInputActionValue& InputActionValue)
{
float CurrentW2MScale = UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(GetOwner()->GetWorld());
float PreviousScale = CurrentW2MScale; // store for computing scale ratio
CurrentW2MScale = CurrentW2MScale * (1 + ScaleStepSize);
GNearClippingPlane = CurrentW2MScale;
FloatingPawnMovement->Acceleration = DefaultAcceleration * CurrentW2MScale/100;
FloatingPawnMovement->Deceleration = DefaultDeceleration* CurrentW2MScale/100;
FloatingPawnMovement->MaxSpeed = DefaultMaxSpeed * CurrentW2MScale/100;
if (CurrentW2MScale > MaxScale || CurrentW2MScale < MinScale)
{
UE_LOG(LogTemp, Warning, TEXT("Exceeding scale limits!"));
return;
}
CurrentW2MScale = FMath::Clamp(CurrentW2MScale, MinScale, MaxScale);
// min and max are swapped here due to the inverse nature of the WorldToMeterScale attribute
float ScaleRatio = CurrentW2MScale / PreviousScale;
AdjustEqualSizedWorldObjects(ScaleRatio);
UHeadMountedDisplayFunctionLibrary::SetWorldToMetersScale(GetOwner()->GetWorld(), CurrentW2MScale);
// inform all subscribed actors of the scale change
ScalingDelegate.Broadcast(ScaleRatio, false, VRPawn->GetActorLocation());
//DrawDebugSphere(GetWorld(), ParentActor->GetActorLocation(), 15, 15, FColor::Red, false, 10);
}
void UScalingComponent::OnShrink(const FInputActionValue& InputActionValue)
{
float CurrentW2MScale = UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(GetOwner()->GetWorld());
float PreviousScale = CurrentW2MScale; // store for computing scale ratio
CurrentW2MScale = CurrentW2MScale / (1.f + ScaleStepSize);
GNearClippingPlane = CurrentW2MScale;
FloatingPawnMovement->Acceleration = DefaultAcceleration * CurrentW2MScale/100;
FloatingPawnMovement->Deceleration = DefaultDeceleration * CurrentW2MScale/100;
FloatingPawnMovement->MaxSpeed = DefaultMaxSpeed * CurrentW2MScale/100;
if (CurrentW2MScale > MaxScale || CurrentW2MScale < MinScale)
{
UE_LOG(LogTemp, Warning, TEXT("Exceeding scale limits!"));
return;
}
CurrentW2MScale = FMath::Clamp(CurrentW2MScale, MinScale, MaxScale);
// min and max are swapped here due to the inverse nature of the WorldToMeterScale attribute
float ScaleRatio = CurrentW2MScale / PreviousScale;
AdjustEqualSizedWorldObjects(ScaleRatio);
UHeadMountedDisplayFunctionLibrary::SetWorldToMetersScale(GetOwner()->GetWorld(), CurrentW2MScale);
// inform all subscribed actors of the scale change
ScalingDelegate.Broadcast(ScaleRatio, false, VRPawn->GetActorLocation());
//DrawDebugSphere(GetWorld(), ParentActor->GetActorLocation(), 15, 15, FColor::Red, true, 10);
}
void UScalingComponent::OnResetScale(const FInputActionValue& InputActionValue)
{
UE_LOG(LogTemp, Error, TEXT("Resetting Scale"));
float CurrentW2MScale = UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(GetOwner()->GetWorld());
float PreviousScale = CurrentW2MScale; // store for computing scale ratio
CurrentW2MScale = OriginalScale;
GNearClippingPlane = CurrentW2MScale;
FloatingPawnMovement->Acceleration = DefaultAcceleration * CurrentW2MScale/100;
FloatingPawnMovement->Deceleration = DefaultDeceleration * CurrentW2MScale/100;
FloatingPawnMovement->MaxSpeed = DefaultMaxSpeed * CurrentW2MScale/100;
CurrentW2MScale = FMath::Clamp(CurrentW2MScale, MinScale, MaxScale);
// min and max are swapped here due to the inverse nature of the WorldToMeterScale attribute
float ScaleRatio = CurrentW2MScale / PreviousScale;
AdjustEqualSizedWorldObjects(ScaleRatio);
UHeadMountedDisplayFunctionLibrary::SetWorldToMetersScale(GetOwner()->GetWorld(), CurrentW2MScale);
// Scale the visualizer
//CurrentSizeMesh->SetActorScale3D(FVector(0.1,0.1,0.1));
// inform all subscribed actors of the scale change
ScalingDelegate.Broadcast(ScaleRatio, true, VRPawn->GetActorLocation());
//ScalingResetDelegate.Broadcast();
}
float UScalingComponent::GetTotalScaleRatio()
{
return OriginalScale / UHeadMountedDisplayFunctionLibrary::GetWorldToMetersScale(GetOwner()->GetWorld());
}
void UScalingComponent::AddEqualSizedActor(AActor* ActorToKeepEqual)
{
if (ActorToKeepEqual != nullptr)
{
EqualSizedActors.AddUnique(ActorToKeepEqual);
}
else
{
UE_LOG(LogTemp, Fatal, TEXT("No valid actor was passed to method AddEqualSizedActor()!"));
}
}
void UScalingComponent::AddEqualSizedComponent(USceneComponent* SceneComponentToKeepEqual)
{
if (SceneComponentToKeepEqual != nullptr)
{
EqualSizedSceneComponents.AddUnique(SceneComponentToKeepEqual);
}
else
{
UE_LOG(LogTemp, Fatal, TEXT("No valid USceneComponent was passed to method AddEqualSizedComponent() !"));
}
}
void UScalingComponent::AdjustEqualSizedWorldObjects(float ScaleRatio)
{
// adjust actors
for (AActor* CurrActor : EqualSizedActors)
{
//UE_LOG(LogTemp, Warning, TEXT("Scaling Actor"));
FVector CurrentScaling = CurrActor->GetActorScale3D();
CurrActor->SetActorScale3D(CurrentScaling * ScaleRatio);
}
// adjust scene components
for (USceneComponent* CurrComponent : EqualSizedSceneComponents)
{
//UE_LOG(LogTemp, Warning, TEXT("Scaling Component"));
FVector CurrentScaling = CurrComponent->GetComponentScale();
CurrComponent->SetWorldScale3D(CurrentScaling * ScaleRatio);
}
}
void UScalingComponent::AdjustFeedbackMesh(float NewWMSRation, bool isReset, FVector NewPos)
{
TotalScaleRatio *= NewWMSRation;
FVector Scale = CurrentSizeMesh->GetActorScale3D();
Scale *= NewWMSRation;
CurrentSizeMesh->SetActorScale3D(Scale);
ControllerUpOffset *= NewWMSRation;
ControllerForwardOffset *= NewWMSRation;
FeedbackMeshDistance *= NewWMSRation;
FVector LocationAboveController = MotionController->GetComponentLocation();
LocationAboveController +=
MotionController->GetUpVector() * ControllerUpOffset;
LocationAboveController +=
MotionController->GetForwardVector() * ControllerForwardOffset;
LocationAboveController +=
MotionController->GetRightVector() * FeedbackMeshDistance / 2.f;
OriginalSizeMesh->SetActorLocation(LocationAboveController);
LocationAboveController -=
MotionController->GetRightVector() * FeedbackMeshDistance;
CurrentSizeMesh->SetActorLocation(LocationAboveController);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Pawn/Navigation/MovementComponentBase.h"
#include "HeightTeleportationComponent.generated.h"
/**
*
*/
UCLASS()
class RWTHVRTOOLKIT_API UHeightTeleportationComponent : public UMovementComponentBase
{
GENERATED_BODY()
// Spawn the TwoStepHandler etc. (Basically what was done in the VRPawn directly)
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "Engine/StaticMeshActor.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "GameFramework/MovementComponent.h"
#include "Pawn/Navigation/MovementComponentBase.h"
#include "ScalingComponent.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class RWTHVRTOOLKIT_API UScalingComponent : public UMovementComponentBase
{
GENERATED_BODY()
public:
virtual void SetupPlayerInput(UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "VR Movement")
bool bMoveWithRightHand = true;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
UInputAction* Grow;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
UInputAction* Shrink;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "VR Movement|Input|Actions")
UInputAction* ResetScale;
UFUNCTION(BlueprintCallable)
void OnGrow(const FInputActionValue& InputActionValue);
UFUNCTION(BlueprintCallable)
void OnShrink(const FInputActionValue& InputActionValue);
UFUNCTION(BlueprintCallable)
void OnResetScale(const FInputActionValue& InputActionValue);
// code from marius:
UFUNCTION(BlueprintCallable)
void SetMaxScale(float UpperLimit)
{
MaxScale = UpperLimit;
}
UFUNCTION(BlueprintCallable)
void SetMinScale(float LowerLimit)
{
MinScale = LowerLimit;
}
UFUNCTION(BlueprintCallable)
void SetStepSize(float StepSize)
{
ScaleStepSize = StepSize;
}
UFUNCTION(BlueprintCallable)
float GetStepSize()
{
return ScaleStepSize;
}
float GetTotalScaleRatio();
UFUNCTION(BlueprintCallable)
void AddEqualSizedActor(AActor* ActorToKeepEqual);
UFUNCTION(BlueprintCallable)
void AddEqualSizedComponent(USceneComponent* SceneComponentToKeepEqual);
void AdjustEqualSizedWorldObjects(float ScaleRatio);
UFUNCTION()
void AdjustFeedbackMesh(float NewWMSRation, bool isReset = true, FVector NewPos = FVector::Zero());
void SetFeedbackMesh(UStaticMesh* NewMesh)
{
FeedbackMesh = NewMesh;
OriginalSizeMesh->GetStaticMeshComponent()->SetStaticMesh(FeedbackMesh);
CurrentSizeMesh->GetStaticMeshComponent()->SetStaticMesh(FeedbackMesh);
}
// Scaling Delegate that other actors can subscribe to
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FScalingDelegate, float, NewWorldScaling, bool, isReset, FVector,
AdjustedPawnLocation);
UPROPERTY(BlueprintAssignable)
FScalingDelegate ScalingDelegate;
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FScalingResetDelegate);
UPROPERTY(BlueprintAssignable)
FScalingResetDelegate ScalingResetDelegate;
UPROPERTY(EditAnywhere)
UMotionControllerComponent* MotionController = nullptr;
UPROPERTY(EditAnywhere)
TArray<AActor*> EqualSizedActors;
UPROPERTY(EditAnywhere)
TArray<USceneComponent*> EqualSizedSceneComponents;
//Boundaries of Scaling (inverse relation: Increasing the WorldToMeterScale decreases the pawns size (as the world increases))
UPROPERTY(EditAnywhere)
float MinScale = 10;
UPROPERTY(EditAnywhere)
float MaxScale = 350;
UPROPERTY(EditAnywhere)
float ScaleStepSize = 0.2;
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
UPROPERTY()
float OriginalScale = 100;
float TotalScaleRatio = 1;
// Components For Visual Feedback
UPROPERTY(EditAnywhere)
UStaticMesh* FeedbackMesh = nullptr;
UPROPERTY(EditAnywhere)
AStaticMeshActor* OriginalSizeMesh = nullptr;
UPROPERTY(EditAnywhere)
AStaticMeshActor* CurrentSizeMesh = nullptr;
float FeedbackMeshDistance = 15;
float ControllerForwardOffset = 15;
float ControllerUpOffset = 10;
// boolean for scaling behaviour
bool bHasScaled = false;
UFloatingPawnMovement* FloatingPawnMovement;
float DefaultAcceleration;
float DefaultDeceleration;
float DefaultMaxSpeed;
};
...@@ -60,6 +60,8 @@ public: ...@@ -60,6 +60,8 @@ public:
virtual void SetupPlayerInput(UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInput(UInputComponent* PlayerInputComponent) override;
UPROPERTY(BlueprintReadOnly)
AActor* TeleportVisualizer;
private: private:
UPROPERTY() UPROPERTY()
UMotionControllerComponent* TeleportationHand; UMotionControllerComponent* TeleportationHand;
...@@ -74,6 +76,4 @@ private: ...@@ -74,6 +76,4 @@ private:
bool bValidTeleportLocation = false; bool bValidTeleportLocation = false;
FVector FinalTeleportLocation; FVector FinalTeleportLocation;
UPROPERTY()
AActor* TeleportVisualizer;
}; };
...@@ -38,7 +38,7 @@ public class RWTHVRToolkit : ModuleRules ...@@ -38,7 +38,7 @@ public class RWTHVRToolkit : ModuleRules
PrivateDependencyModuleNames.AddRange( PrivateDependencyModuleNames.AddRange(
new string[] new string[]
{ {
"NetCore" "NetCore", "XRBase", "XRBase"
} }
); );
if (Target.bBuildEditor == true) if (Target.bBuildEditor == true)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment