From 36bc72a044109b72474124340625aa85cbe71d54 Mon Sep 17 00:00:00 2001 From: Luke VanderHart Date: Fri, 15 Mar 2013 16:03:36 -0400 Subject: [PATCH] Add weights to configurable options --- src/cljs/boids/control_panel.cljs | 6 +++++- src/cljs/boids/main.cljs | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/cljs/boids/control_panel.cljs b/src/cljs/boids/control_panel.cljs index 3ff2a1b..84b9a61 100644 --- a/src/cljs/boids/control_panel.cljs +++ b/src/cljs/boids/control_panel.cljs @@ -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 diff --git a/src/cljs/boids/main.cljs b/src/cljs/boids/main.cljs index 0062c34..85dbcb2 100644 --- a/src/cljs/boids/main.cljs +++ b/src/cljs/boids/main.cljs @@ -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]) @@ -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)]}) @@ -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}))