简体   繁体   English

使用 Unreal C++ 获取 VR 头显的世界位置/方向

[英]Getting the world position/orientation of a VR headset using Unreal C++

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.我需要找到 VR 耳机(Oculus 和 HTC)的世界位置/方向,然后将演员设置在同一位置。 Getting the camera's position seems to not work well in VR, so I thought this code was the proper way to do this:在 VR 中获取相机的位置似乎效果不佳,因此我认为此代码是执行此操作的正确方法:

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.但是,生成的坐标在 Z 轴上太低并且方向不正确匹配。

So what is the proper way to do this in C++?那么在 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:更深入地研究这一点,似乎您必须添加由玩家 pawn 旋转旋转的 HMD 位置:

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.我已将相机设置为锁定 HMD,所以我不确定这里还缺少什么。

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: GetCurrentPose 只提供 HMD 的偏移位置,因此需要将其添加到玩家 pawn 的变换中,如下所示:

        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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM