Page 1 of 1

Box2D Concave Polygons?

Posted: Fri Dec 27, 2013 3:53 am
by Maxwell
My goal is to make physical concave polygons. As of now I'm just testing triangulation on a convex shape but as of now I am having trouble of how to use the results from the triangulation function.. Would I have to use the returned triangle table to make each individual triangle and then attach them all using Joints? And then having to draw each triangle in the physical world too? Or is there an easier way I hope :)?!

Here is some code I have so far..

Code: Select all

function love.load()
world = love.physics.newWorld(0, 9.81*64, true)
triangles = love.math.triangulate(50, 50, 100, 50, 120, 100, 80, 200)
myshape = love.physics.newPolygonShape( 50, 50, 100, 50, 120, 100, 80, 200)
mybody = love.physics.newBody( world, 50, 50, "dynamic")
myfixture = love.physics.newFixture( mybody, myshape, 1 )
end

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

function love.draw()
love.graphics.polygon('fill', mybody:getWorldPoints(myshape:getPoints()))
end
THANKS!!!

Re: Box2D Concave Polygons?

Posted: Fri Dec 27, 2013 4:03 am
by markgo
A body can have multiple shapes attached to it. It should be easy to attach multiple triangles to your body to form a concave shape.

Re: Box2D Concave Polygons?

Posted: Fri Dec 27, 2013 1:33 pm
by ivan
As markgo, said you want to create all of the triangles on 1 body.
1 body can contain several shapes/fixtures.
And then having to draw each triangle in the physical world too?
You can do that, or you can store the vertices of the concave shape and use that for rendering.

Re: Box2D Concave Polygons?

Posted: Fri Dec 27, 2013 10:58 pm
by Maxwell
Thanks guys for the help I guess I heard what I didn't want to hear haha, but that's alright. I just don't want to make something inefficient if there was some sneaky easy way of doing things.
You can do that, or you can store the vertices of the concave shape and use that for rendering.
If I do the latter won't artifacts happen, or a possibility of it happening? Will I be better off just making every drawn shape converted into triangles and rendered on screen as triangles that are joined together, etc? I'm making a sandbox game.
Note: when in fill mode, the polygon must be convex and simple or rendering artifacts may occur.