Spawn a sphere in the middle of the field of view having a 2d Value as an input in VR

Hi community,
I want to do something similar to a raycast (Like in this post Raycasting a 2d value in the world.C++ - C++ Gameplay Programming - Unreal Engine Forums) I want to spawn a sphere in the world having as input X and Y on a 2D plane :

The problem is that if I move my head, the sphere spawning is not relative to the headset and it does not work as intended. If I move my head to the right the sphere spawning (in the case I just want it in the center (0.5, 0.5) overshoots to the right. If I move my head to the left it overshoots to the left.

XGaze += Radius * (float)FMath::Cos( 2 * PI * (float)(CurrentCalibrationPoint - 1) / (CalibrationType2DPointsNumber - 1) );
YGaze+= Radius * (float)FMath::Sin( 2 * PI * (float)(CurrentCalibrationPoint - 1) / (CalibrationType2DPointsNumber - 1) + Offset);

I have tryied something like this:

if ( UPupilPlayerController->DeprojectScreenPositionToWorld(ViewportSize.X * XGaze , ViewportSize.Y * (1 - YGaze), WorldLocation, WorldDirection))
{
FRotator CameraRotation = Camera->GetComponentRotation();

const float TraceDistance = 200.0f; // Your desired trace distance (in UU - centimiters)
TraceStart = WorldLocation;
TraceEnd = WorldDirection * TraceDistance; //TODO FIGURE THIS ONE OUT
SphereComponent->SetRelativeRotation(CameraRotation);
SphereComponent->SetRelativeLocation(TraceEnd);
DrawDebugLine(GetWorld(), TraceStart, TraceEnd, FColor(238, 0, 238), false);

The problem is that as I move the VR Headset the sphere spawning is still dependent on the VR Orientation. Even though the sphereComponent is attached to the Camera.

How can I make it so I can always spawn a sphere in the middle of my field of view with
float XGaze = 0.5;
float YGaze = 0.5;
?!?

I’m sure I have to do something with the camera/controller/vr something rotation…

P.S. If I manage to not move my head away from the initial camera positioning the spheres spawns right in the center. If I move to the right the sphere spawns a little bit more to the right and if I move my head to the left the sphere spawns a little bit to the left.

Try calculating TraceEnd like this:



TraceEnd = WorldLocation + WorldDirection * TraceDistance;