From 507faf8875481a198577cbb7d664ab2435db5781 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 30 Jul 2026 22:25:10 -0600 Subject: [PATCH] try to stop jitter --- src/frontend/il/update.cljs | 14 ++++++++++---- src/il/common/constant.cljc | 1 + src/il/common/{velocity.cljc => math.cljc} | 5 ++++- src/il/constant.clj | 1 - src/il/handler.clj | 4 ++-- 5 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 src/il/common/constant.cljc rename src/il/common/{velocity.cljc => math.cljc} (64%) delete mode 100644 src/il/constant.clj diff --git a/src/frontend/il/update.cljs b/src/frontend/il/update.cljs index d936c9a..adc391c 100644 --- a/src/frontend/il/update.cljs +++ b/src/frontend/il/update.cljs @@ -2,14 +2,14 @@ (:require [frontend.il.entity :as entity] [cljs.core.async :as async] - [il.common.velocity :as v])) + [il.common.math :as ilmath])) (defn apply-velocity [e dt] (if (contains? e :velocity) (let [{x :x y :y vel :velocity} e {vx :x vy :y} vel - dtvx (* dt vx) - dtvy (* dt vy)] + dtvx (Math/floor (* dt vx)) + dtvy (Math/floor (* dt vy))] (assoc e :x (+ x dtvx) :y (+ y dtvy))) e)) @@ -20,14 +20,20 @@ (assoc e :renderable renderable)) e)) +(def min-distance-for-update 1) + (defn apply-server-position [e] (let [{:keys [x y tx ty]} e speed (get e :speed 15) has-server-pos (not (and (nil? tx) (nil? ty))) has-actual-pos (not (and (nil? x) (nil? y)))] (cond + (and (= x tx) (= y ty)) e (and has-server-pos (not has-actual-pos)) (assoc e :x tx :y ty) - (and has-server-pos has-actual-pos) (assoc e :velocity (v/velocity-for x y tx ty speed)) + (and has-server-pos has-actual-pos) (let [delta (ilmath/euclidean-distance x y tx ty)] + (cond + (> delta min-distance-for-update) (assoc e :velocity (ilmath/velocity-for x y tx ty speed) + :else e))) :else e))) (defn apply-player-shoot [state mouse-pos] diff --git a/src/il/common/constant.cljc b/src/il/common/constant.cljc new file mode 100644 index 0000000..0824415 --- /dev/null +++ b/src/il/common/constant.cljc @@ -0,0 +1 @@ +(ns il.common.constant) diff --git a/src/il/common/velocity.cljc b/src/il/common/math.cljc similarity index 64% rename from src/il/common/velocity.cljc rename to src/il/common/math.cljc index 32f1fc1..108ebc8 100644 --- a/src/il/common/velocity.cljc +++ b/src/il/common/math.cljc @@ -1,4 +1,4 @@ -(ns il.common.velocity) +(ns il.common.math) (defn velocity-for [x y tx ty speed] (let [dx (- tx x) @@ -9,3 +9,6 @@ (if (zero? h) {:x 0.0 :y 0.0} {:x vx :y vy}))) + +(defn euclidean-distance [x1 y1 x2 y2] + (Math/sqrt (+ (Math/pow (- x2 x1) 2) (Math/pow (- y2 y1) 2)))) diff --git a/src/il/constant.clj b/src/il/constant.clj deleted file mode 100644 index a75689a..0000000 --- a/src/il/constant.clj +++ /dev/null @@ -1 +0,0 @@ -(ns il.constant) diff --git a/src/il/handler.clj b/src/il/handler.clj index 7a95a78..66b0420 100644 --- a/src/il/handler.clj +++ b/src/il/handler.clj @@ -1,6 +1,6 @@ (ns il.handler (:require [il.state :as s] - [il.common.velocity :as v])) + [il.common.math :as ilmath])) (defn handle-position [_ state-chan player-id {x :x y :y}] (s/change state-chan update-in [:players player-id] assoc :x x :y y)) @@ -14,7 +14,7 @@ {tx :x ty :y player :player} msg {x :x y :y} player speed 50 ; TODO: Get projectile speed from player weapon - velocity (v/velocity-for x y tx ty speed) + velocity (ilmath/velocity-for x y tx ty speed) bullet {:sprite "/sprite/projectile.png" ; TODO: Render sprite based on player weapon :x x :y y