How can I detect the Solid particles with the aid of the raycast?


as an example I have 4 boxes which build with the solid particle systems , I want to get the thickness of the boxes with the help of the raycast,

here is part of my code:

    let SPS = new BABYLON.SolidParticleSystem("SPS", scene, {
        isPickable: true,
        updatable: true,
        enableDepthSort: true,
        particleIntersection: true,


              const ray1 = new BABYLON.Ray(
                    new BABYLON.Vector3(impact.position.x, 3000, impact.position.z),
                    new BABYLON.Vector3(0, -1, 0),
                const ray2 = new BABYLON.Ray(
                    new BABYLON.Vector3(impact.position.x, -3000, impact.position.z),
                    new BABYLON.Vector3(0, 1, 0),

                for (let i = 1; i < SPS.particles.length; i++) {
                    bTop[i] = ray1.intersectsMesh(SPS.particles[i]);
                    bBottom[i] = ray2.intersectsMesh(SPS.particles[i]);

                    if (bTop[i].hit && bBottom[i].hit) {
                        let d = BABYLON.Vector3.Distance(
                    } else {
                        console.log("dd", "no intersection");

but when I run my code I got an error which said that t.getWorldMatrix is not a function , how can I solve this problem?

here is my PG:

the function ray . intersectMesh can only work with Meshes and not with Solid Particle.

Maybe our friend @jerome (the amazing daddy of the SPS) has an idea ?

1 Like

Actually it should work (unless the code has been modified since … ages).
Everything is explained here : Physics and Solid Particles | Babylon.js Documentation

[EDIT] I just can’t remember if the Ray-SolidParticle intersection check was implemented, but I know the Mesh or SolidParticle vs SolidParticle intersection used to work quite well :wink:

You’re right @sebavan : the Ray should not work with SolidParticles…


Yup I have found the mesh to SPS intersect and I know picking is ok too but I can t find the ray intersect method, and as you are the best @jerome I thought I would cry for help :wink:


Thank you so much @jerome , so i cannot use SPS with RAYCAST?
if not, is it possible to use thin instance with raycast? because I have more than 10,000 boxes and I want to use the raycast to get the thickness of each cubes, do you have any recommend?

@sebavan, I answered to fast and you were right : only the mesh/particle intersection was implemented
@Arash_Bagheri the particle va raycast intersection is not implemented, so not available.
That said, I think it would be quite easy to copy the mesh-raycast intersection code and to adapt it to the solid particles… as solid particles have almost every geometry properties that the meshes own.
Maybe it would be exactly the same code actually.

Unfortunately, I don’t have any time to chech this and to add this feature in the SPS :frowning:

@Arash_Bagheri you could do smthg like this:


Thank you so much @sebavan :pray: :pray: