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:
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:
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.
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.
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?