Page 1 of 1

Love Physics Drawing Error

Posted: Sat Sep 27, 2014 1:27 am
by Assossa
Hello, I am trying to modify the physics example on the wiki page. It should spawn boxes where you click, which is working as far as I can tell, except when I draw them it errors.
Here is my code:

Code: Select all

function love.load()
	meter = 64

	love.physics.setMeter(meter)
	world = love.physics.newWorld(0, 9.81*meter, true)

	objects = {}

	objects.ground = {}
  	objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2)
  	objects.ground.shape = love.physics.newRectangleShape(650, 50)
  	objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape)

  	love.graphics.setBackgroundColor(104, 136, 248)
 	love.window.setMode(650, 650)

 	options = {}
 	options.type = "box"
end

function addObj(type, x, y)
	i = #objects

	objects[i] = {}
 	objects[i].body = love.physics.newBody(world, x, y, "dynamic")
 	if type == "ball" then
		objects[i].shape = love.physics.newCircleShape(25)
		objects[i].fixture = love.physics.newFixture(objects[i].body, objects[i].shape, 1)
		objects[i].fixture:setRestitution(0.9)
	elseif type == "box" then
 		objects[i].shape = love.physics.newRectangleShape(0, 0, 50, 50)
 		objects[i].fixture = love.physics.newFixture(objects[i].body, objects[i].shape, 5)
 	end
end

function remObj(name)
	objects[name] = nil
end

function love.update(dt)
	world:update(dt)
end

function love.draw()
 	love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
 	love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

	local Count = 0
	for Index, Value in pairs( objects ) do
		Count = Count + 1
	end

 	if Count > 1 then
 		for i=1,Count-1 do
 			love.graphics.setColor(50, 50, 50)
 			love.graphics.polygon("fill", objects[i].body:getWorldPoints(objects[i].shape:getPoints()))
 		end
 	end

 	love.graphics.setColor(255, 255, 255)
 	love.graphics.print(options.type, 10, 10)
 	love.graphics.print(Count, 10, 25)
end

function love.keypressed(key, isrepeat)
	if not isrepeat then
		if key == "right" then
			options.type = "box"
		elseif key == "left" then
			options.type = "ball"
		end
	end
end

function love.mousepressed(x, y, button)
	addObj(options.type,x,y)
end
And the Error:

Code: Select all

Error: main.lua:56: attempt to index a nil value
stack traceback:
	main.lua:56: in function 'draw'
	[string "boot.lua"]:438: in function <[string "boot.lua"]:399>
	[C]: in function 'xpcall'
And yes I know if it tries to draw a ball it will error, I'm gonna fix that later.

Thanks for the help!

Re: Love Physics Drawing Error

Posted: Sat Sep 27, 2014 8:28 am
by Robin
Hi, please upload a .love next time you have a problem, because that makes it much easier for us to help you. (It's in the forum rules.)

Code: Select all

i = #objects
should be

Code: Select all

local i = #objects + 1
(otherwise i would be a global variable, and sequences start at 1 in Lua)

Code: Select all

   local Count = 0
   for Index, Value in pairs( objects ) do
      Count = Count + 1
   end

    if Count > 1 then
       for i=1,Count-1 do
          love.graphics.setColor(50, 50, 50)
          love.graphics.polygon("fill", objects[i].body:getWorldPoints(objects[i].shape:getPoints()))
       end
    end
should be

Code: Select all

   love.graphics.setColor(50, 50, 50)
   for Index, Value in ipairs( objects ) do
      love.graphics.polygon("fill", Value.body:getWorldPoints(Value.shape:getPoints()))
   end

Re: Love Physics Drawing Error

Posted: Sat Sep 27, 2014 8:51 am
by artofwork
To fix the error on line 56 of main.lua you need to look at line 22 of main.lua

Code: Select all

i = #objects

The index i will always be equal to 0 no matter how many time you click it, so in order to resolve this issue you add 1

Code: Select all

i = #objects + 1


Now you get a new error when you switch to balls

Code: Select all

main:56: attempt to call method 'getpoints' (a nil value) 
I haven't really tapped into this aspect of love yet however by the looks of it your trying to draw a polygon of circles which is not possible so to resolve this you can comment out line 56 and add this

Code: Select all

        --love.graphics.polygon("fill", objects[i].body:getWorldPoints(objects[i].shape:getPoints()))
	if options.type == "box" then
		love.graphics.polygon("fill", objects[i].body:getWorldPoints(objects[i].shape:getPoints()))
	elseif options.type == "ball" then
		love.graphics.circle("fill", objects[i].body:getX(), objects[i].body:getY(), objects[i].shape:getRadius())
	end
Now you can create boxes and then switch to balls and it will create the ball but the boxes are removed however they still exist they are just not drawn on the screen, as well as when you switch back to boxes it generates this error below.

Code: Select all

main:56: attempt to call method 'getpoints' (a nil value) 
I'm sure there is an easy solution to resolve this, however I'm not really interested in spending too much time on this.
I believe your trying to cram everything all in 1 function which is not always a good thing but anyhow I'll leave the rest to you or someone else to figure out :D

Re: Love Physics Drawing Error

Posted: Sat Sep 27, 2014 3:59 pm
by Assossa
Thanks everyone for your help! Both of your answers worked fine.