From 1dd26857628fc8aee99347840fc4604da9131778 Mon Sep 17 00:00:00 2001 From: goynov Date: Fri, 17 Oct 2025 19:03:31 +0300 Subject: [PATCH] obstacle avoidance --- .../MazeQuizGame/MazeObject.js | 10 ++-- src/lib/CharacterControls.js | 57 ++++++++++++++----- src/lib/Hero.js | 4 +- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/components/InteractiveObjects/MazeQuizGame/MazeObject.js b/src/components/InteractiveObjects/MazeQuizGame/MazeObject.js index f52084b..ae6162a 100644 --- a/src/components/InteractiveObjects/MazeQuizGame/MazeObject.js +++ b/src/components/InteractiveObjects/MazeQuizGame/MazeObject.js @@ -79,11 +79,11 @@ class MazeObject { ) left.rigidBody.setRotation(quat, true) - // let right = engine.phy.add( - // {position: room.localToWorld(new Vector3(-context.tubeSize / 2, 0.6, offsetZ/2))}, - // 'fixed', false, undefined, 'cuboid',{ width: 0.01, height:1, depth:offsetZ/2 } - // ) - // right.rigidBody.setRotation(quat, true) + let right = engine.phy.add( + {position: room.localToWorld(new Vector3(-context.tubeSize / 2, 0.6, offsetZ/2))}, + 'fixed', false, undefined, 'cuboid',{ width: 0.01, height:1, depth:offsetZ/2 } + ) + right.rigidBody.setRotation(quat, true) if (def.type == 'area') { diff --git a/src/lib/CharacterControls.js b/src/lib/CharacterControls.js index 2e5e9b7..8c81bb7 100644 --- a/src/lib/CharacterControls.js +++ b/src/lib/CharacterControls.js @@ -53,7 +53,7 @@ export class CharacterControls { this.toggleRun = !this.toggleRun } - update0(world, delta, pointerControls) { + update(world, delta, pointerControls) { const directionPressed = pointerControls.moving() var play = ''; @@ -102,11 +102,10 @@ export class CharacterControls { velocity = this.currentAction == 'run' ? this.runVelocity : this.walkVelocity } - 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.x = this.walkDirection.x * velocity * delta// + this.model.position.x + this.walkDirection.z = this.walkDirection.z * velocity * delta// + this.model.position.z //const translation = this.rigidBody.translation(); - //const translation = characterController.computedMovement(); this.characterController.computeColliderMovement( this.po.collider, // The collider we would like to move. @@ -121,7 +120,13 @@ export class CharacterControls { 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); // if (translation.y < -1) { @@ -132,13 +137,16 @@ export class CharacterControls { // z: 0 // }); // } 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 // 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) @@ -169,7 +177,7 @@ export class CharacterControls { updateCameraTarget(offset, cm) { // 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.y = rigidTranslation.y + offset.y this.camera.position.z = rigidTranslation.z + offset.z @@ -207,7 +215,7 @@ export class CharacterControls { return directionOffset } - update (world, delta, pointerControls){ + update1 (world, delta, pointerControls){ // Calculate input buffer // if (this.jumpBuffer > 0) { // this.jumpBuffer -= loop.delta; // ms @@ -227,10 +235,29 @@ export class CharacterControls { // 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; + 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 @@ -257,11 +284,15 @@ export class CharacterControls { // 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; diff --git a/src/lib/Hero.js b/src/lib/Hero.js index 32b58c7..b2ec335 100644 --- a/src/lib/Hero.js +++ b/src/lib/Hero.js @@ -58,9 +58,9 @@ class Hero{ 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) + if (this.delta > 0.00001){ this.gameEngine.phy.step() + this.characterControls.update(this.gameEngine.phy.world, this.delta, pc) this.delta = 0; } }