I found that even if I say body.awake = true
, right before applying force, it still doesn’t move sometimes. Does awake get cleared when it thinks an object is still?
@RonJeffries I think the awake variable is read only. That’s used to check if the object is sleeping or awake. The sleepingAllowed variable is what gets set. Should set that variable to true then check the awake variable and see what it shows.
PS. I tried it and when I set sleepingAllowed to false, awake is false. If set to true, awake is true.
Add this to draw(), you can watch it change from true to false in a few seconds.
if BoxBody.awake == false then
text("awake is false",WIDTH/2,HEIGHT-50)
else
text("awake is true",WIDTH/2,HEIGHT-50)
end
you’d think that sleepingAllowed = false would set awake = true.
Is it really backward?
I’ve seen awake change. Mostly it stays off even when the thing does respond. And nothing I do makes it respond every time. I think there’s a bug in there somewhere.
I guess awake could be just a status that is written but not read inside. If so, I’d like to know what I have to do to wake the thing up. Right now I’m afraid to document the force stuff because I can’t explain it.
This somewhat convoluted version uses sleepAllowed=false
, which does seem to work backward. The force button applies an upward force on the next draw cycle. if you set force = n, it’ll apply force that many times, in the next n draw cycles.
The wake button sets the body.awake
to true and force to 1. That usually results in the object waking up and moving. but not always.
the awake/asleep status is displayed near the bottom of the screen.
-- ApplyForce?
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("Wake", wake)
parameter.action("Force", applyForce)
force = 0
end
function wake()
BoxBody.awake = true
force = 1
end
function applyForce()
force = 1
end
function update(dt)
scene:update(dt)
end
-- Called automatically by codea
function draw()
if force > 0 then
print("force ", BoxBody.awake)
--BoxBody.awake = true
BoxBody:applyForce(vec3(00,400,00))
force = force - 1
end
--print(scene.physics.paused)
update(DeltaTime)
scene:draw()
if BoxBody.awake then
text("awake",300,100)
else
text("asleep", 300,100)
end
end
function createBox()
local box = scene:entity()
local body = box:add(craft.rigidbody, DYNAMIC, 1) -- mass
body.restitution = 0.8
body.sleepingAllowed = true
BoxBody = body
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)
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.1, 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