Create Polygon

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
kpj
Prole
Posts: 6
Joined: Fri Apr 27, 2012 8:38 pm

Create Polygon

Post by kpj »

Hello,
I'd like to create some variable Polygons with the love.physics engine. Since 0.8 I do this with "shape = love.physics.newPolygonShape( x1, y1, x2, y2, x3, y3, ... )". Indeed, my points are in a table. How can I convert my table (of variable size) to anything, that would fit into the new constructor?
Thanks for any help,
kpj
User avatar
pk
Citizen
Posts: 67
Joined: Wed Dec 14, 2011 2:13 am
Location: Texas, United States
Contact:

Re: Create Polygon

Post by pk »

Lua's unpack() should do the trick.

Code: Select all

local t = {x1, y1, x2, y2, x3, y3, ...}
shape = love.graphics.newPolygonShape(unpack(t))
ALL CREATURE WILL DIE AND ALL THE THINGS WILL BE BROKEN. THAT'S THE LAW OF SAMURAI.
kpj
Prole
Posts: 6
Joined: Fri Apr 27, 2012 8:38 pm

Re: Create Polygon

Post by kpj »

Yes, that did it :awesome:

Now there is another problem, I can only create a polygon with up to 8 different points.
In my programm, I want the user to draw a shape, and then add this shape to love.physics, so it could interact with all other objects.
Any suggestions how I could do this? Maybe I do not need a polygon?
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Create Polygon

Post by mickeyjm »

Polygons can only be up to 8 sides but you could try attahing multiple shapes together and treat them as one
Your screen is very zoomed in...
kpj
Prole
Posts: 6
Joined: Fri Apr 27, 2012 8:38 pm

Re: Create Polygon

Post by kpj »

Nice idea, I tried it:
Note: "drawmevert" contains all coordinates in the following pattern: "x1, y1, x2, y2, ..."

Code: Select all

function create_poly()
	local tsize = table.getn(drawmevert)
	local verter = {}
	local vt = {}
	for i,v in ipairs(drawmevert) do
		-- divide all entries in 8 sized chunks
		table.insert(vt, v)

		if i % 8 == 0 then
			table.insert(verter, vt)
			vt = {}
		end
	end
	if table.getn(vt) ~= 0 then
		table.insert(verter, vt)
	end

	--[[print(tsize .. " nums -> " .. tsize/2 .. " coords")
	print("into " .. table.getn(verter) .. " tables")
	for i,v in ipairs(verter) do
		print(i .. " | num-items: " .. table.getn(v))
	end]]

	local newp = {}
	for i,v in ipairs(verter) do
		newp = {body = love.physics.newBody(world, 0, 0), shape = love.physics.newPolygonShape(unpack(v))}

		local fixture = love.physics.newFixture(newp.body, newp.shape)

		table.insert(newp, fixture)
		table.insert(object.polygs, newp)
	end
end
Sorry for stupid code, I started learning lua yesterday :crazy:

Of course this line

Code: Select all

love.physics.newBody(world, 0, 0)
needs to be changed, but I did not completely understand ithe meaning of "0,0" (is it the middle, upper-left corner, etc?!). :?

Furthermore this code gives me the following error:

Code: Select all

Box2D error: edge.LengthSquared() > b2_epsilon * b2_epsilon
What extactly does it mean? I tried google, but it did not reveal anything helpful :(

Any suggestions ?
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Create Polygon

Post by Zeliarden »

You can try use physics.newChainShape. here is an exampel: use l to draw a line and i to make its solid. u, n and p for poly
Attachments
phys11.love
(103.03 KiB) Downloaded 173 times
kpj
Prole
Posts: 6
Joined: Fri Apr 27, 2012 8:38 pm

Re: Create Polygon

Post by kpj »

Thanks a lot!
That was perfect :)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests