# Ray through particle.position of a SPS digested mesh is not in the right place

**URL:** <https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281>\
**Category:** Questions\
**Created:** [August 26, 2019, 8:17am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281 "2019-08-26T08:17:29Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![frederic](https://avatars.discourse-cdn.com/v4/letter/f/4491bb/32.png) [@frederic](https://forum.babylonjs.com/u/frederic)\
**Post date:** [August 26, 2019, 8:17am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/1 "2019-08-26T08:17:29Z")

</div>

Hi All,

I have a SPS from a digested mesh. I try to detect which particle is in the path between a point and a box. But when I draw the ray with rayHelper, the rays are not passing through the particle but close to it. Below is a playground to illustrate.

[https://playground.babylonjs.com/#R56QQU#1](https://playground.babylonjs.com/#R56QQU#1)

As you can see, some rays are missing the SPS mesh.

And when I try do detect ray intersection with one box, I have some rays tagged as intersecting the box which are missing it when I draw them as in the playground below.

[https://playground.babylonjs.com/#KWUDUG](https://playground.babylonjs.com/#KWUDUG)

Am I missing something?

Thank you for you help.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 9:16am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/2 "2019-08-26T09:16:41Z")

</div>

When you digest a mesh, the particles are created by isolating one or more successive facets of this mesh (facetNb = 2 in your example) and sticking them together. Then these particles are located in the SPS local space to the barycenter of the vertices composing them.

In your example, I suppose that the first 10 particles of the digested SPS are probably a mix of sectors from the cylinder cap and probably some facets from the cylinder side. This could a reason why the barycenter is not where you expect it. Not sure…  
Actually, if you don’t know how the digested mesh geometry is designed, you’ll probably expect something different from the digesting process will product.  
Say, if you still select 10 particles, but not from the first one (the cap), from the 30th to the 40th, it looks like what you maybe expect, doesn’t it ?  
[https://playground.babylonjs.com/#R56QQU#2](https://playground.babylonjs.com/#R56QQU#2)

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 9:21am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/3 "2019-08-26T09:21:43Z")

</div>

That said, it seems still buggy : [https://playground.babylonjs.com/#R56QQU#3](https://playground.babylonjs.com/#R56QQU#3)  
Something went probably wrong either in the barycenter computation or in the BBox computation when fixing the last bug [Intersection between a SPS from au digested mesh and a mesh](https://forum.babylonjs.com/t/intersection-between-a-sps-from-au-digested-mesh-and-a-mesh/5210/19)

Investigating …

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [August 26, 2019, 10:02am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/4 "2019-08-26T10:02:38Z")

</div>

Here is a slightly simplified PG [Babylon.js Playground](https://playground.babylonjs.com/#GE578Z) where the cylinder is yellow to see the contrast with the ray helper lines. As @jerome says you can see the difference on changing facetNb by playing with line 29

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 10:14am UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/5 "2019-08-26T10:14:05Z")

</div>

not sure it’s a bug or a misunderstanding of the digested geometry : [https://playground.babylonjs.com/#GE578Z#1](https://playground.babylonjs.com/#GE578Z#1)

Seems to work here

[EDIT] [https://playground.babylonjs.com/#GE578Z#2](https://playground.babylonjs.com/#GE578Z#2)  
A ray every 10 particles of 8 facets  
some rays intersect no particle indeed

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 12:38pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/6 "2019-08-26T12:38:41Z")

</div>

mmmh…  
@JohnK or @sebavan , as I can’t dev/fix the BJS code yet, could you please add the line :  
`
barycenter.copyFromFloats(0, 0, 0);
`  
just after this comment [Babylon.js/solidParticleSystem.ts at master · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/blob/master/src/Particles/solidParticleSystem.ts#L279)

I think the variable vector _barycenter_ is not reset to zero from a particle to the next within the loop. Not sure (abstract debugging with no dev platform 😊 ) …

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [August 26, 2019, 12:40pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/7 "2019-08-26T12:40:27Z")

</div>

Have finally (I think) worked out what the option facetNb does. It groups the facets of the mesh to form particles with facetNb facets as far as possible. When the number of facets in the mesh is not divisible by facetNb then some groups will have lower number of facets to make up the required total number. This can be seen in [https://www.babylonjs-playground.com/#HDHQN#58](https://www.babylonjs-playground.com/#HDHQN#58). The green mesh shows the facets of the original mesh and the red one the facets of the particles of the digested mesh.

In this PG example with facetNb = 3 it forms particles when its grouped facets have spaces in them. It might be possible that in some cases the barycenter falls within this space and so a ray will not hit the particle. This is only a perhaps at this stage, more investigation necessary.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 12:47pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/8 "2019-08-26T12:47:02Z")

</div>

This is a correct argument. Unless we know exactly the mesh geometry, we can’t easily predict by using _facetNb_ how some of the particles will be built : some barycenters can bet set off the particle surfaces and some facets may also be grouped even if they aren’t linked together on the mesh surface… just because they are successive in the mesh indice array.

_digest()_ can’t know the geometry neither the user expectations. It just does the best it can by following the mesh indice array order and by trying to build the highest number of particles with the required _facetNb_ value per particle.

That said, I think that the barycenter might not be correctly reset in the loop anyway.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [August 26, 2019, 1:02pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/9 "2019-08-26T13:02:39Z")

</div>

Agree that

```auto
barycenter.copyFromFloats(0, 0, 0);

```

is needed in the loop to reset its value to 0. Will let @sebavan do it for same reason as before.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [August 26, 2019, 1:12pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/10 "2019-08-26T13:12:00Z")

</div>

PR is here: [Fix MRTT Parallel Shader Compile + SPS Barycenter by sebavan · Pull Request #6774 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/6774)

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 1:26pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/11 "2019-08-26T13:26:19Z")

</div>

does it fix the PG ? I mean : are there still rays passing outside the surface in this PG ?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [August 26, 2019, 1:27pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/12 "2019-08-26T13:27:51Z")

</div>

Not sure how to fully test ☹ Could you describe me the full procedure ?

Sorry for being dumb on this one, just want to be sure I look at the right thing.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 1:29pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/13 "2019-08-26T13:29:14Z")

</div>

oops I forgot the link : [https://playground.babylonjs.com/#GE578Z#2](https://playground.babylonjs.com/#GE578Z#2)

Just rotate the cam around the cylinder and check if some rays still pass outside the cylinder without intersecting it, please

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [August 26, 2019, 1:48pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/14 "2019-08-26T13:48:32Z")

</div>

Some intersect on the opposite side which is normal from what I understand but they all intersect with a red spot

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 2:01pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/15 "2019-08-26T14:01:44Z")

</div>

yep, the “bug” was that some lines didn’t intersect at all  
so : fixed

😊

---

<div class="post-metadata">

**Author:** ![frederic](https://avatars.discourse-cdn.com/v4/letter/f/4491bb/32.png) [@frederic](https://forum.babylonjs.com/u/frederic)\
**Post date:** [August 26, 2019, 2:56pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/16 "2019-08-26T14:56:15Z")

</div>

@jerome there are still rays passing outside the cylinder in your PG ([https://playground.babylonjs.com/#GE578Z#2](https://playground.babylonjs.com/#GE578Z#2))  
Here is a printSreen: ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/2/2492972fa8caea23f5ed0a54729c7e364669cd81.png)

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [August 26, 2019, 2:57pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/17 "2019-08-26T14:57:37Z")

</div>

Just wait for @sebavan 's fix to be published in the next nightly build. This should work now (tomorrow actually).

---

<div class="post-metadata">

**Author:** ![frederic](https://avatars.discourse-cdn.com/v4/letter/f/4491bb/32.png) [@frederic](https://forum.babylonjs.com/u/frederic)\
**Post date:** [August 26, 2019, 3:07pm UTC](https://forum.babylonjs.com/t/ray-through-particle-position-of-a-sps-digested-mesh-is-not-in-the-right-place/5281/18 "2019-08-26T15:07:51Z")

</div>

OK thank you very much,all of you.  
I can’t believe in your responsiveness.
