The code example for test raycast query is not working for collideWith == 3

Hi Team Babylon,

I was trying the raycast query playground code and found the collideWith == 3 option is not working as expected. Or is it that I am mistaking the feature.

Attached an image for reference to show the issue.

Hi, can you please provide actual example in Playground instead of screenshot? Thanks.

Sure, it’s in the official list of examples in the babylon website under the name Raycast Filtering Example.

I modified the box transparency to check whether the intersecting sphere are inside the box and attached the image.

cc @Cedric and @eoin

This is actually working as expected. The filter checks are performed using a bit mask:

  • The blue box has a membership of “1”, or, in binary, “0001”
  • The green box has a membership of “2”, or, in binary, “0010”
  • When the ray tests for “3” (binary “0011”), it should hit both, and it happens to hit the blue one first.

You can learn some more about bit masks here: Mask (computing) - Wikipedia

1 Like

@eoin Okay, thanks for answering, so is there a way we can get the list of object a ray hits in it’s length sorted with the hit distance.

Oh sorry, I missed your follow-up question.
By default, the raycast query interface just returns the closest hit.

However, there is support for collecting multiple hits; take a look at how havokPlugin.ts does raycasts.

First, the plugin creates a “query collector” with a capacity of 1: Babylon.js/packages/dev/core/src/Physics/v2/Plugins/havokPlugin.ts at master · BabylonJS/Babylon.js · GitHub - you could make yourself a collector with a higher capacity, say 100:

this._queryCollector = this._hknp.HP_QueryCollector_Create(100)[1];

Then, look at the raycast function (Babylon.js/packages/dev/core/src/Physics/v2/Plugins/havokPlugin.ts at master · BabylonJS/Babylon.js · GitHub) it currently checks if the collector has any hits, and populates from the 0th hit:

if (this._hknp.HP_QueryCollector_GetNumHits(this._queryCollector)[1] > 0) {
            const [, hitData] = this._hknp.HP_QueryCollector_GetCastRayResult(this._queryCollector, 0)[1];

So you could write a for loop to iterate over the number of hits to access each one from your expanded collector.