FBC: NIGHT SHIFT - cross-faded agent animations and smoothed the locomotion blending

This commit is contained in:
dandovlatov 2026-11-24 03:00:20 +04:00
parent 02f94c9695
commit 714af1b173
4 changed files with 87 additions and 10 deletions

View file

@ -8,6 +8,7 @@
#define DrawCylinder(...) do { PROF_DRAW_CALL(); DrawCylinder(__VA_ARGS__); } while(0)
#define DrawSphere(...) do { PROF_DRAW_CALL(); DrawSphere(__VA_ARGS__); } while(0)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
@ -144,6 +145,11 @@ void ModelBank::shutdown() {
e.animations = nullptr;
e.animation_count = 0;
e.current_animation = -1;
e.animation_clock = 0.0f;
e.blend_from = -1;
e.blend_from_clock = 0.0f;
e.blend_time = 0.0f;
e.blend_duration = 0.0f;
if (e.loaded) UnloadModel(e.model);
e.loaded = false;
}
@ -220,7 +226,8 @@ void ModelBank::draw(ActorModel id, Vector3 pos, float yaw, float scale,
Vector3{scale, scale, scale}, tint);
}
void ModelBank::update_animation(ActorModel id, const char* name, float dt) {
void ModelBank::update_animation(ActorModel id, const char* name, float dt,
float blend_seconds) {
const int index = static_cast<int>(id);
if (index < 0 || index >= static_cast<int>(ActorModel::Count)) return;
Entry& e = entries_[index];
@ -231,14 +238,49 @@ void ModelBank::update_animation(ActorModel id, const char* name, float dt) {
if (std::strcmp(e.animations[a].name, name) == 0) { found = a; break; }
}
if (found < 0) return;
if (found != e.current_animation) {
if (e.blend_duration > 0.0f) {
// Retargeted mid-fade: carry the dominant pose forward instead of
// snapping to frame zero of the new clip.
if (e.blend_time / e.blend_duration >= 0.5f) {
e.blend_from = e.current_animation;
e.blend_from_clock = e.animation_clock;
}
} else {
e.blend_from = e.current_animation;
e.blend_from_clock = e.animation_clock;
}
e.current_animation = found;
e.animation_clock = 0.0f;
e.blend_time = 0.0f;
e.blend_duration = (e.blend_from >= 0 && blend_seconds > 0.0f)
? blend_seconds : 0.0f;
}
e.animation_clock += dt;
const float frame = std::fmod(e.animation_clock * 60.0f,
static_cast<float>(e.animations[found].keyframeCount));
UpdateModelAnimation(e.model, e.animations[found], frame);
const ModelAnimation& to = e.animations[found];
const float to_frame = std::fmod(e.animation_clock * 60.0f,
static_cast<float>(to.keyframeCount));
if (e.blend_duration <= 0.0f) {
UpdateModelAnimation(e.model, to, to_frame);
return;
}
e.blend_time += dt;
const float w = std::min(1.0f, e.blend_time / e.blend_duration);
const float eased = w * w * (3.0f - 2.0f * w);
e.blend_from_clock += dt;
const ModelAnimation& from = e.animations[e.blend_from];
const float from_frame = std::fmod(e.blend_from_clock * 60.0f,
static_cast<float>(from.keyframeCount));
UpdateModelAnimationEx(e.model, from, from_frame, to, to_frame, eased);
if (w >= 1.0f) {
e.blend_from = -1;
e.blend_duration = 0.0f;
}
}
float ModelBank::height(ActorModel id) {

View file

@ -30,7 +30,10 @@ public:
float stagger = 0.0f, float phase = 0.0f, float hiss = 0.0f);
// Plays a named GLB action on a skinned model (dt in seconds, 60 fps clips).
void update_animation(ActorModel id, const char* name, float dt);
// blend_seconds > 0 cross-fades from the outgoing clip into the new one via
// raylib's two-animation blend; 0 keeps the old hard cut.
void update_animation(ActorModel id, const char* name, float dt,
float blend_seconds = 0.0f);
// Per-model visual height, used for health bars / floating labels.
static float height(ActorModel id);
@ -42,6 +45,10 @@ private:
int animation_count = 0;
int current_animation = -1;
float animation_clock = 0.0f;
int blend_from = -1; // outgoing clip during a cross-fade
float blend_from_clock = 0.0f;
float blend_time = 0.0f;
float blend_duration = 0.0f; // 0 => no cross-fade in progress
bool loaded = false;
};

View file

@ -491,6 +491,10 @@ void Presenter::reset() {
debris_.clear();
concrete_debris_.clear();
scorch_.clear();
player_walk_ = 0.0f;
dash_pose_ = 0.0f;
dash_prev_ = 0.0f;
player_running_ = false;
}
void Presenter::shutdown() {
@ -1248,17 +1252,37 @@ void Presenter::draw_scene(Game& game) {
const Vec2 moved = p - last_player_pos_;
if (length_sq(moved) > 1e-5f) player_yaw_ = std::atan2(moved.x, moved.y);
last_player_pos_ = p;
const float player_walk = std::min(1.0f, length(moved) * 14.0f);
// Speed from distance / time rather than raw per-frame delta: stable
// when the fixed-step sim runs zero or several ticks in one render.
const float raw_speed = frame_dt > 1e-5f ? length(moved) / frame_dt : 0.0f;
const float ref_speed = std::max(0.01f, game.player.stats.move_speed);
const float target_walk = std::clamp(raw_speed / ref_speed, 0.0f, 1.0f);
const float walk_k = target_walk > player_walk_ ? 12.0f : 7.0f;
player_walk_ += (target_walk - player_walk_) *
(1.0f - std::exp(-walk_k * frame_dt));
// Hysteresis keeps a player scraping a wall from flapping Idle <-> Run
// every frame, which was the main source of the visible stutter.
if (!player_running_ && player_walk_ > 0.28f) player_running_ = true;
else if (player_running_ && player_walk_ < 0.12f) player_running_ = false;
// The dash burst is only 0.16 s; hold its lean a little longer so the
// pose reads, then cross-fade back into locomotion.
if (game.player.dash_timer > 0.0f && dash_prev_ <= 0.0f) dash_pose_ = 0.42f;
dash_prev_ = game.player.dash_timer;
dash_pose_ = std::max(0.0f, dash_pose_ - frame_dt);
const float player_flash = actor_flash(game.player.hit_flash, 4.0f);
// The Blender agent carries its own root bob; the old sine bob would
// double up on top of the skeletal Run/Idle clips.
const char* agent_clip = "Idle";
if (game.player.dash_timer > 0.0f) agent_clip = "Dash";
if (dash_pose_ > 0.0f) agent_clip = "Dash";
else if (player_flash > 0.05f) agent_clip = "Stagger";
else if (player_walk > 0.08f) agent_clip = "Run";
models_.update_animation(ActorModel::Guard, agent_clip, GetFrameTime());
else if (player_running_) agent_clip = "Run";
models_.update_animation(ActorModel::Guard, agent_clip, frame_dt, 0.16f);
models_.draw(ActorModel::Guard, {p.x, 0.0f, p.y}, player_yaw_, 1.0f,
WHITE, player_flash, true, player_walk, player_flash,
WHITE, player_flash, true, player_walk_, player_flash,
anim_time_);
// Enemies face the player; Hiss units add a nervous jitter.

View file

@ -82,6 +82,10 @@ private:
float damage_flash_ = 0.0f;
float anim_time_ = 0.0f;
float player_yaw_ = 0.0f;
float player_walk_ = 0.0f; // smoothed locomotion weight for the agent clips
float dash_pose_ = 0.0f; // presentation-only hold for the Dash pose
float dash_prev_ = 0.0f; // previous dash timer, for rising-edge detection
bool player_running_ = false; // hysteresis latch for Idle <-> Run
bool hiss_test_ = false;
bool floor_mode_ = false;
bool floor_overview_ = false;