2
\$\begingroup\$

This is a simple sprite rock

This is the sprite

and on my LibGDX game I would create a PolygonShape like the following image

This is the sprite with vertices

Like you see, I need 22 vertices and Box2D doesn't allow me create a PolygonShape with not more than 8 vertices so I think a possible way is merge two or more Polygons, is it possible? or I may use something else like for example a ChainShape?

\$\endgroup\$
2
\$\begingroup\$

Box2D, as of 2.3.2, doesn't support polygons with that many vertices as you're aware. It also doesn't support concave polygons. Not directly at least.

That said, it turns out mathematically speaking that any concave polygon can be expressed as a composition of convex polygons. So that's a way to solve these limitations in Box2D. Algorithmically speaking you may find it helpful to read of things like Ear Clipping. And library/function code for concave polygon decomposition into collections of convex polygons can be found in the extras portion (under the Contributions folder) of Box2D as well as around the Internet.

OTOH, if you will never need the concave shape to move physically, i.e. if it's only going to be associated with a static body, you can use the looped chain shape to implement concave polygon shapes. Box2D just doesn't have support for chain shapes to be used with dynamic bodies however.

Lastly, there may be forks of Box2D or similar engines which have support for convex polygons with more vertices and or for other solutions that can be used dynamically. You can also make a fork yourself and increase the max number of polygon vertices by upping the value of b2_maxPolygonVertices that's currently at 8.

I am the author of such a fork though it's only available for C++ right now and not quite release ready IMO. You can find my code on GitHub under my PlayRho repo. This repo has support for using chain shapes with dynamic bodies as well as supporting convex polygons with up to 254 vertices. I'd love more testers and/or people assisting with it.

Hope this helps.

\$\endgroup\$
0
\$\begingroup\$

You can construct your Body from multiple convex PolygonShapes by creating many Fixtures for it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.