matter-js
matter-js copied to clipboard
Poly-decomp and Uncaught TypeError: Cannot read properties of undefined (reading 'type')
When generating a body from vertices which represent a concave polygon I see the following error in the console in a Webpack/Typescript project using matterjs running in Chrome browser Version 98.0.4758.80 (Official Build) (x86_64) on an Intel Mac:
bundle.js?84db9c7fcca2acde6e77:307 Uncaught TypeError: Cannot read properties of undefined (reading 'type') at Object.Composite.add (matter.js?b8bf:1798:1) at eval (index.ts?ffb4:29:1) at Module../src/index.ts (bundle.js?84db9c7fcca2acde6e77:105:1) at __webpack_require__ (bundle.js?84db9c7fcca2acde6e77:304:33) at bundle.js?84db9c7fcca2acde6e77:1360:37 at bundle.js?84db9c7fcca2acde6e77:1362:12
but only when I use Common.setDecomp(require('poly-decomp'));
in the code.
I've debugged matterjs and I can see poly-decomp is imported and is being used by decomp.removeCollinearPoints
and decomp.removeDuplicatePoints
but it's reducing the number of sets of vertices to 6, rather than the 7 sets I have in this minimal reproduction use case: https://github.com/richbrill/matterjs-poly-decomp-bug
Uncommenting this line https://github.com/richbrill/matterjs-poly-decomp-bug/blob/afa97bec01ffc3fb87966469936b59f3e6151fdb/src/index.ts#L7 causes the issue
It feels like this might be a problem with poly-decomp but thought I may glean valuable insight by posting here.
For anyone who might stumble across this I worked around it by ensuring my concave polygon vertices were listed counter-clockwise in the first place as described in poly-decomp readme: https://github.com/schteppe/poly-decomp.js
I think there might still be an issue though because even after matterjs calls decomp.makeCCW(concave);
(passing in a clockwise concave polygon) matterjs still errored as above.
Thanks for the example case, hopefully it is just an issue with them not being CCW. Do you know if makeCCW
gives different results to your manual fix?
I can see why it's reducing the set of vertices from 7 to 6 since it calling decomp.removeDuplicatePoints
so no problem there but after debugging and passing in a set of counter-clockwise vertices the decomp.makeCCW(concave);
line in the Bodies.fromVertices
method transforms those vertices into a set of clockwise vertices which matterjs doesn't mind.
If I pass in a set of clockwise vertices it transforms them into a set of counter-clockwise vertices which matter errors on even though matterjs mentions vertices should be in clockwise order: https://brm.io/matter-js/docs/classes/Vertices.html (mind you, that is only for creating vertices by themselves rather than passing as a parameter to the fromVertices method).
(I've updated the repo with a newer commit which includes a commented out set of clockwise vertices): https://github.com/richbrill/matterjs-poly-decomp-bug/commit/efdea9252af8f0e5f32b1c7983c8bf775daa9f08
It does not mention clockwise/counter-clockwise ordering on https://brm.io/matter-js/docs/classes/Bodies.html#method_fromVertices though so wondering if it should be added there for future readers?