interactive objects integration
This commit is contained in:
@@ -23,9 +23,10 @@ export class CharacterControls {
|
||||
this.currentAction = currentAction
|
||||
this.animationsMap[currentAction].play()
|
||||
|
||||
this.characterController = engine.phy.world.createCharacterController(0.1);
|
||||
this.characterController = engine.physics.world.createCharacterController(0.1);
|
||||
this.characterController.setUp({x:0, y:1, z:0});
|
||||
po.rigidBody.setTranslation(this.model.position)
|
||||
po.characterController = this.characterController;
|
||||
// this.characterController.enableSnapToGround(0.5);
|
||||
// // Don’t allow climbing slopes larger than 45 degrees.
|
||||
// this.characterController.setMaxSlopeClimbAngle(45 * Math.PI / 180);
|
||||
|
||||
@@ -91,6 +91,13 @@ class DashBoard {
|
||||
dash.visible = false;
|
||||
}
|
||||
|
||||
this.reset = ()=>{
|
||||
this.updateProgress(0);
|
||||
this.update({
|
||||
hint: ''
|
||||
});
|
||||
}
|
||||
|
||||
this.createProgressBar();
|
||||
this.update();
|
||||
}
|
||||
|
||||
@@ -262,8 +262,8 @@ class GameEngine extends THREE.EventDispatcher{
|
||||
}
|
||||
|
||||
async initPhysics() {
|
||||
this.phy = new Physics(this);
|
||||
await this.phy.init();
|
||||
this.physics = new Physics(this);
|
||||
await this.physics.init();
|
||||
}
|
||||
|
||||
handleXrAction(gameEngine, delta) {
|
||||
|
||||
+7
-124
@@ -9,7 +9,6 @@ class Hero{
|
||||
this.object = object;
|
||||
this.data = data;
|
||||
this.model = object.scene
|
||||
//console.log(object)
|
||||
}
|
||||
|
||||
init(gameEngine){
|
||||
@@ -17,10 +16,6 @@ class Hero{
|
||||
gameEngine.camera.position.set(0,17,-30)
|
||||
gameEngine.camera.lookAt(new THREE.Vector3(this.model.position.x, 5, this.model.position.z))
|
||||
|
||||
// this.heroCamera = new THREE.Object3D()
|
||||
// this.model.add(this.heroCamera)
|
||||
// this.heroCamera.applyMatrix4(gameEngine.camera.matrix)
|
||||
|
||||
this.mixer = new AnimationMixer(this.model);
|
||||
gameEngine.mixers.push( this.mixer );
|
||||
|
||||
@@ -32,7 +27,7 @@ class Hero{
|
||||
this.pointerControls = new PointerControls(gameEngine.camera, this.model, gameEngine.renderer.domElement);
|
||||
gameEngine.hero = this;
|
||||
|
||||
let po = gameEngine.phy.add(this.model, 'kinematicPositionBased', false, undefined, 'capsule', { radius: 0.5, halfHeight: 1})
|
||||
let po = gameEngine.physics.add(this.model, 'kinematicPositionBased', false, undefined, 'capsule', { radius: 0.5, halfHeight: 1})
|
||||
po.collider.setTranslationWrtParent({ x: 0, y: 2.0, z: 0 });
|
||||
//po.collider.setActiveEvents(RAPIER.ActiveEvents.COLLISION_EVENTS);
|
||||
|
||||
@@ -59,128 +54,16 @@ class Hero{
|
||||
let dlt = this.clock.getDelta();
|
||||
this.delta += dlt;
|
||||
//if (this.delta > 0.00001){
|
||||
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
|
||||
this.gameEngine.phy.step()
|
||||
this.characterControls.update(this.gameEngine.physics.world, this.delta, pc)
|
||||
this.gameEngine.physics.step()
|
||||
this.delta = 0;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
let { inputVelocity, velocity, quat, v, targetQuaternion, clock, rotation } = this;
|
||||
// let pc = this.pointerControls;
|
||||
// pc.update();
|
||||
if (this.ready) {
|
||||
if (this.canJump) {
|
||||
//walking
|
||||
this.mixer.update(this.distance / 100)
|
||||
} else {
|
||||
//were in the air
|
||||
this.mixer.update(this.delta)
|
||||
}
|
||||
const p = this.characterCollider.position
|
||||
p.y -= 1
|
||||
this.model.position.y = this.characterCollider.position.y
|
||||
this.distance = this.model.position.distanceTo(p)
|
||||
|
||||
// const rotationMatrix = new THREE.Matrix4()
|
||||
// rotationMatrix.lookAt(p, this.model.position, this.model.up)
|
||||
// targetQuaternion.setFromRotationMatrix(rotationMatrix)
|
||||
|
||||
// if (!this.model.quaternion.equals(targetQuaternion)) {
|
||||
// this.model.quaternion.rotateTowards(targetQuaternion, this.delta * 10)
|
||||
// }
|
||||
|
||||
if (this.canJump) {
|
||||
inputVelocity.set(0, 0, 0)
|
||||
|
||||
if (pc.moveForward) {
|
||||
inputVelocity.z = 1
|
||||
}
|
||||
if (pc.moveBackward) {
|
||||
inputVelocity.z = -1
|
||||
}
|
||||
|
||||
if (pc.moveLeft) {
|
||||
//inputVelocity.x = -1
|
||||
rotation.y+=2*this.delta
|
||||
}
|
||||
if (pc.moveRight) {
|
||||
//inputVelocity.x = 1
|
||||
rotation.y-=2*this.delta
|
||||
}
|
||||
|
||||
//let d = this.gameEngine.camera.position.distanceTo(v);
|
||||
|
||||
inputVelocity.setLength(this.delta * 10)
|
||||
|
||||
// apply camera rotation to inputVelocity
|
||||
//euler.y = this.gameEngine.camera.rotation.y;
|
||||
quat.setFromEuler(rotation)
|
||||
inputVelocity.applyQuaternion(quat)
|
||||
//this.gameEngine.camera.updateProjectionMatrix()
|
||||
//this.gameEngine.orbitControls.update()
|
||||
// if (this.gameEngine.orbitControls){
|
||||
//this.gameEngine.orbitControls.target = v//this.model.position;
|
||||
// //this.gameEngine.orbitControls.update()
|
||||
// }
|
||||
|
||||
//this.gameEngine.camera.rotateOnAxis(v, rotation.y)
|
||||
|
||||
}
|
||||
|
||||
if(inputVelocity.x || inputVelocity.z){
|
||||
this.setAction(this.actionWalk, true)
|
||||
}else{
|
||||
this.setAction(this.actionIdle, true)
|
||||
}
|
||||
|
||||
// this.gameEngine.camera.position.lerp(this.characterCollider.position, 1)
|
||||
// this.gameEngine.camera.rotation.y = rotation.y;
|
||||
|
||||
this.model.position.lerp(this.characterCollider.position, 1)
|
||||
this.model.rotation.y = rotation.y;
|
||||
|
||||
}
|
||||
velocity.set(inputVelocity.x, inputVelocity.y, inputVelocity.z)
|
||||
this.colliderBody.applyImpulse(velocity)
|
||||
|
||||
this.delta = Math.min(clock.getDelta(), 0.1)
|
||||
this.gameEngine.phy.world.step(this.delta)
|
||||
|
||||
//cannonDebugRenderer.update()
|
||||
|
||||
this.characterCollider.position.set(this.colliderBody.position.x, this.colliderBody.position.y, this.colliderBody.position.z)
|
||||
// boxes.forEach((b, i) => {
|
||||
// boxMeshes[i].position.set(b.position.x, b.position.y, b.position.z)
|
||||
// boxMeshes[i].quaternion.set(b.quaternion.x, b.quaternion.y, b.quaternion.z, b.quaternion.w)
|
||||
// })
|
||||
|
||||
this.characterCollider.getWorldPosition(v)
|
||||
//this.gameEngine.cameraPivot.position.lerp(v, 0.1)
|
||||
|
||||
// if (this.gameEngine.camera.position.distanceTo(v) > 2){
|
||||
// this.gameEngine.camera.position.set(v.x+1, 3, v.z + 3);
|
||||
// }
|
||||
//this.gameEngine.camera.lookAt(v.x, 3, v.z);
|
||||
//this.gameEngine.camera.rotation.y = this.model.rotation.y;
|
||||
|
||||
// this.gameEngine.camera.updateMatrix();
|
||||
// this.model.updateMatrix();
|
||||
let cam = this.gameEngine.camera;
|
||||
let vv = new THREE.Vector3(), qq = new THREE.Quaternion;
|
||||
this.heroCamera.getWorldPosition(vv)
|
||||
//this.heroCamera.getWorldQuaternion(qq);
|
||||
cam.position.copy(vv)
|
||||
//cam.setRotationFromQuaternion(qq)
|
||||
cam.lookAt(new THREE.Vector3(this.model.position.x, 2.8, this.model.position.z))
|
||||
//console.log(vv)
|
||||
// cam.matrixAutoUpdate = false;
|
||||
// cam.matrix.multiplyMatrices(this.model.matrix, cam.matrix)
|
||||
// //cam.matrix.decompose(cam.position, cam.quaternion, cam.scale)
|
||||
// //this.gameEngine.camera.updateMatrix();
|
||||
// cam.matrixAutoUpdate = true;
|
||||
// this.gameEngine.orbitControls.update()
|
||||
destroy(){
|
||||
delete this.gameEngine.hero;
|
||||
this.gameEngine.mixers.splice(this.gameEngine.mixers.indexOf(this.mixer), 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-10
@@ -31,29 +31,25 @@ class Physics{
|
||||
let colliderDesc
|
||||
|
||||
switch (colliderType) {
|
||||
case 'cuboid':
|
||||
{
|
||||
case 'cuboid':{
|
||||
const { width, height, depth } = colliderSettings
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(width, height, depth)
|
||||
}
|
||||
break
|
||||
|
||||
case 'ball':
|
||||
{
|
||||
case 'ball':{
|
||||
const { radius } = colliderSettings
|
||||
colliderDesc = RAPIER.ColliderDesc.ball(radius)
|
||||
}
|
||||
break
|
||||
|
||||
case 'capsule':
|
||||
{
|
||||
case 'capsule':{
|
||||
const { halfHeight, radius } = colliderSettings
|
||||
colliderDesc = RAPIER.ColliderDesc.capsule(halfHeight, radius)
|
||||
}
|
||||
break
|
||||
|
||||
default:
|
||||
{
|
||||
default:{
|
||||
colliderDesc = RAPIER.ColliderDesc.trimesh(
|
||||
mesh.geometry.attributes.position.array,
|
||||
mesh.geometry.index?.array
|
||||
@@ -74,14 +70,21 @@ class Physics{
|
||||
|
||||
// * Responsible for collision detection
|
||||
const collider = this.world.createCollider(colliderDesc, rigidBody)
|
||||
|
||||
const physicsObject = { mesh, collider, rigidBody, fn: postPhysicsFn, autoAnimate }
|
||||
|
||||
this.physicsObjects.push(physicsObject)
|
||||
|
||||
return physicsObject
|
||||
}
|
||||
|
||||
clear(){
|
||||
this.physicsObjects.forEach(po=>{
|
||||
po.characterController && this.world.removeCharacterController(po.characterController)
|
||||
this.world.removeCollider(po.collider);
|
||||
this.world.removeRigidBody(po.rigidBody);
|
||||
})
|
||||
this.physicsObjects = [];
|
||||
}
|
||||
|
||||
step(){
|
||||
this.world.step(this.eventQueue)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user