diff --git a/cocos/physics-2d/box2d-wasm/physics-world.ts b/cocos/physics-2d/box2d-wasm/physics-world.ts index 07882428b10..9311aa38f94 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -345,6 +345,17 @@ export class B2PhysicsWorld implements IPhysicsWorld { } } + public _updateBodyType$ (body: B2RigidBody2D): void { + if (!this._animatedBodies.includes(body)) { + return; + } + + const comp = body.rigidBody; + if (comp.type !== ERigidBody2DType.Animated) { + js.array.remove(this._animatedBodies, body); + } + } + registerContactFixture (fixture: number): void { //B2.Fixture ptr this._contactListener.registerContactFixture(fixture); } diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index 5027bef2b72..2e5a7ef0e1a 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -222,6 +222,7 @@ export class B2RigidBody2D implements IRigidBody2D { } setType (v: ERigidBody2DType): void { + (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld)._updateBodyType$(this); if (v === ERigidBody2DType.Dynamic) { this._body!.SetType(B2.BodyType.b2_dynamicBody as B2.BodyType); } else if (v === ERigidBody2DType.Kinematic) { diff --git a/cocos/physics-2d/box2d/physics-world.ts b/cocos/physics-2d/box2d/physics-world.ts index d1ffc8df3a1..d8370e07a9b 100644 --- a/cocos/physics-2d/box2d/physics-world.ts +++ b/cocos/physics-2d/box2d/physics-world.ts @@ -326,6 +326,17 @@ export class b2PhysicsWorld implements IPhysicsWorld { } } + public _updateBodyType$ (body: b2RigidBody2D): void { + if (!this._animatedBodies.includes(body)) { + return; + } + + const comp = body.rigidBody; + if (comp.type !== ERigidBody2DType.Animated) { + js.array.remove(this._animatedBodies, body); + } + } + registerContactFixture (fixture: b2.Fixture): void { this._contactListener.registerContactFixture(fixture); } diff --git a/cocos/physics-2d/box2d/rigid-body.ts b/cocos/physics-2d/box2d/rigid-body.ts index 5691586bd3b..d4c66f62638 100644 --- a/cocos/physics-2d/box2d/rigid-body.ts +++ b/cocos/physics-2d/box2d/rigid-body.ts @@ -33,6 +33,7 @@ import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-typ import { Node } from '../../scene-graph/node'; import { Collider2D } from '../framework'; +import { TransformBit } from '../../scene-graph/node-enum'; const tempVec3 = new Vec3(); @@ -82,7 +83,7 @@ export class b2RigidBody2D implements IRigidBody2D { this.setActive(false); } - nodeTransformChanged (type): void { + nodeTransformChanged (type: TransformBit): void { if (PhysicsSystem2D.instance.stepping) { return; } @@ -160,7 +161,7 @@ export class b2RigidBody2D implements IRigidBody2D { const pos = this._rigidBody.node.worldPosition; - let temp; + let temp: b2.Vec2; const bodyType = this._rigidBody.type; if (bodyType === ERigidBody2DType.Animated) { temp = b2body.GetLinearVelocity(); @@ -207,6 +208,7 @@ export class b2RigidBody2D implements IRigidBody2D { } setType (v: ERigidBody2DType): void { + (PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld)._updateBodyType$(this); this._body!.SetType(v as number); } setLinearDamping (v: number): void { diff --git a/cocos/physics-2d/framework/physics-system.ts b/cocos/physics-2d/framework/physics-system.ts index 1cc2ad1740e..f000db614ca 100644 --- a/cocos/physics-2d/framework/physics-system.ts +++ b/cocos/physics-2d/framework/physics-system.ts @@ -23,7 +23,7 @@ */ import { BUILD, EDITOR_NOT_IN_PREVIEW, LOAD_BOX2D_MANUALLY } from 'internal:constants'; -import { System, Vec2, IVec2Like, Rect, Eventify, Enum, Settings, settings, cclegacy, SettingsCategory } from '../../core'; +import { System, Vec2, IVec2Like, Rect, Eventify, Enum, settings, cclegacy, SettingsCategory } from '../../core'; import { createPhysicsWorld, selector, IPhysicsSelector } from './physics-selector'; import { DelayEvent } from './physics-internal-types';