Skip to content

Commit

Permalink
V3.8.2 bullet embind (#16350)
Browse files Browse the repository at this point in the history
* bullet wasm embind

# Conflicts:
#	tests/init.ts

* replace imports with abstract methods of class

* embind performance opt

* wasm split loading

* tweak

* update native/external-config.json 3.8.2-14
  • Loading branch information
lealzhan authored Oct 13, 2023
1 parent f8b5d13 commit 38bcd20
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 125 deletions.
4 changes: 2 additions & 2 deletions cocos/physics/bullet/bullet-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { Collider, TriggerEventType, CollisionEventType, IContactEquation, CharacterController } from '../../../exports/physics-framework';
import { Vec3, Quat, Mat4, Color } from '../../core';
import { CharacterTriggerEventType } from '../framework';
import { bt } from './instantiated';
import { bt, btCache } from './instantiated';

export const TriggerEventObject = {
type: 'onTriggerEnter' as unknown as TriggerEventType,
Expand Down Expand Up @@ -90,4 +90,4 @@ export const CC_MAT4_0 = new Mat4();
export const CC_MAT4_1 = new Mat4();
export const CC_COLOR_0 = new Color();

bt.CACHE = BulletCache;
btCache.CACHE = BulletCache;
26 changes: 7 additions & 19 deletions cocos/physics/bullet/bullet-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,31 @@
THE SOFTWARE.
*/

// Wasm Memory Page Size is 65536
export const pageSize = 65536; // 64KiB

// How many pages of the wasm memory
// TODO: let this can be canfiguable by user.
export const pageCount = 250;

// How mush memory size of the wasm memory
export const memorySize = pageSize * pageCount; // 16 MiB
import { btCache } from './instantiated';

// The import function used in c++ code, same as DLL Import
export const importFunc = {
syncPhysicsToGraphics (id: number): void {
const bt = globalThis.Bullet;
const body = bt.CACHE.getWrapper(id, bt.BODY_CACHE_NAME);
const body = btCache.CACHE.getWrapper(id, btCache.BODY_CACHE_NAME);
body.syncPhysicsToGraphics();
},
onShapeHitExt (hit: number, controller: number): void {
const bt = globalThis.Bullet;
const cct = bt.CACHE.getWrapper(controller, bt.CCT_CACHE_NAME);
const cct = btCache.CACHE.getWrapper(controller, btCache.CCT_CACHE_NAME);
cct.onShapeHitExt(hit);
},
onDebugDrawLine (from: number, to: number, color: number): void {
const bt = globalThis.Bullet;
const world = bt.CACHE.world;
const world = btCache.CACHE.world;
if (world) {
world.onDebugDrawLine(from, to, color);
}
},
onClearLines (): void {
const bt = globalThis.Bullet;
const world = bt.CACHE.world;
clearLines (): void {
const world = btCache.CACHE.world;
if (world) {
world.onClearLines();
}
},
onFlushLines (): void {
flushLines (): void {
//empty
},
};
26 changes: 14 additions & 12 deletions cocos/physics/bullet/bullet-shared-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import { CC_V3_0, CC_QUAT_0, BulletCache } from './bullet-cache';
import { PhysicsSystem } from '../framework';
import { ERigidBodyType, PhysicsGroup } from '../framework/physics-enum';
import { js } from '../../core';
import { bt, EBulletType } from './instantiated';
import { bt, btCache, EBulletType } from './instantiated';
import { BulletConstraint } from './constraints/bullet-constraint';
import { importFunc } from './bullet-env';

const v3_0 = CC_V3_0;
const quat_0 = CC_QUAT_0;
Expand Down Expand Up @@ -211,14 +212,15 @@ export class BulletSharedBody {
cocos2BulletQuat(quat, this.node.worldRotation);
bt.Transform_setRotation(trans, quat);

const motionState = bt.ccMotionState_new(this.id, trans);
const motionState = bt.MotionState.implement(importFunc).$$.ptr as number;
bt.ccMotionState_setup(motionState, this.id, trans);
const body = bt.RigidBody_new(mass, motionState);
const sleepTd = PhysicsSystem.instance.sleepThreshold;
bt.RigidBody_setSleepingThresholds(body, sleepTd, sleepTd);
this._bodyStruct = {
id: IDCounter++, body, motionState, compound: bt.ccCompoundShape_new(), wrappedShapes: [], useCompound: false,
};
BulletCache.setWrapper(this.id, bt.BODY_CACHE_NAME, this);
BulletCache.setWrapper(this.id, btCache.BODY_CACHE_NAME, this);
if (this._ghostStruct) bt.CollisionObject_setIgnoreCollisionCheck(this.ghost, this.body, true);
if (this._wrappedBody) this.setBodyType(this._wrappedBody.rigidBody.type);
}
Expand Down Expand Up @@ -409,7 +411,7 @@ export class BulletSharedBody {
// Kinematic objects must be updated using motion state
const ms = bt.RigidBody_getMotionState(this.body);
if (ms) bt.MotionState_setWorldTransform(ms, bt_transform);
} else if (this.isBodySleeping()) bt.CollisionObject_activate(this.body);
} else if (this.isBodySleeping()) bt.CollisionObject_activate(this.body, false);
}
}

Expand All @@ -422,10 +424,10 @@ export class BulletSharedBody {
if (this.isBodySleeping()) return;
const bt_quat = BulletCache.instance.BT_QUAT_0;
const bt_transform = BulletCache.instance.BT_TRANSFORM_0;
bt.MotionState_getWorldTransform(bt.RigidBody_getMotionState(this.body), bt_transform);
bt.Transform_getRotation(bt_transform, bt_quat);
bt.RigidBody_getWorldTransform(this.body, bt_transform);
const originPosPtr = bt.Transform_getRotationAndOrigin(bt_transform, bt_quat) as number;
this.node.worldRotation = bullet2CocosQuat(quat_0, bt_quat);
this.node.worldPosition = bullet2CocosVec3(v3_0, bt.Transform_getOrigin(bt_transform));
this.node.worldPosition = bullet2CocosVec3(v3_0, originPosPtr);

// sync node to ghost
if (this._ghostStruct) {
Expand All @@ -444,7 +446,7 @@ export class BulletSharedBody {
cocos2BulletQuat(bt_quat, this.node.worldRotation);
bt.Transform_setRotation(bt_transform, bt_quat);
if (this.node.hasChangedFlags & TransformBit.SCALE) this.syncGhostScale();
bt.CollisionObject_activate(this.ghost);
bt.CollisionObject_activate(this.ghost, false);
}
}

Expand All @@ -455,7 +457,7 @@ export class BulletSharedBody {
cocos2BulletQuat(bt_quat, this.node.worldRotation);
bt.Transform_setRotation(bt_transform, bt_quat);
this.syncBodyScale();
bt.CollisionObject_activate(this.body);
bt.CollisionObject_activate(this.body, false);
}

syncInitialGhost (): void {
Expand All @@ -465,7 +467,7 @@ export class BulletSharedBody {
cocos2BulletQuat(bt_quat, this.node.worldRotation);
bt.Transform_setRotation(bt_transform, bt_quat);
this.syncGhostScale();
bt.CollisionObject_activate(this.body);
bt.CollisionObject_activate(this.body, false);
}

syncBodyScale (): void {
Expand Down Expand Up @@ -511,7 +513,7 @@ export class BulletSharedBody {
(this.wrappedWorld as any) = null;
if (this._bodyStruct) {
const bodyStruct = this._bodyStruct;
BulletCache.delWrapper(bodyStruct.body, bt.BODY_CACHE_NAME);
BulletCache.delWrapper(bodyStruct.body, btCache.BODY_CACHE_NAME);
bt._safe_delete(bodyStruct.motionState, EBulletType.EBulletTypeMotionState);
bt._safe_delete(bodyStruct.compound, EBulletType.EBulletTypeCollisionShape);
bt._safe_delete(bodyStruct.body, EBulletType.EBulletTypeCollisionObject);
Expand All @@ -527,6 +529,6 @@ export class BulletSharedBody {
}

private isBodySleeping (): boolean {
return bt.CollisionObject_getActivationState(this.body) === btCollisionObjectStates.ISLAND_SLEEPING;
return bt.CollisionObject_isSleeping(this.body);
}
}
22 changes: 12 additions & 10 deletions cocos/physics/bullet/bullet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export function cocos2BulletVec3 (out: Bullet.ptr, v: IVec3Like): Bullet.ptr {
}

export function bullet2CocosVec3<T extends IVec3Like> (out: T, v: Bullet.ptr): T {
out.x = bt.Vec3_x(v);
out.y = bt.Vec3_y(v);
out.z = bt.Vec3_z(v);
const rawVertexBuffer = bt.HEAPF32.subarray(v / 4, v / 4 + 3);
out.x = rawVertexBuffer[0];
out.y = rawVertexBuffer[1];
out.z = rawVertexBuffer[2];
return out;
}

Expand All @@ -46,10 +47,11 @@ export function cocos2BulletQuat (out: Bullet.ptr, q: IQuatLike): Bullet.ptr {
}

export function bullet2CocosQuat<T extends IQuatLike> (out: T, q: Bullet.ptr): T {
out.x = bt.Quat_x(q);
out.y = bt.Quat_y(q);
out.z = bt.Quat_z(q);
out.w = bt.Quat_w(q);
const rawVertexBuffer = bt.HEAPF32.subarray(q / 4, q / 4 + 4);
out.x = rawVertexBuffer[0];
out.y = rawVertexBuffer[1];
out.z = rawVertexBuffer[2];
out.w = rawVertexBuffer[3];
return out;
}

Expand All @@ -74,7 +76,7 @@ export function cocos2BulletTriMesh (out: Bullet.ptr, mesh: Mesh): any {
bt.Vec3_set(v0, vb[i0], vb[i0 + 1], vb[i0 + 2]);
bt.Vec3_set(v1, vb[i1], vb[i1 + 1], vb[i1 + 2]);
bt.Vec3_set(v2, vb[i2], vb[i2 + 1], vb[i2 + 2]);
bt.TriangleMesh_addTriangle(out, v0, v1, v2);
bt.TriangleMesh_addTriangle(out, v0, v1, v2, false);
}
} else if (primitiveMode === PrimitiveMode.TRIANGLE_STRIP) {
const cnt = ib.length - 2;
Expand All @@ -87,7 +89,7 @@ export function cocos2BulletTriMesh (out: Bullet.ptr, mesh: Mesh): any {
bt.Vec3_set(v0, vb[i0], vb[i0 + 1], vb[i0 + 2]);
bt.Vec3_set(v1, vb[i1], vb[i1 + 1], vb[i1 + 2]);
bt.Vec3_set(v2, vb[i2], vb[i2 + 1], vb[i2 + 2]);
bt.TriangleMesh_addTriangle(out, v0, v1, v2);
bt.TriangleMesh_addTriangle(out, v0, v1, v2, false);
}
} else if (primitiveMode === PrimitiveMode.TRIANGLE_FAN) {
const cnt = ib.length - 1;
Expand All @@ -98,7 +100,7 @@ export function cocos2BulletTriMesh (out: Bullet.ptr, mesh: Mesh): any {
const i2 = ib[j + 1] * 3;
bt.Vec3_set(v1, vb[i1], vb[i1 + 1], vb[i1 + 2]);
bt.Vec3_set(v2, vb[i2], vb[i2 + 1], vb[i2 + 2]);
bt.TriangleMesh_addTriangle(out, v0, v1, v2);
bt.TriangleMesh_addTriangle(out, v0, v1, v2, false);
}
}
}
Expand Down
33 changes: 21 additions & 12 deletions cocos/physics/bullet/bullet-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ import { error, RecyclePool, Vec3, js, IVec3Like, geometry, IQuatLike, Quat, Col
import { BulletContactData } from './bullet-contact-data';
import { BulletConstraint } from './constraints/bullet-constraint';
import { BulletCharacterController } from './character-controllers/bullet-character-controller';
import { bt, EBulletType, EBulletTriangleRaycastFlag, EBulletDebugDrawModes } from './instantiated';
import { bt, EBulletType, EBulletTriangleRaycastFlag, EBulletDebugDrawModes, btCache } from './instantiated';
import { Node } from '../../scene-graph';
import { director } from '../../game';
import { GeometryRenderer } from '../../rendering/geometry-renderer';
import { importFunc } from './bullet-env';

const contactsPool: BulletContactData[] = [];
const v3_0 = CC_V3_0;
Expand Down Expand Up @@ -145,12 +146,14 @@ export class BulletWorld implements IPhysicsWorld {
private static _sweepCapsuleGeometry: number;

constructor () {
bt.CACHE.world = this;
btCache.CACHE.world = this;
this._broadphase = bt.DbvtBroadphase_new();
this._dispatcher = bt.CollisionDispatcher_new();
this._solver = bt.SequentialImpulseConstraintSolver_new();
this._debugDraw = bt.DebugDraw_new();
this._world = bt.ccDiscreteDynamicsWorld_new(this._dispatcher, this._broadphase, this._solver);

const debugDraw = bt.DebugDraw.implement(importFunc);//new PhysicsDebugDraw();
this._debugDraw = debugDraw.$$.ptr;
bt.CollisionWorld_setDebugDrawer(this._world, this._debugDraw);

bt.DebugDraw_setDebugMode(this._debugDraw, EBulletDebugDrawModes.DBG_NoDebug);
Expand Down Expand Up @@ -225,7 +228,7 @@ export class BulletWorld implements IPhysicsWorld {
const to = cocos2BulletVec3(BulletCache.instance.BT_V3_0, v3_0);
const from = cocos2BulletVec3(BulletCache.instance.BT_V3_1, worldRay.o);
const allHitsCB = bt.ccAllRayCallback_static();
bt.ccAllRayCallback_reset(allHitsCB, from, to, options.mask, options.queryTrigger);
bt.ccAllRayCallback_reset(allHitsCB, from, to, options.mask >>> 0, options.queryTrigger);
bt.ccAllRayCallback_setFlags(allHitsCB, EBulletTriangleRaycastFlag.UseSubSimplexConvexCastRaytest);
bt.CollisionWorld_rayTest(this._world, from, to, allHitsCB);
if (bt.RayCallback_hasHit(allHitsCB)) {
Expand All @@ -249,7 +252,7 @@ export class BulletWorld implements IPhysicsWorld {
const to = cocos2BulletVec3(BulletCache.instance.BT_V3_0, v3_0);
const from = cocos2BulletVec3(BulletCache.instance.BT_V3_1, worldRay.o);
const closeHitCB = bt.ccClosestRayCallback_static();
bt.ccClosestRayCallback_reset(closeHitCB, from, to, options.mask, options.queryTrigger);
bt.ccClosestRayCallback_reset(closeHitCB, from, to, options.mask >>> 0, options.queryTrigger);
bt.ccClosestRayCallback_setFlags(closeHitCB, EBulletTriangleRaycastFlag.UseSubSimplexConvexCastRaytest);
bt.CollisionWorld_rayTest(this._world, from, to, closeHitCB);
if (bt.RayCallback_hasHit(closeHitCB)) {
Expand Down Expand Up @@ -387,7 +390,7 @@ export class BulletWorld implements IPhysicsWorld {
bt.Transform_setRotation(BT_toTransform, BT_orientation);

const allHitsCB = bt.ccAllConvexCallback_static();
bt.ccAllConvexCallback_reset(allHitsCB, BT_fromTransform, BT_toTransform, options.mask, options.queryTrigger);
bt.ccAllConvexCallback_reset(allHitsCB, BT_fromTransform, BT_toTransform, options.mask >>> 0, options.queryTrigger);
bt.CollisionWorld_convexSweepTest(this._world, btShapePtr, BT_fromTransform, BT_toTransform, allHitsCB, 0);
if (bt.ConvexCallback_hasHit(allHitsCB)) {
const posArray = bt.ccAllConvexCallback_getHitPointWorld(allHitsCB);
Expand Down Expand Up @@ -428,7 +431,7 @@ export class BulletWorld implements IPhysicsWorld {
bt.Transform_setRotation(BT_toTransform, BT_orientation);

const closeHitCB = bt.ccClosestConvexCallback_static();
bt.ccClosestConvexCallback_reset(closeHitCB, BT_fromTransform, BT_toTransform, options.mask, options.queryTrigger);
bt.ccClosestConvexCallback_reset(closeHitCB, BT_fromTransform, BT_toTransform, options.mask >>> 0, options.queryTrigger);
bt.CollisionWorld_convexSweepTest(this._world, btShapePtr, BT_fromTransform, BT_toTransform, closeHitCB, 0);
if (bt.ConvexCallback_hasHit(closeHitCB)) {
bullet2CocosVec3(v3_0, bt.ccClosestConvexCallback_getHitPointWorld(closeHitCB));
Expand All @@ -448,7 +451,9 @@ export class BulletWorld implements IPhysicsWorld {
const i = this.bodies.indexOf(sharedBody);
if (i < 0) {
this.bodies.push(sharedBody);
bt.DynamicsWorld_addRigidBody(this._world, sharedBody.body, sharedBody.collisionFilterGroup, sharedBody.collisionFilterMask);
const group = sharedBody.collisionFilterGroup;
const mask = sharedBody.collisionFilterMask;
bt.DynamicsWorld_addRigidBody(this._world, sharedBody.body, group >>> 0, mask >>> 0);
}
}

Expand All @@ -464,7 +469,9 @@ export class BulletWorld implements IPhysicsWorld {
const i = this.ghosts.indexOf(sharedBody);
if (i < 0) {
this.ghosts.push(sharedBody);
bt.CollisionWorld_addCollisionObject(this._world, sharedBody.ghost, sharedBody.collisionFilterGroup, sharedBody.collisionFilterMask);
const group = sharedBody.collisionFilterGroup;
const mask = sharedBody.collisionFilterMask;
bt.CollisionWorld_addCollisionObject(this._world, sharedBody.ghost, group >>> 0, mask >>> 0);
}
}

Expand All @@ -481,7 +488,9 @@ export class BulletWorld implements IPhysicsWorld {
if (index < 0) {
this.ccts.push(cct);
const cctGhost = bt.CharacterController_getGhostObject(cct.impl);
bt.CollisionWorld_addCollisionObject(this._world, cctGhost, cct.getGroup(), cct.getMask());
const group = cct.getGroup();
const mask = cct.getMask();
bt.CollisionWorld_addCollisionObject(this._world, cctGhost, group >>> 0, mask >>> 0);
bt.DynamicsWorld_addAction(this._world, cct.impl);
}
}
Expand Down Expand Up @@ -790,7 +799,7 @@ export class BulletWorld implements IPhysicsWorld {
//cct - collider trigger event
if (!processed) {
const shape: BulletShape = BulletCache.getWrapper(s0, BulletShape.TYPE);
const cct: BulletCharacterController = BulletCache.getWrapper(s1, bt.CCT_CACHE_NAME);
const cct: BulletCharacterController = BulletCache.getWrapper(s1, btCache.CCT_CACHE_NAME);
if (shape && cct) {
processed = true;
if (shape.collider.needTriggerEvent) {
Expand All @@ -811,7 +820,7 @@ export class BulletWorld implements IPhysicsWorld {

//cct - collider trigger event
if (!processed) {
const cct: BulletCharacterController = BulletCache.getWrapper(s0, bt.CCT_CACHE_NAME);
const cct: BulletCharacterController = BulletCache.getWrapper(s0, btCache.CCT_CACHE_NAME);
const shape: BulletShape = BulletCache.getWrapper(s1, BulletShape.TYPE);
if (shape && cct) {
processed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { BulletWorld } from '../bullet-world';
import { degreesToRadians } from '../../../core/utils/misc';
import { bt } from '../instantiated';
import { BulletCache } from '../bullet-cache';
import { importFunc } from '../bullet-env';

const v3_0 = new Vec3(0, 0, 0);

Expand All @@ -49,8 +50,7 @@ export class BulletBoxCharacterController extends BulletCharacterController impl
const up = BulletCache.instance.BT_V3_1;
bt.Vec3_set(up, upDir.x, upDir.y, upDir.z);

const report = bt.ControllerHitReport_new();

const report = bt.ControllerHitReport.implement(importFunc).$$.ptr as number;
const bulletWorld = (PhysicsSystem.instance.physicsWorld as BulletWorld);
const controllerDesc = bt.BoxCharacterControllerDesc_new(
degreesToRadians(this.component.slopeLimit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { BulletWorld } from '../bullet-world';
import { degreesToRadians } from '../../../core/utils/misc';
import { bt } from '../instantiated';
import { BulletCache } from '../bullet-cache';
import { importFunc } from '../bullet-env';

const v3_0 = new Vec3(0, 0, 0);

Expand All @@ -49,8 +50,7 @@ export class BulletCapsuleCharacterController extends BulletCharacterController
const up = BulletCache.instance.BT_V3_1;
bt.Vec3_set(up, upDir.x, upDir.y, upDir.z);

const report = bt.ControllerHitReport_new();

const report = bt.ControllerHitReport.implement(importFunc).$$.ptr as number;
const bulletWorld = (PhysicsSystem.instance.physicsWorld as BulletWorld);
const controllerDesc = bt.CapsuleCharacterControllerDesc_new(
degreesToRadians(this.component.slopeLimit),
Expand Down
Loading

0 comments on commit 38bcd20

Please sign in to comment.