Repeatedly query Quest right-hand controller's coordinates

I’m using Oculus Quest. I’m tring to query repeatedly for Oculus Touch right-hand controller’s coordinates. The frequency of the query is very high, something like 120 times per second. The coordinates are then assigned to a sphere, so that the sphere is always following the controller’s position.

I’ve tried this code:

xrHelper.input.onControllerAddedObservable.add((controller) => {
    controller.onMotionControllerInitObservable.add((motionController) => {
        if (motionController.handness === 'right') {
            controller.getWorldPointerRayToRef(controllerRay);
            sphere.position.x = controllerRay.origin.x;
            sphere.position.y = controllerRay.origin.y;
            sphere.position.z = controllerRay.origin.z;
        }
    });
});

But the coordinates are not updated. Am I missing something?

pinging @RaananW

You are adding a callback that will be executed when the controller is ready - which means once.
I am not sure why you want to query 120 times a second, but i can tell you that it is not really possible. You can technically query max 72 times a second, which is the max FPS of your oculus quest.

The best way to get the latest position of the controller is to run a code on each xr frame. the session manger has onXRFrameObservable, which will run every time an update is sent from the device.

1 Like

Thank you very much! Here’s the fixed code for future reference:

const xr = await scene.createDefaultXRExperienceAsync();
const controllerRay = new BABYLON.Ray(new BABYLON.Vector3(), new BABYLON.Vector3());

xr.input.onControllerAddedObservable.add((controller) => {
    controller.onMotionControllerInitObservable.add((motionController) => {
        if (motionController.handness === 'right') {
            const frameObserver = xr.baseExperience.sessionManager.onXRFrameObservable.add(() => {
                controller.getWorldPointerRayToRef(controllerRay, true);
                sphere.position.x = controllerRay.origin.x;
                sphere.position.y = controllerRay.origin.y;
                sphere.position.z = controllerRay.origin.z;
            });
        }
    });
});
1 Like