Skip to content

Instantly share code, notes, and snippets.

@andrewmunro
Created July 28, 2020 10:42
Show Gist options
  • Save andrewmunro/9bc6e5c85481d1c72ff9d97a6968f40d to your computer and use it in GitHub Desktop.
Save andrewmunro/9bc6e5c85481d1c72ff9d97a6968f40d to your computer and use it in GitHub Desktop.
Adding PhysxSystem
export class PhysxSystem extends System {
private physics: any;
private scene: any;
private bodies = [];
constructor() {
super();
const PhysX = physx({
locateFile(path) {
if (path.endsWith('.wasm')) {
return physxModule;
}
return path;
},
onRuntimeInitialized: () => this.setup(PhysX)
});
}
setup(PhysX) {
const version = PhysX.PX_PHYSICS_VERSION;
const defaultErrorCallback = new PhysX.PxDefaultErrorCallback();
const allocator = new PhysX.PxDefaultAllocator();
const foundation = PhysX.PxCreateFoundation(version, allocator, defaultErrorCallback);
const triggerCallback = {
onContactBegin: () => {
console.log('contact', ...arguments);
},
onContactEnd: () => {},
onContactPersist: () => {},
onTriggerBegin: () => {},
onTriggerEnd: () => {}
};
const physxSimulationCallbackInstance = PhysX.PxSimulationEventCallback.implement(triggerCallback);
this.physics = PhysX.PxCreatePhysics(version, foundation, new PhysX.PxTolerancesScale(), false, null);
PhysX.PxInitExtensions(this.physics, null);
const sceneDesc = PhysX.getDefaultSceneDesc(this.physics.getTolerancesScale(), 0, physxSimulationCallbackInstance);
this.scene = this.physics.createScene(sceneDesc);
let geometry = new PhysX.PxBoxGeometry(5, 5, 5);
const flags = new PhysX.PxShapeFlags(PhysX.PxShapeFlag.eSCENE_QUERY_SHAPE.value | PhysX.PxShapeFlag.eSIMULATION_SHAPE.value);
const material = this.physics.createMaterial(0.2, 0.2, 0.2);
const shape = this.physics.createShape(geometry, material, false, flags);
const transform = {
translation: {
x: 0,
y: 0,
z: 0
},
rotation: {
w: 0, // PhysX uses WXYZ quaternions,
x: 0,
y: 0,
z: 0
}
};
const body = this.physics.createRigidDynamic(transform);
body.attachShape(shape);
this.scene.addActor(body, null);
this.bodies.push(body);
}
update(dt) {
if (this.scene) {
this.scene.simulate(1 / 60, true);
this.scene.fetchResults(true);
this.bodies.forEach(b => {
const transform = b.getGlobalPose();
console.log(transform);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment