diff --git a/src/lib/CharacterControls.js b/src/lib/CharacterControls.js index 8c81bb7..6274786 100644 --- a/src/lib/CharacterControls.js +++ b/src/lib/CharacterControls.js @@ -13,8 +13,8 @@ export class CharacterControls { // constants fadeDuration = 0.2 - runVelocity = 7 - walkVelocity = 3 + runVelocity = 11 + walkVelocity = 7 lerp = (x, y, a) => x * (1 - a) + y * a; constructor(model, mixer, animationsMap, engine, currentAction, po, pointerControls) { @@ -41,12 +41,6 @@ export class CharacterControls { this.orbitControl = engine.orbitControls this.camera = engine.camera this.updateCameraTarget(new THREE.Vector3(0,1,5)) - - this._v = new THREE.Vector3(); - this.velocity = new THREE.Vector3(); - this.forceDirection = new THREE.Vector3(); - this.forceAcceleration = 1 / 32; - this.forceSpeedMax = 0.075; } switchRunToggle() { @@ -90,7 +84,7 @@ export class CharacterControls { // rotate model this.rotateQuarternion.setFromAxisAngle(this.rotateAngle, Math.PI + angleYCameraDirection + directionOffset) - this.model.quaternion.rotateTowards(this.rotateQuarternion, 0.2) + this.model.quaternion.slerp(this.rotateQuarternion, delta*7) // calculate direction this.camera.getWorldDirection(this.walkDirection) @@ -98,6 +92,26 @@ export class CharacterControls { this.walkDirection.normalize() this.walkDirection.applyAxisAngle(this.rotateAngle, directionOffset) + //this.camera.quaternion.rotateTowards(this.rotateQuarternion, delta) + + let cameraPosition = new THREE.Vector3().copy(this.camera.position) + let cameraDesiredPosition = new THREE.Vector3( + this.model.position.x + 5* Math.sin(angleYCameraDirection + directionOffset), + 3, + this.model.position.z + 5* Math.cos(angleYCameraDirection + directionOffset) + ) + + cameraPosition.lerp(cameraDesiredPosition, delta*3) + + if (Math.abs(cameraPosition.x - this.model.position.x) + Math.abs(cameraPosition.z - this.model.position.z) > 2){ + this.camera.position.copy(cameraPosition) + this.camera.lookAt(new THREE.Vector3( + this.model.position.x, + 2, + this.model.position.z + )) + } + // run/walk velocity velocity = this.currentAction == 'run' ? this.runVelocity : this.walkVelocity } @@ -190,6 +204,7 @@ export class CharacterControls { } directionOffset(pointerControls) { + ///TODO: REWRITE!!!! var directionOffset = 0 // w if (pointerControls.moveForward) { @@ -214,90 +229,4 @@ export class CharacterControls { return directionOffset } - - update1 (world, delta, pointerControls){ - // Calculate input buffer - // if (this.jumpBuffer > 0) { - // this.jumpBuffer -= loop.delta; // ms - - // // Automatically jump if buffer is set - // if (this.allowJump === true) { - // this.jumpBuffer = 0; - // this.jump(); - // } - // } - - // Add fake friction and fake gravity - this.velocity.x *= 0.75; - this.velocity.z *= 0.75; - this.velocity.y -= 0.005; - - // Update force direction from user input - let xDirection = 0; - let zDirection = 0; - if (pointerControls.moveForward) zDirection = 1; - if (pointerControls.moveBackward) zDirection = -1; - if (pointerControls.moveRight) xDirection = -1; - if (pointerControls.moveLeft) xDirection = 1; - - let play = ''; - if (zDirection || xDirection) { - play = 'walk' - } else { - play = 'idle' - } - - if (this.currentAction != play) { - const toPlay = this.animationsMap[play] - const current = this.animationsMap[this.currentAction] - - current.fadeOut(this.fadeDuration) - toPlay.reset().fadeIn(this.fadeDuration).play(); - - this.currentAction = play - } - - this.mixer.update(delta) - - // Set the new force direction - this.forceDirection.copy({ x: xDirection, y: 0, z: zDirection }); // Ex: -1.0 to 1.0 - - // Decrease acceleration if the velocity speed equals the force speed - this._v.copy(this.velocity); - const speed = this._v.dot(this.forceDirection); - const speedNext = speed + this.forceAcceleration; - const speedClamped = Math.max(speed, Math.min(speedNext, this.forceSpeedMax)); - const acceleration = speedClamped - speed; // Ex: 0.5 (or 0 at max speed) - - // Add force to velocity using new acceleration - this.velocity.x += this.forceDirection.x * acceleration; - this.velocity.y += this.forceDirection.y * acceleration; - this.velocity.z += this.forceDirection.z * acceleration; - - // Set the next kinematic translation - if (this.po.rigidBody.numColliders() > 0) { - this.characterController.computeColliderMovement(this.po.collider, this.velocity); - this._v.copy(this.po.rigidBody.translation()); - this._v.add(this.characterController.computedMovement()); - this.po.rigidBody.setNextKinematicTranslation(this._v); - } - - // Calculate 3D object rotation from character translation - this._v.copy(this.po.rigidBody.nextTranslation()); - - - if (this._v.distanceTo(this.po.rigidBody.translation()) > 0.01) { - this.model.lookAt(this._v.x, this.model.position.y, this._v.z); - this.po.rigidBody.setNextKinematicRotation(this.model.quaternion); - } - - this.model.position.copy(this._v) - - // Set vertical velocity to zero if grounded - // if (this.characterController.computedGrounded()) { - // this.allowJump = true; - // this.entity.velocity.y = 0; - // } - } - } \ No newline at end of file