Page 1 of 1

Body not moving when calling applyForce

Posted: Fri Apr 15, 2016 11:12 pm
by Sawbones

Code: Select all

local Class = require 'lib.hump.class'

local Player = Class{
    init = function(self, x, y)
        self.body = love.physics.newBody(world, x, y, "dynamic")
        -- self.shape = love.physics.newRectangleShape(50, 50)
        -- self.fixture = love.physics.newFixture(self.body, self.shape, 1)
        self.image = love.graphics.newImage("assets/helo.png")
    end,
    update = function(self, dt)
        if love.keyboard.isDown('d') then
            self.body:setAngle(.4)
            self.body:applyForce(200, 0)
        elseif love.keyboard.isDown('a') then
            self.body:setAngle(-.4)
            self.body:applyForce(-200, 0)
        else
            self.body:setAngle(0)
        end
    end,
    draw = function(self)
        local x, y = self.body:getPosition()
        local width, height = self.image:getDimensions()
        love.graphics.draw(self.image, x, y, self.body:getAngle(), .5, .5, width/2, height/2)
    end
}

return Player;

When I press 'a' or 'd' the angle changes but the applyForce does not move my sprite.

Re: Body not moving when calling applyForce

Posted: Sat Apr 16, 2016 6:32 am
by pgimeno
Works for me using your snippet. I can't say what you're doing wrong without a complete example.

Re: Body not moving when calling applyForce

Posted: Sat Apr 16, 2016 2:46 pm
by Tanner
Shot in the dark but maybe you aren't updating your world object. https://love2d.org/wiki/World:update