Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can't make applyForce do anything

in Codea Craft Posts: 1,275

Here's my example. I expect that when I press the applyForce button, the box should go flying. It doesn't move at all.

What am I missing? Thanks!

-- 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("Force", applyForce)
end

function applyForce()
    BoxBody:applyForce(vec3(100,100,100))
end

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

-- Called automatically by codea 
function draw()
    update(DeltaTime)
    scene:draw()    
end

function createBox()
    local box = scene:entity()
    local body = box:add(craft.rigidbody, DYNAMIC, 1) -- mass
    body.restitution = 0.8
    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.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

Comments

  • Posts: 1,275

    Curiously, once in a while it moves. Very infrequently.

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries When I tap on Force, the box jumps to the left. If I uncomment the physics.gravity line, then it continues in a up/left direction. So it appears to work for me.

  • Posts: 1,275

    Weird. Doesn't work on either of my iPads. Once in a long while it moves.

  • Posts: 1,275

    @John any idea what's up?

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries I deleted your example on my iPad and loaded it again. It still works. With gravity, it jumps to the left on the floor. Without gravity, it continues to move up and left. I’ll try it on my other iPads.

  • Posts: 1,275

    on my fastest ipad it moved very occasionally. even putting huge numbers in the vector does nothing.

  • Posts: 1,547
    Doesn’t move at all for me when testing on iPhone 8.
  • edited November 24 Posts: 1,275

    ah! if i tap while it is still settling, it responds. if i wait till it's dead stopped, it doesn't go. it's as if physics stops running if nothing is going on.

  • dave1707dave1707 Mod
    edited November 24 Posts: 9,725

    It works on my iPad Air 3, iPad Air 4 and iPad Pro 1 and iPhone 8 SE.

  • dave1707dave1707 Mod
    Posts: 9,725

    Tried it on my iPad Air 1 and it worked.

  • Posts: 1,275

    does it work even if you wait a long time, letting the falling one completely settle and then press the button?

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries Try adding this.

        box.sleepingAllowed=false
    
  • dave1707dave1707 Mod
    edited November 24 Posts: 9,725

    That didn’t seem to work. If I wait 2 minutes, the Force button doesn’t work. I thought for sure the sleepingAllowed= false would work.

    PS. Just waiting about 10 sec and it doesn’t work.

  • edited November 24 Posts: 1,275

    body.sleepingAllowed makes it move almost every time. I wonder what sleeping is.

    it's body, not box tho.

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries That was my mistake. The sleepingAllowed should be applied to the rigidbody variable, not the entity variable.

  • edited November 24 Posts: 1,275

    interestingly even with that in place it doesn't always work. I checked to be sure physics wasn't paused. it wasn't. i hope @John or @Simeon can take a look.

  • dave1707dave1707 Mod
    Posts: 9,725

    @RonJeffries I waited 3 minutes and it still worked.

  • Posts: 1,275

    it usually works for me, but not always. if i'm to document it, i'm like to know what's up.

    with sleep permission false, i display awake or asleep in draw depending on BixBody.awake and it is always false, i.e. not awake.

    Very weird.

  • JohnJohn Admin Mod
    Posts: 679

    @RonJeffries That's weird, it's supposed to wake up the rigidbody automatically whenever you call addForce / addTorque, must be a bug

  • edited November 25 Posts: 1,275

    I found that even if I saybody.awake = true, right before applying force, it still doesn't move sometimes. Does awake get cleared when it thinks an object is still?

  • dave1707dave1707 Mod
    edited November 25 Posts: 9,725

    @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
    
  • edited November 25 Posts: 1,275

    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.

  • edited November 25 Posts: 1,275

    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
    
Sign In or Register to comment.