rigidbody.awake and sleepingAllowed are funky, as are applyForce and Torque

With this code in draw, a dropped object shows awake for a while. When it settles, it shows asleep. This seems reasonable.

function draw()
    update(DeltaTime)
    scene:draw()	
    if BoxBody.awake then
        text("awake", 400,100)
    else
        text("asleep", 400,100)
    end
end

However if we say this in setting up the body:

    BoxBody.sleepingAllowed = false

The display now shows “asleep” all the time. From the object’s behavior it seems that it is not asleep, but thinks it is.

I test this with applyTorque and applyForce, neither of which seems to wake up a sleeping object: they probably should. Even when the object is awake, they do not always move the object. I think they probably should.

Below is my full test program:

-- CoCraTu-006

function setup()
    scene = craft.scene()
    --scene.physics.gravity = vec3(0,0,0)
    createFloor()
    createBox()
    scene.camera:add(OrbitViewer, vec3(0,0,0), 20, 1, 20)
    angle = 0
    parameter.action("Twist", twist)
end

function twist()
    BoxBody:applyTorque(vec3(0,40,0))
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()	
    if BoxBody.awake then
        text("awake", 400,100)
    else
        text("asleep", 400,100)
    end
end

function createBox()
    local box = scene:entity()
    BoxBody = box:add(craft.rigidbody, DYNAMIC, 1) -- mass
    BoxBody.restitution = 0.8
    BoxBody.sleepingAllowed = false
    box:add(craft.shape.box, vec3(1,1,1))
    box.model = craft. model.cube(vec3(1,1,1))
    box.material = craft.material(asset.builtin.Materials.Specular)
    box.material.map = readImage(asset.builtin.Blocks.Missing)
    box.y = 5
    return box
end

function createFloor()
    local floor = scene:entity()
    local body = floor:add(craft.rigidbody, STATIC)
    body.restitution = 0.9
    floor:add(craft.shape.box, vec3(25, 0.01, 25))
    floor.model = craft.model.cube(vec3(25, 0.1, 25))
    floor.y = -1.05
    floor.material = craft.material(asset.builtin.Materials.Specular)
    floor.material.map = readImage(asset.builtin.Blocks.Brick_Grey)
    floor.material.offsetRepeat = vec4(0,0,25,25)
    return floor
end

My opinion is if you say sleepingAllowed is true, then if the object is sleeping it should stay sleeping until I say sleepingAllowed is false. Then it should wake up when I want it to do something. That gives me control over the object. Whether it works that way now or not I’m not sure.

@RonJeffries When I run your above code, even though it says asleep, I can still twist it. It never says it’s awake. Even if I wait a minute, I can still twist it.

i cannot. the asleep thing is a bug, i think. i suspect you saw it in the other thread.

interesting idea about the allowed waking it up. but the awake flag doesn’t change, as we’ve seen.

do we know where codea lifted physics from?

@RonJeffries Codea uses Bullet3D. Having a look at the code, there’s some logic to do with how waking works. There may be a bug in some combination of states, maybe when sleeping is disallowed since the awake property getter checks for Bullet’s ACTIVE_TAG but there’s also DISABLE_DEACTIVATION. I need to go over the logic for this…

Also applyForce() and applyTorque() do not appear to automatically wake up rigidbodies, but that could be added

It would seem to be a good idea to add it. Can we wake a thing up by setting thing.awake? Or is that really kind of read only?

Thanks, I’ll see what I can find about Bullet3D.

Oh, and one more thing: is the 2D physics a different engine?

I see in bullet docs that apply force doesn’t activate. do we have an activate function in there? it’s definitely not `activated :smile:

I’ll need to look into it since I thought body.awake = true would work, maybe there’s something I missed

Yes. Note in particular that when we set sleepingAllowed = false, the object.awake remains false forever, i.e. it shows asleep. But it does interact, mostly.

While you’re here, I am positing that physics cycle time may always be 1/60th, rather than varying as draw cycle time does. Can you confirm or deny that, please?

Also, asked elsewhere: fixedUpdate is not called (at least not on my DYNAMIC object. Is it never called, or maybe only on STATIC or something?

Thanks @John !