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

After call setVertices on the body, his collisions are not updated

Open ararati opened this issue 2 years ago • 2 comments

Used dependencies

  • "matter-js": "^0.18.0"

Background

I updated body vertices with call setVertices but after the call, his collisions are not updated, so other object does not fall. Maybe I skipped some basic principles of using matter.js so I present the short version of using them if this is not enough I will create a separate working example.

// Init engine
const engine = Engine.create();
const runner = Runner.create();
Runner.run(runner, engine);

// Add a circle for check collision
const circle = Bodies.polygon(300, 100, 5, 12, {
    frictionAir: 0, friction: 0.0001, restitution: 0.6
});
World.add(engine.world, circle);

// Create a body from vertex (parsed from SVG path)
// Original vertexes
const originalMapVertices = [[..]];
const ground = Bodies.fromVertices(400, 400, originalMapVertices, { isStatic: true }, true);
World.add(engine.world, ground);

// On click to update vertices for polygon
const newVertices = [...]
Body.setVertices(ground, newVertices);

Before updating vertices:

image

After updating vertices (called setVertices):

image

Question

Why another physic body like a small polygon does not fall in the updated rectangle? Continues to stay in the same place, given that the vertices have already been updated.

ararati avatar Aug 24 '22 08:08 ararati

Thanks for the detailed report.

Could you try using Engine.clear(engine) after using Body.setVertices?

Also do you have poly-decomp set up in your project? See docs for Bodies.fromVertices for more info.

liabru avatar Aug 25 '22 12:08 liabru

Thanks for the answer! I tried to use Engine.clear but it does not help. I used the last version of poly-decomp.

I created the sandbox with dynamic updating vertices.

Sandbox control:

  • Left click - update vertices
  • Right click - add a circle

ararati avatar Aug 25 '22 19:08 ararati