Skip to content

Commit

Permalink
fixed cocos#17867: [bug] b2RigidBody2D was not removed from b2Physics…
Browse files Browse the repository at this point in the history
…World _animatedBodies container if its type was changed. (cocos#17873)
  • Loading branch information
dumganhar authored and qiuguohua committed Nov 26, 2024
1 parent c9078b4 commit cd4e4ea
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cocos/particle-2d/particle-system-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,9 @@ export class ParticleSystem2D extends UIRenderer {
private _positionType = PositionType.FREE;

private _stopped = true;
private declare _previewTimer;
private declare _previewTimer: number | null;
private declare _focused: boolean;
private declare _plistFile;
private declare _plistFile: string;
private declare _tiffReader;
private _useFile: boolean;

Expand Down Expand Up @@ -849,7 +849,7 @@ export class ParticleSystem2D extends UIRenderer {
}
}

protected lateUpdate (dt): void {
protected lateUpdate (dt: number): void {
if (!this._simulator.finished) {
this._simulator.step(dt);
}
Expand Down Expand Up @@ -967,7 +967,7 @@ export class ParticleSystem2D extends UIRenderer {
}
});
} else if (dict.textureImageData) {
const textureData = dict.textureImageData;
const textureData = dict.textureImageData as string;

if (textureData && textureData.length > 0) {
let imgPathName = imgPath;
Expand Down
13 changes: 13 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,19 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}
}

public _updateBodyType$ (body: B2RigidBody2D): void {
const animatedBodies = this._animatedBodies;
const comp = body.rigidBody;
if (comp.type !== ERigidBody2DType.Animated) {
js.array.remove(animatedBodies, body);
} else {
if (animatedBodies.includes(body)) {
return;
}
animatedBodies.push(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
13 changes: 13 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,19 @@ export class b2PhysicsWorld implements IPhysicsWorld {
}
}

public _updateBodyType$ (body: b2RigidBody2D): void {
const animatedBodies = this._animatedBodies;
const comp = body.rigidBody;
if (comp.type !== ERigidBody2DType.Animated) {
js.array.remove(animatedBodies, body);
} else {
if (animatedBodies.includes(body)) {
return;
}
animatedBodies.push(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 cd4e4ea

Please sign in to comment.