[Physics][4.2.0-beta.9] Ammo.js crashes browser

With 4.2.0-beta.9 the physics engine in combination with ammo.js seems to be causing the app to freeze or even crash the browser. I am using Firefox on Linux. Babylon.js 4.1.0 works fine.

The playground has random hiccups and restarting it with the play button makes it stop working (physics engine not initialized error).

Adding the following to a scene crashes the browser (“something is slowing down your browser…”)

import * as Ammo from 'ammo.js'
const AmmoLoaded: any = await Ammo()
const gravityVector = new Vector3(0, 0, 0)
const ammoPlugin = new AmmoJSPlugin(true, AmmoLoaded)
scene.enablePhysics(gravityVector, ammoPlugin)

adding @cedric to see if he can dig it up :slight_smile:

hi. its not bug. your problem in BABYLON.PhysicsImpostor.MeshImpostor for sphere with 32 segments. Use Advanced Physics Features - Babylon.js Documentation
for sphere use sphere impostor or decrease sphere segments Babylon.js Playground

but i see other bug. https://playground.babylonjs.com/#JIGV5V#4 when PG start all works fine but after restart PG with play button see message in console Physics not enabled. its because setInterval async function?

Hi, it indeed seems to be a problem with MeshImposter. For this example I used a sphere but if I import a mesh I would still need the MeshImposter and the problem remains (so not using MeshImposter is not a solution). Also, in 4.1.0 everything works fine.

I can also see no hiccups with sphereImpostor but I’m getting error ‘Physics not enabled’ popping up.
Let me check that …

Swapping SphereImposter and MeshImposter between ball and sphere seems to break the physics too. No errors, but there are no physics either.

This is what I get (and restarting it results in ‘physics not enabled’)

Nevermind… I misunderstood your post.

After some tests, the hiccupps are not random. When creating a mesh impostor, ammo creates internal acceleration structures (BVH) that takes time. The more segments in your sphere, the longer the hiccup.
Then, the error message ‘Physics is not enabled’ happens because after clicking the play button in PG, the intervals are not cleared and the interval callback still runs. So the CB function keeps track of a half disposed scene (wihtout physics) and you get the error.
It’s the same with settimeout.
Aren’t those timers cleared when clicking the play button? Was it the same before? @Deltakosh @Evgeni_Popov

AFAICT it has always been like that.

It’s the responsibility of the PG writer to remove the timeout/intervals on scene.dispose, for eg.

The PG ecosystem does not keep track of the timers/intervals you could have set.

Note it’s the same thing if you add HTML elements to the rendering area: if you don’t remove them on scene.dispose (or on any other event that would trigger when the PG is restarted) they will remain on screen at the next click on “Play”.


Hmm, I managed to get it to work locally with:

import * as Ammo from 'ammo.js'
const AmmoPlugin = new AmmoJSPlugin(true, Ammo)

I probably messed up trying to reproduce it in the Playground by adding a MeshImposter to a sphere. I’d swear the code in the OP worked in 4.1.0 but I can’t actually reproduce it now :expressionless:. Things worked and then stopped working after a yarn upgrade

Anyway, I think the problem is solved but I’m still not entirely sure what went wrong exactly (probably a bunch of things).