Wind first pass
This commit is contained in:
		| @@ -8,12 +8,10 @@ | |||||||
|  |  | ||||||
| (def revenge (ref {:position [0. 0.] | (def revenge (ref {:position [0. 0.] | ||||||
|                    :name "Revenge" |                    :name "Revenge" | ||||||
|                    :heading (normalize [1.0 1.0]) |                    :heading (normalize [0. 1.0]) | ||||||
|                    :slots [{:type :downwind-sail :length 2 :furl 1.0}] |                    :slots [{:type :downwind-sail :length 2 :furl 1.0}] | ||||||
|                    :velocity [1. 0.]})) |                    :velocity [1. 0.]})) | ||||||
|  |  | ||||||
| (reset! ships [virginia-woolfe revenge]) | (reset! ships [virginia-woolfe revenge]) | ||||||
|  |  | ||||||
| (show-ui) | (show-ui) | ||||||
|  |  | ||||||
| @tracked-ship |  | ||||||
|   | |||||||
| @@ -4,8 +4,19 @@ | |||||||
|             [seesaw.core :refer :all] |             [seesaw.core :refer :all] | ||||||
|             [seesaw.bind :as b]) |             [seesaw.bind :as b]) | ||||||
|   (:import com.formdev.flatlaf.FlatDarculaLaf)) |   (:import com.formdev.flatlaf.FlatDarculaLaf)) | ||||||
|  | ;; Wind | ||||||
| (def hardcoded-wind [0.1 3.0]) ;; A strong easternly 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 [])) | (defonce ships (atom [])) | ||||||
|  |  | ||||||
| (defn ship-names | (defn ship-names | ||||||
| @@ -35,19 +46,20 @@ | |||||||
|       force))) |       force))) | ||||||
|  |  | ||||||
| (defn physics-step | (defn physics-step | ||||||
|   [ship wind] |   [ship winds] | ||||||
|   (dosync |   (dosync | ||||||
|  |    (let [wind (sum-wind (:position @ship) winds)] | ||||||
|      ;; Update position |      ;; Update position | ||||||
|      (alter ship update :position add (scale (:velocity @ship) (/ tickrate))) |      (alter ship update :position add (scale (:velocity @ship) (/ tickrate))) | ||||||
|      ;; linear dampening |      ;; linear dampening | ||||||
|      (alter ship update :velocity scale (- 1.0 (/ 0.5 tickrate))) |      (alter ship update :velocity scale (- 1.0 (/ 0.5 tickrate))) | ||||||
|      ;; wind force |      ;; wind force | ||||||
|    (alter ship update :velocity add (scale (downwind-sails-force @ship wind) (/ tickrate))))) |      (alter ship update :velocity add (scale (downwind-sails-force @ship wind) (/ tickrate)))))) | ||||||
|  |  | ||||||
| (defn tick | (defn tick | ||||||
|   [ships] |   [ships] | ||||||
|   (doseq [ship ships] |   (doseq [ship ships] | ||||||
|     (-> ship (physics-step hardcoded-wind)))) |     (-> ship (physics-step hardcoded-winds)))) | ||||||
|  |  | ||||||
| (subscribe! #(tick @ships)) | (subscribe! #(tick @ships)) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,6 +9,10 @@ | |||||||
|   [v1 v2] |   [v1 v2] | ||||||
|   (mapv + v1 v2)) |   (mapv + v1 v2)) | ||||||
|  |  | ||||||
|  | (defn sub | ||||||
|  |   [v1 v2] | ||||||
|  |   (mapv - v1 v2)) | ||||||
|  |  | ||||||
| (defn scale | (defn scale | ||||||
|   [v & scalars] |   [v & scalars] | ||||||
|   (mapv #(* (apply * scalars) %) v)) |   (mapv #(* (apply * scalars) %) v)) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user