Ray/RayHelper incorrectly registering hit

Hi all! Having an issue with Ray and RayHelper incorrectly registering a hit.

Please find the link to the Playground here: https://www.babylonjs-playground.com/#3EDS3A#234

Please click inside the Playground scene to activate pointer lock.
WASD: Movement
Space: Jump
Click: Fire bullet

When a bullet mesh is created in front of your player, a Ray and RayHelper are attached to it. The first 1 or 2 pickWithRay's register false positive hits on the red slide (even if your player is far away from the slide or not even facing it). Subsequent pickWithRay's correctly and no longer register these hits against the slide.

For debugging, the bullets are stationary, and the code below is for debugging:

const pick = scene.pickWithRay(ray);
if (pick.hit) {

I wonder if I’m missing something like computeWorldMatrix

Thank you for your time and help! :smiley:

I can’t wait to see what you come up with after all of those experiments :slight_smile:

So - here is the issue - check the log here - https://www.babylonjs-playground.com/#3EDS3A#235 , and check what I am doing in line 430. The thing is - the console is updated with new values after the old values were used for the calculation (as it is only a reference, and not a new object). It seems like the ray helper requires a frame to get the values correctly, but the playground is a bit too complex for me to dive in and understand why this happens. If you want to simplify it, we can investigate why this happens.

1 Like

Thank you so much for your time and help, @RaananW :slight_smile:

Please find a simpler Playground reproducing the issue linked here: https://www.babylonjs-playground.com/#R7Y6RK#6

Using a setTimeout after 1 second, a bullet is created with a cyan Ray and RayHelper. The console.log usually registers 1 (or usually 2) hits against the pillar in scene.registerBeforeRender before no longer registering hits.


So - it is not a bug nor a feature :slight_smile:

Here is w roking example - https://www.babylonjs-playground.com/debug.html#R7Y6RK#8

The world matrix of the bullter is not yet calculated when the ray helper is triggered and uses its worldMatrix to calculate the ray’s origin (and direction). This is done to save a few CPU cycles, but can be solved by adding compute world matrix when attaching the mesh (on our side). I will add this call later, in the meantime you can simply update the bullet world matrix before creating the ray helper :slight_smile:


Thank you, @RaananW :smiley:

I also tried adding bullet.computeWorldMatrix(true) to the original Playground: https://www.babylonjs-playground.com/#3EDS3A#239

but am still running into the Ray's direction not matching between the first and subsequent calculations.

I’ll be looking further into this.

Have the original Playground working :stuck_out_tongue: https://www.babylonjs-playground.com/#3EDS3A#249

Added q.normalize() on Line 518. Without it, it gave the bullet’s rotationQuaternion very high xyzw values, which caused initial xyz values of the Ray's direction to be massive (as shown in above image).

Not sure if this is a new bug (Ray attached to mesh with unnormalized rotationQuaternion via RayHelper initially has incorrect direction xyz values but then corrects itself in subsequent frames/physics time steps).

Very thankful the Playground is now working. Thank you so much for your help, @RaananW!


An explanation of why this could be necessary from earlier thread


Thank you so much, @JohnK :smiley: