Fix the camera smoothing for frame stalls
Your task
Your camera eases toward the player each frame. After a one-second asset-streaming stall, the old code teleported it across the level, because it used speed * dt as the blend weight and that can be far above 1. The fix uses an exponential blend that can never pass the target, and clamps long frames.
An assistant wrote smooth_follow_position(start, target, smooth_speed, frame_dts, max_dt). It is in the editor. It has a bug. Find it and fix it.
Input and output:
start,target: the camera's starting position and the position it follows, on one axis.smooth_speed: how fast the camera closes the gap, per second.frame_dts: each frame's delta time in seconds, in order. A clock adjustment can produce a delta of 0 or less.max_dt: the longest delta one frame may use. 0 or less means no clamping.- Return the camera position after all the frames.
Rules the tests check:
- If
smooth_speedis 0 or less, the camera never moves. - For each frame in order: if
max_dtis greater than 0 and the delta is greater thanmax_dt, usemax_dtinstead. - A delta of 0 or less, after that step, is not a frame: skip it.
- Otherwise compute
alpha = 1 - e^(-smooth_speed * delta)and move the camera that share of the way:position = position + (target - position) * alpha. - Answers within 0.000001 of the expected value pass.
Examples
smooth_follow_position(start: float, target: float, smooth_speed: float, frame_dts: list[float], max_dt: float) -> floatExample 1
Input
smooth_follow_position(0, 100, 8, [0.016, 0.016, 0.016], 0.05)Output
31.8868572820453Example 2
Input
smooth_follow_position(0, 100, 8, [1], 0.05)Output
32.967995396436066Example 3
Input
smooth_follow_position(0, 100, 8, [1], 0)Output
99.96645373720975