[Box2D] Polygon triangulation - area > b2_epsilon

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
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

[Box2D] Polygon triangulation - area > b2_epsilon

Post by Ulydev »

Hello everyone,

I'm trying to make a level editor where users can create polygons to be placed in a Box2D world.

Because there are Bézier curves involved, there can be hundreds of vertices on a single polygon. love.physics.newPolygonShape is limited to 8 vertices, so I decided to use love.math.triangulate to triangulate the polygons and add each triangle as a fixture. This works so far, except for some cases where I fiddle around with the level editor and get this Box2D error:

Code: Select all

"Box2D assertion failed: area > b2_epsilon"
After some research, I found that it could be linked to the vertices not being counter-clockwise (thus the area being negative?), so I implemented a check to reverse the vertices if they aren't counter-clockwise. It seemed to work for a few moments, but I started getting the error again.

My second guess would be that some triangles computed using love.math.triangulate are too tiny for Box2D, and thus the need to use another triangulation algorithm maybe? I am afraid I will get the same result.
One could also lower the resolution of the Bézier curve so that the vertices are further away from each other, but I am looking for another solution.

Thanks in advance for your suggestions / help!
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: [Box2D] Polygon triangulation - area > b2_epsilon

Post by Ulydev »

If anyone is looking for a quick solution, in case your polygons are static you can use love.physics.newChainShape (which entirely removes the need for triangulation / order check).
Else you could tessellate and proceed like HC, which merges triangles together.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [Box2D] Polygon triangulation - area > b2_epsilon

Post by ivan »

No, box2d polygons have several limitations. Off the top of my head:
- the area of each polygon must be greater than b2_epsilon (you cannot have tiny polygons)
- the angle of each vertex in the polygon cannot be too low (you cannot have near 0 or 180-degree vertex corners)
- each edge of the polygon cannot be shorter than a certain threshold (you cannot have thin polygons)
The same limitations would probably apply to chain shapes as well.
Furthermore you want to use optimal triangulation for box2d.
love.math.triangulate looks like it uses ear clipping which results in poor quality triangles.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 26 guests