liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rendering composite bodies

mreinstein opened this issue · comments

I've got 2 circles in my body:

const partA = Matter.Bodies.circle(50, 50, 10)
const partB = Matter.Bodies.circle(70, 50, 10, { render: partA.render })

const boxA = Matter.Body.create({ parts: [ partA, partB ] })

Matter.Composite.add(engine.world, [ boxA ])

I use the .vertices property to render this:

const bodies = Matter.Composite.allBodies(engine.world)
for (const vertices of bodies) {
       ctx.moveTo(vertices[0].x, vertices[0].y)

       for (let j = 1; j < vertices.length; j += 1)
           ctx.lineTo(vertices[j].x, vertices[j].y)

       ctx.lineTo(vertices[0].x, vertices[0].y)
}

This renders an outline around the whole composite body:
Screenshot 2023-09-24 at 1 22 11 PM

This wasn't expected; I thought I would see 2 circles rendered seperately.

Is the actual collision object the individual part shapes, or is it using this combined outline shape when doing the collision handling?

Said differently, should I be rendering the individual bodies to get the true collision shape, or is this combined outline some special feature of matter, where it builds a new collision shape for me?

If you add a mouse constraint, it should be apparent that the collision shape is defined by the two circles, while the outline you're drawing is the vertices of the "box" composite. So, yes, you should be rendering the individual bodies to get the true collision shape.

Here's a complete, runnable example with the circles spaced further apart. You should be able to drag your mouse all the way through without triggering a collision between the mouse and the body. But if you grab the circles towards the ends, it'll collide.

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
canvas {
  border: 1px solid black;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
</head>
<body>
<canvas></canvas>
<script>
const engine = Matter.Engine.create();
engine.gravity.y = 0;
const partA = Matter.Bodies.circle(50, 50, 10);
const partB = Matter.Bodies.circle(100, 50, 10);
const boxA = Matter.Body.create({parts: [partA, partB]});
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const mouseConstraint = Matter.MouseConstraint.create(engine, {
  element: canvas,
});
Matter.Composite.add(engine.world, [boxA, mouseConstraint]);
const bodies = Matter.Composite.allBodies(engine.world);

let last = 0;
(function rerender(ts) {
  const dt = Math.min(ts - last, 50);
  last = ts;
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  for (const body of bodies) {
    const {x, y} = body.vertices[0];
    ctx.beginPath();
    ctx.moveTo(x, y);
    body.vertices.forEach(({x, y}) => ctx.lineTo(x, y));
    ctx.lineTo(x, y);
    ctx.stroke();
  }

  Matter.Engine.update(engine, dt);
  requestAnimationFrame(rerender);
})();
</script>
</body>
</html>

That makes sense. Thanks for the clarification!