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

**URL:** https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045
**Category:** Questions
**Created:** [May 23, 2021, 1:52pm UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045 "2021-05-23T13:52:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cronvel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cronvel/32/6760_2.png) [@cronvel](https://forum.babylonjs.com/u/cronvel)
#### Post date: [May 23, 2021, 1:52pm UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/1 "2021-05-23T13:52:33Z")

</div>

Hi,

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](https://playground.babylonjs.com/#JJGVMJ#5).

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.

---

<div class="post-metadata">

### Author: ![PirateJC](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/piratejc/32/1003_2.png) [@PirateJC](https://forum.babylonjs.com/u/PirateJC)
#### Post date: [May 24, 2021, 4:09pm UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/2 "2021-05-24T16:09:35Z")

</div>

Hi @cronvel,

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

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:

> **[PhysicsImpostor | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.physicsimpostor#getlinearvelocity)**
>
> Represents a physics imposter \[API\]

> **[PhysicsImpostor | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.physicsimpostor#getangularvelocity)**
>
> Represents a physics imposter \[API\]

> **[PhysicsImpostor | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.physicsimpostor#sleep)**
>
> Represents a physics imposter \[API\]

Hope this makes some sense. Here’s an example playground where the sleep method is being used:  
[https://playground.babylonjs.com/#0IRV8X](https://playground.babylonjs.com/#0IRV8X)

---

<div class="post-metadata">

### Author: ![cronvel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cronvel/32/6760_2.png) [@cronvel](https://forum.babylonjs.com/u/cronvel)
#### Post date: [May 25, 2021, 9:56am UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/3 "2021-05-25T09:56:28Z")

</div>

@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).

---

<div class="post-metadata">

### Author: ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)
#### Post date: [May 25, 2021, 3:29pm UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/4 "2021-05-25T15:29:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cronvel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cronvel/32/6760_2.png) [@cronvel](https://forum.babylonjs.com/u/cronvel)
#### Post date: [May 25, 2021, 7:41pm UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/5 "2021-05-25T19:41:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)
#### Post date: [May 26, 2021, 10:29am UTC](https://forum.babylonjs.com/t/how-to-detect-when-a-bodys-physics-is-stable-still-use-case-dice-roller/21045/6 "2021-05-26T10:29:10Z")

</div>

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

[bingo | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#JJGVMJ#16)

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).
