Box2D Concave Polygons?

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
Maxwell
Prole
Posts: 11
Joined: Tue Dec 24, 2013 2:28 am

Box2D Concave Polygons?

Post 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!!!
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Box2D Concave Polygons?

Post 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.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Box2D Concave Polygons?

Post 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.
Maxwell
Prole
Posts: 11
Joined: Tue Dec 24, 2013 2:28 am

Re: Box2D Concave Polygons?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests