# Cannon Physics: physics body never switches to sleep state

**URL:** <https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405>\
**Category:** Bugs\
**Created:** [April 15, 2022, 4:29pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405 "2022-04-15T16:29:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [April 15, 2022, 4:29pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/1 "2022-04-15T16:29:10Z")

</div>

I noticed an issue when using the Cannon Physics Plugin, where the physics body never goes to a sleep state. To demonstrate this, please check the following playground:

> **[Babylon.js Playground](https://playground.babylonjs.com/#BEFOO%231641)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

In cannon the body apparently falls to sleep when the angular and directional velocity stay under a threshold for a some time. To debug I printed both the internal velocity and the one on the babylon physics impostor and additionally the cannon sleep state. The sleep state never changes from 0 to 1 or 2 (which would be sleepy or sleeping). Both velocities are the same but there’s something weird going on. Even though the sphere is not moving, the velocity never really drops below 0.02 (to be precise it might drop to 0.004 but would actually increase to something around 0.02 on the next frame):

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/9/19865206ce690b56cc76fdf66dc2ee15b6d151e6.png)

It makes perfect sense, the physics body never falls asleep, since the per default settings the treshold needs to be below 0.01 for at least 1 second. Please note I’m deliberately ignoring the angular velocity in all cases.

I created a simliar example with `cannon-es`, which is not the version babylonjs is using, but should not make any differences. To reproduce I did the following:

- Download zip archive on github: [GitHub - pmndrs/cannon-es: 💣 A lightweight 3D physics engine written in JavaScript.](https://github.com/pmndrs/cannon-es) and extract
- Start a local webserver in the root folder of the archive
- Visit [http://localhost:8000/examples/sleep.html](http://localhost:8000/examples/sleep.html)
- Update sleep.html by adding the following code to the end of the `demo.addScene('sleep')` method:

```auto
const animate = () => {
  requestAnimationFrame(animate)
  console.log(sphereBody.velocity.lengthSquared())
}
animate()

```

The console log is something like this towards the end:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/f/4/f4f992bd1724d9e08e70105b9ac84eafcf8921e3.png)

As you can see the velocity gets smaller on each step and eventually the sphere falls asleep.

There seems to be something wrong the the Physics Plugin for Cannon (or maybe other engines aswell? I didn’t check). Also this might be related to this issue: [Updating the mass applies a force to the physics body - #7 by Cedric](https://forum.babylonjs.com/t/updating-the-mass-applies-a-force-to-the-physics-body/29398/7)

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [April 15, 2022, 4:45pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/2 "2022-04-15T16:45:29Z")

</div>

It might be something along these lines, which cause this behaviour: [Babylon.js/cannonJSPlugin.ts at master · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Physics/Plugins/cannonJSPlugin.ts#L720) But I’m afraid I understand to little about the integration of the physics engines to be able to tell what’s happening there and why

@sebavan Pinging you, since `git blame` says you wrote those lines, I hope that‘s ok.

---

<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:** [April 15, 2022, 5:47pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/3 "2022-04-15T17:47:02Z")

</div>

cc @cedric our physics champion as well

---

<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:** [April 19, 2022, 4:19pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/4 "2022-04-19T16:19:50Z")

</div>

Is there any difference between our cannon and cannon-es versions @sebavan ?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 19, 2022, 7:21pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/5 "2022-04-19T19:21:35Z")

</div>

There should not be (like babylon and @babylon/core) so I wonder if the sleep detection might be in cause.

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [April 19, 2022, 8:50pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/6 "2022-04-19T20:50:14Z")

</div>

I think they merged some open PRs and updated some other smaller stuff. Details can be read on the github-page: [GitHub - pmndrs/cannon-es: 💣 A lightweight 3D physics engine written in JavaScript.](https://github.com/pmndrs/cannon-es)

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [April 19, 2022, 8:57pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/7 "2022-04-19T20:57:34Z")

</div>

A quick update: I recreated my experiment from the original post with `cannon.js` and it shows the same behaviour like `cannon-es`:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/8/184cc7bc64e024be00800b05b9dcd4b21c70073b.png)

So I’d still assume there’s something wrong on the babylonjs side 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [April 19, 2022, 9:00pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/8 "2022-04-19T21:00:43Z")

</div>

there has been reports of sleep issue with ammo too, perhaps related? Although, if i remember correctly, raggar said that an older version of ammo fixed the problem, which, at least for that case, seems like it may not be the bjs plugin.

side note, cannon-es did have some arbitrary breaking changes, like changing the shape schema to typescript enums from the original js object look up. they also removed a method that had an alias, because it had a comment over it that was like “todo , remove”. I know this because I rewrote the original cannon to esm/typescript. I have a copy somewhere anyone wants it

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 19, 2022, 9:14pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/9 "2022-04-19T21:14:57Z")

</div>

Assigned @Cedric on April 19, 2022, 9:14pm UTC



---

<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:** [April 20, 2022, 12:11pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/10 "2022-04-20T12:11:03Z")

</div>

I get different ‘stabilized’ velocity depending on gravity and physics time step.  
The small velocity difference is due to the sphere penetrating a little in the ground, and the ground responding.  
This said, even when setting linear velocity to 0, I don’t get the impostor to go to sleep.

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [April 24, 2022, 3:41pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/11 "2022-04-24T15:41:30Z")

</div>

@Cedric Any idea why that is?

---

<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:** [April 25, 2022, 8:32am UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/12 "2022-04-25T08:32:07Z")

</div>

No, getting deeper in this behavior will take some time. I’ll do my best to check it in the coming days.

---

<div class="post-metadata">

**Author:** ![Roronoa\_zoro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roronoa_zoro/32/24273_2.png) [@Roronoa\_zoro](https://forum.babylonjs.com/u/Roronoa_zoro)\
**Post date:** [May 10, 2022, 11:56am UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/13 "2022-05-10T11:56:57Z")

</div>

Also switching tabs seems to throw the physics simulation off.

---

<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:** [May 10, 2022, 12:14pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/14 "2022-05-10T12:14:11Z")

</div>

This can happen if delta time has spike.

> **[AmmoJSPlugin | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.ammojsplugin#constructor)**
>
> AmmoJS Physics plugin \[API\]

Check `useDeltaForWorldStep` for changing it to fixed frame rate.

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [June 2, 2022, 7:48am UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/15 "2022-06-02T07:48:14Z")

</div>

@Cedric Sorry to bother you again, do you have anything to go forward with this or should I search for an alternative solution?

---

<div class="post-metadata">

**Author:** ![thomasaull](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thomasaull/32/40115_2.png) [@thomasaull](https://forum.babylonjs.com/u/thomasaull)\
**Post date:** [June 2, 2022, 8:09am UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/16 "2022-06-02T08:09:02Z")

</div>

I think I found the solution, you need to enable `allowSleep` on the cannon world 🙄

```auto
const plugin = scene.getPhysicsEngine().getPhysicsPlugin()
plugin.world.allowSleep = true

```

Updated playground: [https://playground.babylonjs.com/#BEFOO#1705](https://playground.babylonjs.com/#BEFOO#1705)

I didn’t anticipate it was that easy of a solution 😅

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 2, 2022, 4:06pm UTC](https://forum.babylonjs.com/t/cannon-physics-physics-body-never-switches-to-sleep-state/29405/17 "2022-06-02T16:06:00Z")

</div>

Unassigned @Cedric on June 2, 2022, 4:06pm UTC


