Skip to content

Commit

Permalink
[v3.8.6] Optimize code size, box2d-js backend. (#18167)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar authored Jan 13, 2025
1 parent b7835fc commit e939677
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 22 deletions.
4 changes: 4 additions & 0 deletions EngineErrorMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -3892,3 +3892,7 @@ Can't find the spriteFrame of tilesets %s
### 16407

Spline error: invalid mode

### 16408

[Physics2D] b2PolygonShape failed to decompose polygon into convex polygons, node name: %s
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/distance-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d';
import { DistanceJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';

/** @mangle */
export class b2DistanceJoint extends b2Joint implements IDistanceJoint {
setMaxLength (v: number): void {
if (this._b2joint) {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d';
import { FixedJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';

/** @mangle */
export class b2FixedJoint extends b2Joint implements IFixedJoint {
setFrequency (v: number): void {
if (this._b2joint) {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/hinge-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { b2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

/** @mangle */
export class b2HingeJoint extends b2Joint implements IHingeJoint {
enableLimit (v: boolean): void {
if (this._b2joint) {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { b2PhysicsWorld } from '../physics-world';

/** @mangle */
export class b2Joint implements IJoint2D {
get impl (): b2.Joint | null {
return this._b2joint;
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/mouse-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { find } from '../../../scene-graph';

const tempB2Vec2 = new b2.Vec2();

/** @mangle */
export class b2MouseJoint extends b2Joint implements IMouseJoint {
_touchPoint = new Vec2();
_isTouched = false;
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/relative-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { RelativeJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

/** @mangle */
export class b2RelativeJoint extends b2Joint implements IRelativeJoint {
setMaxForce (v: number): void {
if (this._b2joint) {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/slider-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { SliderJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

/** @mangle */
export class b2SliderJoint extends b2Joint implements ISliderJoint {
// limit
enableLimit (v: boolean): void {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/spring-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d';
import { SpringJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';

/** @mangle */
export class b2SpringJoint extends b2Joint implements ISpringJoint {
setDampingRatio (v: number): void {
if (this._b2joint) {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/joints/wheel-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { b2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

/** @mangle */
export class b2WheelJoint extends b2Joint implements IWheelJoint {
setDampingRatio (v: number): void {
if (this._b2joint) {
Expand Down
3 changes: 2 additions & 1 deletion cocos/physics-2d/box2d/physics-contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { b2Shape2D } from './shapes/shape-2d';
import { IPhysics2DContact, IPhysics2DImpulse, IPhysics2DManifoldPoint, IPhysics2DWorldManifold } from '../spec/i-physics-contact';

export type b2ContactExtends = b2.Contact & {
m_userData: any
m_userData: any;
}

const pools: PhysicsContact[] = [];
Expand Down Expand Up @@ -66,6 +66,7 @@ const impulse: IPhysics2DImpulse = {
tangentImpulses: [] as number[],
};

/** @mangle */
export class PhysicsContact implements IPhysics2DContact {
static get (b2contact: b2ContactExtends): PhysicsContact {
let c = pools.pop();
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const tempB2AABB = new b2.AABB();

const testResults: Collider2D[] = [];

/** @mangle */
export class b2PhysicsWorld implements IPhysicsWorld {
protected _world: b2.World;
protected _bodies: b2RigidBody2D[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import b2 from '@cocos/box2d';
import { Vec2 } from '../../../core';

/** @mangle */
export class PhysicsAABBQueryCallback extends b2.QueryCallback {
_point = new b2.Vec2();
_isPoint = false;
Expand Down
35 changes: 21 additions & 14 deletions cocos/physics-2d/box2d/platform/physics-contact-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,39 @@

import b2 from '@cocos/box2d';
import { js } from '../../../core';
import type { b2ContactExtends } from '../physics-contact';

type BeginContactCallback = (contact: b2ContactExtends) => void;
type EndContactCallback = (contact: b2ContactExtends) => void;
type PreSolveCallback = (contact: b2ContactExtends, oldManifold: b2.Manifold) => void;
type PostSolveCallback = (contact: b2ContactExtends, impulse: b2.ContactImpulse) => void;

/** @mangle */
export class PhysicsContactListener extends b2.ContactListener {
_contactFixtures: b2.Fixture[] = [];

_BeginContact: Function | null = null;
_EndContact: Function | null = null;
_PreSolve: Function | null = null;
_PostSolve: Function | null = null;
_BeginContact: BeginContactCallback | null = null;
_EndContact: EndContactCallback | null = null;
_PreSolve: PreSolveCallback | null = null;
_PostSolve: PostSolveCallback | null = null;

setBeginContact (cb): void {
setBeginContact (cb: BeginContactCallback): void {
this._BeginContact = cb;
}

setEndContact (cb): void {
setEndContact (cb: EndContactCallback): void {
this._EndContact = cb;
}

setPreSolve (cb): void {
setPreSolve (cb: PreSolveCallback): void {
this._PreSolve = cb;
}

setPostSolve (cb): void {
setPostSolve (cb: PostSolveCallback): void {
this._PostSolve = cb;
}

BeginContact (contact: b2.Contact): void {
BeginContact (contact: b2ContactExtends): void {
if (!this._BeginContact) return;

const fixtureA = contact.GetFixtureA();
Expand All @@ -64,30 +71,30 @@ export class PhysicsContactListener extends b2.ContactListener {
}
}

EndContact (contact: b2.Contact): void {
EndContact (contact: b2ContactExtends): void {
if (this._EndContact && (contact as any)._shouldReport) {
(contact as any)._shouldReport = false;
this._EndContact(contact);
}
}

PreSolve (contact: b2.Contact, oldManifold: b2.Manifold): void {
PreSolve (contact: b2ContactExtends, oldManifold: b2.Manifold): void {
if (this._PreSolve && (contact as any)._shouldReport) {
this._PreSolve(contact, oldManifold);
}
}

PostSolve (contact: b2.Contact, impulse: b2.ContactImpulse): void {
PostSolve (contact: b2ContactExtends, impulse: b2.ContactImpulse): void {
if (this._PostSolve && (contact as any)._shouldReport) {
this._PostSolve(contact, impulse);
}
}

registerContactFixture (fixture): void {
registerContactFixture (fixture: b2.Fixture): void {
this._contactFixtures.push(fixture);
}

unregisterContactFixture (fixture): void {
unregisterContactFixture (fixture: b2.Fixture): void {
js.array.remove(this._contactFixtures, fixture);
}
}
9 changes: 5 additions & 4 deletions cocos/physics-2d/box2d/platform/physics-debug-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const _tmp_color = new Color();
const GREEN_COLOR = Color.GREEN;
const RED_COLOR = Color.RED;

/** @mangle */
export class PhysicsDebugDraw extends b2.Draw {
_drawer: Graphics | null = null;

Expand All @@ -44,11 +45,11 @@ export class PhysicsDebugDraw extends b2.Draw {
this._drawer = drawer;
}

_DrawPolygon (vertices, vertexCount): void {
_DrawPolygon (vertices: Vec2[], vertexCount: number): void {
const drawer = this._drawer!;

for (let i = 0; i < vertexCount; i++) {
b2.Transform.MulXV(this._xf, vertices[i] as Vec2, _tmp_vec2);
b2.Transform.MulXV(this._xf, vertices[i], _tmp_vec2);
const x = _tmp_vec2.x * PHYSICS_2D_PTM_RATIO;
const y = _tmp_vec2.y * PHYSICS_2D_PTM_RATIO;
if (i === 0) drawer.moveTo(x, y);
Expand All @@ -60,13 +61,13 @@ export class PhysicsDebugDraw extends b2.Draw {
drawer.close();
}

DrawPolygon (vertices, vertexCount, color): void {
DrawPolygon (vertices: Vec2[], vertexCount: number, color: Color): void {
this._applyStrokeColor(color);
this._DrawPolygon(vertices, vertexCount);
this._drawer!.stroke();
}

DrawSolidPolygon (vertices, vertexCount, color): void {
DrawSolidPolygon (vertices: Vec2[], vertexCount: number, color: Color): void {
this._applyFillColor(color);
this._DrawPolygon(vertices, vertexCount);
this._drawer!.fill();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import b2 from '@cocos/box2d';
import { Vec2 } from '../../../core';
import { ERaycast2DType } from '../../framework';

/** @mangle */
export class PhysicsRayCastCallback extends b2.RayCastCallback {
_type = ERaycast2DType.Closest;
_fixtures: b2.Fixture[] = [];
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const tempVec3 = new Vec3();

const tempVec2_1 = new b2.Vec2();

/** @mangle */
export class b2RigidBody2D implements IRigidBody2D {
get impl (): b2.Body | null {
return this._body;
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/shapes/box-shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Vec2, Rect } from '../../../core';

const tempAabb = new Rect();

/** @mangle */
export class b2BoxShape extends b2Shape2D implements IBoxShape {
_worldPoints: Vec2[] = [new Vec2(), new Vec2(), new Vec2(), new Vec2()];
get worldPoints (): Readonly<Vec2>[] {
Expand Down
1 change: 1 addition & 0 deletions cocos/physics-2d/box2d/shapes/circle-shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { ICircleShape } from '../../spec/i-physics-shape';
import { Vec2 } from '../../../core';

/** @mangle */
export class b2CircleShape extends b2Shape2D implements ICircleShape {
get worldRadius (): number {
return (this._shapes[0] as b2.CircleShape).m_radius * PHYSICS_2D_PTM_RATIO;
Expand Down
5 changes: 3 additions & 2 deletions cocos/physics-2d/box2d/shapes/polygon-shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import * as PolygonPartition from '../../framework/utils/polygon-partition';
import { PolygonCollider2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { IPolygonShape } from '../../spec/i-physics-shape';
import { Vec2 } from '../../../core';
import { logID, Vec2 } from '../../../core';

/** @mangle */
export class b2PolygonShape extends b2Shape2D implements IPolygonShape {
_worldPoints: Vec2[] = [];
get worldPoints (): Readonly<Vec2>[] {
Expand Down Expand Up @@ -61,7 +62,7 @@ export class b2PolygonShape extends b2Shape2D implements IPolygonShape {

const polys = PolygonPartition.ConvexPartition(points);
if (!polys) {
console.log('[Physics2D] b2PolygonShape failed to decompose polygon into convex polygons, node name: ', comp.node.name);
logID(16408, comp.node.name);
return shapes;
}

Expand Down
3 changes: 2 additions & 1 deletion cocos/physics-2d/box2d/shapes/shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const tempFilter = new b2.Filter();
const lowerBound = new b2.Vec2();
const upperBound = new b2.Vec2();

function getFilter (shape: b2Shape2D): any {
function getFilter (shape: b2Shape2D): b2.Filter {
const comp = shape.collider;
if (comp.body) {
tempFilter.categoryBits = comp.group === PhysicsGroup.DEFAULT ? comp.body.group : comp.group;
Expand All @@ -45,6 +45,7 @@ function getFilter (shape: b2Shape2D): any {
return tempFilter;
}

/** @mangle */
export class b2Shape2D implements IBaseShape {
protected _shapes: b2.Shape[] = [];
protected _fixtures: b2.Fixture[] = [];
Expand Down

0 comments on commit e939677

Please sign in to comment.