From c14bcecccc1f486ae35ac138999c0aa383097d5e Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sun, 26 Nov 2023 13:15:24 -0600 Subject: [PATCH] Wind first pass --- scratches/bootstrap.clj | 4 +--- src/age_of_sail/core.clj | 32 ++++++++++++++++++++++---------- src/age_of_sail/vec2.clj | 4 ++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/scratches/bootstrap.clj b/scratches/bootstrap.clj index edb70e8..516f0b4 100644 --- a/scratches/bootstrap.clj +++ b/scratches/bootstrap.clj @@ -8,12 +8,10 @@ (def revenge (ref {:position [0. 0.] :name "Revenge" - :heading (normalize [1.0 1.0]) + :heading (normalize [0. 1.0]) :slots [{:type :downwind-sail :length 2 :furl 1.0}] :velocity [1. 0.]})) (reset! ships [virginia-woolfe revenge]) (show-ui) - -@tracked-ship diff --git a/src/age_of_sail/core.clj b/src/age_of_sail/core.clj index 52b5bb9..d91ba31 100644 --- a/src/age_of_sail/core.clj +++ b/src/age_of_sail/core.clj @@ -4,8 +4,19 @@ [seesaw.core :refer :all] [seesaw.bind :as b]) (:import com.formdev.flatlaf.FlatDarculaLaf)) - -(def hardcoded-wind [0.1 3.0]) ;; A strong easternly wind! +;; Wind +(def hardcoded-winds '({:strength [0.1 3.0] :position [0. 0.]})) ;; A strong easternly wind! +(defn sum-wind + "Wind at x is the sum of each wind force divided by its distance away" + [x winds] + (reduce (fn [sum wind] + (let [dist (len (sub x (:position wind)))] + (if (> dist 1.0) + (add sum (scale (:strength wind) (/ (len (sub x (:position wind)))))) + (add sum (:strength wind))))) + (zero) + winds)) +;; Ships (defonce ships (atom [])) (defn ship-names @@ -35,19 +46,20 @@ force))) (defn physics-step - [ship wind] + [ship winds] (dosync - ;; Update position - (alter ship update :position add (scale (:velocity @ship) (/ tickrate))) - ;; linear dampening - (alter ship update :velocity scale (- 1.0 (/ 0.5 tickrate))) - ;; wind force - (alter ship update :velocity add (scale (downwind-sails-force @ship wind) (/ tickrate))))) + (let [wind (sum-wind (:position @ship) winds)] + ;; Update position + (alter ship update :position add (scale (:velocity @ship) (/ tickrate))) + ;; linear dampening + (alter ship update :velocity scale (- 1.0 (/ 0.5 tickrate))) + ;; wind force + (alter ship update :velocity add (scale (downwind-sails-force @ship wind) (/ tickrate)))))) (defn tick [ships] (doseq [ship ships] - (-> ship (physics-step hardcoded-wind)))) + (-> ship (physics-step hardcoded-winds)))) (subscribe! #(tick @ships)) diff --git a/src/age_of_sail/vec2.clj b/src/age_of_sail/vec2.clj index 3f76e3c..8a088a2 100644 --- a/src/age_of_sail/vec2.clj +++ b/src/age_of_sail/vec2.clj @@ -9,6 +9,10 @@ [v1 v2] (mapv + v1 v2)) +(defn sub + [v1 v2] + (mapv - v1 v2)) + (defn scale [v & scalars] (mapv #(* (apply * scalars) %) v))