Mouse position in love.physics?
Posted: Mon Jun 22, 2009 3:51 pm
I'm in the process of learning Box2D, and I tried to make a small program that let you create boxes by clicking anywhere on the screen. However, there's a strange offset that occurs: when the mouse is close to the top left of the window, a box is created at the mouse. The farther away the mouse is from that point, the farther away a box is made. I'm sure this has to do with the screen's coordinate system being different from the physics engine's. The code's pretty short so I'll just post it here:
Code: Select all
function load()
world = love.physics.newWorld(2000, 2000)
world:setGravity(0, 5.0)
ground = love.physics.newBody(world, 0, 0, 0)
ground_shape = love.physics.newRectangleShape(ground, 400, 500, 600, 10)
blocks = {bodies = {}, shapes = {}}
end
function update(dt)
dt = dt * 10
world:update(dt)
end
function draw()
love.graphics.polygon(love.draw_line, ground_shape:getPoints())
for i=1,#blocks.shapes do
love.graphics.polygon(0, blocks.shapes[i]:getPoints())
end
end
function block_new(x, y)
local body = love.physics.newBody(world, x, y)
local shape = love.physics.newRectangleShape(body, x, y, 50, 50)
body:setMassFromShapes()
table.insert(blocks.bodies, body)
table.insert(blocks.shapes, shape)
end
function mousepressed(x, y, button)
block_new(x, y)
end