# Stacked meshes are glitching down and into each other with Havok Physics

**URL:** <https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574>\
**Category:** Questions\
**Tags:** physics, havok, physicsshape\
**Created:** [November 5, 2024, 11:50pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574 "2024-11-05T23:50:29Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 5, 2024, 11:50pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/1 "2024-11-05T23:50:29Z")

</div>

Hi everybody ! I’m picking up Babylon.js after a long time off, so I’m a bit lost.  
I got a small 3D board game project where players simply stack pearls on vertical piles.  
I’m using models I made in Blender, and basic geometry for PhysicsShape.

But when I keep stacking pearls, they eventually start to glitch down into the ones below on the pile. It feels like it could be related to my assets being heavy because it happens less without textures.

I set up a basic playground here : [https://playground.babylonjs.com/#89YSX9#1](https://playground.babylonjs.com/#89YSX9#1)  
You have to click on the vertical piles to spawn pearls on top of them. When it’s getting a bit crowed some of them start to glitch and it looks like that :

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/7/f787f6722678e7d160239182619f781f31bcdfd0.jpeg)

Sometimes they even collapse into the one below until they overlap.

(on line 38 in the playground, you can pick which version of the asset to import, the textured version can need 15 sec or so to load)

Thanks to whoever will read this, I’ll take any help and advice. ^^  
I hope it’s not a too silly question, but I couldn’t find a similar case in my research.

---

<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 5, 2024, 11:59pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/2 "2024-11-05T23:59:25Z")

</div>

cc @eoin / @Cedric

---

<div class="post-metadata">

**Author:** ![HiGreg](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@HiGreg](https://forum.babylonjs.com/u/HiGreg)\
**Post date:** [November 6, 2024, 12:50am UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/3 "2024-11-06T00:50:50Z")

</div>

I think this is semi-related to [falling through ground](https://forum.babylonjs.com/t/issue-with-character-falling-through-ground-in-havok-physics/54423). But in your case, it’s the size and normal of the faces colliding.

Try using a Box-shaped physics body since that will totally be sufficient for your use case. Or, if the “hole” is necessary, try using CSG2 to create it.

Edit: another method might be to implement an invisible box at the bottom of each “pole” and create a linear constraint between it and each pearl LOCKED in X and Z. PhysicsBody in ground with 0 mass (or box to pearl constraint with collisions enabled) and each pearl non-zero mass and box physicsBody. Pole is NOT a physics body. With this setup, physics calculations are super simplified (being all boxes and linear constraints).

---

<div class="post-metadata">

**Author:** ![regna](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/regna/32/23915_2.png) [@regna](https://forum.babylonjs.com/u/regna)\
**Post date:** [November 6, 2024, 4:17am UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/4 "2024-11-06T04:17:20Z")

</div>

I added `deterministicLockstep` to the Engine using [@Evgeni\_Popov’s way of recreating Engine in the Playground](https://forum.babylonjs.com/t/havok-precision/52959/29): [https://playground.babylonjs.com/#89YSX9#2](https://playground.babylonjs.com/#89YSX9#2)

This seems to mostly solve the issue.

It’s still possible to have spheres glitch into each other by rapidly clicking the middle of any pole, but I think this is a separate issue that can be solved by not spawning bodies inside/overlapping one another.

---

<div class="post-metadata">

**Author:** ![ertugrulcetin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ertugrulcetin/32/33011_2.png) [@ertugrulcetin](https://forum.babylonjs.com/u/ertugrulcetin)\
**Post date:** [November 6, 2024, 7:21am UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/5 "2024-11-06T07:21:14Z")

</div>

I have the similar issue here [falling through ground](https://forum.babylonjs.com/t/issue-with-character-falling-through-ground-in-havok-physics/54423). Also in your PG, after a couple of pearls the FPS drops a lot and then glitching happens even more.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [November 6, 2024, 9:40am UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/6 "2024-11-06T09:40:37Z")

</div>

Using a mesh shape will be less performant that spheres, cylinder or converhull. I’m not surprised to see glitches and bad performances with many bodies.  
I would use a compound of more simple shapes instead.

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 6, 2024, 4:28pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/7 "2024-11-06T16:28:26Z")

</div>

Hi guys, thank you all for your replies !

@HiGreg  
Slider or linear constraint would be a neat fix, thanks for the tip ! I didn’t think of an invisible anchor. It would skip the slight pearl-pole collision, but I think it’s the best bet.

@regna  
Can you explain how the deterministic lock step affects the issue ? Because I tried your PG and swapped to the textured assest to try and see any change, and the pearls pop out of line before falling on the poles. Do you have any clue why ?

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/2/0226a006c61c6d1198b6182228bd1001b7342c9d.jpeg)

@Cedric yeah, I tried to stick with really basic geometry, that’s why I made a very plain pearl shape with about 50 vertices as collider for the actual pearls. But I still wanted the pole to thread through them, so I couldn’t use a box or converhullShape. Linear constraints might do the trick though.

Just to make sure, it is a performance issue ? When I compare to some projects I’ve seen, I didn’t find my scene to be over complex geometry wise and regarding the amount of objects, so I thought I might have a missunderstanding somewhere on how I went about this.

---

<div class="post-metadata">

**Author:** ![HiGreg](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@HiGreg](https://forum.babylonjs.com/u/HiGreg)\
**Post date:** [November 6, 2024, 5:01pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/8 "2024-11-06T17:01:59Z")

</div>

I’m not an expert, but if I’m correct about face normals and collosions, then that is not DIRECTLY related to “performance.” However, if deltaTime increases from a delay in processing a step (aka "performance), even for one step, then the impulse applied by the physics engine from collisions increases the delta velocity and therefore the predicted delta position beyond the collided’s faces.

So in summary, maybe tangentially related to performance. Under this theory, restricting step size seems like it restricts the size of the calculated delta position and recognizes a collision.

Edit: @Cedric’s [comment here](https://forum.babylonjs.com/t/shaking-of-physical-bodies/54482/6) is relevant

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 6, 2024, 5:35pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/9 "2024-11-06T17:35:43Z")

</div>

Would you call that a synchronization issue between the babylon engine et the havok physics engine ? I think I’ve seen some issues mentioning it when I was looking.

---

<div class="post-metadata">

**Author:** ![HiGreg](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@HiGreg](https://forum.babylonjs.com/u/HiGreg)\
**Post date:** [November 6, 2024, 5:41pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/10 "2024-11-06T17:41:59Z")

</div>

Not directly. But synchronizing them and having a fixed timeStep helps. Though I think having a fixed physics time step is the relevant solution. I’m not familiar enough with the relationship between physics timeStep and engine tineStep to really say for sure. Particularly what happens to the next timeStep, of either, to know how either one “catches up.”

---

<div class="post-metadata">

**Author:** ![regna](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/regna/32/23915_2.png) [@regna](https://forum.babylonjs.com/u/regna)\
**Post date:** [November 7, 2024, 1:13am UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/11 "2024-11-07T01:13:33Z")

</div>

> [@Faber](#):
>
> Because I tried your PG and swapped to the textured assest to try and see any change, and the pearls pop out of line before falling on the poles. Do you have any clue why ?

The fast-loading placeholder (red) asset and slower-loading textured asset have different orientations. The hole is correcly vertical for the red ball but is slanted for the textured ball: [https://playground.babylonjs.com/#89YSX9#5](https://playground.babylonjs.com/#89YSX9#5)

 ![Screenshot (5865)](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/b/3b75db39477bce1753d8923107406f9450653044.png)

 ![Screenshot (5864)](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/0/0043218baaf771b59f1d4862c8a29e8301eae72a.jpeg)

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 7, 2024, 1:58pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/12 "2024-11-07T13:58:33Z")

</div>

I’m confused. I see that the pearl with the texture are tilted, but I don’t understand why. Because if you get the asset (from where it is imported [here](https://github.com/Faber-smythe/tetra/blob/main/public/assets/Tetra.glb) and drop it in the sandbox, it shows the pearl specimen I’m using to i the instantiate pearls with .clone() is correctly oriented.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/a/6a9bd730a7b8161c465c0b6a2343aaeb5818e35a.jpeg)  
Also I’m using the same assets in my first playground and it was not missaligned.

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 7, 2024, 2:08pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/13 "2024-11-07T14:08:49Z")

</div>

I tried using the slider constraints [on the PG](https://playground.babylonjs.com/#89YSX9#7) but the same issue happens, even with `deterministicLockstep: true`.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/a/5a5da41864d223d8e798b8e5fc6c745e15b31e55.jpeg)  
I still don’t really understand why this happens. Maybe I could find a way to just put the pearls’ physics to sleep apart from a click on a pile ?

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [November 7, 2024, 4:35pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/14 "2024-11-07T16:35:25Z")

</div>

if I take care to not have pearl intersections, then every is fine. frame rate starts dropping with intersections. so, I would try to avoid that situation by doing some book keeping and avoid pearl creation where it would create intersection.  
another solution that might be possible depending on your design is to avoid using physics and stack pearl on top of the over. adding free fall should no be more than 1 or 2 lines of code.

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 7, 2024, 4:41pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/15 "2024-11-07T16:41:21Z")

</div>

What do you mean by pearl intersection ? Like when they collide on the ones below as they drop ? Apart from that, I thought pearls geometry should not be intersecting, since they always spawn in a free space.

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 7, 2024, 4:55pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/16 "2024-11-07T16:55:47Z")

</div>

I ended up fixing it in the [playground](https://playground.babylonjs.com/#89YSX9#8) by setting the pearl motion type to “STATIC” after a 1000ms delay (line 102).  
I don’t like that I don’t understand why physics glitch on a fairly simple scene (I remember seeing showcases with a lot more going on) and end up kind of disabling them, but it does the trick.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [November 7, 2024, 6:25pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/17 "2024-11-07T18:25:04Z")

</div>

the issue seems more on intersection. like double click (or clicking multiple times quickly) will result in intersection that can be difficult to resolve, generate many contact points and slows down rendering. I’d suggest to now allow spawning pearls faster that 1 every second or 2.

EDIT: after some tests, I think freezing pearl like you did might be a better idea.

---

<div class="post-metadata">

**Author:** ![Faber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/faber/32/15884_2.png) [@Faber](https://forum.babylonjs.com/u/Faber)\
**Post date:** [November 7, 2024, 11:56pm UTC](https://forum.babylonjs.com/t/stacked-meshes-are-glitching-down-and-into-each-other-with-havok-physics/54574/18 "2024-11-07T23:56:11Z")

</div>

Yes, I think that would be a good idea too, if another pearl spawns before the first one drops that’s an issue.
