Skip to content

Commit

Permalink
WASM_OBJECT_PTR_2_TS_OBJECT, WASM_OBJECT_PTR_2_WASM_OBJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
lealzhan committed Aug 8, 2023
1 parent e05a9d2 commit df2939b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 79 deletions.
62 changes: 41 additions & 21 deletions cocos/physics-2d/box2d-wasm/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,62 @@ import { getError, error, sys, debug } from '../../core';
import { WebAssemblySupportMode } from '../../misc/webassembly-support';

export const B2 = {} as any;
const B2_IMPL_PTR = {};

export function addImplReference (B2Object: any, impl: any): void {
if (!impl) return;
if (impl.$$) { B2_IMPL_PTR[impl.$$.ptr] = B2Object; }
export function getImplPtr (wasmObject: any): number {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return (wasmObject).$$.ptr;
}

export function removeImplReference (B2Object: any, impl: any): void {
if (!impl) return;
if (impl.$$) {
B2_IMPL_PTR[impl.$$.ptr] = null;
delete B2_IMPL_PTR[impl.$$.ptr];
/**
* mapping wasm-object-ptr to ts-object
* B2.Fixture pointer -->B2Shape2D
* B2.Body pointer --> B2RigidBody2D
* B2.Contact pointer --> PhysicsContact
* 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 {
if (implPtr) {
WASM_OBJECT_PTR_2_TS_OBJECT[implPtr] = null;
delete WASM_OBJECT_PTR_2_TS_OBJECT[implPtr];
}
}

export function getB2ObjectFromImpl<T> (impl: any): T {
export function getTSObjectFromWASMObjectPtr<T> (implPtr: number): T {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return WASM_OBJECT_PTR_2_TS_OBJECT[implPtr];
}
export function getTSObjectFromWASMObject<T> (impl: any): T {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return B2_IMPL_PTR[impl.$$.ptr];
return WASM_OBJECT_PTR_2_TS_OBJECT[getImplPtr(impl)];
}

export function addImplPtrReference (B2Object: any, implPtr: number): void {
if (implPtr) { B2_IMPL_PTR[implPtr] = B2Object; }
/**
* mapping wasm-object-ptr to wasm-object
* B2.Fixture pointer -->B2.Fixture
* B2.Body pointer --> B2.Body
* B2.Contact pointer --> B2.Contact
* 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; }
}

export function removeImplPtrReference (B2Object: any, implPtr: number): void {
export function removeImplPtrReferenceWASM (implPtr: number): void {
if (implPtr) {
B2_IMPL_PTR[implPtr] = null;
delete B2_IMPL_PTR[implPtr];
WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr] = null;
delete WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr];
}
}

export function getB2ObjectFromImplPtr<T> (implPtr: number): T {
export function getWASMObjectFromWASMObjectPtr<T> (implPtr: number): T {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return B2_IMPL_PTR[implPtr];
return WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -83,7 +105,6 @@ function initWasm (wasmUrl): Promise<void> {
}).then((Instance: any) => {
if (!EDITOR && !TEST) debug('[box2d]:box2d wasm lib loaded.');
Object.assign(B2, Instance);
B2.B2_IMPL_PTR = B2_IMPL_PTR;
}).then(resolve).catch((err: any) => reject(errorMessage(err)));
});
}
Expand All @@ -93,7 +114,6 @@ function initAsm (): Promise<void> {
return asmFactory().then((instance: any) => {
if (!EDITOR && !TEST) debug('[box2d]:box2d asm lib loaded.');
Object.assign(B2, instance);
B2.B2_IMPL_PTR = B2_IMPL_PTR;
});
} else {
return new Promise<void>((resolve, reject) => {
Expand Down
4 changes: 3 additions & 1 deletion cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { B2 } from '../instantiated';
import { B2, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr } from '../instantiated';
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { B2PhysicsWorld } from '../physics-world';
Expand Down Expand Up @@ -98,6 +98,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));

this.UpdateStiffnessAndDamping();

Expand Down
17 changes: 7 additions & 10 deletions cocos/physics-2d/box2d-wasm/physics-contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
THE SOFTWARE.
*/

import { Fixture } from '@cocos/box2d';
import { B2, addImplReference, getB2ObjectFromImpl, removeImplReference,
addImplPtrReference, getB2ObjectFromImplPtr, removeImplPtrReference,
} from './instantiated';
import { B2, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated';
import { Vec2 } from '../../core';
import { PHYSICS_2D_PTM_RATIO } from '../framework/physics-types';
import { Collider2D, Contact2DType, PhysicsSystem2D } from '../framework';
Expand Down Expand Up @@ -77,7 +74,7 @@ export class PhysicsContact implements IPhysics2DContact {
}

static put (b2contact: number): void {
const c = getB2ObjectFromImplPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
if (!c) return;

pools.push(c);
Expand All @@ -92,16 +89,16 @@ export class PhysicsContact implements IPhysics2DContact {

private _impulsePtr: number = 0;
private _inverted = false;
private _implPtr: number = 0;
private _b2WorldmanifoldPtr: number = 0;
private _implPtr: number = 0; //wasm object pointer
private _b2WorldmanifoldPtr: number = 0; //wasm object pointer

_setImpulse (impulse: number): void {
this._impulsePtr = impulse;
}

init (b2contact: number): void {
this.colliderA = (getB2ObjectFromImplPtr<B2Shape2D>(B2.ContactGetFixtureA(b2contact))).collider;
this.colliderB = (getB2ObjectFromImplPtr<B2Shape2D>(B2.ContactGetFixtureB(b2contact))).collider;
this.colliderA = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2.ContactGetFixtureA(b2contact))).collider;
this.colliderB = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2.ContactGetFixtureB(b2contact))).collider;
this.disabled = false;
this.disabledOnce = false;
this._impulsePtr = 0;
Expand All @@ -122,7 +119,7 @@ export class PhysicsContact implements IPhysics2DContact {
this.disabled = false;
this._impulsePtr = 0;

removeImplPtrReference(this, this._implPtr);
removeImplPtrReference(this._implPtr);
this._implPtr = 0;

B2.WorldManifoldDelete(this._b2WorldmanifoldPtr);
Expand Down
26 changes: 14 additions & 12 deletions cocos/physics-2d/box2d-wasm/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/

import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { B2, addImplReference, getB2ObjectFromImpl, getB2ObjectFromImplPtr, removeImplReference } from './instantiated';

import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject,
getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM } 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';
Expand Down Expand Up @@ -190,7 +190,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 = getB2ObjectFromImpl<B2Shape2D>(fixture);
const shape = getTSObjectFromWASMObject<B2Shape2D>(fixture);
const collider = shape.collider;

if (type === ERaycast2DType.AllClosest) {
Expand Down Expand Up @@ -320,7 +320,8 @@ export class B2PhysicsWorld implements IPhysicsWorld {
bodyDef.angularVelocity = toRadian(compPrivate._angularVelocity);

const b2Body = this._world.CreateBody(bodyDef);
addImplReference(body, b2Body);
addImplPtrReference(body, getImplPtr(b2Body));
addImplPtrReferenceWASM(b2Body, getImplPtr(b2Body));
body._imp = b2Body;

this._bodies.push(body);
Expand All @@ -331,7 +332,8 @@ export class B2PhysicsWorld implements IPhysicsWorld {
return;
}
if (body.impl) {
removeImplReference(body, body.impl);
removeImplPtrReference(getImplPtr(body.impl));
removeImplPtrReferenceWASM(getImplPtr(body.impl));
this._world.DestroyBody(body.impl);
body._imp = null;
}
Expand All @@ -344,10 +346,10 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

registerContactFixture (fixture: B2.Fixture): void {
this._contactListener.registerContactFixture((fixture as any).$$.ptr);
this._contactListener.registerContactFixture(getImplPtr(fixture));
}
unregisterContactFixture (fixture: B2.Fixture): void {
this._contactListener.unregisterContactFixture((fixture as any).$$.ptr);
this._contactListener.unregisterContactFixture(getImplPtr(fixture));
}

testPoint (point: Vec2): readonly Collider2D[] {
Expand All @@ -365,7 +367,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
const fixtures = PhysicsAABBQueryCallback.getFixtures();
testResults.length = 0;
for (let i = 0; i < fixtures.length; i++) {
const collider = getB2ObjectFromImpl<B2Shape2D>(fixtures[i]).collider;
const collider = getTSObjectFromWASMObject<B2Shape2D>(fixtures[i]).collider;
if (!testResults.includes(collider)) {
testResults.push(collider);
}
Expand All @@ -384,7 +386,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
const fixtures = PhysicsAABBQueryCallback.getFixtures();
testResults.length = 0;
for (let i = 0; i < fixtures.length; i++) {
const collider = getB2ObjectFromImpl<B2Shape2D>(fixtures[i]).collider;
const collider = getTSObjectFromWASMObject<B2Shape2D>(fixtures[i]).collider;
if (!testResults.includes(collider)) {
testResults.push(collider);
}
Expand All @@ -408,7 +410,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onEndContact (b2contact: number): void {
const c = getB2ObjectFromImplPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
if (!c) {
return;
}
Expand All @@ -418,7 +420,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onPreSolve (b2contact: number): void {
const c = getB2ObjectFromImplPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
if (!c) {
return;
}
Expand All @@ -427,7 +429,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onPostSolve (b2contact: number, impulse: number): void {
const c = getB2ObjectFromImplPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
if (!c) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { B2, getB2ObjectFromImpl } from '../instantiated';
import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated';
import { Vec2 } from '../../../core';
import { B2RigidBody2D } from '../rigid-body';

Expand Down Expand Up @@ -65,9 +65,8 @@ export class PhysicsAABBQueryCallback {
}

static callback = {
ReportFixture (fixture: B2.Fixture): boolean {
const rigidBody = getB2ObjectFromImpl<B2RigidBody2D>(fixture.GetBody());
const f = rigidBody.getFixtureWithFixtureImplPtr((fixture as any).$$.ptr)!;
ReportFixture (fixture: number): boolean {
const f = getWASMObjectFromWASMObjectPtr<B2.Fixture>(fixture);
return PhysicsAABBQueryCallback.ReportFixture(f);
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { B2, getB2ObjectFromImpl } from '../instantiated';
import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated';
import { Vec2 } from '../../../core';
import { ERaycast2DType } from '../../framework';
import { B2Shape2D } from '../shapes/shape-2d';
Expand Down Expand Up @@ -90,9 +90,8 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback {
}

static callback = {
ReportFixture (fixture: B2.Fixture, point, normal, fraction): any {
const rigidBody = getB2ObjectFromImpl<B2RigidBody2D>(fixture.GetBody());
const f = rigidBody.getFixtureWithFixtureImplPtr((fixture as any).$$.ptr)!;
ReportFixture (fixture: number, point, normal, fraction): any {
const f = getWASMObjectFromWASMObjectPtr<B2.Fixture>(fixture);
return PhysicsRayCastCallback.ReportFixture(f, point, normal, fraction);
},
}
Expand Down
14 changes: 0 additions & 14 deletions cocos/physics-2d/box2d-wasm/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,4 @@ export class B2RigidBody2D implements IRigidBody2D {
this._body.ApplyAngularImpulse(impulse, wake);
}
}

getFixtureWithFixtureImplPtr (implPtr: number): B2.Fixture| null {
let fixtureList = this._body!.GetFixtureList();
while (fixtureList) {
const b2Fixture = fixtureList;
if (b2Fixture) {
if ((b2Fixture as any).$$.ptr === implPtr) {
return b2Fixture;
}
fixtureList = b2Fixture.GetNext();
}
}
return null;
}
}
19 changes: 6 additions & 13 deletions cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/

//import b2, { Vec2 } from '@cocos/box2d';
import { B2, addImplReference, removeImplReference } from '../instantiated';

import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, removeImplPtrReference,
removeImplPtrReferenceWASM } 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';
Expand Down Expand Up @@ -184,18 +184,10 @@ export class B2Shape2D implements IBaseShape {
fixDef.restitution = comp.restitution;
fixDef.SetShape(shape);
fixDef.filter = filter;
// const fixDef: B2.FixtureDef = {
// density: comp.density,
// isSensor: comp.sensor,
// friction: comp.friction,
// restitution: comp.restitution,
// shape,
// filter,
// };

const fixture = this._body.CreateFixture(fixDef);
//fixture.m_userData = this;
addImplReference(this, fixture);
addImplPtrReference(this, getImplPtr(fixture));
addImplPtrReferenceWASM(fixture, getImplPtr(fixture));

if (body?.enabledContactListener) {
(PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).registerContactFixture(fixture);
Expand All @@ -217,7 +209,8 @@ export class B2Shape2D implements IBaseShape {
for (let i = fixtures.length - 1; i >= 0; i--) {
const fixture = fixtures[i];
//fixture.m_userData = null;
removeImplReference(this, fixture);
removeImplPtrReference(getImplPtr(fixture));
removeImplPtrReferenceWASM(getImplPtr(fixture));

(PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).unregisterContactFixture(fixture);

Expand Down

0 comments on commit df2939b

Please sign in to comment.