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
+ 157
29
Compare changes
  • Side-by-side
  • Inline

Files

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