I apply the force using controls:
Code: Select all
function math.lengthdir(len, dir)
return len * math.cos(dir), len * math.sin(dir)
end
function hostPlayer:checkControls(dt)
if love.keyboard.isDown('w') then
self.body:applyForce(math.lengthdir(1000, self.body:getAngle()))
elseif love.keyboard.isDown('s') then
self.body:applyForce(math.lengthdir(1000, self.body:getAngle() + math.pi))
end
if love.keyboard.isDown('d') then
self.body:applyAngularImpulse(100)
elseif love.keyboard.isDown('a') then
self.body:applyAngularImpulse(-100)
end
end
It happens every timestep.
Here's all code that uses physics:
Code: Select all
function love.load()
world = love.physics.newWorld(0, 0, true)
love.physics.setMeter(64)
hostPlayer:create(0, 0, "EaJet", 0, 0, 0, 0.5) -- this function calls physical:init (a parent class) with the same arguments.
end
function physical:init(x, y, image, speed, dir, angle, imageScale)
table.insert(objList, self)
self.id = table.maxn(objList)
self.image = love.graphics.newImage(image .. ".png")
self.imageWidth = self.image:getWidth()
self.imageHeight = self.image:getHeight()
if imageScale == nil then
self.imageScale = 1
else
self.imageScale = imageScale
end
self.body = love.physics.newBody(world, x, y, "dynamic")
self.body:setMass(25)
self.body:setInertia(25)
self.body:setPosition(x, y)
self.shape = love.physics.newPolygonShape(0, 0, 71, 0, 95, 11, 102, 22, 102, 60, 95, 71, 71, 82, 0, 82)
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setRestitution(0.05)
if angle == nil then
self.body:setAngle(0)
else
self.body:setAngle(math.rad(angle))
end
if speed ~= nil then
self.body:setLinearVelocity(math.lengthdir(speed, dir))
end
end
--Then hostPlayer:checkControls(dt) is called every frame.
Basically, there's nothing else that uses physics.