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);
class Hero{
walkDirection = new Vector3()
rotateAngle = new Vector3(0, 1, 0)
#walkDirection = new Vector3()
#desiredMovement = new Vector2(0,0)
#correctedMovement = new Vector2(0,0);
#rotateAngle = new Vector3(0, 1, 0)
cameraMode = 'rotate';
#cameraZ = 4
#timeScale = 1;
get cameraZ(){
return this.#cameraZ;
@@ -92,7 +95,7 @@ class Hero{
this.po.characterController = this.characterController;
//this.characterController.enableSnapToGround(0.5);
// // 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.
// this.characterController.setMinSlopeSlideAngle(30 * Math.PI / 180);
// this.characterController.enableAutostep(0.5, 0.2, true);
@@ -169,10 +172,10 @@ class Hero{
let play = this.currentAction || 'idle', velocity = this.walkVelocity;
this.fadeDuration = 0.2;
if (input[1] && pc.running) {
if (input[1] && pc.running && this.#timeScale > 0.5) {
play = 'run';
velocity = this.runVelocity;
} else if (input[1] > 0) {
} else if (input[1] > 0 && this.#timeScale > 0.2) {
play = 'walk'
} else if (input[1] < 0) {
play = 'backward'
@@ -216,9 +219,11 @@ class Hero{
this.actionStart = 0;
}
this.animationsMap[this.currentAction].timeScale = this.#timeScale;
this.actionStart += delta;
this.cameraDelta += delta * ( pc.cameraLeft * -1 + pc.cameraRight * 1)
this.walkDirection.setScalar(0);
this.#walkDirection.setScalar(0);
if (pc.cameraUp && this.cameraY < 5){
this.cameraY+=delta;
@@ -239,35 +244,35 @@ class Hero{
if (!this.fpv){
//this.directionVelocity = this.directionVelocity * 2.5 * Math.abs(input[0])
this.direction += input[0] * delta * 2.5 //this.directionVelocity;
this.walkDirection.set(0, 0, input[1])
this.#walkDirection.set(0, 0, input[1])
}else{
if (this.engine.renderer.xr.isPresenting){
this.direction = this.engine.camera.rotation.y;
}else{
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);
// this.camera.getWorldDirection(this.walkDirection);
// this.walkDirection.applyAxisAngle(this.rotateAngle, input[0]*Math.PI/2 + (input[1]<0 ? Math.PI : 0))
//this.walkDirection.multiplyScalar(input[1]);
}
this.walkDirection.applyAxisAngle(this.rotateAngle, this.direction)
this.walkDirection.normalize();
this.#walkDirection.applyAxisAngle(this.#rotateAngle, this.direction)
this.#walkDirection.normalize();
this.model.rotation.y = this.direction;
}else if (this.fpv){
this.model.rotation.y = this.camera.rotation.y;
}
this.walkDirection.x = this.walkDirection.x * velocity * delta// + this.model.position.x
this.walkDirection.y = -delta * 0.5 * 9.8; //gravity!!!
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.y = -delta * velocity * 0.1 * 9.8; //gravity!!!
this.#walkDirection.z = this.#walkDirection.z * velocity * delta// + this.model.position.z
//const translation = this.rigidBody.translation();
this.characterController.computeColliderMovement(
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']
);
@@ -278,7 +283,15 @@ class Hero{
// }
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();
v.copy(this.po.rigidBody.translation());