Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rendering composite bodies #1244

Closed
mreinstein opened this issue Sep 24, 2023 · 2 comments
Closed

rendering composite bodies #1244

mreinstein opened this issue Sep 24, 2023 · 2 comments

Comments

@mreinstein
Copy link

mreinstein commented Sep 24, 2023

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?

@ggorlen
Copy link

ggorlen commented Oct 2, 2023

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>

@mreinstein
Copy link
Author

That makes sense. Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants