-2

I need to find the world position/orientation of a VR headset (both Oculus and HTC) and then set an actor to be at the same location. Getting the camera's position seems to not work well in VR, so I thought this code was the proper way to do this:

FQuat DeviceRotation;
FVector DevicePosition;

GEngine->XRSystem->GetCurrentPose(0, OUT DeviceRotation, OUT DevicePosition);
myActor->SetActorLocation(DevicePosition);
myActor->SetActorRotation(DeviceRotation);

However, the resulting coordinates are too low on the Z axis and the orientation doesn't properly match.

So what is the proper way to do this in C++? Do I need to factor in the player controller somehow?

UPDATE:

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 correct, as there seems to be a rotation offset. I have the camera set to lock on to the HMD, so I'm not sure what else I'm missing here.

Andrew Langley
  • 41
  • 1
  • 10

1 Answers1

0

Figured it out. GetCurrentPose only gives an offset position for the HMD, so it needs to be added to the player pawn's transform like so:

        FQuat hmdRotation;
        FVector hmdLocationOffset;
        GEngine->XRSystem->GetCurrentPose(IXRTrackingSystem::HMDDeviceId, hmdRotation, hmdLocationOffset);

        APawn * playerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);

        myActor->SetActorLocation(playerPawn->GetTransform().TransformPosition(hmdLocationOffset));
        myActor->SetActorRotation(hmdRotation);
Andrew Langley
  • 41
  • 1
  • 10