Skip to content

Commit

Permalink
Add weights to configurable options
Browse files Browse the repository at this point in the history
  • Loading branch information
levand committed Mar 15, 2013
1 parent 5663e16 commit 36bc72a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/cljs/boids/control_panel.cljs
Expand Up @@ -58,7 +58,11 @@
(init-control options-atom :steer-force {:min 0 :max 1 :step 0.02})
(init-control options-atom :cohere-distance {:min 0 :max 1000})
(init-control options-atom :avoid-distance {:min 0 :max 500})
(init-control options-atom :align-distance {:min 0 :max 1000}))
(init-control options-atom :align-distance {:min 0 :max 1000})
(init-control options-atom :cohesion-weight {:min 0 :max 2 :step 0.1})
(init-control options-atom :avoidance-weight {:min 0 :max 2 :step 0.1})
(init-control options-atom :alignment-weight {:min 0 :max 2 :step 0.1})
(init-control options-atom :goal-weight {:min 0 :max 2 :step 0.1}))

(defn init-mouse
"Initialize keeping track of the mouse, updating the goal to its
Expand Down
23 changes: 14 additions & 9 deletions src/cljs/boids/main.cljs
Expand Up @@ -2,7 +2,7 @@
(:require [boids.euclidean-vector :as v]
[boids.view :as view]
[boids.control-panel :as cp]
[boids.behaviors :as behaviors]))
[boids.behaviors :as b]))

;; A boid consists of a position and a velocity (both EuclideanVectors)
(defrecord Boid [pos vel])
Expand All @@ -14,6 +14,10 @@
:cohere-distance 300
:avoid-distance 50
:align-distance 200
:cohesion-weight 1
:avoidance-weight 1
:alignment-weight 1
:goal-weight 1
:goal [(/ (.-innerWidth js/window) 2)
(/ (.-innerHeight js/window) 2)]})

Expand All @@ -24,20 +28,21 @@
(rand-int (.-innerHeight js/window))]
:vel [0 0]}))

;; Behaviors and weights
(def behaviors { behaviors/cohesion 1
behaviors/avoidance 1
behaviors/alignment 1
behaviors/goal 1 })
;; A map of behavior functions to the option key used to weight it
(def behaviors {b/cohesion :cohesion-weight
b/avoidance :avoidance-weight
b/alignment :alignment-weight
b/goal :goal-weight})

(defn update-boid
"Given a collection containing the flock and an individual boid,
return an updated boid, using the provided options atom."
[options-atom boid flock]
(let [accelerations (map (fn [[behavior weight]]
(v/mul (behavior @options-atom boid flock) weight))
(let [cohestion (b/cohesion @options-atom boid flock)
accelerations (map (fn [[behavior weight-key]]
(v/mul (behavior @options-atom boid flock) (@options-atom weight-key)))
behaviors)
velocity (v/limit (reduce v/add (:vel boid) accelerations) behaviors/max-speed)]
velocity (v/limit (reduce v/add (:vel boid) accelerations) (:max-speed @options-atom))]
{:pos (v/add (:pos boid) velocity)
:vel velocity}))

Expand Down

0 comments on commit 36bc72a

Please sign in to comment.