piqnt / planck.js

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

Cannot read properties of undefined (reading "x") #280

Open codingMASTER398 opened 2 months ago

codingMASTER398 commented 2 months ago

I'm currently developing a game that uses p5play, and thus planck.js under the hood.

There's been a ton of circumstances where I've come across the same error- Cannot read properties of undefined (reading "x") in planck.js, scattered across a ton of functions- from Distance, to vector transforms, all stemming from the step call.

From my hours of debugging these countless errors, I have never been able to pin it down to one function call, one variable that was NaN, or anything of the sorts. It's seemingly completely random, and drives me completely insane.

This is a plea to have an option to attempt to catch these kinds of errors- when there's something undefined, NaN or unusable, I expect the library to be able to tell me. Something similar to p5's friendly error system, non-existent in the minified version, perhaps, or handling these sorts of issues in a way where my entire program doesn't crash, and the offender is just excluded from the physics calculation.

All of these errors seem to look the same:

TypeError: Cannot read properties of undefined (reading 'x')
    at transformVec2 (eval at worldInit (/home/ubuntu/me-when/server/index.js:417:16), <anonymous>:2408:28) [planck line 2408]
    at Distance (eval at worldInit (/home/ubuntu/me-when/server/index.js:417:16), <anonymous>:4590:13) [planck line 4590]
    at TimeOfImpact (eval at worldInit (/home/ubuntu/me-when/server/index.js:417:16), <anonymous>:5368:13)  [planck line etc, etc]
    at Solver.solveWorldTOI (eval at worldInit (/home/ubuntu/me-when/server/index.js:417:16), <anonymous>:6140:25)
    at World.step (eval at worldInit (/home/ubuntu/me-when/server/index.js:417:16), <anonymous>:8692:31)
    at World.step (eval at worldInit (/home/ubuntu/me-when/server/index.js:418:16), <anonymous>:6285:10)
    at draw (eval at worldInit (/home/ubuntu/me-when/server/index.js:429:16), <anonymous>:382:19)
    at e.default.redraw (eval at worldInit (/home/ubuntu/me-when/server/index.js:416:16), <anonymous>:2:545491)
    at _draw (eval at worldInit (/home/ubuntu/me-when/server/index.js:416:16), <anonymous>:2:463338)
    at Timeout.task [as _onTimeout] (/home/ubuntu/me-when/server/node_modules/jsdom/lib/jsdom/browser/Window.js:552:19)

If I edit planck's code to try/catch that one function, another one appears. There's no way to tell what sprite or circumstances causes it, and thus no easy way to replicate it. It's such a frustrating issue that makes me want to just quit.

Please, any help here?

shakiba commented 2 months ago

I remember there was a similar issue here: https://github.com/piqnt/planck.js/issues/269

Does it solve your issue?

codingMASTER398 commented 2 months ago

No, it doesn't- looking at the thread there, it was a specific circumstance where they had direct access to Planck and put in something blatantly wrong.

In my case, I don't use any direct access to Planck, and input validation seems to be the issue. It happens a lot of times throughout my project, in seemingly random places, caused by different things.

Something I'll try tomorrow is to put input validation into every crevace I can find in p5play, as I do have some knowledge on editing that.

shakiba commented 2 months ago

There is a variable (ASSERT) defined in "vite.config.js", you can set it to true to get more typing validation.

You need to clone and build the project locally, tho.