piqnt / planck.js

2D JavaScript Physics Engine
http://piqnt.com/planck.js/
MIT License
4.87k stars 236 forks source link

Using planck in a webworker #160

Open zjeff222 opened 3 years ago

zjeff222 commented 3 years ago

I don't know anything about webpack but it seams to be blocking planck from loading into a web worker. There is an access to the window element, which is not defined in a worker, and it really seams to be webpack related.

So I replaced the "window" reference to "this" and it works, but I really don't know what I'm doing!

shakiba commented 3 years ago

Might be related to this: https://github.com/webpack/webpack/issues/6525

zjeff222 commented 3 years ago

Just to be clear, I was talking about the webpack code embeded in planck.js. So I edited line 34 of the planck.js file. Patching the library will do for me for now. It's great to have box2d on javascript by the way :)

shakiba commented 3 years ago

Could you please try the solution described in this and this comments?

zjeff222 commented 3 years ago

If I understand, this would be a solution where I am to use webpack. I'm not using webpack. So if I do understand, some webpack code is added to planck.js to make it better for those who use webpack ? I only used 2-3 libraries up to now and it's the first one I see with embeded webpack code. (I'm new to js)

shakiba commented 3 years ago

Webpack combines source files and generates files like planck.js file, Planck distribution files are built with webpack. We need to configure webpack to fix the issue that you reported in the build file.

shakiba commented 3 years ago

Oh, what I meant by asking if that solution works for you was that if rebuilding planck with that solution will solve the problem. I did not mean to use that solution in your project.

zjeff222 commented 3 years ago

Sorry, I don't want to learn how to build a js library at this time. If you want to reproduce the problem, you only have to call a worker with myWorker = new Worker('./worker.js'); and in the worker.js import planck: importScripts('../../lib/planck.min.js');

shakiba commented 3 years ago

Makes sense, no worries; if you need to build it for any other reason here are the commands (assuming git and npm are already installed):

git clone git@github.com:shakiba/planck.js.git
cd planck.js
npm install
npm run build