Wind first pass
This commit is contained in:
		
							parent
							
								
									572726df7c
								
							
						
					
					
						commit
						c14bcecccc
					
				| @ -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 | ||||
|  | ||||
| @ -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)) | ||||
| 
 | ||||
|  | ||||
| @ -9,6 +9,10 @@ | ||||
|   [v1 v2] | ||||
|   (mapv + v1 v2)) | ||||
| 
 | ||||
| (defn sub | ||||
|   [v1 v2] | ||||
|   (mapv - v1 v2)) | ||||
| 
 | ||||
| (defn scale | ||||
|   [v & scalars] | ||||
|   (mapv #(* (apply * scalars) %) v)) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user