From 7342b0276bc20f63ed5d75e34feab783a914cf8b Mon Sep 17 00:00:00 2001 From: lealzhan Date: Sun, 8 Oct 2023 15:52:23 +0800 Subject: [PATCH 1/5] node scale cause box2d shape recreate, low perf --- cocos/physics-2d/box2d-wasm/physics-world.ts | 12 ++- .../platform/physics-aabb-query-callback.ts | 12 +-- .../platform/physics-ray-cast-callback.ts | 13 ++-- .../box2d-wasm/shapes/box-shape-2d.ts | 5 +- .../box2d-wasm/shapes/circle-shape-2d.ts | 19 +++-- .../box2d-wasm/shapes/polygon-shape-2d.ts | 8 +- .../physics-2d/box2d-wasm/shapes/shape-2d.ts | 75 +++++++++++++------ 7 files changed, 92 insertions(+), 52 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/physics-world.ts b/cocos/physics-2d/box2d-wasm/physics-world.ts index c0997b14915..68f8e4bf510 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -64,6 +64,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { private _temoBodyDef: B2.BodyDef; private _tempB2AABB: B2.AABB; + public tempB2FixtureDefPtr: number; get impl (): B2.World { return this._world; @@ -88,6 +89,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { this._temoBodyDef = new B2.BodyDef(); this._tempB2AABB = new B2.AABB(); + this.tempB2FixtureDefPtr = B2.FixtureDefNew(); } _debugGraphics: Graphics | null = null; @@ -344,11 +346,13 @@ export class B2PhysicsWorld implements IPhysicsWorld { } } - registerContactFixture (fixture: B2.Fixture): void { - this._contactListener.registerContactFixture(getImplPtr(fixture)); + registerContactFixture (fixture: number): void { //B2.Fixture ptr + // this._contactListener.registerContactFixture(getImplPtr(fixture)); + this._contactListener.registerContactFixture(fixture); } - unregisterContactFixture (fixture: B2.Fixture): void { - this._contactListener.unregisterContactFixture(getImplPtr(fixture)); + unregisterContactFixture (fixture: number): void { //B2.Fixture ptr + // this._contactListener.unregisterContactFixture(getImplPtr(fixture)); + this._contactListener.unregisterContactFixture(fixture); } testPoint (point: Vec2): readonly Collider2D[] { diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts index 657dcc2b407..fbf516b6400 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts @@ -26,10 +26,11 @@ import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '. import { Vec2 } from '../../../core'; import { B2RigidBody2D } from '../rigid-body'; +const tempVec2 = { x: 0, y: 0 }; export class PhysicsAABBQueryCallback { static _point = { x: 0, y: 0 }; static _isPoint = false; - static _fixtures: B2.Fixture[] = []; + static _fixtures: number[] = [];//B2.Fixture ptr static init (point?: Vec2): void { if (point) { @@ -43,9 +44,10 @@ export class PhysicsAABBQueryCallback { this._fixtures.length = 0; } - static ReportFixture (fixture: B2.Fixture): boolean { + static ReportFixture (fixture: number): boolean { if (this._isPoint) { - if (fixture.TestPoint(this._point)) { + //if (fixture.TestPoint(this._point)) { + if (B2.FixtureTestPoint(fixture, this._point)) { this._fixtures.push(fixture); } } else { @@ -66,8 +68,8 @@ export class PhysicsAABBQueryCallback { static callback = { ReportFixture (fixture: number): boolean { - const f = getWASMObjectFromWASMObjectPtr(fixture); - return PhysicsAABBQueryCallback.ReportFixture(f); + // const f = getWASMObjectFromWASMObjectPtr(fixture); + return PhysicsAABBQueryCallback.ReportFixture(fixture); }, }; } diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts index 3c067a73da2..36974a9c607 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts @@ -30,7 +30,7 @@ import { B2RigidBody2D } from '../rigid-body'; export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static _type = ERaycast2DType.Closest; - static _fixtures: B2.Fixture[] = []; + static _fixtures: number[] = [];//B2.Fixture ptr static _points: Vec2[] = []; static _normals: Vec2[] = []; static _fractions: number[] = []; @@ -46,8 +46,9 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { PhysicsRayCastCallback._fractions.length = 0; } - static ReportFixture (fixture: B2.Fixture, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { - if ((fixture.GetFilterData().categoryBits & PhysicsRayCastCallback._mask) === 0) { + static ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { + if ((B2.FixtureGetFilterData(fixture).categoryBits & PhysicsRayCastCallback._mask) === 0) { + //if ((fixture.GetFilterData().categoryBits & PhysicsRayCastCallback._mask) === 0) { return 0; } @@ -73,7 +74,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { return fraction; } - static getFixtures (): B2.Fixture[] { + static getFixtures (): number[] { return PhysicsRayCastCallback._fixtures; } @@ -91,8 +92,8 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static callback = { ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { - const f = getWASMObjectFromWASMObjectPtr(fixture); - return PhysicsRayCastCallback.ReportFixture(f, point, normal, fraction); + //const f = getWASMObjectFromWASMObjectPtr(fixture); + return PhysicsRayCastCallback.ReportFixture(fixture, point, normal, fraction); }, }; } diff --git a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts index 381632fb03a..87bf504f2ed 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts @@ -55,7 +55,7 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { return wps; } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.PolygonShape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { //B2.PolygonShape[] scaleX = Math.abs(scaleX); scaleY = Math.abs(scaleY); @@ -71,6 +71,7 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { tempB2Vec2_1.y = offsetY; shape.SetAsBoxWithCenterAndAngle(width, height, tempB2Vec2_1, 0); - return [shape as unknown as B2.PolygonShape]; + // return [shape as unknown as B2.PolygonShape]; + return [shape.$$.ptr as number]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts index 828fa57cadd..e3a32be385a 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts @@ -31,16 +31,18 @@ import { Vec2 } from '../../../core'; export class B2CircleShape extends B2Shape2D implements ICircleShape { get worldRadius (): number { - return (this._shapes[0]).m_radius * PHYSICS_2D_PTM_RATIO; + //return (this._shapes[0]).m_radius * PHYSICS_2D_PTM_RATIO; + return B2.CircleShapeGetRadius(this._shapes[0]) * PHYSICS_2D_PTM_RATIO; } _worldPosition = new Vec2(); get worldPosition (): Vec2 { - const p = (this._shapes[0] as B2.CircleShape).m_p; + //const p = (this._shapes[0] as B2.CircleShape).m_p; + const p = B2.CircleShapeGetPosition(this._shapes[0]) as B2.Vec2; return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO); } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.CircleShape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { //B2.CircleShape[] scaleX = Math.abs(scaleX); scaleY = Math.abs(scaleY); @@ -49,10 +51,13 @@ export class B2CircleShape extends B2Shape2D implements ICircleShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - const shape = new B2.CircleShape(); - shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX; - shape.m_p = { x: offsetX, y: offsetY }; + const shape = B2.CircleShapeNew() as number;//new B2.CircleShape(); + // shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX; + // shape.m_p = { x: offsetX, y: offsetY }; + B2.CircleShapeSetRadius(shape, comp.radius / PHYSICS_2D_PTM_RATIO * scaleX); + B2.CircleShapeSetPosition(shape, offsetX, offsetY); - return [shape as unknown as B2.CircleShape]; + // return [shape as unknown as B2.CircleShape]; + return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts index e192901f5ad..a7eaa4800ec 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts @@ -48,8 +48,8 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { return this._worldPoints; } - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.PolygonShape[] { - const shapes: B2.PolygonShape[] = []; + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { + const shapes: number[] = []; const comp = this.collider as PolygonCollider2D; const points = comp.points; @@ -90,7 +90,7 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { if (vertices.size() === B2.maxPolygonVertices) { shape!.Set(vertices, vertices.size() as number); - shapes.push(shape!); + shapes.push(shape!.$$.ptr); shape = null; @@ -105,7 +105,7 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { if (shape) { shape.Set(vertices, vertices.size() as number); - shapes.push(shape); + shapes.push(shape.$$.ptr); } vertices.delete(); } diff --git a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts index 84ab4a5881b..c6b09d0c4e9 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts @@ -46,8 +46,8 @@ function getFilter (shape: B2Shape2D): B2.Filter { } export class B2Shape2D implements IBaseShape { - protected _shapes: B2.Shape[] = []; - protected _fixtures: B2.Fixture[] = []; + protected _shapes: number[] = []; + protected _fixtures: number[] = [];//B2.Fixture ptr protected _collider: Collider2D | null = null; protected _body: B2.Body | null = null; @@ -56,7 +56,7 @@ export class B2Shape2D implements IBaseShape { private _rect = new Rect(); - get impl (): B2.Shape[] { + get impl (): number[] { return this._shapes; } @@ -87,7 +87,8 @@ export class B2Shape2D implements IBaseShape { onGroupChanged (): void { const filter = getFilter(this); this._fixtures.forEach((f): void => { - f.SetFilterData(filter); + //f.SetFilterData(filter); + B2.FixtureSetFilterData(f, filter); }); } @@ -108,15 +109,18 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < fixtures.length; i++) { const fixture = fixtures[i]; - const count = fixture.GetShape().GetChildCount(); + //const count = fixture.GetShape().GetChildCount(); + const shape = B2.FixtureGetShape(fixture) as B2.Shape; + const count = shape.GetChildCount(); for (let j = 0; j < count; j++) { - const aabb = fixture.GetAABB(j); + // const aabb = fixture.GetAABB(j); + const aabb = B2.FixtureGetAABB(fixture, j); lowerBound.x = aabb.lowerBound.x; lowerBound.y = aabb.lowerBound.y; upperBound.x = aabb.upperBound.x; upperBound.y = aabb.upperBound.y; - if (fixture.GetShape().m_type === B2.ShapeType.e_polygon) { //b2ShapeType.e_polygonShape - const skinWidth = fixture.GetShape().m_radius; + if (shape.m_type === B2.ShapeType.e_polygon) { //b2ShapeType.e_polygonShape + const skinWidth = shape.m_radius; lowerBound.x += skinWidth; lowerBound.y += skinWidth; upperBound.x += skinWidth; @@ -143,12 +147,12 @@ export class B2Shape2D implements IBaseShape { return r; } - getFixtureIndex (fixture: B2.Fixture): number { + getFixtureIndex (fixture: number): number { //B2.Fixture ptr return this._fixtures.indexOf(fixture); } //relativePositionX/Y : relative Position from shape to rigid body - _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): B2.Shape[] { + _createShapes (scaleX: number, scaleY: number, relativePositionX: number, relativePositionY: number): number[] { return []; } @@ -177,17 +181,36 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < shapes.length; i++) { const shape = shapes[i]; - const fixDef = new B2.FixtureDef(); - fixDef.density = comp.density; - fixDef.isSensor = comp.sensor; - fixDef.friction = comp.friction; - fixDef.restitution = comp.restitution; - fixDef.SetShape(shape); - fixDef.filter = filter; - const fixture = this._body.CreateFixture(fixDef as B2.FixtureDef); + // const fixDef = new B2.FixtureDef(); + // fixDef.density = comp.density; + // fixDef.isSensor = comp.sensor; + // fixDef.friction = comp.friction; + // fixDef.restitution = comp.restitution; + // fixDef.SetShape(shape); + // fixDef.filter = filter; + + const fixDef = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).tempB2FixtureDefPtr;//B2.FixtureDefNew(); + B2.FixtureDefSetAll( + fixDef, + shape, + 0, + comp.friction, + comp.restitution, + comp.density, + comp.sensor, + filter.categoryBits, + filter.maskBits, + filter.groupIndex, + ); + + // const fixture = this._body.CreateFixture(fixDef as B2.FixtureDef); + //const fixture = this._body.CreateFixtureRaw(fixDef); + const fixture = B2.BodyCreateFixture(this._body.$$.ptr, fixDef) as number; //fixture.m_userData = this; - addImplPtrReference(this, getImplPtr(fixture)); - addImplPtrReferenceWASM(fixture, getImplPtr(fixture)); + // addImplPtrReference(this, getImplPtr(fixture)); + // addImplPtrReferenceWASM(fixture, getImplPtr(fixture)); + addImplPtrReference(this, fixture); + addImplPtrReferenceWASM(fixture, fixture); if (body?.enabledContactListener) { (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).registerContactFixture(fixture); @@ -209,13 +232,17 @@ export class B2Shape2D implements IBaseShape { for (let i = fixtures.length - 1; i >= 0; i--) { const fixture = fixtures[i]; //fixture.m_userData = null; - removeImplPtrReference(getImplPtr(fixture)); - removeImplPtrReferenceWASM(getImplPtr(fixture)); + // removeImplPtrReference(getImplPtr(fixture)); + // removeImplPtrReferenceWASM(getImplPtr(fixture)); + removeImplPtrReference(fixture); + removeImplPtrReferenceWASM(fixture); - (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); + // (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); if (body) { - body.DestroyFixture(fixture); + //body.DestroyFixture(fixture); + // body.DestroyFixtureRaw(fixture.$$.ptr); + B2.BodyDestroyFixture(body.$$.ptr, fixture); } } From 0a2786053483fe24f3fe997a82c3cc5e6829e1aa Mon Sep 17 00:00:00 2001 From: lealzhan Date: Mon, 9 Oct 2023 13:01:16 +0800 Subject: [PATCH 2/5] box2d and polygon2d shape --- .../box2d-wasm/shapes/box-shape-2d.ts | 14 ++++-- .../box2d-wasm/shapes/polygon-shape-2d.ts | 50 +++++++++++++------ 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts index 87bf504f2ed..324d8c3ab91 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts @@ -66,12 +66,16 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - const shape = new B2.PolygonShape(); - tempB2Vec2_1.x = offsetX; - tempB2Vec2_1.y = offsetY; - shape.SetAsBoxWithCenterAndAngle(width, height, tempB2Vec2_1, 0); + // const shape = new B2.PolygonShape(); + // tempB2Vec2_1.x = offsetX; + // tempB2Vec2_1.y = offsetY; + // shape.SetAsBoxWithCenterAndAngle(width, height, tempB2Vec2_1, 0); + + const shape = B2.PolygonShapeNew() as number;//new B2.PolygonShape() + B2.PolygonShapeSetAsBoxWithCenterAndAngle(shape, width, height, offsetX, offsetY, 0); // return [shape as unknown as B2.PolygonShape]; - return [shape.$$.ptr as number]; + // return [shape.$$.ptr as number]; + return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts index a7eaa4800ec..4b017afc058 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts @@ -70,44 +70,62 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { for (let i = 0; i < polys.length; i++) { const poly = polys[i]; - let shape: B2.PolygonShape | null = null; - const vertices = new B2.Vec2Vector(); + let shape: number = 0;//B2.PolygonShape | null = null; + //const vertices = new B2.Vec2Vector(); + const vertices = B2.Vec2VectorNew(); let firstVertice: B2.Vec2 | null = null; for (let j = 0, l = poly.length; j < l; j++) { if (!shape) { - shape = new B2.PolygonShape(); + // shape = new B2.PolygonShape(); + shape = B2.PolygonShapeNew() as number;//new B2.PolygonShape(); } const p = poly[j]; const x = (relativePositionX + (p.x + offset.x) * scaleX) / PHYSICS_2D_PTM_RATIO; const y = (relativePositionY + (p.y + offset.y) * scaleY) / PHYSICS_2D_PTM_RATIO; const v = { x, y }; - vertices.push_back(v); + // vertices.push_back(v); + B2.Vec2VectorPush(vertices, x, y); if (!firstVertice) { firstVertice = v; } - if (vertices.size() === B2.maxPolygonVertices) { - shape!.Set(vertices, vertices.size() as number); - shapes.push(shape!.$$.ptr); - - shape = null; + // if (vertices.size() === B2.maxPolygonVertices) { + if (B2.Vec2VectorSize(vertices) === B2.maxPolygonVertices) { + // shape!.Set(vertices, vertices.size() as number); + // shapes.push(shape!.$$.ptr); + // B2.PolygonShapeSet(shape, vertices.$$.ptr, vertices.size() as number); + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices)); + // B2.PolygonShapeSet(shape, vertices, B2.Vec2VectorSize(vertices)); + shapes.push(shape); + shape = 0; if (j < l - 1) { - const temp = vertices.get(vertices.size() - 1); - vertices.resize(0, { x: 0, y: 0 });//clear - vertices.push_back(firstVertice); - vertices.push_back(temp); + // const temp = vertices.get(vertices.size() - 1); + // vertices.resize(0, { x: 0, y: 0 });//clear + // vertices.push_back(firstVertice); + // vertices.push_back(temp); + // const x = B2.Vec2VectorGetX(vertices, B2.Vec2VectorSize(vertices) - 1); + // const y = B2.Vec2VectorGetY(vertices, B2.Vec2VectorSize(vertices) - 1); + const temp = B2.Vec2VectorGet(vertices, B2.Vec2VectorSize(vertices) - 1); + B2.Vec2VectorResize(vertices, 0, 0, 0);//clear + B2.Vec2VectorPush(vertices, firstVertice.x, firstVertice.y); + B2.Vec2VectorPush(vertices, temp.x, temp.y); } } } if (shape) { - shape.Set(vertices, vertices.size() as number); - shapes.push(shape.$$.ptr); + // shape.Set(vertices, vertices.size() as number); + // shapes.push(shape.$$.ptr); + // B2.PolygonShapeSet(shape, vertices.$$.ptr, vertices.size() as number); + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices) as number); + // B2.PolygonShapeSet(shape, vertices, B2.Vec2VectorSize(vertices) as number); + shapes.push(shape); } - vertices.delete(); + //vertices.delete(); + B2.Vec2VectorDelete(vertices); } return shapes; From cfcfae6c26a035220d2b1fcf9f044a0300859dac Mon Sep 17 00:00:00 2001 From: lealzhan Date: Mon, 9 Oct 2023 17:45:24 +0800 Subject: [PATCH 3/5] WASM_OBJECT_PTR_2_TS_OBJECT WASM_OBJECT_PTR_2_WASM_OBJECT --- cocos/physics-2d/box2d-wasm/instantiated.ts | 74 +++++++++++++++---- .../box2d-wasm/joints/fixed-joint.ts | 8 +- .../physics-2d/box2d-wasm/joints/joint-2d.ts | 10 +-- .../box2d-wasm/joints/spring-joint.ts | 9 ++- .../box2d-wasm/joints/wheel-joint.ts | 8 +- .../physics-2d/box2d-wasm/physics-contact.ts | 13 ++-- cocos/physics-2d/box2d-wasm/physics-world.ts | 24 +++--- .../platform/physics-aabb-query-callback.ts | 6 +- .../platform/physics-ray-cast-callback.ts | 2 - cocos/physics-2d/box2d-wasm/rigid-body.ts | 12 +-- .../box2d-wasm/shapes/box-shape-2d.ts | 10 +-- .../box2d-wasm/shapes/circle-shape-2d.ts | 9 +-- .../box2d-wasm/shapes/polygon-shape-2d.ts | 23 +----- .../physics-2d/box2d-wasm/shapes/shape-2d.ts | 47 ++++-------- 14 files changed, 126 insertions(+), 129 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index fcb536b7263..134c742935d 100644 --- a/cocos/physics-2d/box2d-wasm/instantiated.ts +++ b/cocos/physics-2d/box2d-wasm/instantiated.ts @@ -40,6 +40,14 @@ export function getImplPtr (wasmObject: any): number { return (wasmObject).$$.ptr as number; } +// type : Fixture, Body, Contact, Joint, ... +export const enum B2ObjectType { + Fixture = 0, + Body, + Contact, + Joint, +} + /** * mapping wasm-object-ptr to ts-object * B2.Fixture pointer -->B2Shape2D @@ -48,22 +56,42 @@ export function getImplPtr (wasmObject: any): number { * B2.Joint pointer --> B2Joint * ... */ -const WASM_OBJECT_PTR_2_TS_OBJECT = {}; -export function addImplPtrReference (TSObject: any, implPtr: number): void { - if (implPtr) { WASM_OBJECT_PTR_2_TS_OBJECT[implPtr] = TSObject; } +const WASM_OBJECT_PTR_2_TS_OBJECT = new Map>(); + +export function addImplPtrReference (Type: B2ObjectType, TSObject: any, implPtr: number): void { + if (implPtr) { + let map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); + if (!map) { + map = new Map(); + WASM_OBJECT_PTR_2_TS_OBJECT.set(Type, map); + } + map.set(implPtr, TSObject); + } } -export function removeImplPtrReference (implPtr: number): void { + +export function removeImplPtrReference (Type: B2ObjectType, implPtr: number): void { if (implPtr) { - delete WASM_OBJECT_PTR_2_TS_OBJECT[implPtr]; + const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); + if (map && map.has(implPtr)) { + map.delete(implPtr); + if (map.size === 0) { + WASM_OBJECT_PTR_2_TS_OBJECT.delete(Type); + } + } } } -export function getTSObjectFromWASMObjectPtr (implPtr: number): T { + +export function getTSObjectFromWASMObjectPtr (Type: B2ObjectType, implPtr: number): T { + const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_TS_OBJECT[implPtr]; + return map?.get(implPtr); } -export function getTSObjectFromWASMObject (impl: any): T { + +export function getTSObjectFromWASMObject (Type: B2ObjectType, impl: any): T { + const implPtr = getImplPtr(impl); + const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_TS_OBJECT[getImplPtr(impl)]; + return map?.get(implPtr); } /** @@ -74,20 +102,34 @@ export function getTSObjectFromWASMObject (impl: any): T { * B2.Joint pointer --> B2.Joint * ... */ -const WASM_OBJECT_PTR_2_WASM_OBJECT = {}; -export function addImplPtrReferenceWASM (WASMObject: any, implPtr: number): void { - if (implPtr) { WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr] = WASMObject; } +const WASM_OBJECT_PTR_2_WASM_OBJECT = new Map>(); +export function addImplPtrReferenceWASM (Type: B2ObjectType, WASMObject: any, implPtr: number): void { + if (implPtr) { + let map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); + if (!map) { + map = new Map(); + WASM_OBJECT_PTR_2_WASM_OBJECT.set(Type, map); + } + map.set(implPtr, WASMObject); + } } -export function removeImplPtrReferenceWASM (implPtr: number): void { +export function removeImplPtrReferenceWASM (Type: B2ObjectType, implPtr: number): void { if (implPtr) { - delete WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr]; + const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); + if (map && map.has(implPtr)) { + map.delete(implPtr); + if (map.size === 0) { + WASM_OBJECT_PTR_2_WASM_OBJECT.delete(Type); + } + } } } -export function getWASMObjectFromWASMObjectPtr (implPtr: number): T { +export function getWASMObjectFromWASMObjectPtr (Type: B2ObjectType, implPtr: number): T { + const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type); // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr]; + return map?.get(implPtr); } /** diff --git a/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts b/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts index 6084d7e55c5..32ea72a8a65 100644 --- a/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts @@ -30,10 +30,14 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; export class B2FixedJoint extends B2Joint implements IFixedJoint { setFrequency (v: number): void { - (this._b2joint as B2.WeldJoint).SetFrequency(v); + if (this._b2joint) { + (this._b2joint as B2.WeldJoint).SetFrequency(v); + } } setDampingRatio (v: number): void { - (this._b2joint as B2.WeldJoint).SetDampingRatio(v); + if (this._b2joint) { + (this._b2joint as B2.WeldJoint).SetDampingRatio(v); + } } _createJointDef (): any { diff --git a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts index b0ba8173bff..841c2aa3fea 100644 --- a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated'; +import { B2, B2ObjectType, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated'; import { IJoint2D } from '../../spec/i-physics-joint'; import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework'; import { B2PhysicsWorld } from '../physics-world'; @@ -105,8 +105,8 @@ export class B2Joint implements IJoint2D { def.collideConnected = comp.collideConnected; this._b2joint = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.CreateJoint(def); - addImplPtrReference(this, getImplPtr(this._b2joint)); - addImplPtrReferenceWASM(this._b2joint, getImplPtr(this._b2joint)); + addImplPtrReference(B2ObjectType.Joint, this, getImplPtr(this._b2joint)); + addImplPtrReferenceWASM(B2ObjectType.Joint, this._b2joint, getImplPtr(this._b2joint)); this._inited = true; } @@ -114,8 +114,8 @@ export class B2Joint implements IJoint2D { destroy (): void { if (!this._inited) return; - removeImplPtrReference(getImplPtr(this._b2joint)); - removeImplPtrReferenceWASM(getImplPtr(this._b2joint)); + removeImplPtrReference(B2ObjectType.Joint, getImplPtr(this._b2joint)); + removeImplPtrReferenceWASM(B2ObjectType.Joint, getImplPtr(this._b2joint)); (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.DestroyJoint(this._b2joint!); this._b2joint = null; diff --git a/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts b/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts index e0ca9102b88..81e9451c6bf 100644 --- a/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts @@ -30,12 +30,15 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; export class B2SpringJoint extends B2Joint implements ISpringJoint { setFrequency (v: number): void { - (this._b2joint as B2.DistanceJoint).SetFrequency(v); + if (this._b2joint) { + (this._b2joint as B2.DistanceJoint).SetFrequency(v); + } } setDampingRatio (v: number): void { - //this.updateStiffnessAndDamping(); - (this._b2joint as B2.DistanceJoint).SetDampingRatio(v); + if (this._b2joint) { + (this._b2joint as B2.DistanceJoint).SetDampingRatio(v); + } } setDistance (v: number): void { diff --git a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts index c2faa6e1654..9f8625ae944 100644 --- a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts @@ -31,10 +31,14 @@ import { toRadian } from '../../../core'; export class B2WheelJoint extends B2Joint implements IWheelJoint { setFrequency (v: number): void { - (this._b2joint as B2.WheelJoint as any).SetSpringFrequencyHz(v); + if (this._b2joint) { + (this._b2joint as B2.WheelJoint as any).SetSpringFrequencyHz(v); + } } setDampingRatio (v: number): void { - (this._b2joint as B2.WheelJoint as any).SetSpringDampingRatio(v); + if (this._b2joint) { + (this._b2joint as B2.WheelJoint as any).SetSpringDampingRatio(v); + } } // motor diff --git a/cocos/physics-2d/box2d-wasm/physics-contact.ts b/cocos/physics-2d/box2d-wasm/physics-contact.ts index 85f180a9a0e..9eeceb5bcb7 100644 --- a/cocos/physics-2d/box2d-wasm/physics-contact.ts +++ b/cocos/physics-2d/box2d-wasm/physics-contact.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated'; +import { B2, B2ObjectType, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated'; import { Vec2 } from '../../core'; import { PHYSICS_2D_PTM_RATIO } from '../framework/physics-types'; import { Collider2D, Contact2DType, PhysicsSystem2D } from '../framework'; @@ -74,7 +74,7 @@ export class PhysicsContact implements IPhysics2DContact { } static put (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) return; pools.push(c); @@ -97,15 +97,16 @@ export class PhysicsContact implements IPhysics2DContact { } init (b2contact: number): void { - this.colliderA = (getTSObjectFromWASMObjectPtr(B2.ContactGetFixtureA(b2contact) as number)).collider; - this.colliderB = (getTSObjectFromWASMObjectPtr(B2.ContactGetFixtureB(b2contact) as number)).collider; + const ab = B2.ContactGetFixture(b2contact) as Vec2; + this.colliderA = (getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, ab.x)).collider; + this.colliderB = (getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, ab.y)).collider; this.disabled = false; this.disabledOnce = false; this._impulsePtr = 0; this._inverted = false; this._implPtr = b2contact; - addImplPtrReference(this, this._implPtr); + addImplPtrReference(B2ObjectType.Contact, this, this._implPtr); this._b2WorldmanifoldPtr = B2.WorldManifoldNew(); } @@ -119,7 +120,7 @@ export class PhysicsContact implements IPhysics2DContact { this.disabled = false; this._impulsePtr = 0; - removeImplPtrReference(this._implPtr); + removeImplPtrReference(B2ObjectType.Contact, this._implPtr); this._implPtr = 0; B2.WorldManifoldDelete(this._b2WorldmanifoldPtr); diff --git a/cocos/physics-2d/box2d-wasm/physics-world.ts b/cocos/physics-2d/box2d-wasm/physics-world.ts index 68f8e4bf510..49f75a51f90 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -24,7 +24,7 @@ import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject, - getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from './instantiated'; + getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM, B2ObjectType } from './instantiated'; import { IPhysicsWorld } from '../spec/i-physics-world'; import { IVec2Like, Vec3, Quat, toRadian, Vec2, toDegree, Rect, CCObject, js } from '../../core'; import { PHYSICS_2D_PTM_RATIO, ERaycast2DType, ERigidBody2DType } from '../framework/physics-types'; @@ -191,7 +191,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const results: RaycastResult2D[] = []; for (let i = 0, l = fixtures.length; i < l; i++) { const fixture = fixtures[i]; - const shape = getTSObjectFromWASMObject(fixture); + const shape = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixture); const collider = shape.collider; if (type === ERaycast2DType.AllClosest) { @@ -321,8 +321,8 @@ export class B2PhysicsWorld implements IPhysicsWorld { bodyDef.angularVelocity = toRadian(compPrivate._angularVelocity as number); const b2Body = this._world.CreateBody(bodyDef); - addImplPtrReference(body, getImplPtr(b2Body)); - addImplPtrReferenceWASM(b2Body, getImplPtr(b2Body)); + addImplPtrReference(B2ObjectType.Body, body, getImplPtr(b2Body)); + addImplPtrReferenceWASM(B2ObjectType.Body, b2Body, getImplPtr(b2Body)); body._imp = b2Body; this._bodies.push(body); @@ -333,8 +333,8 @@ export class B2PhysicsWorld implements IPhysicsWorld { return; } if (body.impl) { - removeImplPtrReference(getImplPtr(body.impl)); - removeImplPtrReferenceWASM(getImplPtr(body.impl)); + removeImplPtrReference(B2ObjectType.Body, getImplPtr(body.impl)); + removeImplPtrReferenceWASM(B2ObjectType.Body, getImplPtr(body.impl)); this._world.DestroyBody(body.impl); body._imp = null; } @@ -347,11 +347,9 @@ export class B2PhysicsWorld implements IPhysicsWorld { } registerContactFixture (fixture: number): void { //B2.Fixture ptr - // this._contactListener.registerContactFixture(getImplPtr(fixture)); this._contactListener.registerContactFixture(fixture); } unregisterContactFixture (fixture: number): void { //B2.Fixture ptr - // this._contactListener.unregisterContactFixture(getImplPtr(fixture)); this._contactListener.unregisterContactFixture(fixture); } @@ -370,7 +368,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const fixtures = PhysicsAABBQueryCallback.getFixtures(); testResults.length = 0; for (let i = 0; i < fixtures.length; i++) { - const collider = getTSObjectFromWASMObject(fixtures[i]).collider; + const collider = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtures[i]).collider; if (!testResults.includes(collider)) { testResults.push(collider); } @@ -389,7 +387,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { const fixtures = PhysicsAABBQueryCallback.getFixtures(); testResults.length = 0; for (let i = 0; i < fixtures.length; i++) { - const collider = getTSObjectFromWASMObject(fixtures[i]).collider; + const collider = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtures[i]).collider; if (!testResults.includes(collider)) { testResults.push(collider); } @@ -413,7 +411,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onEndContact (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } @@ -423,7 +421,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onPreSolve (b2contact: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } @@ -432,7 +430,7 @@ export class B2PhysicsWorld implements IPhysicsWorld { } _onPostSolve (b2contact: number, impulse: number): void { - const c = getTSObjectFromWASMObjectPtr(b2contact); + const c = getTSObjectFromWASMObjectPtr(B2ObjectType.Contact, b2contact); if (!c) { return; } diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts index fbf516b6400..7596a09b1de 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts @@ -46,7 +46,6 @@ export class PhysicsAABBQueryCallback { static ReportFixture (fixture: number): boolean { if (this._isPoint) { - //if (fixture.TestPoint(this._point)) { if (B2.FixtureTestPoint(fixture, this._point)) { this._fixtures.push(fixture); } @@ -58,17 +57,16 @@ export class PhysicsAABBQueryCallback { return true; } - static getFixture (): any { + static getFixture (): number { return this._fixtures[0]; } - static getFixtures (): any[] { + static getFixtures (): number[] { return this._fixtures; } static callback = { ReportFixture (fixture: number): boolean { - // const f = getWASMObjectFromWASMObjectPtr(fixture); return PhysicsAABBQueryCallback.ReportFixture(fixture); }, }; diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts index 36974a9c607..da9a26b3706 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts @@ -48,7 +48,6 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { if ((B2.FixtureGetFilterData(fixture).categoryBits & PhysicsRayCastCallback._mask) === 0) { - //if ((fixture.GetFilterData().categoryBits & PhysicsRayCastCallback._mask) === 0) { return 0; } @@ -92,7 +91,6 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static callback = { ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any { - //const f = getWASMObjectFromWASMObjectPtr(fixture); return PhysicsRayCastCallback.ReportFixture(fixture, point, normal, fraction); }, }; diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index 569142e0944..0718e28de06 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getTSObjectFromWASMObjectPtr } from './instantiated'; +import { B2, B2ObjectType, getTSObjectFromWASMObject, getTSObjectFromWASMObjectPtr } from './instantiated'; import { IRigidBody2D } from '../spec/i-rigid-body'; import { RigidBody2D } from '../framework/components/rigid-body-2d'; import { PhysicsSystem2D } from '../framework/physics-system'; @@ -119,11 +119,11 @@ export class B2RigidBody2D implements IRigidBody2D { //collect all fixtures attached to this rigid body and process them const fixtureList = this.impl?.GetFixtureList(); if (fixtureList) { - let shapeTSObj = getTSObjectFromWASMObject(fixtureList); + let shapeTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, fixtureList); while (shapeTSObj && shapeTSObj.impl) { shapeTSObj.destroy(); - const nextFixture = fixtureList.GetNext(); - shapeTSObj = getTSObjectFromWASMObject(nextFixture); + const nextFixture = B2.FixtureGetNext(fixtureList) as number; + shapeTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, nextFixture); } } @@ -131,11 +131,11 @@ export class B2RigidBody2D implements IRigidBody2D { const jointListPtr = this.impl?.GetJointList(); if (jointListPtr) { let jointWASMPtr = B2.JointEdgeGetJoint(jointListPtr) as number; - let jointTSObj = getTSObjectFromWASMObjectPtr(jointWASMPtr); + let jointTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, jointWASMPtr); while (jointTSObj) { jointTSObj.destroy(); jointWASMPtr = B2.JointEdgeGetNext(jointListPtr) as number; - jointTSObj = getTSObjectFromWASMObjectPtr(jointWASMPtr); + jointTSObj = getTSObjectFromWASMObjectPtr(B2ObjectType.Fixture, jointWASMPtr); } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts index 324d8c3ab91..b32d55b5ba6 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/box-shape-2d.ts @@ -66,16 +66,8 @@ export class B2BoxShape extends B2Shape2D implements IBoxShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - // const shape = new B2.PolygonShape(); - // tempB2Vec2_1.x = offsetX; - // tempB2Vec2_1.y = offsetY; - // shape.SetAsBoxWithCenterAndAngle(width, height, tempB2Vec2_1, 0); - - const shape = B2.PolygonShapeNew() as number;//new B2.PolygonShape() + const shape = B2.PolygonShapeNew() as number; B2.PolygonShapeSetAsBoxWithCenterAndAngle(shape, width, height, offsetX, offsetY, 0); - - // return [shape as unknown as B2.PolygonShape]; - // return [shape.$$.ptr as number]; return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts index e3a32be385a..9a4a8eae614 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts @@ -31,13 +31,11 @@ import { Vec2 } from '../../../core'; export class B2CircleShape extends B2Shape2D implements ICircleShape { get worldRadius (): number { - //return (this._shapes[0]).m_radius * PHYSICS_2D_PTM_RATIO; return B2.CircleShapeGetRadius(this._shapes[0]) * PHYSICS_2D_PTM_RATIO; } _worldPosition = new Vec2(); get worldPosition (): Vec2 { - //const p = (this._shapes[0] as B2.CircleShape).m_p; const p = B2.CircleShapeGetPosition(this._shapes[0]) as B2.Vec2; return this._worldPosition.set(p.x * PHYSICS_2D_PTM_RATIO, p.y * PHYSICS_2D_PTM_RATIO); } @@ -51,13 +49,10 @@ export class B2CircleShape extends B2Shape2D implements ICircleShape { const offsetX = (relativePositionX + comp.offset.x * scaleX) / PHYSICS_2D_PTM_RATIO; const offsetY = (relativePositionY + comp.offset.y * scaleY) / PHYSICS_2D_PTM_RATIO; - const shape = B2.CircleShapeNew() as number;//new B2.CircleShape(); - // shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX; - // shape.m_p = { x: offsetX, y: offsetY }; - B2.CircleShapeSetRadius(shape, comp.radius / PHYSICS_2D_PTM_RATIO * scaleX); + const shape = B2.CircleShapeNew() as number; + B2.ShapeSetRadius(shape, comp.radius / PHYSICS_2D_PTM_RATIO * scaleX); B2.CircleShapeSetPosition(shape, offsetX, offsetY); - // return [shape as unknown as B2.CircleShape]; return [shape]; } } diff --git a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts index 4b017afc058..8bd31e5be25 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/polygon-shape-2d.ts @@ -70,44 +70,30 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { for (let i = 0; i < polys.length; i++) { const poly = polys[i]; - let shape: number = 0;//B2.PolygonShape | null = null; - //const vertices = new B2.Vec2Vector(); + let shape: number = 0;//B2.PolygonShape ptr const vertices = B2.Vec2VectorNew(); let firstVertice: B2.Vec2 | null = null; for (let j = 0, l = poly.length; j < l; j++) { if (!shape) { - // shape = new B2.PolygonShape(); - shape = B2.PolygonShapeNew() as number;//new B2.PolygonShape(); + shape = B2.PolygonShapeNew() as number; } const p = poly[j]; const x = (relativePositionX + (p.x + offset.x) * scaleX) / PHYSICS_2D_PTM_RATIO; const y = (relativePositionY + (p.y + offset.y) * scaleY) / PHYSICS_2D_PTM_RATIO; const v = { x, y }; - // vertices.push_back(v); B2.Vec2VectorPush(vertices, x, y); if (!firstVertice) { firstVertice = v; } - // if (vertices.size() === B2.maxPolygonVertices) { if (B2.Vec2VectorSize(vertices) === B2.maxPolygonVertices) { - // shape!.Set(vertices, vertices.size() as number); - // shapes.push(shape!.$$.ptr); - // B2.PolygonShapeSet(shape, vertices.$$.ptr, vertices.size() as number); B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices)); - // B2.PolygonShapeSet(shape, vertices, B2.Vec2VectorSize(vertices)); shapes.push(shape); shape = 0; if (j < l - 1) { - // const temp = vertices.get(vertices.size() - 1); - // vertices.resize(0, { x: 0, y: 0 });//clear - // vertices.push_back(firstVertice); - // vertices.push_back(temp); - // const x = B2.Vec2VectorGetX(vertices, B2.Vec2VectorSize(vertices) - 1); - // const y = B2.Vec2VectorGetY(vertices, B2.Vec2VectorSize(vertices) - 1); const temp = B2.Vec2VectorGet(vertices, B2.Vec2VectorSize(vertices) - 1); B2.Vec2VectorResize(vertices, 0, 0, 0);//clear B2.Vec2VectorPush(vertices, firstVertice.x, firstVertice.y); @@ -117,14 +103,9 @@ export class B2PolygonShape extends B2Shape2D implements IPolygonShape { } if (shape) { - // shape.Set(vertices, vertices.size() as number); - // shapes.push(shape.$$.ptr); - // B2.PolygonShapeSet(shape, vertices.$$.ptr, vertices.size() as number); B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices) as number); - // B2.PolygonShapeSet(shape, vertices, B2.Vec2VectorSize(vertices) as number); shapes.push(shape); } - //vertices.delete(); B2.Vec2VectorDelete(vertices); } diff --git a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts index c6b09d0c4e9..0b4d119299b 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts @@ -23,7 +23,8 @@ */ import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, removeImplPtrReference, - removeImplPtrReferenceWASM } from '../instantiated'; + removeImplPtrReferenceWASM, + B2ObjectType } from '../instantiated'; import { IBaseShape } from '../../spec/i-physics-shape'; import { Collider2D, PhysicsSystem2D, RigidBody2D, PHYSICS_2D_PTM_RATIO } from '../../../../exports/physics-2d-framework'; import { Rect, Vec3 } from '../../../core'; @@ -87,7 +88,6 @@ export class B2Shape2D implements IBaseShape { onGroupChanged (): void { const filter = getFilter(this); this._fixtures.forEach((f): void => { - //f.SetFilterData(filter); B2.FixtureSetFilterData(f, filter); }); } @@ -109,18 +109,16 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < fixtures.length; i++) { const fixture = fixtures[i]; - //const count = fixture.GetShape().GetChildCount(); - const shape = B2.FixtureGetShape(fixture) as B2.Shape; - const count = shape.GetChildCount(); + const shape = B2.FixtureGetShape(fixture) as number; + const count = B2.ShapeGetChildCount(shape); for (let j = 0; j < count; j++) { - // const aabb = fixture.GetAABB(j); const aabb = B2.FixtureGetAABB(fixture, j); lowerBound.x = aabb.lowerBound.x; lowerBound.y = aabb.lowerBound.y; upperBound.x = aabb.upperBound.x; upperBound.y = aabb.upperBound.y; - if (shape.m_type === B2.ShapeType.e_polygon) { //b2ShapeType.e_polygonShape - const skinWidth = shape.m_radius; + if (B2.ShapeGetType(shape) === 2) { //b2ShapeType.e_polygonShape + const skinWidth = B2.ShapeGetRadius(shape); lowerBound.x += skinWidth; lowerBound.y += skinWidth; upperBound.x += skinWidth; @@ -181,15 +179,7 @@ export class B2Shape2D implements IBaseShape { for (let i = 0; i < shapes.length; i++) { const shape = shapes[i]; - // const fixDef = new B2.FixtureDef(); - // fixDef.density = comp.density; - // fixDef.isSensor = comp.sensor; - // fixDef.friction = comp.friction; - // fixDef.restitution = comp.restitution; - // fixDef.SetShape(shape); - // fixDef.filter = filter; - - const fixDef = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).tempB2FixtureDefPtr;//B2.FixtureDefNew(); + const fixDef = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).tempB2FixtureDefPtr; B2.FixtureDefSetAll( fixDef, shape, @@ -203,14 +193,9 @@ export class B2Shape2D implements IBaseShape { filter.groupIndex, ); - // const fixture = this._body.CreateFixture(fixDef as B2.FixtureDef); - //const fixture = this._body.CreateFixtureRaw(fixDef); - const fixture = B2.BodyCreateFixture(this._body.$$.ptr, fixDef) as number; - //fixture.m_userData = this; - // addImplPtrReference(this, getImplPtr(fixture)); - // addImplPtrReferenceWASM(fixture, getImplPtr(fixture)); - addImplPtrReference(this, fixture); - addImplPtrReferenceWASM(fixture, fixture); + const fixture = B2.BodyCreateFixture(getImplPtr(this._body), fixDef) as number; + addImplPtrReference(B2ObjectType.Fixture, this, fixture); + addImplPtrReferenceWASM(B2ObjectType.Fixture, fixture, fixture); if (body?.enabledContactListener) { (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).registerContactFixture(fixture); @@ -232,17 +217,13 @@ export class B2Shape2D implements IBaseShape { for (let i = fixtures.length - 1; i >= 0; i--) { const fixture = fixtures[i]; //fixture.m_userData = null; - // removeImplPtrReference(getImplPtr(fixture)); - // removeImplPtrReferenceWASM(getImplPtr(fixture)); - removeImplPtrReference(fixture); - removeImplPtrReferenceWASM(fixture); + removeImplPtrReference(B2ObjectType.Fixture, fixture); + removeImplPtrReferenceWASM(B2ObjectType.Fixture, fixture); - // (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); + (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); if (body) { - //body.DestroyFixture(fixture); - // body.DestroyFixtureRaw(fixture.$$.ptr); - B2.BodyDestroyFixture(body.$$.ptr, fixture); + B2.BodyDestroyFixture(getImplPtr(body), fixture); } } From 0d7568476267ece08b97af25c317938290fbfd9a Mon Sep 17 00:00:00 2001 From: lealzhan Date: Tue, 10 Oct 2023 14:41:40 +0800 Subject: [PATCH 4/5] tweak --- cocos/physics-2d/box2d-wasm/instantiated.ts | 7 ------- cocos/physics-2d/box2d-wasm/physics-world.ts | 2 +- .../box2d-wasm/platform/physics-aabb-query-callback.ts | 4 +--- .../box2d-wasm/platform/physics-ray-cast-callback.ts | 4 +--- cocos/physics-2d/box2d-wasm/rigid-body.ts | 3 +-- 5 files changed, 4 insertions(+), 16 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index 134c742935d..a59969209c8 100644 --- a/cocos/physics-2d/box2d-wasm/instantiated.ts +++ b/cocos/physics-2d/box2d-wasm/instantiated.ts @@ -87,13 +87,6 @@ export function getTSObjectFromWASMObjectPtr (Type: B2ObjectType, implPtr: nu return map?.get(implPtr); } -export function getTSObjectFromWASMObject (Type: B2ObjectType, impl: any): T { - const implPtr = getImplPtr(impl); - const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type); - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return map?.get(implPtr); -} - /** * mapping wasm-object-ptr to wasm-object * B2.Fixture pointer -->B2.Fixture diff --git a/cocos/physics-2d/box2d-wasm/physics-world.ts b/cocos/physics-2d/box2d-wasm/physics-world.ts index 49f75a51f90..e5f7b95fa07 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -23,7 +23,7 @@ */ import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; -import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject, +import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM, B2ObjectType } from './instantiated'; import { IPhysicsWorld } from '../spec/i-physics-world'; import { IVec2Like, Vec3, Quat, toRadian, Vec2, toDegree, Rect, CCObject, js } from '../../core'; diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts index 7596a09b1de..6cc67b023e1 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-aabb-query-callback.ts @@ -22,11 +22,9 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated'; +import { B2 } from '../instantiated'; import { Vec2 } from '../../../core'; -import { B2RigidBody2D } from '../rigid-body'; -const tempVec2 = { x: 0, y: 0 }; export class PhysicsAABBQueryCallback { static _point = { x: 0, y: 0 }; static _isPoint = false; diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts index da9a26b3706..801165e8419 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-ray-cast-callback.ts @@ -22,11 +22,9 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated'; +import { B2 } from '../instantiated'; import { Vec2 } from '../../../core'; import { ERaycast2DType } from '../../framework'; -import { B2Shape2D } from '../shapes/shape-2d'; -import { B2RigidBody2D } from '../rigid-body'; export class PhysicsRayCastCallback {// extends B2.RayCastCallback { static _type = ERaycast2DType.Closest; diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index 0718e28de06..e8de966ef32 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { B2, B2ObjectType, getTSObjectFromWASMObject, getTSObjectFromWASMObjectPtr } from './instantiated'; +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'; @@ -32,7 +32,6 @@ import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-typ import { Node } from '../../scene-graph/node'; import { Collider2D, Joint2D } from '../framework'; -import { NodeEventType } from '../../scene-graph/node-event'; import { B2Shape2D } from './shapes/shape-2d'; import { B2Joint } from './joints/joint-2d'; From cf58bc0d8642fe914869c669ce60fa6783a6a575 Mon Sep 17 00:00:00 2001 From: lealzhan Date: Tue, 10 Oct 2023 16:18:21 +0800 Subject: [PATCH 5/5] update external-config.json to v3.8.2-12 --- native/external-config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/external-config.json b/native/external-config.json index 88416cdce72..bfb8870c10e 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.8.2-9" + "checkout": "v3.8.2-12" } } \ No newline at end of file