Body parts not aligning to same center
See original GitHub issueHello. I’m having issues with a concave body not building correctly, and am starting to suspect a bug either in poly-decomp or matter.
here’s what I have:
compared to what I expect:
Initially, I relied upon poly-decomp, using the Matter.Bodies.fromVertices function to which I passed the following array as the vertices parameter:
[{x: 0, y: 0},{x: 60, y: 24},{x: 0, y: 48},{x: 10, y: 24}]
When that failed, I manually decomposed the ship into two convex polygons, as such:
[[{x: 0, y: 0},{x: 60, y: 24},{x: 10, y: 24}],[{x: 60, y: 24},{x: 0, y: 48},{x: 10, y: 24}]]
And got (basically) the same result. Well, it was slightly different:
Poly-Decomp:
Manual decomposition:
Any ideas what might be going on? Would be happy to post more info in the morning, just let me know what you’d like to see.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (3 by maintainers)
Top GitHub Comments
I think this would be the same class of issue.
For reasons that probably aren’t of interest, I’m running poly-decomp myself. This leaves me with an array of polygons, each composed of an array of vertices.
My first attempt was to create a body, passing that array (after converting the vertices to objects) directly to fromVertices as the documentation says that it supports
an array containing multiple sets of vertices
. That resulted in only one of the polygons being drawn.I moved from there to creating a compound body composed of several other bodies which is how I originally assumed I would have to tackle it:
Which would result in this:
The issue seems to be related to the one bfishman was describing - matter is ignoring the actual offset of the vertices when creating the body, and centering them around the point passed to fromVertices. In my case, that means all of my polygons end up overlapping and centered.
It’s not clear from the documentation that this would be the intended behaviour (though in retrospect makes sense), so it led to some confusion for sure, and the obvious fix for this (passing in all the polygons at once so that matter could handle it) didn’t seem to work. Is there another method I missed that allows creating several polygons at once while retaining their relative positioning in some way?
What I ended up doing, based off of bfishman’s solution, was:
Which resulted in my intended behaviour:
I can’t imagine this is a horribly unusual use-case, so I’m sure I must have missed an easier solution somewhere? If not, then hopefully this can help someone else that ends up googling their weird overlapping polygon issues. 😃
EDIT: Sorry, forgot to include: I’m using Matter 0.14.1 / 2018-01-10.
I can confirm that what @TobiasWehrum mentioned here might be the solution:
Using
Vertices.centre(vertices)
wil align parts properly. vs.I can make a pull request when I’m done with my project. Tight deadline 😅
Edit: To be more precise,
Vertices.centre
don’t work when it’s only one set in the vertexSet, so what I had to do was this:Note that I have written a custom fromVertices since I running decomp outsite Matter.js in some preprocessing of my geometry.