Skip to content

Commit

Permalink
fixed #17867: [bug] b2RigidBody2D was not removed from b2PhysicsWorld…
Browse files Browse the repository at this point in the history
…._animatedBodies container if its type was changed.
  • Loading branch information
dumganhar committed Nov 18, 2024
1 parent bd2dc0a commit 4a79ad3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cocos/physics-2d/box2d-wasm/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d-wasm/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions cocos/physics-2d/box2d/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -82,7 +83,7 @@ export class b2RigidBody2D implements IRigidBody2D {
this.setActive(false);
}

nodeTransformChanged (type): void {
nodeTransformChanged (type: TransformBit): void {
if (PhysicsSystem2D.instance.stepping) {
return;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cocos/physics-2d/framework/physics-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 4a79ad3

Please sign in to comment.