Spawning with a click

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
NickL
Prole
Posts: 3
Joined: Wed Jul 21, 2010 3:14 pm

Spawning with a click

Post by NickL »

I was just wondering how I would spawn a physics object, such as a square, wherever on the screen I click.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Spawning with a click

Post by bmelts »

You might want to take a look at http://love2d.org/wiki/love.mousepressed and try using that.
NickL
Prole
Posts: 3
Joined: Wed Jul 21, 2010 3:14 pm

Re: Spawning with a click

Post by NickL »

How would I combine these two to create a ball where I click the mouse?

Code: Select all

  bodies[1] = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
  shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) --the ball has a shape of 20


  love.mousepressed( x, y, l )
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Spawning with a click

Post by Robin »

It all depends on how you map game coordinates (thus World coordinates) to screen coordinates.

By default, one unit in love.physics translates to about 30 pixels (guys: am I right here?), but you still have to have an offset, so basically if your printing code is something like:

Code: Select all

function love.draw()
    love.graphics.draw(image, body:getX() * 30 + offsetX, body:getY() * 30 + offsetY)
end
then you'd use:

Code: Select all

function love.mousepressed(x,y, button)
    spawnBodyAt((x - offsetX)/30, (y - offsetY)/30)
end
(this code won't work as-is, but is just to demonstrate you would handle something like that.)
Help us help you: attach a .love.
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Spawning with a click

Post by The Burrito »

Code: Select all

love.mousepressed( x, y, l )
bodies[1] = love.physics.newBody(world, x, y, 15, 0)
shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20)
end
like that? its pretty straightforward.

if you're dealing with a lot of bodies and shapes, you may want to organize them differently, I use something like this:

Code: Select all

function newBall(x,y,r)
local t = {}
	t.x = x
	t.y = y
	t.r = r
	t.b = love.physics.newBody(world, x, y)
	t.s = love.physics.newCircleShape( t.b,0,0, r)
	t.b:setMassFromShapes( )
	table.insert(balls, t)
end
edit: didn't see Robin's post, sometimes you will have offset values, but if you don't adjust the scale or anything I'm pretty sure it works as is.
Last edited by The Burrito on Wed Jul 21, 2010 8:43 pm, edited 1 time in total.
NickL
Prole
Posts: 3
Joined: Wed Jul 21, 2010 3:14 pm

Re: Spawning with a click

Post by NickL »

So, how exactly would I mash all of that together with the physics tutorial on the wiki, so wherever I click a ball spawns? Here is the code:

Code: Select all

function love.load()
  world = love.physics.newWorld(650, 650) --create a world for the bodies to exist in with width and height of 2,000
  world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
  world:setMeter(64) --the height of a meter in this world will be 64px
  
  bodies = {} --create tables for the bodies and shapes so that the garbage collector doesn't delete them
  shapes = {}
  
  --let's create the ground
  --we need to give the ground a mass of zero so that the ground wont move
  bodies[0] = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
  shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
  
  --let's create a ball
  bodies[1] = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
  shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) --the ball has a shape of 20

  --initial graphics setup
  love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
  love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
end


function love.update(dt)
  world:update(dt) --this puts the world into motion
  
  --here we are going to create some keyboard events
  if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
    bodies[1]:applyForce(400, 0)
  elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
    bodies[1]:applyForce(-400, 0)
  elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
    bodies[1]:setY(650/2)
  end
end

function love.draw()
  local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox() --get the x,y coordinates of all 4 corners of the box.
  --x1, y1 represent the bottom left corner of the bounding box
  --x2, y2 represent the top left corner of the bounding box
  --x3, y3 represent the top right corner of the bounding box
  --x4, y4 represent the top right corner of the boudning box
  local boxwidth = x3 - x2 --calculate the width of the box
  local boxheight = y2 - y1 --calculate the height of the box
  love.graphics.setColor(72, 160, 14) --set the drawing color to green for the ground
  --the rectangle is drawing from the top left corner
  --so we need to compensate for that
  love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
  love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
  --the circle is drawing from the center
  --so we do not need to compensate
  love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
end
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Spawning with a click

Post by The Burrito »

Here is a modified version:
main.lua
(2.01 KiB) Downloaded 116 times

Code: Select all

function love.load()
  world = love.physics.newWorld(650, 650) --create a world for the bodies to exist in with width and height of 2,000
  world:setGravity(0, 600) --the x component of the gravity will be 0, and the y component of the gravity will be 700
  world:setMeter(64) --the height of a meter in this world will be 64px
  
  bodies = {} --create tables for the bodies and shapes so that the garbage collector doesn't delete them
  shapes = {}
  
  --let's create the ground
  --we need to give the ground a mass of zero so that the ground wont move
  ground = love.physics.newBody(world, 650/2, 625, 0, 0) --I changed this so it would be less bothersome to make balls later
  groundshape = love.physics.newRectangleShape(ground, 0, 0, 650, 50, 0) 
  

  --initial graphics setup
  love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
  love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
end


function love.update(dt)
  world:update(dt) --this puts the world into motion
  

end

function love.draw()
  love.graphics.setColor(72, 160, 14) --set the drawing color to green for the ground
  love.graphics.polygon('fill',groundshape:getPoints()) --I didn't like the other method, this is easier
  
  love.graphics.setColor(193, 47, 14) --set the drawing color to red for the balls
  for i=1, # bodies do --this iterates through all of the bodies and draws them
  love.graphics.circle("fill", bodies[i]:getX(), bodies[i]:getY(), shapes[i]:getRadius(), 20)
  end
end


function love.mousepressed( x, y, click )
	bodies[#bodies+1] = love.physics.newBody(world, x, y, 15, 0) --place a body at mouse coordinates
	shapes[#shapes+1] = love.physics.newCircleShape(bodies[#bodies], 0, 0, 20) --use the body we just created
	bodies[#bodies]:setMassFromShapes( ) --this automatically calculates mass and makes the balls act more naturally
	shapes[#shapes]:setRestitution(.6) --this makes the balls bouncy, because bouncy balls are more fun!
end
don't know how familiar you are with lua, you may want to look up some info on tables to get a better idea of whats going on
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 101 guests