How to detect when a body's physics is stable/still? (use case: dice roller)


I’m trying to build a dice roller, and so far it works well. But I don’t know how to detect body stillness. At the moment, I detect the roll result after a fixed amount of time, but some dice may still hang (especially when there are many of them). I have seen demo (in Three.js) where they change the color of the mesh when it is physically stable, so it should be possible somehow, but I didn’t find any Babylon.js example for that.

The playground.

Side question: what engine are you guys using for physics? For the dice roller, it seems that Cannon is the more accurate, but it is also the slower (on my rather old laptop 50 dice are enough to drop FPS below 5). Oimo is the fastest but have weird behaviors. Ammo is usually good, but here it exhibits strange and unnatural die friction/rotation. Cannon is just perfect for the job but very slow.

Also do those engines support running on a server-side Node.js? It’s to build a multiplayer games, and at one point I may want to prevent cheating.

Hi @cronvel,

Adding @RaananW to this thread to correct any incorrect statements I might make :slight_smile:

It seems to me that the best possible way to do this would be to detect the linear and angular velocities of the physics imposter in question. You can check if they are under a certain threshhold and then set them to sleep if you want.

Here are the 3 methods that you’d probably want to consider using:

Hope this makes some sense. Here’s an example playground where the sleep method is being used:

1 Like

@PirateJC Thanks for the answer, that’s basically what I’ve done on my own, but I was expecting a more accurate way of doing it. I thought physics engines got some internal tracking of objects that stand still (to optimize frame generation).

Yes, each physics engine has this mechanism, but they are not unified, and therefor were not integrated in babylon.js. Depending on the physics engine you are using, you can find the way to detect if the body is awake/active.

@RaananW I’m using Cannon, which looks more realistic for this use-case.

I added a few lines - 20, 123, and 128 to 135

bingo | Babylon.js Playground (

This is one way of doing it. Cannon also allows listening to events (sleepy and sleeping), but I would personally only use them if this before-render loop takes too long (in a very busy scene).