How to get the position and rotation of XR controllers

Hi,

I am using the scene.createDefaultXRExperienceAsync API and testing on an Oculus Quest 2. How can I access the controllers pose?

Thanks,
Mark

1 Like

Pinging @RaananW (just FYi he is on vacation so please bear with us ;))

2 Likes

I have code for that. I have stopped for the day though. I got my info from :

You need to get your controllers through an onControllerAddedObservable. This is because they do not show up till you enter XR.

Then I just started looking around at what I could get using my IDE intellisense.

1 Like

Ok, here is the code to get the controllers. You can get the position / rotationQuaternion in a scene beforerender(), if you need it every frame. Hands are also controllers. I have a different process for them, so there is code to filter them out.

// somewhere in your code
let leftController : BABYLON.AbstractMesh;
let rightController : BABYLON.AbstractMesh;

BABYLON.WebXRDefaultExperience.CreateAsync(scene, options).then((defaultExperience) => {  //
     defaultExperience.input.onControllerAddedObservable.add((controller : BABYLON.WebXRInputSource) => {
         // hands are controllers to; do not want to go do this code; when it is a hand
         const isHand = controller.inputSource.hand;
         if (isHand) return;

         controller.onMotionControllerInitObservable.add((motionController : BABYLON.WebXRAbstractMotionController) =>{
             const isLeft = motionController.handedness === 'left';
             controller.onMeshLoadedObservable.add((mesh : BABYLON.AbstractMesh) => {
                 if (isLeft) leftController = mesh;
                 else rightController = mesh;
             });
         });
     });
});
2 Likes

Thank you all for your prompt replies!