From be3647e310f46dc0c1c058550d23b69635974bab Mon Sep 17 00:00:00 2001 From: Mamadou Babaei Date: Mon, 14 Aug 2023 16:39:46 +0200 Subject: [PATCH] cache all overlapped actors with the real/virtual hands --- .../SenseGlove/GameFramework/SGPawn.cpp | 102 +++++++++--------- .../Public/SenseGlove/GameFramework/SGPawn.h | 7 ++ 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/Source/SenseGlove/Private/SenseGlove/GameFramework/SGPawn.cpp b/Source/SenseGlove/Private/SenseGlove/GameFramework/SGPawn.cpp index db270d03..9737d380 100644 --- a/Source/SenseGlove/Private/SenseGlove/GameFramework/SGPawn.cpp +++ b/Source/SenseGlove/Private/SenseGlove/GameFramework/SGPawn.cpp @@ -53,13 +53,6 @@ struct ASGPawn::FImpl * Static methods ************************/ - /************************ - * Member variables - ************************/ - - bool bUpdateLeftHandLocationAndRotation; - bool bUpdateRightHandLocationAndRotation; - /************************ * Owner object ************************/ @@ -110,6 +103,15 @@ struct ASGPawn::FImpl void UpdateRightHandLocationAndRotation( const FVector& Location, const FRotator& Rotation, const bool bUpdateVirtualHand); + void RegisterLeftHandOverlappedActor(const AActor* Actor) const; + void UnregisterLeftHandOverlappedActor(const AActor* Actor) const; + + void RegisterRightHandOverlappedActor(const AActor* Actor) const; + void UnregisterRightHandOverlappedActor(const AActor* Actor) const; + + bool IsLeftHandOverlappingWithAnyActor() const; + bool IsRightHandOverlappingWithAnyActor() const; + void DisableActorPhysics(const AActor* Actor) const; void EnableActorPhysics(const AActor* Actor) const; @@ -596,22 +598,22 @@ void ASGPawn::BeginPlay() WristTrackerLeft->OnWristLocationChanged().AddWeakLambda(this, [&](const FVector& Location)-> void { - Pimpl->UpdateLeftHandLocation(Location, Pimpl->bUpdateLeftHandLocationAndRotation); + Pimpl->UpdateLeftHandLocation(Location, !Pimpl->IsLeftHandOverlappingWithAnyActor()); }); WristTrackerLeft->OnWristRotationChanged().AddWeakLambda(this, [&](const FRotator& Rotation)-> void { - Pimpl->UpdateLeftHandRotation(Rotation, Pimpl->bUpdateLeftHandLocationAndRotation); + Pimpl->UpdateLeftHandRotation(Rotation, !Pimpl->IsLeftHandOverlappingWithAnyActor()); }); WristTrackerRight->OnWristLocationChanged().AddWeakLambda(this, [&](const FVector& Location)-> void { - Pimpl->UpdateRightHandLocation(Location, Pimpl->bUpdateRightHandLocationAndRotation); + Pimpl->UpdateRightHandLocation(Location, !Pimpl->IsRightHandOverlappingWithAnyActor()); }); WristTrackerRight->OnWristRotationChanged().AddWeakLambda(this, [&](const FRotator& Rotation)-> void { - Pimpl->UpdateRightHandRotation(Rotation, Pimpl->bUpdateRightHandLocationAndRotation); + Pimpl->UpdateRightHandRotation(Rotation, !Pimpl->IsRightHandOverlappingWithAnyActor()); }); Pimpl->BindRealHandsOverlapEvents(); @@ -670,16 +672,7 @@ void ASGPawn::OnRealLeftHandOverlapBegins( return; } - if (!Pimpl->bUpdateLeftHandLocationAndRotation) - { - return; - } - - Pimpl->bUpdateLeftHandLocationAndRotation = false; - - /*FVector WristLocation{WristTrackerLeft->GetWristLocation()}; - FRotator WristRotation{WristTrackerLeft->GetWristRotation()}; - Pimpl->UpdateLeftHandLocationAndRotation(MoveTemp(WristLocation), MoveTemp(WristRotation), true);*/ + Pimpl->RegisterLeftHandOverlappedActor(OtherActor); } void ASGPawn::OnRealLeftHandOverlapEnds( @@ -697,16 +690,7 @@ void ASGPawn::OnRealLeftHandOverlapEnds( return; } - if (Pimpl->bUpdateLeftHandLocationAndRotation) - { - return; - } - - Pimpl->bUpdateLeftHandLocationAndRotation = true; - - /*FVector WristLocation{WristTrackerLeft->GetWristLocation()}; - FRotator WristRotation{WristTrackerLeft->GetWristRotation()}; - Pimpl->UpdateLeftHandLocationAndRotation(MoveTemp(WristLocation), MoveTemp(WristRotation), true);*/ + Pimpl->UnregisterLeftHandOverlappedActor(OtherActor); } void ASGPawn::OnRealRightHandOverlapBegins( @@ -728,16 +712,7 @@ void ASGPawn::OnRealRightHandOverlapBegins( return; } - if (!Pimpl->bUpdateRightHandLocationAndRotation) - { - return; - } - - Pimpl->bUpdateRightHandLocationAndRotation = false; - - /*FVector WristLocation{WristTrackerRight->GetWristLocation()}; - FRotator WristRotation{WristTrackerRight->GetWristRotation()}; - Pimpl->UpdateRightHandLocationAndRotation(MoveTemp(WristLocation), MoveTemp(WristRotation), true);*/ + Pimpl->RegisterRightHandOverlappedActor(OtherActor); } void ASGPawn::OnRealRightHandOverlapEnds( @@ -755,16 +730,7 @@ void ASGPawn::OnRealRightHandOverlapEnds( return; } - if (Pimpl->bUpdateRightHandLocationAndRotation) - { - return; - } - - Pimpl->bUpdateRightHandLocationAndRotation = true; - - /*FVector WristLocation{WristTrackerRight->GetWristLocation()}; - FRotator WristRotation{WristTrackerRight->GetWristRotation()}; - Pimpl->UpdateRightHandLocationAndRotation(MoveTemp(WristLocation), MoveTemp(WristRotation), true);*/ + Pimpl->UnregisterRightHandOverlappedActor(OtherActor); } void ASGPawn::OnLeftThumbFingertipGrabColliderOverlapBegins( @@ -1478,9 +1444,7 @@ void ASGPawn::ReleaseRight() } ASGPawn::FImpl::FImpl(ASGPawn* InOwner) - : bUpdateLeftHandLocationAndRotation(true), - bUpdateRightHandLocationAndRotation(true), - Owner(InOwner) + : Owner(InOwner) { } @@ -1795,6 +1759,36 @@ void ASGPawn::FImpl::UpdateRightHandLocationAndRotation( UpdateRightHandRotation(Rotation, bUpdateVirtualHand); } +void ASGPawn::FImpl::RegisterLeftHandOverlappedActor(const AActor* Actor) const +{ + Owner->LeftHandOverlappedActors.AddUnique(Actor); +} + +void ASGPawn::FImpl::UnregisterLeftHandOverlappedActor(const AActor* Actor) const +{ + Owner->LeftHandOverlappedActors.Remove(Actor); +} + +void ASGPawn::FImpl::RegisterRightHandOverlappedActor(const AActor* Actor) const +{ + Owner->RightHandOverlappedActors.AddUnique(Actor); +} + +void ASGPawn::FImpl::UnregisterRightHandOverlappedActor(const AActor* Actor) const +{ + Owner->RightHandOverlappedActors.Remove(Actor); +} + +bool ASGPawn::FImpl::IsLeftHandOverlappingWithAnyActor() const +{ + return Owner->LeftHandOverlappedActors.Num() > 0; +} + +bool ASGPawn::FImpl::IsRightHandOverlappingWithAnyActor() const +{ + return Owner->RightHandOverlappedActors.Num() > 0; +} + void ASGPawn::FImpl::DisableActorPhysics(const AActor* Actor) const { UPrimitiveComponent* ActorRootComponent{Cast(Actor->GetRootComponent())}; diff --git a/Source/SenseGlove/Public/SenseGlove/GameFramework/SGPawn.h b/Source/SenseGlove/Public/SenseGlove/GameFramework/SGPawn.h index 3a5352ec..0efe3653 100644 --- a/Source/SenseGlove/Public/SenseGlove/GameFramework/SGPawn.h +++ b/Source/SenseGlove/Public/SenseGlove/GameFramework/SGPawn.h @@ -35,6 +35,7 @@ #pragma once +#include "Containers/Array.h" #include "GameFramework/Pawn.h" #include "SenseGlove/Components/SGVirtualHandComponent.h" #include "Templates/UniquePtr.h" @@ -175,6 +176,12 @@ private: UPROPERTY(Transient) FSGTouchState RightHandTouchState; + UPROPERTY(Transient) + TArray LeftHandOverlappedActors; + + UPROPERTY(Transient) + TArray RightHandOverlappedActors; + public: FORCEINLINE UCameraComponent* GetCamera() const {