From aec016fb1d4e58b8e68cc6b43e4613a914a8f754 Mon Sep 17 00:00:00 2001 From: Mamadou Babaei Date: Wed, 12 Jun 2024 18:33:49 +0200 Subject: [PATCH] fix the how we determine the motion source inside the GetControllerOrientationAndPosition and GetControllerTrackingStatus for ue versions earlier than 5.3 --- .../Private/SGTracking/SGXRTracker.cpp | 156 ++++++++---------- 1 file changed, 70 insertions(+), 86 deletions(-) diff --git a/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp b/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp index bc7d88d9..e1e7b2f4 100644 --- a/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp +++ b/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp @@ -1117,96 +1117,82 @@ bool FSGXRTracker::GetControllerOrientationAndPosition(const int32 ControllerInd return false; } + if (ControllerIndex != Pimpl->DeviceIndex) + { + return false; + } + bool bTracked = false; - if (ControllerIndex == Pimpl->DeviceIndex) - { - FTransform ControllerTransform{FTransform::Identity}; - - const bool bFallbackToHandTrackingIfNoGloveDetected = FImpl::ShouldFallbackToHandTrackingIfNoGloveDetected(); - if (bFallbackToHandTrackingIfNoGloveDetected) - { -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 - const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)}; - if (KeyPointInfoPtr) -#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - if (GetControllerTrackingStatus(ControllerIndex, DeviceHand) != ETrackingStatus::NotTracked) -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - { -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 - const EHandKeypoint KeyPoint = KeyPointInfoPtr->Key; - const bool bIsLeft = KeyPointInfoPtr->Value; -#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - const bool bIsLeft = DeviceHand == EControllerHand::Left; -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - if (bIsLeft) - { -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 - ControllerTransform = GetLeftHandState().GetTransform(KeyPoint); -#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - ControllerTransform = GetLeftHandState().GetTransform(EHandKeypoint::Wrist); -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - bTracked = !!GetLeftHandState().bReceivedJointPoses; - } - else - { -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 - ControllerTransform = GetRightHandState().GetTransform(KeyPoint); -#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - ControllerTransform = GetRightHandState().GetTransform(EHandKeypoint::Wrist); -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - bTracked = !!GetRightHandState().bReceivedJointPoses; - } - } - } - - if (bTracked) - { - OutPosition = ControllerTransform.GetLocation(); - OutOrientation = ControllerTransform.GetRotation().Rotator(); - } - else - { - static const FName OpenXRName{TEXT("OpenXR")}; - - static constexpr int32 CurrentPlayerControllerIndex = 0; + FTransform ControllerTransform{FTransform::Identity}; #if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 - const FName& MotionSource{FImpl::GetDefaultMotionSourceName(DeviceHand)}; + const FName& MotionSource{StaticEnum()->GetNameByValue(static_cast(DeviceHand))}; #endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 */ - const TArray MotionControllers{ - IModularFeatures::Get().GetModularFeatureImplementations( - IMotionController::GetModularFeatureName()) - }; - - for (const IMotionController* MotionController: MotionControllers) + const bool bFallbackToHandTrackingIfNoGloveDetected = FImpl::ShouldFallbackToHandTrackingIfNoGloveDetected(); + if (bFallbackToHandTrackingIfNoGloveDetected) + { + const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)}; + if (KeyPointInfoPtr) + { + const EHandKeypoint KeyPoint = KeyPointInfoPtr->Key; + const bool bIsLeft = KeyPointInfoPtr->Value; + if (bIsLeft) { - if (!MotionController) - { - continue; - } + ControllerTransform = GetLeftHandState().GetTransform(KeyPoint); + bTracked = !!GetLeftHandState().bReceivedJointPoses; + } + else + { + ControllerTransform = GetRightHandState().GetTransform(KeyPoint); + bTracked = !!GetRightHandState().bReceivedJointPoses; + } + } + } - const FName DeviceTypeName{MotionController->GetMotionControllerDeviceTypeName()}; - if (DeviceTypeName != OpenXRName) - { - continue; - } + if (bTracked) + { + OutPosition = ControllerTransform.GetLocation(); + OutOrientation = ControllerTransform.GetRotation().Rotator(); + } + else + { + static const FName OpenXRName{TEXT("OpenXR")}; - const ETrackingStatus TrackingStatus = MotionController->GetControllerTrackingStatus( - CurrentPlayerControllerIndex, MotionSource); - if (TrackingStatus == ETrackingStatus::NotTracked) - { - continue; - } + static constexpr int32 CurrentPlayerControllerIndex = 0; - const bool bGotTransform = MotionController->GetControllerOrientationAndPosition( - ControllerIndex, MotionSource, OutOrientation, OutPosition, WorldToMetersScale); - if (bGotTransform) - { - bTracked = true; - break; - } + const TArray MotionControllers{ + IModularFeatures::Get().GetModularFeatureImplementations( + IMotionController::GetModularFeatureName()) + }; + + for (const IMotionController* MotionController: MotionControllers) + { + if (!MotionController) + { + continue; + } + + const FName DeviceTypeName{MotionController->GetMotionControllerDeviceTypeName()}; + if (DeviceTypeName != OpenXRName) + { + continue; + } + + const ETrackingStatus TrackingStatus = MotionController->GetControllerTrackingStatus( + CurrentPlayerControllerIndex, MotionSource); + if (TrackingStatus == ETrackingStatus::NotTracked) + { + continue; + } + + const bool bGotTransform = MotionController->GetControllerOrientationAndPosition( + ControllerIndex, MotionSource, OutOrientation, OutPosition, WorldToMetersScale); + if (bGotTransform) + { + bTracked = true; + break; } } } @@ -1226,14 +1212,14 @@ ETrackingStatus FSGXRTracker::GetControllerTrackingStatus(const int32 Controller return ETrackingStatus::NotTracked; } -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 +#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 + const FName& MotionSource{StaticEnum()->GetNameByValue(static_cast(DeviceHand))}; +#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION <= 2 */ + const FImpl::FSGMotionSourceInfo* KeyPointInfoPtr{Pimpl->MotionSourceToKeypointMap.Find(MotionSource)}; if (KeyPointInfoPtr) { const bool bIsLeft = KeyPointInfoPtr->Value; -#else /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ - const bool bIsLeft = DeviceHand == EControllerHand::Left; -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ if ((bIsLeft && FImpl::IsLeftGloveConnected()) || (!bIsLeft && FImpl::IsRightGloveConnected())) { @@ -1246,9 +1232,7 @@ ETrackingStatus FSGXRTracker::GetControllerTrackingStatus(const int32 Controller const FSGXRHandState& HandState{bIsLeft ? GetLeftHandState() : GetRightHandState()}; return !!HandState.bReceivedJointPoses ? ETrackingStatus::Tracked : ETrackingStatus::NotTracked; } -#if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 } -#endif /* ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 3 */ return ETrackingStatus::NotTracked; }