-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
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! |
I've got 2 circles in my body:
I use the
.vertices
property to render this:This renders an outline around the whole composite body:
![Screenshot 2023-09-24 at 1 22 11 PM](https://private-user-images.githubusercontent.com/718067/270189895-98b95b4c-a6ca-4f64-acf2-8af02c8199aa.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2ODU1NzcsIm5iZiI6MTcyMDY4NTI3NywicGF0aCI6Ii83MTgwNjcvMjcwMTg5ODk1LTk4Yjk1YjRjLWE2Y2EtNGY2NC1hY2YyLThhZjAyYzgxOTlhYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxMVQwODA3NTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03ZjY1ODc1MmExMjU3YmU4Njk4ZmY5YTY0MzM0MzE1M2Y0NzAzOTkzZDAwZGZjYWJjMDU5ZjA2NTAxYzQzYmQ1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.SKPBNEp2ZzAxBVX9sRlvHUuAtNfmerWMvQcHvxJTJ2c)
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?
The text was updated successfully, but these errors were encountered: