Page 1 of 1
Create Polygon
Posted: Fri Apr 27, 2012 8:45 pm
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
Re: Create Polygon
Posted: Sat Apr 28, 2012 7:03 am
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))
Re: Create Polygon
Posted: Sat Apr 28, 2012 9:31 am
by kpj
Yes, that did it
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?
Re: Create Polygon
Posted: Sat Apr 28, 2012 10:54 am
by mickeyjm
Polygons can only be up to 8 sides but you could try attahing multiple shapes together and treat them as one
Re: Create Polygon
Posted: Sat Apr 28, 2012 12:23 pm
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
Of course this line
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 ?
Re: Create Polygon
Posted: Sat Apr 28, 2012 2:18 pm
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
Re: Create Polygon
Posted: Sat Apr 28, 2012 7:38 pm
by kpj
Thanks a lot!
That was perfect