Using love.physics - collision is 1px off
Posted: Sun Oct 07, 2012 11:57 am
Code: Select all
function love.load()
love.physics.setMeter(64)
world = love.physics.newWorld(0, 9.81 * 64, true)
objects = {}
-- Create the ground
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 800/2, 650-50/2) -- Shape is anchored to center!
objects.ground.shape = love.physics.newRectangleShape(800, 50)
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape)
-- Create a player
objects.player = {}
objects.player.body = love.physics.newBody(world, 650/2, 650/2, "dynamic") -- Place player in center
objects.player.shape = love.physics.newRectangleShape(16, 16)
objects.player.fixture = love.physics.newFixture(objects.player.body, objects.player.shape, 1)
objects.player.fixture:setRestitution(0)
-- Create a couple blocks to play around with
objects.block1 = {}
objects.block1.body = love.physics.newBody(world, 200, 550, "static")
objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5)
objects.block2 = {}
objects.block2.body = love.physics.newBody(world, 200, 400, "static")
objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
objects.block2.fixture = love.physics.newFixture(objects.block2.body, objects.block2.shape, 2)
-- Setup graphics
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(800, 640, false, true, 0)
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("right") then
objects.player.body:applyForce(200, 0)
elseif love.keyboard.isDown("left") then
objects.player.body:applyForce(-200, 0)
elseif love.keyboard.isDown("up") then
objects.player.body:applyForce(0, -300)
end
end
function love.draw()
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints()))
love.graphics.setColor(193, 47, 14)
love.graphics.polygon("fill", objects.player.body:getWorldPoints(objects.player.shape:getPoints()))
love.graphics.setColor(50, 50, 50)
love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))
love.graphics.polygon("fill", objects.block2.body:getWorldPoints(objects.block2.shape:getPoints()))
end
Any ideas? Thank you in advance.