advanced animation speed management according to hero climbing or getting down

This commit is contained in:
2026-04-03 23:51:36 +03:00
parent 90cf97aded
commit 401a6cb144
+28 -15
View File
@@ -3,10 +3,13 @@ import { QueryFilterFlags } from '@dimforge/rapier3d';
const zero2 = new Vector2(0,0); const zero2 = new Vector2(0,0);
class Hero{ class Hero{
walkDirection = new Vector3() #walkDirection = new Vector3()
rotateAngle = new Vector3(0, 1, 0) #desiredMovement = new Vector2(0,0)
#correctedMovement = new Vector2(0,0);
#rotateAngle = new Vector3(0, 1, 0)
cameraMode = 'rotate'; cameraMode = 'rotate';
#cameraZ = 4 #cameraZ = 4
#timeScale = 1;
get cameraZ(){ get cameraZ(){
return this.#cameraZ; return this.#cameraZ;
@@ -92,7 +95,7 @@ class Hero{
this.po.characterController = this.characterController; this.po.characterController = this.characterController;
//this.characterController.enableSnapToGround(0.5); //this.characterController.enableSnapToGround(0.5);
// // Dont allow climbing slopes larger than 45 degrees. // // Dont allow climbing slopes larger than 45 degrees.
// this.characterController.setMaxSlopeClimbAngle(45 * Math.PI / 180); //this.characterController.setMaxSlopeClimbAngle(90 * Math.PI / 180);
// Automatically slide down on slopes smaller than 30 degrees. // Automatically slide down on slopes smaller than 30 degrees.
// this.characterController.setMinSlopeSlideAngle(30 * Math.PI / 180); // this.characterController.setMinSlopeSlideAngle(30 * Math.PI / 180);
// this.characterController.enableAutostep(0.5, 0.2, true); // this.characterController.enableAutostep(0.5, 0.2, true);
@@ -169,10 +172,10 @@ class Hero{
let play = this.currentAction || 'idle', velocity = this.walkVelocity; let play = this.currentAction || 'idle', velocity = this.walkVelocity;
this.fadeDuration = 0.2; this.fadeDuration = 0.2;
if (input[1] && pc.running) { if (input[1] && pc.running && this.#timeScale > 0.5) {
play = 'run'; play = 'run';
velocity = this.runVelocity; velocity = this.runVelocity;
} else if (input[1] > 0) { } else if (input[1] > 0 && this.#timeScale > 0.2) {
play = 'walk' play = 'walk'
} else if (input[1] < 0) { } else if (input[1] < 0) {
play = 'backward' play = 'backward'
@@ -216,9 +219,11 @@ class Hero{
this.actionStart = 0; this.actionStart = 0;
} }
this.animationsMap[this.currentAction].timeScale = this.#timeScale;
this.actionStart += delta; this.actionStart += delta;
this.cameraDelta += delta * ( pc.cameraLeft * -1 + pc.cameraRight * 1) this.cameraDelta += delta * ( pc.cameraLeft * -1 + pc.cameraRight * 1)
this.walkDirection.setScalar(0); this.#walkDirection.setScalar(0);
if (pc.cameraUp && this.cameraY < 5){ if (pc.cameraUp && this.cameraY < 5){
this.cameraY+=delta; this.cameraY+=delta;
@@ -239,35 +244,35 @@ class Hero{
if (!this.fpv){ if (!this.fpv){
//this.directionVelocity = this.directionVelocity * 2.5 * Math.abs(input[0]) //this.directionVelocity = this.directionVelocity * 2.5 * Math.abs(input[0])
this.direction += input[0] * delta * 2.5 //this.directionVelocity; this.direction += input[0] * delta * 2.5 //this.directionVelocity;
this.walkDirection.set(0, 0, input[1]) this.#walkDirection.set(0, 0, input[1])
}else{ }else{
if (this.engine.renderer.xr.isPresenting){ if (this.engine.renderer.xr.isPresenting){
this.direction = this.engine.camera.rotation.y; this.direction = this.engine.camera.rotation.y;
}else{ }else{
this.direction = this.camera.rotation.y; this.direction = this.camera.rotation.y;
} }
this.walkDirection.set(input[0], 0, input[1]) this.#walkDirection.set(input[0], 0, input[1])
//console.log(this.camera.rotation.y * 180/Math.PI); //console.log(this.camera.rotation.y * 180/Math.PI);
// this.camera.getWorldDirection(this.walkDirection); // this.camera.getWorldDirection(this.walkDirection);
// this.walkDirection.applyAxisAngle(this.rotateAngle, input[0]*Math.PI/2 + (input[1]<0 ? Math.PI : 0)) // this.walkDirection.applyAxisAngle(this.rotateAngle, input[0]*Math.PI/2 + (input[1]<0 ? Math.PI : 0))
//this.walkDirection.multiplyScalar(input[1]); //this.walkDirection.multiplyScalar(input[1]);
} }
this.walkDirection.applyAxisAngle(this.rotateAngle, this.direction) this.#walkDirection.applyAxisAngle(this.#rotateAngle, this.direction)
this.walkDirection.normalize(); this.#walkDirection.normalize();
this.model.rotation.y = this.direction; this.model.rotation.y = this.direction;
}else if (this.fpv){ }else if (this.fpv){
this.model.rotation.y = this.camera.rotation.y; this.model.rotation.y = this.camera.rotation.y;
} }
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.y = -delta * 0.5 * 9.8; //gravity!!! this.#walkDirection.y = -delta * velocity * 0.1 * 9.8; //gravity!!!
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();
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.
this.walkDirection, // The movement we would like to apply if there wasnt any obstacle. this.#walkDirection, // The movement we would like to apply if there wasnt any obstacle.
QueryFilterFlags['EXCLUDE_SENSORS'] QueryFilterFlags['EXCLUDE_SENSORS']
); );
@@ -278,7 +283,15 @@ class Hero{
// } // }
let correctedMovement = this.characterController.computedMovement(); let correctedMovement = this.characterController.computedMovement();
//console.log(correctedMovement); // this.walkDirection.sub(correctedMovement)
// let diff = Math.sqrt(this.walkDirection.x * this.walkDirection.x +
// this.walkDirection.z * this.walkDirection.z) / delta / velocity
let l1 = this.#desiredMovement.set(this.#walkDirection.x, this.#walkDirection.z).lengthSq(),
l2 = this.#correctedMovement.set(correctedMovement.x, correctedMovement.z).lengthSq();
let ts = l2 / l1
if (ts>0 && ts<10){
this.#timeScale = this.lerp(this.#timeScale, Math.sqrt(ts), delta*2);
}
let v = new Vector3(); let v = new Vector3();
v.copy(this.po.rigidBody.translation()); v.copy(this.po.rigidBody.translation());