Page 1 of 1

Using love.physics - collision is 1px off

Posted: Sun Oct 07, 2012 11:57 am
by munchor

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
I'm using the love.physics demo as a base to make a game prototype. However, I'm running into a very odd issue. The collision (for all sides) is 1 pixel off. It behaves as if the player was a 17*17 object, but I set it up 16*16. I looked through the documentation and tweaked some values with no luck.

Any ideas? Thank you in advance.

Re: Using love.physics - collision is 1px off

Posted: Sun Oct 07, 2012 4:14 pm
by Boolsheet
munchor wrote:I looked through the documentation and tweaked some values with no luck.
It's probably the polygon skin. Take a look at the end of 4.4 Polygon Shapes in the Box2D Manual.

Re: Using love.physics - collision is 1px off

Posted: Sun Oct 07, 2012 5:18 pm
by munchor
Boolsheet wrote:
munchor wrote:I looked through the documentation and tweaked some values with no luck.
It's probably the polygon skin. Take a look at the end of 4.4 Polygon Shapes in the Box2D Manual.
From what I read in the manual about skins, it sure is skins, but I can't find out a way to get rid of them, any idea?

Re: Using love.physics - collision is 1px off

Posted: Sun Oct 07, 2012 5:31 pm
by slime
You don't want to get rid of them. From the manual:
The polygon skin helps prevent tunneling by keeping the polygons separated. This results in small gaps between the shapes. Your visual representation can be larger than the polygon to hide any gaps.

Re: Using love.physics - collision is 1px off

Posted: Mon Oct 08, 2012 8:18 am
by munchor
Ooh I have to use a sprite to cover for it, thanks :)