fuzzyworld

herd / commit

Keep dog facing its last direction of travel when stopped

31314a3624771cd944758d59f804010cbaa94e53 · Pavel Safronov · 2026-05-17 20:47:38 -0700 · parents 2b484d78e462

Keep dog facing its last direction of travel when stopped

Previously the dog snapped to face up whenever it reached its target.
Now facing is stored as a field and updated only while moving, so the
dog holds its arrival direction when idle.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
 src/entities/Dog.ts | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/entities/Dog.ts b/src/entities/Dog.ts
index f24981d..cd6043b 100644
--- a/src/entities/Dog.ts
+++ b/src/entities/Dog.ts
@@ -7,6 +7,7 @@ export class Dog {
   vel: Vec2 = Vec2.ZERO
   target: Vec2 | null = null
   speed: number
+  private facing: number = -Math.PI / 2
 
   constructor(x: number, y: number, speed = 160) {
     this.pos = new Vec2(x, y)
@@ -26,14 +27,13 @@ export class Dog {
       return
     }
     this.vel = diff.normalize().scale(this.speed)
+    this.facing = Math.atan2(this.vel.y, this.vel.x)
     this.pos = this.pos.add(this.vel.scale(dt))
   }
 
   draw(ctx: CanvasRenderingContext2D, scale = 1) {
     const { x, y } = this.pos
-    const facing = this.vel.lengthSq() > 0
-      ? Math.atan2(this.vel.y, this.vel.x)
-      : -Math.PI / 2
+    const facing = this.facing
 
     ctx.save()
     ctx.translate(x, y)