Skip to content

Commit

Permalink
Fix the issue that can not destroy Node in contract listener call bac…
Browse files Browse the repository at this point in the history
…k in Box2D (cocos#17701)

(cherry picked from commit 186fa0f)

# Conflicts:
#	cocos/physics-2d/box2d-wasm/rigid-body.ts
#	cocos/physics-2d/box2d/rigid-body.ts
#	native/external-config.json
  • Loading branch information
minggo authored and AILHC committed Oct 17, 2024
1 parent b01b257 commit 57fc90f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cocos/physics-2d/box2d-wasm/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
THE SOFTWARE.
*/

import { DEBUG } from 'internal:constants';
import { B2, B2ObjectType, getTSObjectFromWASMObjectPtr } from './instantiated';
import { IRigidBody2D } from '../spec/i-rigid-body';
import { RigidBody2D } from '../framework/components/rigid-body-2d';
import { PhysicsSystem2D } from '../framework/physics-system';
import { B2PhysicsWorld } from './physics-world';
import { Vec2, toRadian, Vec3, Quat, IVec2Like, toDegree, TWO_PI, HALF_PI } from '../../core';
import { Vec2, toRadian, Vec3, Quat, IVec2Like, TWO_PI, HALF_PI, warn } from '../../core';
import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-types';

import { Node } from '../../scene-graph/node';
Expand Down Expand Up @@ -249,7 +250,11 @@ export class B2RigidBody2D implements IRigidBody2D {
return this._body!.IsEnabled();
}
setActive (v: boolean): void {
this._body!.SetEnabled(v);
if (!this._body!.GetWorld().IsLocked()) {
this._body!.SetEnabled(v);
} else if (DEBUG) {
warn('Can not active RigidBody in contract listener.');
}
}
wakeUp (): void {
this._body!.SetAwake(true);
Expand Down
9 changes: 7 additions & 2 deletions cocos/physics-2d/box2d/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
*/

import b2 from '@cocos/box2d';
import { DEBUG } from 'internal:constants';
import { IRigidBody2D } from '../spec/i-rigid-body';
import { RigidBody2D } from '../framework/components/rigid-body-2d';
import { PhysicsSystem2D } from '../framework/physics-system';
import { b2PhysicsWorld } from './physics-world';
import { Vec2, toRadian, Vec3, Quat, IVec2Like, toDegree, TWO_PI, HALF_PI } from '../../core';
import { Vec2, toRadian, Vec3, Quat, IVec2Like, TWO_PI, HALF_PI, warn } from '../../core';
import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-types';

import { Node } from '../../scene-graph/node';
Expand Down Expand Up @@ -228,7 +229,11 @@ export class b2RigidBody2D implements IRigidBody2D {
return this._body!.IsActive();
}
setActive (v: boolean): void {
this._body!.SetActive(v);
if (!this._body!.m_world.IsLocked()) {
this._body!.SetActive(v);
} else if (DEBUG) {
warn('Can not active RigidBody in contract listener.');
}
}
wakeUp (): void {
this._body!.SetAwake(true);
Expand Down

0 comments on commit 57fc90f

Please sign in to comment.