cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

Error when trying to destroy or deactive a node with rigibody and collider with box2D

Open BenoitFreslon opened this issue 1 year ago • 6 comments

Cocos Creator version

3.8 beta

System information

Windows 11

Issue description

The game causes an error if I destroy a node in the BEGIN_CONTANT event or update.

Example:

    setContactListeners() {
        const collider: Collider2D = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

        }
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log("onBeginContact");
        this.node.destroy();
    }

Relevant error log output

physics-contact.ts:236 Uncaught TypeError: Cannot read properties of null (reading 'enabledContactListener') at PhysicsContact.emit (physics-contact.ts:236:20) at PhysicsContactListener._onPreSolve [as _PreSolve] (physics-world.ts:415:11) at PhysicsContactListener.PreSolve (physics-contact-listener.ts:76:18) at b2PolygonAndCircleContact.Update (box2d.umd.js:11809:24) at b2ContactManager.Collide (box2d.umd.js:12479:17) at b2World.Step (box2d.umd.js:19588:33) at b2PhysicsWorld.step (physics-world.ts:161:21) at PhysicsSystem2D.postUpdate (physics-system.ts:290:31) at Director.tick (director.ts:692:38) at Game._updateCallback (game.ts:1027:22)

Steps to reproduce

Create a node with a collider and a rigibody and destroy the node with it hits another collider in the BEGIN_CONTACT event or update.

Minimal reproduction project

No response

BenoitFreslon avatar Jul 27 '23 13:07 BenoitFreslon

Same problem in CC 3.8.1 Tested with Box2D-wasm Based 2D Physics System. Same problem. I have to destroy the node at the lateUpdate event.

BenoitFreslon avatar Oct 25 '23 08:10 BenoitFreslon

I have the same issue and i cant fix this. Anyone can help us .-. bruhhh

m10barcp1 avatar Dec 13 '23 12:12 m10barcp1

me too

shuishen49 avatar Dec 18 '23 08:12 shuishen49

I tried change version to 3.7.1, and fix it by live call instead call by onContact or schedule, and it worked

m10barcp1 avatar Dec 18 '23 08:12 m10barcp1

Could anyone provide a demo? Thanks.

minggo avatar Dec 18 '23 09:12 minggo

SOLUTION : I have to destroy the node at the lateUpdate event.


private _isDestroyed:boolean = false;

 lateUpdate(deltaTime: number) {
      if (this._isDestroyed) {
        this.node.destroy();
      }
  }

    setContactListeners() {
        const collider: Collider2D = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

        }
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log("onBeginContact");
        this._isDestroyed = true;
    }

BenoitFreslon avatar Dec 18 '23 09:12 BenoitFreslon

Node.destory() will set node's active to false, which will set all components of the node to false too. And set active to false in contact call back will cause exceptions in box2d. So it is the limitation of box2d to setActive to false in contact call back.

minggo avatar Apr 03 '24 03:04 minggo

i still meet this issue on v3.8.3. @BenoitFreslon 's solution works well but i hope this issue will be fixed officially.

ryushinnn avatar Aug 08 '24 10:08 ryushinnn