add physics to interactive objects
This commit is contained in:
+7
-11
@@ -20,9 +20,10 @@ class Physics{
|
||||
return this;
|
||||
}
|
||||
|
||||
add = (mesh, rigidBodyType, autoAnimate = true, postPhysicsFn, colliderType, colliderSettings) => {
|
||||
add = (mesh, rigidBodyType, autoAnimate = true, postPhysicsFn, colliderType, colliderSettings = {}) => {
|
||||
const rigidBodyDesc = RAPIER.RigidBodyDesc[rigidBodyType]()
|
||||
rigidBodyDesc.setTranslation(mesh.position.x, mesh.position.y, mesh.position.z)
|
||||
mesh.quaternion && rigidBodyDesc.setRotation(mesh.quaternion)
|
||||
|
||||
// * Responsible for collision response
|
||||
const rigidBody = this.world.createRigidBody(rigidBodyDesc)
|
||||
@@ -70,6 +71,7 @@ class Physics{
|
||||
|
||||
// * Responsible for collision detection
|
||||
const collider = this.world.createCollider(colliderDesc, rigidBody)
|
||||
mesh.quaternion && collider.setRotationWrtParent(mesh.quaternion)
|
||||
const physicsObject = { mesh, collider, rigidBody, fn: postPhysicsFn, autoAnimate }
|
||||
this.physicsObjects.push(physicsObject)
|
||||
|
||||
@@ -89,17 +91,11 @@ class Physics{
|
||||
this.world.step(this.eventQueue)
|
||||
|
||||
for (let po of this.physicsObjects) {
|
||||
const autoAnimate = po.autoAnimate
|
||||
|
||||
if (autoAnimate) {
|
||||
const mesh = po.mesh
|
||||
const collider = po.collider
|
||||
mesh.position.copy(collider.translation())
|
||||
mesh.quaternion.copy(collider.rotation() )
|
||||
if (po.autoAnimate) {
|
||||
po.mesh.position.copy(po.rigidBody.translation())
|
||||
po.mesh.quaternion.copy(po.rigidBody.rotation() )
|
||||
}
|
||||
|
||||
const fn = po.fn
|
||||
fn && fn()
|
||||
po.fn?.()
|
||||
}
|
||||
|
||||
this.eventQueue.drainCollisionEvents((handle1, handle2, started) => {
|
||||
|
||||
Reference in New Issue
Block a user