matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Fix inconsistent Bodies.fromVertices behavior

Open controversial opened this issue 3 years ago • 1 comments

When calling Bodies.fromVertices with an array of vertexSets in which some vertexSets are convex and some are concave, the convex vertices end up in wildly different places from the concave vertices.

See the following example, where in vectorized text, the three convex parts (making up the “i” and the “o”) are out of place with the rest of the (concave) parts, even though they were correctly positioned in the input data:

Screenshot 2022-11-19 at 1 22 45 PM

This occurs because the Bodies.fromVertices implementation sets the x, y of convex parts to the input x, y (which is the same for every part) but it sets the x, y of concave parts to the center of mass of the part.

This pull request makes the behavior consistent by ensuring every part has its position set to its center of mass, so that multiple vertexSets passed to Matter.fromVertices have their relative positions preserved.

controversial avatar Nov 19 '22 18:11 controversial

Thanks a lot, this Pull request finally solved my problem I was having for over a week. I needed Polygons with holes and did not like the results of poly-decomp so I used another algorithm @mapbox/earcut, but the resulting vertices always were on top of each other. With this small change it works!

dreerr avatar Jun 12 '24 07:06 dreerr