Created
July 28, 2020 10:42
-
-
Save andrewmunro/9bc6e5c85481d1c72ff9d97a6968f40d to your computer and use it in GitHub Desktop.
Adding PhysxSystem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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