# Setting enable\_sleep() isn't persistent

**URL:** <https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877>\
**Category:** Bugs\
**Created:** [June 3, 2026, 8:00am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877 "2026-06-03T08:00:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [June 3, 2026, 8:00am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/1 "2026-06-03T08:00:35Z")

</div>

I’ve bumped into what I think is a bug to do with preventing a dynamic body from sleeping in Box2D v3.

This code doesn’t stop the body from falling asleep, and the body is not affected by the joint when created:

```lua
function init(self)

	self.body = b2d.get_body("/ball#collisionobject")
	b2d.body.enable_sleep(self.body, false)

	timer.delay(2, false, function()
		create_joint() -- This doesn't work because the ball body is asleep
	end)

end

```

However, if enable\_sleep() is continuously triggered in the update(), the body does wake up:

```lua
function update(self, dt)

	b2d.body.enable_sleep(self.body, false) 

end

```

Maybe these two Box2D functions have been mixed up?

**b2Body\_SetAwake()**  
Wake a body from sleep.

**b2Body\_EnableSleep()**  
Enable or disable sleeping for this body. If sleeping is disabled the body will wake.

From the Box2D reference:

> **[Box2D: Body](https://box2d.org/documentation/group__body.html)**

Minimal repro project:

[Sleep.zip](https://forum.defold.com/uploads/short-url/rBR87JgHmk4UN0NmEhRslMGk7R4.zip) (4.6 KB)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 3, 2026, 8:03am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/2 "2026-06-03T08:03:48Z")

</div>

> Maybe these two Box2D functions have been mixed up?  
> No.

It’s easy to check:

> <https://github.com/defold/defold/blob/dev/engine/gamesys/src/gamesys/scripts/box2d/v3/script_box2d_body_v3.cpp#L885-L892>

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [June 3, 2026, 8:04am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/3 "2026-06-03T08:04:37Z")

</div>

Quick fire response!

Hm, any idea what could be causing this then?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 3, 2026, 8:06am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/4 "2026-06-03T08:06:02Z")

</div>

Searching further, you’ll find this, which I’m currently reading to understand:

> <https://github.com/defold/defold/blob/dev/engine/physics/src/physics/box2d/box2d_physics.cpp#L545-L557>

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 3, 2026, 8:11am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/5 "2026-06-03T08:11:26Z")

</div>

I believe it’s related to dynamic scaling of object.  
Since Box2d doesn’t have that concept in V2 (and our V3 implementation is modeled after V2), we need to wake up the object if we alter the transform. And then allow it to go to sleep again.

Perhaps there is another way to do this though. I’ll read some more.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [June 3, 2026, 8:17am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/6 "2026-06-03T08:17:11Z")

</div>

If the body needs waking after being scaled, wouldn’t it be better to use b2Body\_SetAwake() instead, since it’s a one-shot, rather than a persistent flag?

So this:

```cpp
                    if (dp > POS_EPSILON || fabsf(da) > ROT_EPSILON)
                    {
                        b2Vec2 b2_position;
                        ToB2(position, b2_position, scale);

                        b2Rot b2_rot = b2MakeRot(angle);
                        b2Body_SetTransform(body_id, b2_position, b2_rot);
                        b2Body_EnableSleep(body_id, false);
                    }
                    else
                    {
                        b2Body_EnableSleep(body_id, true);
                    }

```

Could be changed to:

```cpp
                    if (dp > POS_EPSILON || fabsf(da) > ROT_EPSILON)
                    {
                        b2Vec2 b2_position;
                        ToB2(position, b2_position, scale);

                        b2Rot b2_rot = b2MakeRot(angle);
                        b2Body_SetTransform(body_id, b2_position, b2_rot);
                        b2Body_SetAwake(body_id, true);
                    }

```

And then the body would respect the EnableSleep flag afterwards automatically, because it was never used.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 3, 2026, 8:18am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/7 "2026-06-03T08:18:47Z")

</div>

Precisely!  
That’s my intended fix, for both Box2d V2 and V3.  
I’m not sure why I didn’t do that 6 years agod, so I’ll have to double check 🤔

I should be able to make a fix to beta today.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [June 3, 2026, 8:20am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/8 "2026-06-03T08:20:03Z")

</div>

Ha ha. Fantastic!

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 3, 2026, 8:25am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/9 "2026-06-03T08:25:01Z")

</div>

In the meantime, as a workaround: If you don’t need dynamic scaling, you can turn it off with `physics.allow_dynamic_transforms = 0`

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 4, 2026, 2:23pm UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/10 "2026-06-04T14:23:30Z")

</div>

I’ve sent a PR for review now, hopefully it lands today in the beta.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [June 11, 2026, 8:25am UTC](https://forum.defold.com/t/setting-enable-sleep-isnt-persistent/82877/11 "2026-06-11T08:25:27Z")

</div>

A fix is now in the 1.13.0 BETA
