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

Matter.Bodies.fromVertices fix: no longer returns 'undefined' #1137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zhijie-os
Copy link

I was trying to create Body with the method Matter.Bodies.fromVertices.

I installed poly-decomp package to enable automatic concave vertices decomposition and supply the the method Matter.Bodies.fromVertices with vertices from drawing.

success

However, for certain sets of vertices, the method Matter.Bodies.fromVertices would return undefined at line 354 of ./src/factory/Bodies.js

            return parts[0];

no_parts_error

This is due to

  1. The quickDecomp() returns empty list of decomposition near line 278 of ./src/factory/Bodies.js
            // use the quick decomposition algorithm (Bayazit)
            var decomposed = decomp.quickDecomp(concave);
  1. The decomposition only contains small parts being skipped near line 293 of ./src/factory/Bodies.js
            // skip small chunks
            if (minimumArea > 0 && Vertices.area(chunkVertices) < minimumArea)
                  continue;

My change checks if the parts array is empty. If the array is empty, fallback to convex hull and ignore the decomposition.
The final result would look like the following.

no_parts_error_handled

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

Successfully merging this pull request may close these issues.

None yet

1 participant