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

Convex bodies with duplicate points don't collide

Open thomaswp opened this issue 3 years ago • 3 comments

When creating a body from vertices, if there is a duplicate point in the vertex list, the body will not collide with anything. While I suppose this isn't ideal input, it's probably a common mistake to make. (For me it was because I was creating the shape dynamically from an image, using test points and a convex hull.)

For example:

var customShape = Bodies.fromVertices(
    0, 0, [
        Vector.create(0, 0),
        Vector.create(80, 0),
        // Duplicate point: Remove this and it works
        Vector.create(80, 80),
        Vector.create(80, 80),
        // Closing point: Add this and it work
        // Vector.create(0, 80),
    ],
);

Creates a triangle which just passes through other bodies.

The issues doesn't occur if the point isn't duplicated, and also seems to be fixed if I add a closing point (see commented line).

If this is intentional behavior, it would good to explain it in the docs.

I fixed it by manually removing collinear and duplicate points using the decomp library, which is already done for concave shapes.

Thanks for the nice, simple physics library!

thomaswp avatar Sep 24 '21 18:09 thomaswp

Are you using the latest version of Matter and decomp? I think Bodies.fromVertices should have these cases covered by the removeCollinear and removeDuplicatePoints feature but let me know if these are not effective.

liabru avatar Dec 20 '21 11:12 liabru

@liabru Yes, I was using the most recent version at the time I posted. Have you tried my sample code to reproduce?

You're correct that Bodies.fromVertices calls removeCollinear and removeDuplicates, but only for concave shapes. See this line. If it's convex, it skips those steps, I assume for efficiency. However, when there are duplicate points this causes an issue, regardless of whether it's convex.

thomaswp avatar Dec 20 '21 16:12 thomaswp

Ah sorry yes I see what you mean now, thanks for clarifying.

For now you should preprocess your vertices to avoid these cases, looks like I need to extend those features to work with all vertices.

liabru avatar Dec 20 '21 17:12 liabru