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

Concave polygons not working even after I installed poly-decomp (node server) #559

Closed
gmoyer opened this issue Jan 21, 2018 · 28 comments
Closed

Comments

@gmoyer
Copy link

gmoyer commented Jan 21, 2018

I've installed poly-decomp with npm install poly-decomp. I then put decomp = require('poly-decomp') above matter = require('matter-js'), when I run my code, it still says poly-decomp.js required. I dug in matter.js code and changed line 6541 to decomp = require('poly-decomp'). It doesn't give the same message anymore, but my shape is still just convex.

I ran console.log(decomp) after line 6541, and it returned

{ decomp: [Function: polygonDecomp],
quickDecomp: [Function: polygonQuickDecomp],
isSimple: [Function: polygonIsSimple],
removeCollinearPoints: [Function: polygonRemoveCollinearPoints],
makeCCW: [Function: polygonMakeCCW] }

which I think is what it should return. I dug even farther and made sure that line 6763 was being run, and it is. Although, when I console.log(decomp.makeCCW(concave)); on line 6769, it returns undefined. What should I do to make it work? Right now it isn't giving me the error message, but it isn't making the shape concave.

@gmoyer gmoyer changed the title Concave polygons not working even after I installed poly-decomp Concave polygons not working even after I installed poly-decomp (node server) Jan 21, 2018
@bryantwells
Copy link

bryantwells commented Mar 12, 2018

Did you ever get this working? I am in the same boat, running a regular linux + apache server.

Also, I see that the SVG demo no longer works.

@bryantwells
Copy link

bryantwells commented Mar 12, 2018

It looks like there is an issue with compiled JS renaming the decomp, causing Bodies.fromVertices() to complain. Adding decomp.js as a separate file within the html page (loaded before the compiled scripts) seems to solve the problem.

@adapap
Copy link

adapap commented Apr 14, 2018

Hi @bryantwells, I'm still having what seems to the same issue. Could you provide me with the versions of poly-decomp and matter that you are using? In my setup I am keeping the current version on github for both programs and using them in my html file as well as requiring the static file for poly decompress in node, which is how I interpreted your response. Perhaps I am still doing something wrong, but it seems to run but then returns as undefined when I create the body from vertices.

@ghost
Copy link

ghost commented Apr 28, 2018

I am having the same issue.
Currently tried to use the import in my index.js file

import decomp from 'poly-decomp'
window.decomp = decomp

that did not solve the issue.

Then tried to include it in the HTML

<script src="./src/decomp.js"></script>
<script src="./src/index.js"></script>

Here are package versions.

"matter-js": "^0.14.1",
"poly-decomp": "^0.2.1"

@jobtalle
Copy link

jobtalle commented Jun 4, 2018

Same problem here while trying to compile a project using webpack. Is there any workaround for this issue?

I'm currently forced to abandon matter-js because of this.

@ghost
Copy link

ghost commented Jun 8, 2018

Hey @jobtalle,
If your using webpack I managed to get poly-decomp detected by including a couple lines to my webpack.config.js file.

At the top of your webpack file include...

const webpack = require('webpack')

... and in the plugins array insert

new webpack.ProvidePlugin({ 'window.decomp': 'poly-decomp' })

Hope this helps.

@fiskhandlarn
Copy link

fiskhandlarn commented Jun 10, 2018

@rykerrumsey Thank you, this solved it for me!

@danielkcz
Copy link
Contributor

I have been fighting with this for 2 hours now and nothing seems to be helping. The ProvidePlugin doesn't work like that, check out the docs. It would work in case there would be a window.decomp present in the code, not with dynamic require like that.

Essentially, the way it was before commit 0cf97f5 would be working just fine if you would have simply included "browser": "src/module/main.js" in the package.json. Webpack (and even the Browserify) respects that field and bundle matter-js by itself instead of grabbing the prebuilt file. That allows it to find that module just fine without any hacks.

@liabru Any chance you would be willing to change it like that or is there some other tool/environment that you are aware of not supporting such an approach?

@liabru
Copy link
Owner

liabru commented Jul 6, 2018

Are you using the latest build? It's supposed to work everywhere unless there's a bug. The idea is it will look for a global first (e.g. when in a browser) otherwise it will fall back to require implementation provided by browserify and since it is a UMD build it will first check the the bundled dependencies (which do not include poly-decomp) then it should fall back to the environment require.

Can you provide a stack trace and tell me more about your setup? What environment and build tools are you using?

@danielkcz
Copy link
Contributor

danielkcz commented Jul 6, 2018

Yes, I have the latest version. I am using create-react-app (not ejected) so pretty straightforward configuration for bundling (with Webpack). I haven't used Browserify for years, it would probably work like that since you have the shim there. However, Webpack is a different beast and doesn't care about those shims.

First of all, when Webpack is using your UMD bundle version, it won't bother parsing it (by default), so the require call will never reach outside to node_modules, only to those modules that are included in the bundle. That's how these things work. That's why I've suggested using browser field so matter-js can be built from source (added benefit is tree-shaking). Sadly that's not enough as dynamic require call is like it doesn't exist to Webpack. In case it would see require('poly-decomp') directly, it would be working flawlessly.

I have tested this approach and it does make much more sense than some hacks with webpack.ProvidePlugin or whatnot which probably work in some very specific setup.

@jobtalle
Copy link

The fix provided by @rykerrumsey did not work for me, unfortunately. Is there currently a way to circumvent this problem without modifying matter.js? As it stands, polygon decomposition is not working for webpack environments.

@liabru
Copy link
Owner

liabru commented Jul 19, 2018

Thanks for the info guys, sorry that this is still an issue. While I look into it more, I think this temporary fix should work with the latest version. Make sure you put it somewhere early on, before you use Bodies.fromVertices:

window.decomp = require('poly-decomp');

@liabru liabru reopened this Jul 19, 2018
@jordancaudill
Copy link

No solutions for me thus far...

@liabru liabru added the bug label Sep 8, 2018
@linux019
Copy link

linux019 commented Sep 25, 2018

all solutions above doesn't work
Webpack replaces
window.decomp = require('poly-decomp');
by
__webpack_provided_window_dot_decomp = __WEBPACK_IMPORTED_MODULE_0_poly_decomp___default.a;

so global window is remains untouched

@Dibble
Copy link

Dibble commented Feb 18, 2019

all solutions above doesn't work
Webpack replaces
window.decomp = require('poly-decomp');
by
__webpack_provided_window_dot_decomp = __WEBPACK_IMPORTED_MODULE_0_poly_decomp___default.a;

so global window is remains untouched

Using global.decomp = require('poly-decomp') is working for me with Webpack

@linux019
Copy link

Finally I switched to box2d-js and it's working like a charm. Matter js is very buggy engine

@marcelschiering
Copy link

Same problem here. Switched to box2d-js.

@david-crespo
Copy link

david-crespo commented Feb 25, 2019

This worked for me (using Parcel as the bundler):

import decomp from 'poly-decomp';
window.decomp = decomp;

@marcelschiering
Copy link

This worked for me (using Parcel as the bundler):

import decomp from 'poly-decomp';
window.decomp = decomp;

thx for your answer.
box2d is working, but i like matter-js more :-) so i tested something.
my game needs hexagons. first i used "Matter.Bodies.fromVertices" - ended up in this poly-decom-error-hell.
i solved the problem with "Matter.Bodies.polygon", using 6 sides and Matter.Body.rotate(30degrees), because i need the hexagon in flat-top design.
now it works. hopefully i can go on with basic geometries :-)

@liabru
Copy link
Owner

liabru commented Dec 29, 2020

Apologies for the problems here, it seems that every approach I had tried when originally using browserify for the build ended up having some edge cases depending on the environment.

The newly released version 0.15.0 is now built with webpack and has a more robust approach for handling the poly-decomp require.

I'd encourage those interested to try out the new version and let me know if this require problem is finally resolved.

That said it is still recommended to eventually implement your own decomposition approaches suitable for your use case, using the built in function as a reference of how set up compound bodies.

@cscuderi
Copy link

I'm now seeing this issue when running 0.15.0, when I didn't see it before.

@liabru
Copy link
Owner

liabru commented Jan 18, 2021

@cscuderi looks like there was a further issue in 0.15.0 with resolving poly-decomp in some cases.

Can you try the latest release 0.16.0 and let me know if that solves the issue if you can?

@cscuderi
Copy link

@liabru Gave it a shot, still seeing the issue though: Can't resolve 'poly-decomp'

@liabru
Copy link
Owner

liabru commented Jan 18, 2021

@cscuderi could you tell me about your setup? Node.js or browser, build tools, config etc.

@liabru
Copy link
Owner

liabru commented Jan 20, 2021

Using 0.16.0 I tried this out myself in a blank project using:

npm install matter-js --save and npm install poly-decomp --save

Then Bodies.fromVertices worked as expected for me. It also should work if decomp is defined globally, like in a browser (the demo does this).

So can anybody else confirm if this is working fine in their project using 0.16.0?

Much appreciated any comments or thumbs up here.

@liabru
Copy link
Owner

liabru commented Jan 20, 2021

In addition to the above: anybody who is having trouble still with this please let me know:

  • Can you confirm you have installed the dependency in the project e.g. npm install poly-decomp --save?
  • What was the last working version of matter-js for this?
  • What environment? npm / node version?
  • Can you share an example or project link?

@Yahnych
Copy link

Yahnych commented Jan 25, 2021

"npm install poly-decomp --save " helped me in new create-react-app project with matter 0.16.

@liabru
Copy link
Owner

liabru commented Feb 12, 2021

An update on this:

A few days ago I pushed version 0.16.1 which includes some changes intended to fix the issue that Webpack users were seeing on this. It seems this was due to Webpack attempting to fully resolve all dependencies at compile time rather than waiting until runtime as they are in Node.

The demo has now been updated to be built with Webpack, so it should be easier to spot these kinds of issues in future. I have also improved the docs for Bodies.fromVertices as well as improving the missing dependency message.

As I've not seen any further issues raised on this topic since the 0.16.1 release, I'll consider it resolved but do feel free to post here with the above requested info if you find problems. Thanks!

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

No branches or pull requests