# SolidParticleSystem and rotation

**URL:** <https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850>\
**Category:** Questions\
**Created:** [November 7, 2019, 11:16am UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850 "2019-11-07T11:16:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 7, 2019, 11:16am UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/1 "2019-11-07T11:16:24Z")

</div>

Hi folks,  
I’m currently facing a problem regarding SPS and rotation. In my game I’m using as an explosion effect a SPS which has the mesh to eplode digested. The code is based on the PG provided by the SPS Docs [“Digest a Mesh”](https://doc.babylonjs.com/how_to/solid_particles#digest-a-mesh). It works as expected as long as the digested mesh istn’t rotated. For better unterstanding see this PG [Babylon.js Playground](https://www.babylonjs-playground.com/#BF4A8E) (copy of the one from the Docs). As you can see, the particles are pulled according to the gravity towards the ground (`-y`). If the digested mesh is now rotated: [https://www.babylonjs-playground.com/#BF4A8E#1](https://www.babylonjs-playground.com/#BF4A8E#1) (see line 42), the particles are pulled against `+x`, which is not what I expect. I suspect it has something to do with wordlspace/localspace but I’m out of luck here because my math-skills are pretty low.  
So what need to be changed that the particles are always pulled against `-y` of worldspace regardless of the digested mesh’s rotation? Any help appreciated 🙂

---

<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:** [November 7, 2019, 1:10pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/2 "2019-11-07T13:10:24Z")

</div>

Would rotating the model work for you? Note you need to bake the transformation into the model.

[https://www.babylonjs-playground.com/#BF4A8E#2](https://www.babylonjs-playground.com/#BF4A8E#2)

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 7, 2019, 2:47pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/3 "2019-11-07T14:47:19Z")

</div>

Thanks @JohnK, that’s basically how it should work!  
However I’m having problems getting this integrated in the context of my game. I’ll first have to dig deeper and come probably back with some more questions 🙂

---

<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:** [November 7, 2019, 8:04pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/4 "2019-11-07T20:04:15Z")

</div>

The particle positions and rotations are simply always expressed within the SPS local space.

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 8, 2019, 8:18am UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/5 "2019-11-08T08:18:23Z")

</div>

Is this also the case for the ‘normal’ particle system?

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 11, 2019, 4:10pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/6 "2019-11-11T16:10:50Z")

</div>

@JohnK: Now I had the time to play more with the PG and modified it so it corresponds more to the actual in-game usecase.  
As already mentioned the SPS is used as an explosion effect. This means that the mesh to explode can have arbitrary rotation/position at the time of explosion. To overcome the problem with gravitation always pulling to `-y` `mesh.bakeCurrentTransformIntoVertices()` is used which works fine so far. The problem I’m facing now is performance. Because most recent rotation/position of the mesh to explode is needed at the time of explosion `bakeCurrentTransformIntoVertices/sps.digest/sps.buildMesh` must be called at this point too, which is quite CPU intense. This leads to hickups in rendering on low end machines. See the updated PG here which does the expensive calculations at the time the moving/rotating cone is clicked: [https://www.babylonjs-playground.com/#BF4A8E#12](https://www.babylonjs-playground.com/#BF4A8E#12)  
Ideally `sps.digest/sps.buildMesh` should be done upon mesh (cone) creation, when explosion is finally triggered the most recent rotation/position should be calculated directly in the `updateParticle()` function.  
I hope this makes sense, any help appreciated.

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 19, 2019, 8:06pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/7 "2019-11-19T20:06:58Z")

</div>

I’m still searching a way of having the gravitation calculated towards world -y directly within `sps.updateParticle()`. As I mentioned earlier the costly digesting/building of the `sps.mesh` should be done in advance. See this playground [https://www.babylonjs-playground.com/#BF4A8E#17](https://www.babylonjs-playground.com/#BF4A8E#17) which sets `sps.mesh.position/sps.mesh.rotation` (line 124-125) just before the actual explosion-animation is started. I think there must be a way to use this rotation for transforming the gravity vector which is added to `p.velocity` (line 97) to actually overcome the problem of local-space which has the rotation as it was at the very beginning whnen digesting the mesh.  
It would be great if someone could have a look how such a transformation could actually been done. Maybe @jerome has an idea 🙂 Thanks in advance!

---

<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:** [November 19, 2019, 8:45pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/8 "2019-11-19T20:45:53Z")

</div>

yes you’re right : transforming the gravity vector is the simplest way to do.  
gravity is (0, -1, 0) in the SPS local space  
the SPS is rotated for (a, b, c)

So compute the rotation matrix for this rotation, invert it and apply this inverted rotation matrix to the gravity vector in the local space, you’ll get the gravity from the world space in the local space.

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 20, 2019, 4:08pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/9 "2019-11-20T16:08:52Z")

</div>

Thanks a lot @jerome that sounds like a plan. So I’m going to activate my non existing math-skills and give it a try 😃

---

<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:** [November 20, 2019, 5:03pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/10 "2019-11-20T17:03:29Z")

</div>

I don’t have an already set PG here, but try this :  
given vector gravity(0, -1, 0) and the mesh rotation(x, y, z)

get the rotation matrix from these euler angles , there are methods like fromEulerToQuaternion and RotationMatrixFromQuaternion or something close to this  
given this rotation matrix RotMatrix built from the mesh rotation, compute the inverted matrix : var inverted = rotMatrix.invert()

then apply this inverted matrix to the gravity vector : TransformNormals(gravity, inverted)  
you’ll get then the world gravity vector (0, -1, 0) expressed in the SPS local space as a result

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 20, 2019, 5:17pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/11 "2019-11-20T17:17:33Z")

</div>

At the time of your writing I manged to come up (or better stolen from different other PGs 🙂) with something that looks exactly as what you’ve suggested: [https://www.babylonjs-playground.com/#BF4A8E#19](https://www.babylonjs-playground.com/#BF4A8E#19) (starting at line 129)  
Thank you so much!

---

<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:** [November 20, 2019, 5:41pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/12 "2019-11-20T17:41:59Z")

</div>

Great.  
If you want to optimize then, use now the same methods suffixed with xxxxxxToRef().  
So you can use only one matrix, one inverted matrix, one quaternion in your code instead of creating new ones each call. Just create all these objects once outside the render loop and reuse them at will.

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 20, 2019, 9:28pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/13 "2019-11-20T21:28:40Z")

</div>

Thanks for the sugestions! Here you can see the sps-explosion in action:  
[![ezgif.com-crop-12c6e424fb7bae8da.gif](https://s5.gifyu.com/images/ezgif.com-crop-12c6e424fb7bae8da.gif)](https://gifyu.com/image/vlKB)

---

<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:** [November 21, 2019, 7:41am UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/14 "2019-11-21T07:41:10Z")

</div>

waoow superb 🙂

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 21, 2019, 3:20pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/15 "2019-11-21T15:20:20Z")

</div>

Looks really good!

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [November 21, 2019, 3:23pm UTC](https://forum.babylonjs.com/t/solidparticlesystem-and-rotation/6850/16 "2019-11-21T15:23:38Z")

</div>

Thank you guys! Really appreciate your feedback 🙂
