Skip to content
Snippets Groups Projects

feature(Interaction): improve grabbing

Merged Daniel Rupp requested to merge daniel.rupp/rwth-vr-toolkit:dev/5.3 into dev/5.3
5 files
+ 186
38
Compare changes
  • Side-by-side
  • Inline

Files

@@ -3,8 +3,8 @@
@@ -3,8 +3,8 @@
#include "Interaction/Interactables/GrabBehavior.h"
#include "Interaction/Interactables/GrabBehavior.h"
#include "Interaction/Interactables/InteractableComponent.h"
#include "Interaction/Interactables/InteractableComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Serialization/JsonTypes.h"
#include "Serialization/JsonTypes.h"
 
#include "Utility/RWTHVRUtilities.h"
UPrimitiveComponent* UGrabBehavior::GetFirstComponentSimulatingPhysics(const AActor* TargetActor)
UPrimitiveComponent* UGrabBehavior::GetFirstComponentSimulatingPhysics(const AActor* TargetActor)
{
{
@@ -36,9 +36,10 @@ UPrimitiveComponent* UGrabBehavior::GetHighestParentSimulatingPhysics(UPrimitive
@@ -36,9 +36,10 @@ UPrimitiveComponent* UGrabBehavior::GetHighestParentSimulatingPhysics(UPrimitive
}
}
void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UInputAction* InputAction,
void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UInputAction* InputAction,
const FInputActionValue& Value)
const FInputActionValue& Value)
{
{
const APawn* Player = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
if (bObjectGrabbed)
 
return;
USceneComponent* Hand = Cast<USceneComponent>(TriggeredComponent->GetAttachParent());
USceneComponent* Hand = Cast<USceneComponent>(TriggeredComponent->GetAttachParent());
@@ -48,16 +49,16 @@ void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UIn
@@ -48,16 +49,16 @@ void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UIn
if (MyPhysicsComponent)
if (MyPhysicsComponent)
{
{
 
bWasSimulatingPhysics = MyPhysicsComponent->IsSimulatingPhysics();
MyPhysicsComponent->SetSimulatePhysics(false);
MyPhysicsComponent->SetSimulatePhysics(false);
MyPhysicsComponent->AttachToComponent(Hand, Rules);
bObjectGrabbed = MyPhysicsComponent->AttachToComponent(Hand, Rules);
}
}
else
else
{
{
GetOwner()->GetRootComponent()->AttachToComponent(Hand, Rules);
bObjectGrabbed = GetOwner()->GetRootComponent()->AttachToComponent(Hand, Rules);
}
}
if (bBlockOtherInteractionsWhileGrabbed && bObjectGrabbed)
if (bBlockOtherInteractionsWhileGrabbed)
{
{
TArray<UInteractableComponent*> Interactables;
TArray<UInteractableComponent*> Interactables;
GetOwner()->GetComponents<UInteractableComponent>(Interactables, false);
GetOwner()->GetComponents<UInteractableComponent>(Interactables, false);
@@ -66,19 +67,26 @@ void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UIn
@@ -66,19 +67,26 @@ void UGrabBehavior::OnActionStart(USceneComponent* TriggeredComponent, const UIn
Interactable->RestrictInteractionToComponent(TriggeredComponent);
Interactable->RestrictInteractionToComponent(TriggeredComponent);
}
}
}
}
}
void UGrabBehavior::OnActionEnd(USceneComponent* TriggeredComponent, const UInputAction* InputAction,
if (bObjectGrabbed)
const FInputActionValue& Value)
{
if (MyPhysicsComponent)
{
{
MyPhysicsComponent->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
OnGrabStartEvent.Broadcast(Hand, MyPhysicsComponent);
MyPhysicsComponent->SetSimulatePhysics(true);
}
}
else
else
{
{
GetOwner()->GetRootComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
UE_LOG(Toolkit, Warning, TEXT("Grab failed! Cannot attach grabbed component to hand"))
 
}
 
}
 
 
void UGrabBehavior::OnActionEnd(USceneComponent* TriggeredComponent, const UInputAction* InputAction,
 
const FInputActionValue& Value)
 
{
 
 
USceneComponent* Hand = Cast<USceneComponent>(TriggeredComponent->GetAttachParent());
 
 
if (TryRelease())
 
{
 
OnGrabEndEvent.Broadcast(Hand, MyPhysicsComponent);
}
}
if (bBlockOtherInteractionsWhileGrabbed)
if (bBlockOtherInteractionsWhileGrabbed)
@@ -91,3 +99,21 @@ void UGrabBehavior::OnActionEnd(USceneComponent* TriggeredComponent, const UInpu
@@ -91,3 +99,21 @@ void UGrabBehavior::OnActionEnd(USceneComponent* TriggeredComponent, const UInpu
}
}
}
}
}
}
 
 
bool UGrabBehavior::TryRelease()
 
{
 
if (!bObjectGrabbed)
 
return false;
 
 
if (MyPhysicsComponent)
 
{
 
MyPhysicsComponent->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
 
MyPhysicsComponent->SetSimulatePhysics(bWasSimulatingPhysics);
 
}
 
else
 
{
 
GetOwner()->GetRootComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
 
}
 
bObjectGrabbed = false;
 
return true;
 
}
Loading