PavolHejný

Intersecting mesh with BABYLON.Ray from VR Controller

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;
        }
    }

});

 

 

 

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();
controller.deviceRotationQuaternion.toRotationMatrix(m);
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.