Ease the camera toward the player's interpolated position

The camera target was the sim tile, which jumps 8 px per movement window,
and the follow was a constant-speed chase that stopped abruptly. Now the
target is the render-side lerp position and the camera closes the gap
exponentially, so it accelerates and settles smoothly. Also spawns the
first NPC and switches the game to World::blocked.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-19 10:51:44 +02:00
co-authored by Claude Opus 5
parent 7ea351f8e5
commit c512bf49da
2 changed files with 42 additions and 38 deletions
+25 -27
View File
@@ -5,7 +5,7 @@ use std::collections::{HashMap, VecDeque};
use crate::assets::{Image, Tile};
use crate::input::{GameAction, InputState};
use sim::entity::Entity;
use sim::entity::{Entity, EntityKind};
use sim::map::TileMap;
use sim::{delta_action, load_world, step_allowed, Sim, TICK_HZ, TICKS_PER_MOVE};
@@ -36,11 +36,12 @@ const VIEW_TILES: i32 = 30;
const TILE_PX: i32 = 8;
const VIEW_PX: i32 = VIEW_TILES * TILE_PX;
/// Camera pan speed. The player walks 8 px per 167 ms ≈ 48 px/s; the camera is a bit
/// faster, so it trails during movement and settles right after the player stops.
const CAM_SPEED: f32 = 64.0; // px/s
/// Camera smoothing time constant: each frame the camera closes this fraction
/// `1 - exp(-dt/tau)` of the remaining distance to its target, so it eases in and
/// out instead of chasing at constant speed. Smaller = snappier, larger = floatier.
const CAM_SMOOTH_TAU_MS: f32 = 120.0;
/// A position correction farther than this is a teleport — snap instead of panning.
/// A position correction farther than this is a teleport — snap instead of easing.
const CAM_SNAP_PX: f32 = 96.0;
/// Render-side smoothing state for one entity: the previous tile and how long ago the
@@ -86,7 +87,8 @@ impl Game {
println!("loaded {map_path}: {}×{} tiles in {:.1} ms",
map.width, map.height, t0.elapsed().as_secs_f64() * 1e3);
let mut sim = Sim::new(world);
let player_id = sim.world.spawn_entity(0, (0, 0), 100);
let player_id = sim.world.spawn_entity(EntityKind::Player, 0, (0, 0), 100);
sim.world.spawn_entity(EntityKind::Npc, 1, (6, 6), 5);
let mut game = Game {
tileset,
@@ -109,12 +111,6 @@ impl Game {
(p.0 as i32, p.1 as i32)
}
/// True if the world tile at `(wx, wy)` blocks movement. Outside the loaded world
/// counts as blocked — the same rule the sim applies.
fn tile_blocked(&self, wx: i32, wy: i32) -> bool {
self.sim.world.tile_flags(wx as i16, wy as i16).map_or(true, |f| f.collidable())
}
pub fn update(&mut self, render_frame: &mut [u16], dt: usize, input: &InputState)
-> Option<GameSignal>
{
@@ -142,18 +138,21 @@ impl Game {
return Some(GameSignal::Quit);
}
self.step_camera(dt);
self.step_lerp(dt);
self.step_camera(dt);
self.render_viewport(render_frame);
None
}
/// Where the camera wants to be: the viewport centered on the player's tile.
/// Where the camera wants to be: the viewport centered on the player's rendered
/// (interpolated) position, not the coarser sim tile — otherwise the target itself
/// jumps in discrete per-tile steps and the easing below has nothing smooth to chase.
fn cam_target(&self) -> (f32, f32) {
let (px, py) = self.player_pos();
let player = &self.sim.world.entities[&self.player_id];
let (px, py) = self.entity_px(player);
(
(px * TILE_PX - VIEW_PX / 2) as f32,
(py * TILE_PX - VIEW_PX / 2) as f32,
(px - VIEW_PX / 2) as f32,
(py - VIEW_PX / 2) as f32,
)
}
@@ -162,19 +161,18 @@ impl Game {
(self.cam.0.round() as i32, self.cam.1.round() as i32)
}
/// Follow the player linearly at `CAM_SPEED`, axis by axis. Corrections beyond
/// `CAM_SNAP_PX` (teleports, respawns) snap outright instead of panning across.
/// Ease toward the target: each frame closes `1 - exp(-dt/tau)` of the remaining
/// gap, so the camera eases out of a stop and eases into a catch-up rather than
/// tracking at constant speed. Corrections beyond `CAM_SNAP_PX` (teleports,
/// respawns) snap outright instead of easing across.
fn step_camera(&mut self, dt: usize) {
let (tx, ty) = self.cam_target();
if (tx - self.cam.0).abs().max((ty - self.cam.1).abs()) > CAM_SNAP_PX {
self.cam = (tx, ty);
return;
}
let step = CAM_SPEED * dt as f32 / 1000.0;
let approach = |c: f32, t: f32| {
if (t - c).abs() <= step { t } else { c + step * (t - c).signum() }
};
self.cam = (approach(self.cam.0, tx), approach(self.cam.1, ty));
let f = 1.0 - (-(dt as f32) / CAM_SMOOTH_TAU_MS).exp();
self.cam = (self.cam.0 + (tx - self.cam.0) * f, self.cam.1 + (ty - self.cam.1) * f);
}
/// Click-to-move: translate a framebuffer click into a world tile and adopt it as
@@ -198,7 +196,7 @@ impl Game {
return;
}
match pathfind::find_path(self.player_pos(), goal, |x, y| self.tile_blocked(x, y)) {
match pathfind::find_path(self.player_pos(), goal, |x, y| self.sim.world.blocked(x, y)) {
Some(steps) => {
self.route = steps.into();
self.goal = Some(goal);
@@ -243,7 +241,7 @@ impl Game {
};
let Some((nx, ny)) = step else { return };
if step_allowed(from, (nx, ny), |x, y| self.tile_blocked(x, y)) {
if step_allowed(from, (nx, ny), |x, y| self.sim.world.blocked(x, y)) {
self.sim.set_action(self.player_id, delta_action(nx - from.0, ny - from.1));
} else if dir.is_none() {
self.replan_route();
@@ -273,7 +271,7 @@ impl Game {
self.goal = None;
return;
}
match pathfind::find_path(pos, goal, |x, y| self.tile_blocked(x, y)) {
match pathfind::find_path(pos, goal, |x, y| self.sim.world.blocked(x, y)) {
Some(steps) => self.route = steps.into(),
None => self.goal = None,
}