Im working in a videogame made with MatterJS,
Im using Bodies.fromVertices to create an irregular polygon. This vertices of this polygon should be dynamic, so should change in time. I already have the values of the new vertices in each frame, so I only need to set it to the polygon. Body.setVertices didnt work for me, so the only way I founded to do that was removing the polygon and creating another one with the new vertices, in the same position, for each frame.
All is working smoothly except for the position of the polygon.
The position is generated around the center of mass, and its impossible to me to have always the same position, the polygon moves chaotically everywhere when I change the vertices. I tryied by calculating the width/height of the polygon with the bounds.min/max/.x/.y, and move it but its now working, i think because the polygon can take any shape, you never know where will be positioned and how to compensate for that movement.
Do you know if there is another way to set the new vertices without remove/create the previous polygon, or if there is any way to get this work as it is?
This is the code where I remove the previous polygon, and I add the new one. The variable "vert" have new vertices each frame. "xPos" and "yPos" is where i want the polygon, this value never changes but the polygon is moving anyway.
Composite.remove(engine.world, groundFromVertices);
groundFromVertices = Bodies.fromVertices(xPos, yPos, vert, {isStatic : true,
render: {
fillStyle: 'red',
strokeStyle: 'white',
lineWidth: 5
}}, false, 0, 0, 0, 0);
Composite.add(engine.world, groundFromVertices);
Thank you so much in advice