Download

Best way to get HMD world position in C++?

I’m trying to set an actor’s world position and orientation to match an Oculus or HTC Vive VR headset. I was using GEngine->XRSystem->GetCurrentPose with device index 0, but the Z position is too low and the orientation has some kind of offset. I also tried getting the location/orientation of the camera through the camera manager, but it doesn’t seem to reflect the X and Y positions correctly.

Can someone tell me what’s the best way to do this outside of using Blueprints?

Looking more into this, it seems you have to add the HMD position rotated by the player pawn rotation:


FQuat DeviceRotation;
FVector DevicePosition;
FVector FinalPosition;

GEngine->XRSystem->GetCurrentPose(IXRTrackingSystem::HMDDeviceId, DeviceRotation, DevicePosition);

APlayerController *PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
FinalPosition = PlayerController->GetPawn()->GetActorRotation().RotateVector(DevicePosition) + PlayerController->PlayerCameraManager->GetCameraLocation();

myActor->SetActorLocation(FinalPosition);
myActor->SetActorRotation(DeviceRotation);

However, the results are still not quite correct, as there is some kind of rotation offset. I have the camera set to lock to the HMD, so I’m not sure what else I’m missing here.

Assuming you have a reference to the camera, just get the component’s location and rotation.

This is incorrect, since the camera’s location values don’t factor in the HMD’s tracked position offset.