obstacle avoidance
This commit is contained in:
@@ -79,11 +79,11 @@ class MazeObject {
|
|||||||
)
|
)
|
||||||
left.rigidBody.setRotation(quat, true)
|
left.rigidBody.setRotation(quat, true)
|
||||||
|
|
||||||
// let right = engine.phy.add(
|
let right = engine.phy.add(
|
||||||
// {position: room.localToWorld(new Vector3(-context.tubeSize / 2, 0.6, offsetZ/2))},
|
{position: room.localToWorld(new Vector3(-context.tubeSize / 2, 0.6, offsetZ/2))},
|
||||||
// 'fixed', false, undefined, 'cuboid',{ width: 0.01, height:1, depth:offsetZ/2 }
|
'fixed', false, undefined, 'cuboid',{ width: 0.01, height:1, depth:offsetZ/2 }
|
||||||
// )
|
)
|
||||||
// right.rigidBody.setRotation(quat, true)
|
right.rigidBody.setRotation(quat, true)
|
||||||
|
|
||||||
|
|
||||||
if (def.type == 'area') {
|
if (def.type == 'area') {
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export class CharacterControls {
|
|||||||
this.toggleRun = !this.toggleRun
|
this.toggleRun = !this.toggleRun
|
||||||
}
|
}
|
||||||
|
|
||||||
update0(world, delta, pointerControls) {
|
update(world, delta, pointerControls) {
|
||||||
const directionPressed = pointerControls.moving()
|
const directionPressed = pointerControls.moving()
|
||||||
|
|
||||||
var play = '';
|
var play = '';
|
||||||
@@ -102,11 +102,10 @@ export class CharacterControls {
|
|||||||
velocity = this.currentAction == 'run' ? this.runVelocity : this.walkVelocity
|
velocity = this.currentAction == 'run' ? this.runVelocity : this.walkVelocity
|
||||||
}
|
}
|
||||||
|
|
||||||
this.walkDirection.x = this.walkDirection.x * velocity * delta + this.model.position.x
|
this.walkDirection.x = this.walkDirection.x * velocity * delta// + this.model.position.x
|
||||||
this.walkDirection.z = this.walkDirection.z * velocity * delta + this.model.position.z
|
this.walkDirection.z = this.walkDirection.z * velocity * delta// + this.model.position.z
|
||||||
|
|
||||||
//const translation = this.rigidBody.translation();
|
//const translation = this.rigidBody.translation();
|
||||||
//const translation = characterController.computedMovement();
|
|
||||||
|
|
||||||
this.characterController.computeColliderMovement(
|
this.characterController.computeColliderMovement(
|
||||||
this.po.collider, // The collider we would like to move.
|
this.po.collider, // The collider we would like to move.
|
||||||
@@ -121,7 +120,13 @@ export class CharacterControls {
|
|||||||
|
|
||||||
let correctedMovement = this.characterController.computedMovement();
|
let correctedMovement = this.characterController.computedMovement();
|
||||||
|
|
||||||
this.po.rigidBody.setNextKinematicTranslation(correctedMovement);
|
let v = new THREE.Vector3();
|
||||||
|
v.copy(this.po.rigidBody.translation());
|
||||||
|
v.add(correctedMovement)
|
||||||
|
|
||||||
|
this.po.rigidBody.setNextKinematicTranslation(v);
|
||||||
|
|
||||||
|
//console.log(this.walkDirection, correctedMovement);
|
||||||
//this.po.rigidBody.setNextKinematicRotation(this.rotateQuarternion);
|
//this.po.rigidBody.setNextKinematicRotation(this.rotateQuarternion);
|
||||||
|
|
||||||
// if (translation.y < -1) {
|
// if (translation.y < -1) {
|
||||||
@@ -132,13 +137,16 @@ export class CharacterControls {
|
|||||||
// z: 0
|
// z: 0
|
||||||
// });
|
// });
|
||||||
// } else {
|
// } else {
|
||||||
const cameraPositionOffset = this.camera.position.sub(this.model.position);
|
|
||||||
|
|
||||||
this.model.position.copy(correctedMovement)
|
|
||||||
|
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
|
// // update model and camera
|
||||||
// this.model.position.x = translation.x
|
// this.model.position.x = translation.x
|
||||||
// this.model.position.y = translation.y
|
// this.model.position.y = translation.y
|
||||||
// this.model.position.z = translation.z
|
// this.model.position.z = translation.z
|
||||||
|
|
||||||
this.updateCameraTarget(cameraPositionOffset, correctedMovement)
|
this.updateCameraTarget(cameraPositionOffset, correctedMovement)
|
||||||
|
|
||||||
// this.walkDirection.y += this.lerp(this.storedFall, -9.81 * delta, 0.10)
|
// this.walkDirection.y += this.lerp(this.storedFall, -9.81 * delta, 0.10)
|
||||||
@@ -169,7 +177,7 @@ export class CharacterControls {
|
|||||||
|
|
||||||
updateCameraTarget(offset, cm) {
|
updateCameraTarget(offset, cm) {
|
||||||
// move camera
|
// move camera
|
||||||
const rigidTranslation = cm || this.po.rigidBody.translation();
|
const rigidTranslation = this.po.rigidBody.nextTranslation();
|
||||||
this.camera.position.x = rigidTranslation.x + offset.x
|
this.camera.position.x = rigidTranslation.x + offset.x
|
||||||
this.camera.position.y = rigidTranslation.y + offset.y
|
this.camera.position.y = rigidTranslation.y + offset.y
|
||||||
this.camera.position.z = rigidTranslation.z + offset.z
|
this.camera.position.z = rigidTranslation.z + offset.z
|
||||||
@@ -207,7 +215,7 @@ export class CharacterControls {
|
|||||||
return directionOffset
|
return directionOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
update (world, delta, pointerControls){
|
update1 (world, delta, pointerControls){
|
||||||
// Calculate input buffer
|
// Calculate input buffer
|
||||||
// if (this.jumpBuffer > 0) {
|
// if (this.jumpBuffer > 0) {
|
||||||
// this.jumpBuffer -= loop.delta; // ms
|
// this.jumpBuffer -= loop.delta; // ms
|
||||||
@@ -227,10 +235,29 @@ export class CharacterControls {
|
|||||||
// Update force direction from user input
|
// Update force direction from user input
|
||||||
let xDirection = 0;
|
let xDirection = 0;
|
||||||
let zDirection = 0;
|
let zDirection = 0;
|
||||||
if (pointerControls.moveForward) zDirection = -1;
|
if (pointerControls.moveForward) zDirection = 1;
|
||||||
if (pointerControls.moveBackward) zDirection = 1;
|
if (pointerControls.moveBackward) zDirection = -1;
|
||||||
if (pointerControls.moveRight) xDirection = 1;
|
if (pointerControls.moveRight) xDirection = -1;
|
||||||
if (pointerControls.moveLeft) 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
|
// Set the new force direction
|
||||||
this.forceDirection.copy({ x: xDirection, y: 0, z: zDirection }); // Ex: -1.0 to 1.0
|
this.forceDirection.copy({ x: xDirection, y: 0, z: zDirection }); // Ex: -1.0 to 1.0
|
||||||
@@ -257,11 +284,15 @@ export class CharacterControls {
|
|||||||
|
|
||||||
// Calculate 3D object rotation from character translation
|
// Calculate 3D object rotation from character translation
|
||||||
this._v.copy(this.po.rigidBody.nextTranslation());
|
this._v.copy(this.po.rigidBody.nextTranslation());
|
||||||
|
|
||||||
|
|
||||||
if (this._v.distanceTo(this.po.rigidBody.translation()) > 0.01) {
|
if (this._v.distanceTo(this.po.rigidBody.translation()) > 0.01) {
|
||||||
this.model.lookAt(this._v.x, this.model.position.y, this._v.z);
|
this.model.lookAt(this._v.x, this.model.position.y, this._v.z);
|
||||||
this.po.rigidBody.setNextKinematicRotation(this.model.quaternion);
|
this.po.rigidBody.setNextKinematicRotation(this.model.quaternion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.model.position.copy(this._v)
|
||||||
|
|
||||||
// Set vertical velocity to zero if grounded
|
// Set vertical velocity to zero if grounded
|
||||||
// if (this.characterController.computedGrounded()) {
|
// if (this.characterController.computedGrounded()) {
|
||||||
// this.allowJump = true;
|
// this.allowJump = true;
|
||||||
|
|||||||
+2
-2
@@ -58,9 +58,9 @@ class Hero{
|
|||||||
pc.update();
|
pc.update();
|
||||||
let dlt = this.clock.getDelta();
|
let dlt = this.clock.getDelta();
|
||||||
this.delta += dlt;
|
this.delta += dlt;
|
||||||
if (this.delta > 0.016){
|
if (this.delta > 0.00001){
|
||||||
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
|
|
||||||
this.gameEngine.phy.step()
|
this.gameEngine.phy.step()
|
||||||
|
this.characterControls.update(this.gameEngine.phy.world, this.delta, pc)
|
||||||
this.delta = 0;
|
this.delta = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user