Fix the camera smoothing for frame stalls

Review the AI's codeEasy10 min

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_speed is 0 or less, the camera never moves.
  • For each frame in order: if max_dt is greater than 0 and the delta is greater than max_dt, use max_dt instead.
  • 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) -> float
Example 1
Inputsmooth_follow_position(0, 100, 8, [0.016, 0.016, 0.016], 0.05)
Output31.8868572820453
Example 2
Inputsmooth_follow_position(0, 100, 8, [1], 0.05)
Output32.967995396436066
Example 3
Inputsmooth_follow_position(0, 100, 8, [1], 0)
Output99.96645373720975