question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Body parts not aligning to same center

See original GitHub issue

Hello. 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: image

compared to what I expect: image

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: image

Manual decomposition: image

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:open
  • Created 7 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
NuclearDogcommented, Jan 31, 2018

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:

// polygons is an array, each element being an array, each element of that being an object with properties x and y
var bodies = [];
for (var i=0; i<polygons.length; i++)
{
	bodies.push(Matter.Bodies.fromVertices(400, 400, polygons[i]));
}
var compound = Matter.Body.create({
	parts: bodies
});
Matter.World.add(myEngine.world, [compound]);

Which would result in this:

image

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:

// polygons is an array, each element being an array, each element of that being an object with properties x and y
var bodies = [];
for (var i=0; i<polygons.length; i++)
{
	var body = Matter.Bodies.fromVertices(400, 400, polygons[i]);
	bodies.push(body);
	var centre = Matter.Vertices.centre(polygons[i]);
	Matter.Body.setPosition(body, {
		x: body.position.x + centre.x,
		y: body.position.y + centre.y
	});
}
var compound = Matter.Body.create({
	parts: bodies
});
Matter.World.add(myEngine.world, [compound]);

Which resulted in my intended behaviour:

image

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.

0reactions
lassemtcommented, Nov 10, 2019

I can confirm that what @TobiasWehrum mentioned here might be the solution:

It seems to me that the problem might be that you set the position directly in https://github.com/liabru/matter-js/blob/master/build/matter.js#L6779 as opposed to using the centre like https://github.com/liabru/matter-js/blob/master/build/matter.js#L6814.

Using Vertices.centre(vertices) wil align parts properly. Screenshot 2019-11-10 at 13 23 55 vs. Screenshot 2019-11-10 at 13 24 07

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:

    return {
      position: vertexSets.length > 1 ? Vertices.centre(vertices) : {x: x, y: y},
      vertices: vertices
    };

Note that I have written a custom fromVertices since I running decomp outsite Matter.js in some preprocessing of my geometry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Simple Tests to See If Your Body Is out of Alignment
1. Check how you walk ... One quick way to check if your body is in good alignment is by the way you...
Read more >
Spine Misalignment Symptoms, Risk Factors, and Treatments
Any sort of misalignment can affect other parts the body, too. Possible signs that your spine is out of alignment include: chronic headaches...
Read more >
How to align entire html body to the center? - Stack Overflow
I just stumbled on this old post, and while I'm sure user01 has long since found his answer, I found the current answers...
Read more >
Fusion 360 Align Command ( How To Line Up Parts Quickly)
Fusion 360 align command is a great way to save time and not have the restrictions of joints. Watch NextBrand New User...
Read more >
How to align or move bodies to the Origin in Fusion 360
Note: If the aligned component is part of an assembly, it may be necessary to use a Rigid Group to move all components...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found