amazing
This commit is contained in:
+33
-126
@@ -40,7 +40,10 @@ export class CharacterControls {
|
||||
|
||||
this.orbitControl = engine.orbitControls
|
||||
this.camera = engine.camera
|
||||
this.updateCameraTarget(new THREE.Vector3(0,1,5))
|
||||
//this.updateCameraTarget(new THREE.Vector3(0,1,5))
|
||||
|
||||
this.direction = this.model.rotation.y;
|
||||
this.directionVelocity = 0;
|
||||
}
|
||||
|
||||
switchRunToggle() {
|
||||
@@ -75,44 +78,19 @@ export class CharacterControls {
|
||||
|
||||
let velocity = 0
|
||||
if (this.currentAction == 'run' || this.currentAction == 'walk') {
|
||||
// calculate towards camera direction
|
||||
var angleYCameraDirection = Math.atan2(
|
||||
(this.camera.position.x - this.model.position.x),
|
||||
(this.camera.position.z - this.model.position.z))
|
||||
// diagonal movement angle offset
|
||||
var directionOffset = this.directionOffset(pointerControls)
|
||||
let input = this.getInput(pointerControls)
|
||||
this.directionVelocity = this.directionVelocity * 2.5 * Math.abs(input[0])
|
||||
this.direction += input[0] * delta * 2.5 //this.directionVelocity;
|
||||
this.model.rotation.y = this.direction;
|
||||
this.walkDirection.set(input[0], 0, input[1])
|
||||
this.walkDirection.applyAxisAngle(this.rotateAngle, this.direction)
|
||||
this.walkDirection.normalize();
|
||||
|
||||
|
||||
// rotate model
|
||||
this.rotateQuarternion.setFromAxisAngle(this.rotateAngle, Math.PI + angleYCameraDirection + directionOffset)
|
||||
this.model.quaternion.slerp(this.rotateQuarternion, delta*7)
|
||||
|
||||
// calculate direction
|
||||
this.camera.getWorldDirection(this.walkDirection)
|
||||
this.walkDirection.y = 0
|
||||
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*2)
|
||||
|
||||
let dst = Math.sqrt(Math.pow(cameraPosition.x - this.model.position.x, 2) + Math.pow(cameraPosition.z - this.model.position.z, 2));
|
||||
//let dst = Math.sqrt(Math.pow(cameraPosition.x - this.model.position.x, 2) + Math.pow(cameraPosition.z - this.model.position.z, 2));
|
||||
//cameraPosition.y = 8 - dst;
|
||||
if (dst >0.52){
|
||||
this.camera.position.copy(cameraPosition)
|
||||
this.camera.lookAt(new THREE.Vector3(
|
||||
this.model.position.x,
|
||||
2,
|
||||
this.model.position.z
|
||||
))
|
||||
}
|
||||
//if (dst >0.52){
|
||||
//}
|
||||
//this.camera.zoom = dst
|
||||
//this.camera.updateProjectionMatrix();
|
||||
|
||||
@@ -130,12 +108,6 @@ export class CharacterControls {
|
||||
this.walkDirection, // The movement we would like to apply if there wasn’t any obstacle.
|
||||
);
|
||||
|
||||
// for (let i = 0; i < this.characterController.numComputedCollisions(); i++) {
|
||||
// let collision = this.characterController.computedCollision(i);
|
||||
// console.log('c', collision)
|
||||
// // Do something with that collision information.
|
||||
// }
|
||||
|
||||
let correctedMovement = this.characterController.computedMovement();
|
||||
|
||||
let v = new THREE.Vector3();
|
||||
@@ -144,93 +116,28 @@ export class CharacterControls {
|
||||
|
||||
this.po.rigidBody.setNextKinematicTranslation(v);
|
||||
|
||||
//console.log(this.walkDirection, correctedMovement);
|
||||
//this.po.rigidBody.setNextKinematicRotation(this.rotateQuarternion);
|
||||
|
||||
// if (translation.y < -1) {
|
||||
// // don't fall below ground
|
||||
// this.rigidBody.setNextKinematicTranslation( {
|
||||
// x: 0,
|
||||
// y: 10,
|
||||
// z: 0
|
||||
// });
|
||||
// } else {
|
||||
|
||||
|
||||
const cameraPositionOffset = this.camera.position.sub(this.model.position);
|
||||
this.model.position.copy(this.po.rigidBody.nextTranslation())
|
||||
//this.camera.position.add(correctedMovement)
|
||||
// // update model and camera
|
||||
// this.model.position.x = translation.x
|
||||
// this.model.position.y = translation.y
|
||||
// this.model.position.z = translation.z
|
||||
|
||||
this.updateCameraTarget(cameraPositionOffset, correctedMovement)
|
||||
|
||||
// this.walkDirection.y += this.lerp(this.storedFall, -9.81 * delta, 0.10)
|
||||
// this.storedFall = this.walkDirection.y
|
||||
// this.ray.origin.x = translation.x
|
||||
// this.ray.origin.y = translation.y
|
||||
// this.ray.origin.z = translation.z
|
||||
// let hit = world.castRay(this.ray, 0.5, false, 0xfffffffff);
|
||||
// if (hit) {
|
||||
// const point = this.ray.pointAt(hit.timeOfImpact);
|
||||
// let diff = translation.y - ( point.y + CONTROLLER_BODY_RADIUS);
|
||||
// if (diff < 0.0) {
|
||||
// this.storedFall = 0
|
||||
// this.walkDirection.y = this.lerp(0, -diff, 0.5)
|
||||
// }
|
||||
// }
|
||||
|
||||
// this.walkDirection.x = this.walkDirection.x * velocity * delta
|
||||
// this.walkDirection.z = this.walkDirection.z * velocity * delta
|
||||
let cameraPosition = new THREE.Vector3().copy(this.camera.position)
|
||||
let cameraDesiredPosition = new THREE.Vector3(
|
||||
this.model.position.x + 5* Math.sin(this.model.rotation.y + Math.PI),
|
||||
3,
|
||||
this.model.position.z + 5* Math.cos(this.model.rotation.y + Math.PI)
|
||||
)
|
||||
|
||||
// this.rigidBody.setNextKinematicTranslation( {
|
||||
// x: translation.x + this.walkDirection.x,
|
||||
// y: translation.y + this.walkDirection.y,
|
||||
// z: translation.z + this.walkDirection.z
|
||||
// });
|
||||
// }
|
||||
cameraPosition.lerp(cameraDesiredPosition, delta*2)
|
||||
this.camera.position.copy(cameraPosition)
|
||||
this.camera.lookAt(new THREE.Vector3(
|
||||
this.model.position.x,
|
||||
2,
|
||||
this.model.position.z
|
||||
))
|
||||
}
|
||||
|
||||
updateCameraTarget(offset, cm) {
|
||||
// move camera
|
||||
const rigidTranslation = this.po.rigidBody.nextTranslation();
|
||||
this.camera.position.x = rigidTranslation.x + offset.x
|
||||
this.camera.position.y = rigidTranslation.y + offset.y
|
||||
this.camera.position.z = rigidTranslation.z + offset.z
|
||||
|
||||
// update camera target
|
||||
this.cameraTarget.x = rigidTranslation.x
|
||||
this.cameraTarget.y = rigidTranslation.y + 1
|
||||
this.cameraTarget.z = rigidTranslation.z
|
||||
this.orbitControl.target = this.cameraTarget
|
||||
}
|
||||
|
||||
directionOffset(pointerControls) {
|
||||
///TODO: REWRITE!!!!
|
||||
var directionOffset = 0 // w
|
||||
|
||||
if (pointerControls.moveForward) {
|
||||
if (pointerControls.moveLeft) {
|
||||
directionOffset = Math.PI / 4 // w+a
|
||||
} else if (pointerControls.moveRight) {
|
||||
directionOffset = - Math.PI / 4 // w+d
|
||||
}
|
||||
} else if (pointerControls.moveBackward) {
|
||||
if (pointerControls.moveLeft) {
|
||||
directionOffset = Math.PI / 4 + Math.PI / 2 // s+a
|
||||
} else if (pointerControls.moveRight) {
|
||||
directionOffset = -Math.PI / 4 - Math.PI / 2 // s+d
|
||||
} else {
|
||||
directionOffset = Math.PI // s
|
||||
}
|
||||
} else if (pointerControls.moveLeft) {
|
||||
directionOffset = Math.PI / 2 // a
|
||||
} else if (pointerControls.moveRight) {
|
||||
directionOffset = - Math.PI / 2 // d
|
||||
}
|
||||
|
||||
return directionOffset
|
||||
getInput(pointerControls) {
|
||||
return [
|
||||
pointerControls.moveLeft * 1 + pointerControls.moveRight * -1,
|
||||
pointerControls.moveForward * 1 + pointerControls.moveBackward * -1
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user