Fix interpolation: use offset backward lerp instead of forward extrapolation
Forward extrapolation caused visible teleporting at every direction change (ghosts jumping ~1.3 tiles at intersections). Replace with lerp(prev, current, 0.5 + t/2) which starts the visual at the midpoint (max 0.5 tile / 12px lag) and smoothly reaches the destination. No jumps, no teleporting, minimal hitbox offset. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -262,9 +262,10 @@
|
||||
(+ a (* t (- b a))))
|
||||
|
||||
;; draw-pacman! :: pacman, number -> /
|
||||
;; Draws Pac-Man with forward extrapolation from current position.
|
||||
;; Formula: current + (current - prev) * t — visual leads toward
|
||||
;; the next tile so it aligns with the logical hitbox.
|
||||
;; Draws Pac-Man with offset backward interpolation. Uses
|
||||
;; lerp(prev, current, 0.5 + t/2) so the visual starts at the
|
||||
;; midpoint (max 0.5 tile lag) and arrives at current by t=1.
|
||||
;; No jumps on direction changes unlike forward extrapolation.
|
||||
(define (draw-pacman! pacman progress)
|
||||
(let* ((pos (pacman 'position))
|
||||
(row (pos 'row))
|
||||
@@ -272,8 +273,9 @@
|
||||
(prev-row (pacman 'prev-row))
|
||||
(prev-col (pacman 'prev-col))
|
||||
(t (min progress 1))
|
||||
(render-row (+ row (* t (- row prev-row))))
|
||||
(render-col (+ col (* t (- col prev-col))))
|
||||
(factor (+ 0.5 (* 0.5 t)))
|
||||
(render-row (lerp prev-row row factor))
|
||||
(render-col (lerp prev-col col factor))
|
||||
(direction (pacman 'direction)))
|
||||
((pacman-sprite 'set-x!) (grid->pixel-x render-col))
|
||||
((pacman-sprite 'set-y!) (grid->pixel-y render-row))
|
||||
@@ -283,7 +285,7 @@
|
||||
((eq? direction 'down) ((pacman-sprite 'rotate!) rotation-down)))))
|
||||
|
||||
;; draw-ghosts! :: list -> /
|
||||
;; Updates all ghost sprite positions with forward extrapolation.
|
||||
;; Updates all ghost sprite positions with offset backward interpolation.
|
||||
(define (draw-ghosts! ghosts)
|
||||
(for-each
|
||||
(lambda (ghost ghost-draw)
|
||||
@@ -293,8 +295,9 @@
|
||||
(prev-row (ghost 'prev-row))
|
||||
(prev-col (ghost 'prev-col))
|
||||
(t (min (/ (ghost 'movement-timer) ghost-speed-ms) 1))
|
||||
(render-row (+ row (* t (- row prev-row))))
|
||||
(render-col (+ col (* t (- col prev-col))))
|
||||
(factor (+ 0.5 (* 0.5 t)))
|
||||
(render-row (lerp prev-row row factor))
|
||||
(render-col (lerp prev-col col factor))
|
||||
(dir (ghost 'direction)))
|
||||
((ghost-draw 'update!) render-row render-col dir)))
|
||||
ghosts ghost-draw-states))
|
||||
|
||||
Reference in New Issue
Block a user