new physics engine - rapier instead of cannon
This commit is contained in:
+107
-65
@@ -1,7 +1,8 @@
|
||||
import { AnimationMixer } from 'three';
|
||||
import { PointerControls } from './PointerControls';
|
||||
import { CharacterControls } from './CharacterControls';
|
||||
import * as THREE from 'three';
|
||||
import * as CANNON from 'cannon-es';
|
||||
import * as RAPIER from '@dimforge/rapier3d'
|
||||
|
||||
class Hero{
|
||||
|
||||
@@ -16,7 +17,8 @@ class Hero{
|
||||
this.gameEngine = gameEngine;
|
||||
//this.gameEngine.orbitControls.object = this.model;
|
||||
//this.gameEngine.orbitControls.target = this.model.position;
|
||||
this.gameEngine.orbitControls.enabled = false;
|
||||
|
||||
//this.gameEngine.orbitControls.enabled = false;
|
||||
|
||||
//this.model.add(gameEngine.camera)
|
||||
gameEngine.camera.position.set(0,17,-30)
|
||||
@@ -24,66 +26,91 @@ class Hero{
|
||||
|
||||
this.heroCamera = new THREE.Object3D()
|
||||
this.model.add(this.heroCamera)
|
||||
this.heroCamera.applyMatrix4(gameEngine.camera.matrix)
|
||||
this.heroCamera.applyMatrix4(gameEngine.camera.matrix)
|
||||
|
||||
this.mixer = new AnimationMixer(this.model);
|
||||
gameEngine.mixers.push( this.mixer );
|
||||
this.actionWalk = this.mixer.clipAction( this.object.animations.find(a=>a.name=='walk') );
|
||||
this.actionIdle = this.mixer.clipAction( this.object.animations.find(a=>a.name=='idle') );
|
||||
this.actionIdle.play();
|
||||
this.activeAction = this.actionIdle;
|
||||
|
||||
this.animationsMap = {};
|
||||
this.object.animations.forEach(a=>{
|
||||
this.animationsMap[a.name] = this.mixer.clipAction(a);
|
||||
})
|
||||
|
||||
// this.actionWalk = this.mixer.clipAction( this.object.animations.find(a=>a.name=='walk') );
|
||||
// this.actionIdle = this.mixer.clipAction( this.object.animations.find(a=>a.name=='idle') );
|
||||
// this.actionIdle.play();
|
||||
// this.activeAction = this.actionIdle;
|
||||
this.pointerControls = new PointerControls(gameEngine.camera, this.model, gameEngine.renderer.domElement);
|
||||
gameEngine.hero = this;
|
||||
|
||||
// Character Collider
|
||||
const characterCollider = new THREE.Object3D()
|
||||
characterCollider.position.y = 3
|
||||
gameEngine.activeObjects.add(characterCollider)
|
||||
const colliderShape = new CANNON.Sphere(0.5)
|
||||
const colliderBody = new CANNON.Body({ mass: 1, material: gameEngine.phy.slipperyMaterial })
|
||||
colliderBody.addShape(colliderShape, new CANNON.Vec3(0, 0.5, 0))
|
||||
colliderBody.addShape(colliderShape, new CANNON.Vec3(0, -0.5, 0))
|
||||
colliderBody.position.set(
|
||||
characterCollider.position.x,
|
||||
characterCollider.position.y,
|
||||
characterCollider.position.z
|
||||
)
|
||||
colliderBody.linearDamping = 0.95
|
||||
colliderBody.angularFactor.set(0, 1, 0) // prevents rotation X,Z axis
|
||||
//colliderBody.sleepSpeedLimit = 1.0;
|
||||
gameEngine.phy.world.addBody(colliderBody)
|
||||
// const characterCollider = new THREE.Object3D()
|
||||
// characterCollider.position.y = 3
|
||||
// gameEngine.activeObjects.add(characterCollider)
|
||||
|
||||
this.characterCollider = characterCollider;
|
||||
this.colliderBody = colliderBody;
|
||||
|
||||
// const colliderShape = new CANNON.Sphere(0.5)
|
||||
// const colliderBody = new CANNON.Body({ mass: 1, material: gameEngine.phy.slipperyMaterial })
|
||||
// colliderBody.addShape(colliderShape, new CANNON.Vec3(0, 0.5, 0))
|
||||
// colliderBody.addShape(colliderShape, new CANNON.Vec3(0, -0.5, 0))
|
||||
// colliderBody.position.set(
|
||||
// characterCollider.position.x,
|
||||
// characterCollider.position.y,
|
||||
// characterCollider.position.z
|
||||
// )
|
||||
// colliderBody.linearDamping = 0.95
|
||||
// colliderBody.angularFactor.set(0, 1, 0) // prevents rotation X,Z axis
|
||||
// //colliderBody.sleepSpeedLimit = 1.0;
|
||||
// gameEngine.phy.world.addBody(colliderBody)
|
||||
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased().setTranslation(
|
||||
// characterCollider.position.x,
|
||||
// characterCollider.position.y,
|
||||
// characterCollider.position.z
|
||||
-1, 3, 1
|
||||
)
|
||||
let rigidBody = gameEngine.phy.world.createRigidBody(bodyDesc);
|
||||
let dynamicCollider = RAPIER.ColliderDesc.ball(0.28);
|
||||
gameEngine.phy.world.createCollider(dynamicCollider, rigidBody.handle);
|
||||
|
||||
this.characterControls = new CharacterControls(this.model, this.mixer,
|
||||
this.animationsMap, gameEngine.orbitControls, gameEngine.camera, 'idle',
|
||||
new RAPIER.Ray(
|
||||
{ x: 0, y: 0, z: 0 },
|
||||
{ x: 0, y: -1, z: 0}
|
||||
), rigidBody, this.pointerControls)
|
||||
|
||||
// this.characterCollider = characterCollider;
|
||||
// this.colliderBody = colliderBody;
|
||||
|
||||
this.clock = new THREE.Clock()
|
||||
this.delta = 0
|
||||
this.inputVelocity = new THREE.Vector3()
|
||||
this.velocity = new CANNON.Vec3()
|
||||
this.quat = new THREE.Quaternion()
|
||||
this.v = new THREE.Vector3()
|
||||
this.rotation = new THREE.Euler()
|
||||
this.targetQuaternion = new THREE.Quaternion()
|
||||
this.distance = 0
|
||||
this.canJump = true;
|
||||
// this.inputVelocity = new THREE.Vector3()
|
||||
// this.velocity = new CANNON.Vec3()
|
||||
// this.quat = new THREE.Quaternion()
|
||||
// this.v = new THREE.Vector3()
|
||||
// this.rotation = new THREE.Euler()
|
||||
// this.targetQuaternion = new THREE.Quaternion()
|
||||
// this.distance = 0
|
||||
// this.canJump = true;
|
||||
|
||||
this.contactNormal = new CANNON.Vec3()
|
||||
this.upAxis = new CANNON.Vec3(0, 1, 0)
|
||||
// this.contactNormal = new CANNON.Vec3()
|
||||
// this.upAxis = new CANNON.Vec3(0, 1, 0)
|
||||
|
||||
colliderBody.addEventListener('collide', function (e) {
|
||||
const contact = e.contact
|
||||
if (contact.bi.id == this.colliderBody.id) {
|
||||
contact.ni.negate(this.contactNormal)
|
||||
} else {
|
||||
this.contactNormal.copy(contact.ni)
|
||||
}
|
||||
if (this.contactNormal.dot(this.upAxis) > 0.5) {
|
||||
if (!this.canJump) {
|
||||
setAction(animationActions[1], true)
|
||||
}
|
||||
this.canJump = true
|
||||
}
|
||||
}.bind(this))
|
||||
// colliderBody.addEventListener('collide', function (e) {
|
||||
// const contact = e.contact
|
||||
// if (contact.bi.id == this.colliderBody.id) {
|
||||
// contact.ni.negate(this.contactNormal)
|
||||
// } else {
|
||||
// this.contactNormal.copy(contact.ni)
|
||||
// }
|
||||
// if (this.contactNormal.dot(this.upAxis) > 0.5) {
|
||||
// if (!this.canJump) {
|
||||
// setAction(animationActions[1], true)
|
||||
// }
|
||||
// this.canJump = true
|
||||
// }
|
||||
// }.bind(this))
|
||||
|
||||
|
||||
this.ready = true;
|
||||
@@ -93,27 +120,42 @@ class Hero{
|
||||
this.pointerControls.controls.lock();
|
||||
}
|
||||
|
||||
setAction(toAction, loop){
|
||||
if (toAction != this.activeAction) {
|
||||
let lastAction = this.activeAction;
|
||||
this.activeAction = toAction
|
||||
lastAction.fadeOut(0.1)
|
||||
this.activeAction.reset()
|
||||
this.activeAction.fadeIn(0.1)
|
||||
this.activeAction.play()
|
||||
if (!loop) {
|
||||
this.activeAction.clampWhenFinished = true
|
||||
this.activeAction.loop = THREE.LoopOnce
|
||||
}
|
||||
}
|
||||
}
|
||||
// setAction(toAction, loop){
|
||||
// if (toAction != this.activeAction) {
|
||||
// let lastAction = this.activeAction;
|
||||
// this.activeAction = toAction
|
||||
// lastAction.fadeOut(0.1)
|
||||
// this.activeAction.reset()
|
||||
// this.activeAction.fadeIn(0.1)
|
||||
// this.activeAction.play()
|
||||
// if (!loop) {
|
||||
// this.activeAction.clampWhenFinished = true
|
||||
// this.activeAction.loop = THREE.LoopOnce
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
update(){
|
||||
//return
|
||||
if (this.gameEngine.renderer.xr.isPresenting) return;
|
||||
|
||||
if (this.ready) {
|
||||
let pc = this.pointerControls;
|
||||
pc.update();
|
||||
let dlt = this.clock.getDelta();
|
||||
this.delta += dlt;
|
||||
if (this.delta > 0.016){
|
||||
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
|
||||
this.gameEngine.phy.world.step()
|
||||
this.delta = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
let { inputVelocity, velocity, quat, v, targetQuaternion, clock, rotation } = this;
|
||||
let pc = this.pointerControls;
|
||||
pc.update();
|
||||
// let pc = this.pointerControls;
|
||||
// pc.update();
|
||||
if (this.ready) {
|
||||
if (this.canJump) {
|
||||
//walking
|
||||
|
||||
Reference in New Issue
Block a user