n5ro / aframe-physics-system

Physics system for A-Frame VR, built on CANNON.js.

Home Page:https://n5ro.github.io/aframe-physics-system/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hitting crash in A-Frame Physics System (maybe when switching between kinematic & dynamic?)

diarmidmackenzie opened this issue · comments

Using Ammo.js in my app here:
https://diarmidmackenzie.github.io/christmas-scene/research/physics-ammo/full-test.html

Sometimes when using this, I hit the following crash (note, these are separate instances, 17 seconds apart).

image

No clear idea what the repro scenario is. Unpredictable when I hit this, but usually after a few minutes.

aframe-physics-system.js:16758 Uncaught TypeError: Cannot read properties of null (reading 'added')
at i.AmmoBody._updateShapes (aframe-physics-system.js:16758)
at i.beforeStep (aframe-physics-system.js:16915)
at o.tick (aframe-physics-system.js:19019)
at HTMLElement.value (a-scene.js:746)
at HTMLElement.value (a-scene.js:790)
at bind.js:12
at u (three.js:19157)
at t (three.js:10460)

The code we are hitting a bug in is here:

for (let j = 0; j < collisionShapes.length; j++) {
            const collisionShape = collisionShapes[j];
            if (!collisionShape.added) {
              this.compoundShape.addChildShape(collisionShape.localTransform, collisionShape);
              collisionShape.added = true;
            }
          }

Why would we be calling "_updateShapes" mid-simulation?

My guess is it is related to the fact that I frequently switch objects between kinetic & dynamic, as described here:
https://diarmidmackenzie.github.io/christmas-scene/research/README.md

("phase 2" is the bit that explains switching between kinetic & dynamic).

Can't see any other reason why shapes would be updated, as I am not modifying object geometry, or creating new objects.

So my guess is there is some sort of race condition when switching an object between dynamic & kinetic, where collision data is ot in the state that this function assumes.

I can imagine it being difficult to understand the exact repro scenario, but might a defensive fix be in order?

for (let j = 0; j < collisionShapes.length; j++) {
            const collisionShape = collisionShapes[j];
            if (collisionShape && 
                !collisionShape.added) {
              this.compoundShape.addChildShape(collisionShape.localTransform, collisionShape);
              collisionShape.added = true;
            }
          }

I'm going to try making this fix to a forked copy, and would be happy to submit a PR if it's wanted.

Any insights as to what might be a better fix would be appreciated.

I have come across a simple reliable desktop repro for this problem.

Not yet looked into what the actual cause is, but I suspect it's related to changing body type after initialization.

https://diarmidmackenzie.github.io/christmas-scene/research/physics-ammo/repro-for-issue-197.html

The link doesn't work for me.

Which link doesn't work?

In what way does it not work?

The link in your latest comment gave a 404 for me.
Now it works.