WebXR Controller collision detection with Havok

Hi,

I’m using Babylon JS for the first time to code a VR project where I intend to make a VR drum kit and I’m having trouble about collision detection with my controllers (I use an Oculus Quest 2 and the Chrome WebXR API Emulator for debugging).

Here’s a playground leading to the current state of my problem (I’ve removed everything that was not related to the issue) :

I’m using Havok as my physics engine as I will be needing precise collision detection (the default bounding boxes on cylinders are not precise enough for my project, even when I activate the option to get accurate detection the bounding box seems to remain a square or a circle) and as it is already used in the global project that my part will fit in.

The idea is to make another mesh that will be a drumstick. It can be an object that is first lying on the floor that I pick up but I thought I’d rather just parent it to my controller grip and use the said mesh to detect collision with other objects.

To start, I’m just trying tu see if I can push a ball with this.
I’ve tried numerous combinations of settings but it seems I’m getting something wrong.
When I set the Motion type of my controller aggregate to Kinematic and give it a mass of 0 (as I thought I should do), it no longers detects collisions with the sphere. If I give it a mass of 1, it falls on the ground (as I supposed it would), but collisions with the sphere are detected as soon as controllers are detected (as if it sent wrong inputs while it’s setting up, thus the fact that I wait for all the controller observables to be generated before attempting any modification of them but it’s still not enough) despite the fact that the sphere is far from me and it shouldn’t be the case. It collides up to 24 times frequently, just on start (and I only count COLLISION_STARTED EVENTS). The sphere then goes flying in the air due to the collision but I can’t reproduce this on purpose with the meshes snapped to my controllers as intended. In both cases it’s an issue. Often, I lost sight of the cubes too, probably because they fell under the ground for some reason.

I’ve read on this forum about similar issues, like this one : How to correctly position a mesh to the side of a webXR controller
But there’s no collision detection aspect in it. The solution given for it improved my code a bit (in my opinion at least).
It consists in these two lines :
controllerMesh.position = BABYLON.Vector3.ZeroReadOnly;
controllerMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
added after parenting the mesh to the controller. Now, my cubes are always where they are supposed to (on the ground if mass > 0 or initially snapped to the controllers if mass = 0), but I still can’t hit the sphere with it. Furthermore, now both controllers are snapped only to one cube, one as it should be and the other is snapped to the same but with inverted controls (try to move both controllers on the playground and you’ll see it). It’s been a couple days and I can’t figure out a solution so I’d really appreciate a bit of help here.

I hope my explanations are clear and I didn’t miss an already existing answer, even if I suspect that is probably the case.
Thank you in advance and don’t hesitate to ask more details if necessary.

1 Like

i’d be happy to get @Cedric’s feedback on the physics part here. Mainly, how do I create a mesh that can be used as a collider to “push” other elements that is attached to a hand/controller (or even a mouse pointer).

1 Like

It should work with kinematic motion. Let me try to make a quick PG.

1 Like

Use ANIMATED motion with TELEPORT prestep values to keep control of the mesh while it can interact with other physics bodies.

3 Likes

Thank you very much it was indeed the issue !
It works perfectly now even in VR.

For those wondering, as I had to go look to understand why it worked, there’s a tutorial page about this in the documentation (that I should’ve read more carefully).
The playground on the page shows the difference well :
https://doc.babylonjs.com/features/featuresDeepDive/physics/prestep/

1 Like