resolves #6
This commit is contained in:
+34
-9
@@ -1,8 +1,17 @@
|
||||
import * as RAPIER from '@dimforge/rapier3d'
|
||||
import { Vector3 } from 'three';
|
||||
import { Quaternion, Vector3 } from 'three';
|
||||
class Physics{
|
||||
constructor(engine){
|
||||
this.engine = engine
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
start(){
|
||||
this.started = true;
|
||||
}
|
||||
|
||||
stop(){
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
init = async ()=>{
|
||||
@@ -10,11 +19,11 @@ class Physics{
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(100, 0, 100);
|
||||
//bodyDesc.setTranslation(-50,0,-50);
|
||||
world.createCollider(colliderDesc, body.handle);
|
||||
// let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
// let body = world.createRigidBody(bodyDesc);
|
||||
// let colliderDesc = RAPIER.ColliderDesc.cuboid(100, 0, 100);
|
||||
// //bodyDesc.setTranslation(-50,0,-50);
|
||||
// world.createCollider(colliderDesc, body.handle);
|
||||
this.world = world;
|
||||
this.physicsObjects = [];
|
||||
this.eventQueue = new RAPIER.EventQueue(true);
|
||||
@@ -23,8 +32,18 @@ class Physics{
|
||||
|
||||
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)
|
||||
let position, quaternion;
|
||||
// if (colliderSettings.root){
|
||||
// // mesh.getWorldPosition(position = new Vector3());
|
||||
// // mesh.getWorldQuaternion(quaternion = new Quaternion());
|
||||
// position = colliderSettings.root.position;
|
||||
// quaternion = colliderSettings.root.quaternion;
|
||||
// }else{
|
||||
position = mesh.position;
|
||||
quaternion = mesh.quaternion;
|
||||
//}
|
||||
rigidBodyDesc.setTranslation(position.x, position.y, position.z)
|
||||
quaternion && rigidBodyDesc.setRotation(quaternion)
|
||||
|
||||
// * Responsible for collision response
|
||||
const rigidBody = this.world.createRigidBody(rigidBodyDesc)
|
||||
@@ -82,7 +101,10 @@ class Physics{
|
||||
|
||||
// * Responsible for collision detection
|
||||
const collider = this.world.createCollider(colliderDesc, rigidBody)
|
||||
mesh.quaternion && collider.setRotationWrtParent(mesh.quaternion)
|
||||
if (colliderSettings.root){
|
||||
collider.setTranslationWrtParent(colliderSettings.root.position)
|
||||
collider.setRotationWrtParent(colliderSettings.root.quaternion)
|
||||
}
|
||||
const physicsObject = { mesh, collider, rigidBody, fn: postPhysicsFn, autoAnimate }
|
||||
this.physicsObjects.push(physicsObject)
|
||||
|
||||
@@ -99,6 +121,9 @@ class Physics{
|
||||
}
|
||||
|
||||
step(){
|
||||
if (!this.started){
|
||||
return;
|
||||
}
|
||||
this.world.step(this.eventQueue)
|
||||
|
||||
for (let po of this.physicsObjects) {
|
||||
|
||||
Reference in New Issue
Block a user