diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index fcb536b7263..a59969209c8 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,35 @@ 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; } -} -export function removeImplPtrReference (implPtr: number): void { +const WASM_OBJECT_PTR_2_TS_OBJECT = new Map>(); + +export function addImplPtrReference (Type: B2ObjectType, TSObject: any, implPtr: number): void { if (implPtr) { - delete WASM_OBJECT_PTR_2_TS_OBJECT[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 getTSObjectFromWASMObjectPtr (implPtr: number): T { - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return WASM_OBJECT_PTR_2_TS_OBJECT[implPtr]; + +export function removeImplPtrReference (Type: B2ObjectType, implPtr: number): void { + if (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 getTSObjectFromWASMObject (impl: any): 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[getImplPtr(impl)]; + return map?.get(implPtr); } /** @@ -74,20 +95,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/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/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 c0997b14915..e5f7b95fa07 100644 --- a/cocos/physics-2d/box2d-wasm/physics-world.ts +++ b/cocos/physics-2d/box2d-wasm/physics-world.ts @@ -23,8 +23,8 @@ */ import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; -import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject, - getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from './instantiated'; +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'; import { PHYSICS_2D_PTM_RATIO, ERaycast2DType, ERigidBody2DType } from '../framework/physics-types'; @@ -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; @@ -189,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) { @@ -319,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); @@ -331,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; } @@ -344,11 +346,11 @@ 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(fixture); } - unregisterContactFixture (fixture: B2.Fixture): void { - this._contactListener.unregisterContactFixture(getImplPtr(fixture)); + unregisterContactFixture (fixture: number): void { //B2.Fixture ptr + this._contactListener.unregisterContactFixture(fixture); } testPoint (point: Vec2): readonly Collider2D[] { @@ -366,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); } @@ -385,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); } @@ -409,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; } @@ -419,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; } @@ -428,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 657dcc2b407..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,14 +22,13 @@ THE SOFTWARE. */ -import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated'; +import { B2 } from '../instantiated'; import { Vec2 } from '../../../core'; -import { B2RigidBody2D } from '../rigid-body'; 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 +42,9 @@ 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 (B2.FixtureTestPoint(fixture, this._point)) { this._fixtures.push(fixture); } } else { @@ -56,18 +55,17 @@ 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(f); + 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..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,15 +22,13 @@ 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; - static _fixtures: B2.Fixture[] = []; + static _fixtures: number[] = [];//B2.Fixture ptr static _points: Vec2[] = []; static _normals: Vec2[] = []; static _fractions: number[] = []; @@ -46,8 +44,8 @@ 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) { return 0; } @@ -73,7 +71,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback { return fraction; } - static getFixtures (): B2.Fixture[] { + static getFixtures (): number[] { return PhysicsRayCastCallback._fixtures; } @@ -91,8 +89,7 @@ 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); + 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..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, 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'; @@ -119,11 +118,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 +130,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 381632fb03a..b32d55b5ba6 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); @@ -66,11 +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); - - return [shape as unknown as B2.PolygonShape]; + const shape = B2.PolygonShapeNew() as number; + B2.PolygonShapeSetAsBoxWithCenterAndAngle(shape, width, height, offsetX, offsetY, 0); + 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 828fa57cadd..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,16 +31,16 @@ 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); } - _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 +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 = 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; + 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 e192901f5ad..8bd31e5be25 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; @@ -70,44 +70,43 @@ 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 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; } 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) { - shape!.Set(vertices, vertices.size() as number); - shapes.push(shape!); - - shape = null; + if (B2.Vec2VectorSize(vertices) === B2.maxPolygonVertices) { + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(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 = 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); + B2.PolygonShapeSet(shape, B2.Vec2VectorGetPtr(vertices), B2.Vec2VectorSize(vertices) as number); shapes.push(shape); } - vertices.delete(); + B2.Vec2VectorDelete(vertices); } return shapes; diff --git a/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts index 84ab4a5881b..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'; @@ -46,8 +47,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 +57,7 @@ export class B2Shape2D implements IBaseShape { private _rect = new Rect(); - get impl (): B2.Shape[] { + get impl (): number[] { return this._shapes; } @@ -87,7 +88,7 @@ export class B2Shape2D implements IBaseShape { onGroupChanged (): void { const filter = getFilter(this); this._fixtures.forEach((f): void => { - f.SetFilterData(filter); + B2.FixtureSetFilterData(f, filter); }); } @@ -108,15 +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 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 (fixture.GetShape().m_type === B2.ShapeType.e_polygon) { //b2ShapeType.e_polygonShape - const skinWidth = fixture.GetShape().m_radius; + if (B2.ShapeGetType(shape) === 2) { //b2ShapeType.e_polygonShape + const skinWidth = B2.ShapeGetRadius(shape); lowerBound.x += skinWidth; lowerBound.y += skinWidth; upperBound.x += skinWidth; @@ -143,12 +145,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 +179,23 @@ 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); - //fixture.m_userData = this; - addImplPtrReference(this, getImplPtr(fixture)); - addImplPtrReferenceWASM(fixture, getImplPtr(fixture)); + const fixDef = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).tempB2FixtureDefPtr; + B2.FixtureDefSetAll( + fixDef, + shape, + 0, + comp.friction, + comp.restitution, + comp.density, + comp.sensor, + filter.categoryBits, + filter.maskBits, + filter.groupIndex, + ); + + 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); @@ -209,13 +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(B2ObjectType.Fixture, fixture); + removeImplPtrReferenceWASM(B2ObjectType.Fixture, fixture); (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture); if (body) { - body.DestroyFixture(fixture); + B2.BodyDestroyFixture(getImplPtr(body), fixture); } } diff --git a/native/external-config.json b/native/external-config.json index 432a1a05969..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-11" + "checkout": "v3.8.2-12" } } \ No newline at end of file