Mouse position in love.physics?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jcw
Prole
Posts: 3
Joined: Mon Jun 22, 2009 3:40 pm

Mouse position in love.physics?

Post by jcw »

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
User avatar
CryoNox
Prole
Posts: 18
Joined: Sun Jun 28, 2009 2:25 pm
Location: Singapore

Re: Mouse position in love.physics?

Post by CryoNox »

Well, I halved the mouse coordinates to fit the physics coordinates and it worked:

Code: Select all

function mousepressed(x, y, button)
   block_new(x*0.5, y*0.5)
end
Thy father beckons.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 199 guests