Intersecting mesh with BABYLON.Ray from VR Controller

Recommended Posts


I am using Babylon.js with HTC Vive and WebVR and I want to pick a point on the mesh (this.wallMesh) with controller ray. I don't want to use default VRHelper interactions.

I expected that this code should work for it but it seems that there is wrong rotation because the rotation of the device behaves totally weird.

Do you have some tips on how to fix it? Thank you a lot.

const pickMesh = BABYLON.Mesh.CreateSphere(

const ray = new BABYLON.Ray(

this.scene.registerAfterRender(() => {

    ray.origin = controller.devicePosition;
    ray.direction = controller.deviceRotationQuaternion.toEulerAngles();

    const hit = this.wallMesh!
        .pickWithRay(ray, (mesh) => mesh === this.wallMesh);

    if (hit) {

        if (hit.pickedPoint) {
            pickMesh.position = hit.pickedPoint;





Share this post

Link to post
Share on other sites

Hey @PavolHejný, from a quick look
ray.direction = controller.deviceRotationQuaternion.toEulerAngles();

looks incorrect, instead to get a direction for your ray you can rotate a forward vector by your rotation quaternion.

var m = new BABYLON.Matrix();
var direction = Vector3.TransformCoordinates(BABYLON.Vector3.Forward(), m);

Let me know if that doesn't work.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.