Jump to content

Intersecting mesh with BABYLON.Ray from VR Controller


PavolHejný
 Share

Recommended Posts

Hi,

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(
    'pickMesh',
    5,
    0.1,
    this.scene,
);

const ray = new BABYLON.Ray(
    BABYLON.Vector3.Zero(),
    BABYLON.Vector3.One(),
    100,
);

this.scene.registerAfterRender(() => {

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

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

    if (hit) {
        console.log(hit.hit);

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

});

 

 

 

Link to comment
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();
controller.deviceRotationQuaternion.toRotationMatrix(m);
var direction = Vector3.TransformCoordinates(BABYLON.Vector3.Forward(), m);

Let me know if that doesn't work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...