Permalink
Browse files

* project.clj: tweaks, can actually see something now, but still slow

  • Loading branch information...
1 parent f7cf42f commit c8b3327cc506eccd51645fdce2f55636290b3a6c @swannodette committed May 15, 2011
Showing with 38 additions and 19 deletions.
  1. +1 −1 project.clj
  2. +18 −17 src/flocking/flocking.clj
  3. +19 −1 src/flocking/perf.clj
View
@@ -1,5 +1,5 @@
(defproject flocking "1.0.0-SNAPSHOT"
:description "Dan Shiffman's flocking code in Clojure"
:dependencies [[org.clojure/clojure "1.3.0-alpha7"]
- [org.clojars.automata/rosado.processing "1.1.0"]]
+ [org.clojars.automata/rosado.processing "1.3.0"]]
:dev-dependencies [[swank-clojure "1.3.0"]])
View
@@ -3,6 +3,7 @@
[rosado.processing.applet :as applet]))
(set! *unchecked-math* true)
+(set! *warn-on-reflection* true)
(defprotocol IVecMath2d
(add [this other])
@@ -53,29 +54,29 @@
(def ^java.util.Random rnd (new java.util.Random))
(def ^:constant width 640.0)
(def ^:constant height 360.0)
-(def ^:constant boid-count 150)
+(def ^:constant boid-count 500)
(def ^:constant cores (.. Runtime getRuntime availableProcessors))
(def aflock (atom nil))
;; =============================================================================
;; Utilities
;; =============================================================================
-(defn limit [v n]
+(defn limit [v ^double n]
(mul (unit v) n))
(defn make-boid [loc ms mf]
{:loc loc
- :vel (Vec2d. (+ (* (.nextDouble rnd) 2) -1)
- (+ (* (.nextDouble rnd) 2) -1))
- :acc (Vec2d. 0 0)
+ :vel (Vec2d. (+ (* (.nextDouble rnd) 2.0) -1.0)
+ (+ (* (.nextDouble rnd) 2.0) -1.0))
+ :acc (Vec2d. 0.0 0.0)
:r 2.0
:max-speed ms
:max-force mf})
(defn bound ^double [^double n ^double ox ^double dx]
(cond
- (< n (- ox)) (+ dx ox)
+ (< n (- ox)) (+ dx ox)
(> n (+ ox dx)) (- ox)
true n))
@@ -131,26 +132,26 @@
(defn separation
[boid boids]
- (let [dsep 25.0
+ (let [dsep 25.0
filtered (separation-map boid (distance-filter boids 0.0 dsep))]
- (if-let [sum (reduce sum filtered)]
- (div sum (count filtered))
- zero)))
+ (reduce sum filtered)))
(defn alignment
[{mf :max-force :as boid} boids]
- (let [nhood 50.0
+ (let [nhood 50.0
filtered (map :vel (distance-filter boids 0.0 nhood))]
- (if-let [sum (reduce sum filtered)]
- (limit (div sum (count filtered)) mf)
- zero)))
+ (let [sum (reduce sum filtered)]
+ (if (not (identical? sum zero))
+ (limit (div sum (count filtered)) mf)
+ sum))))
(defn cohesion [boid boids]
(let [nhood 50.0
filtered (map :loc (distance-filter boids 0.0 nhood))]
- (if-let [sum (reduce sum filtered)]
- (steer boid (div sum (count filtered)) false)
- zero)))
+ (let [sum (reduce sum filtered)]
+ (if (not (identical? sum zero))
+ (steer boid (div sum (count filtered)) false)
+ zero))))
(defn flock [{acc :acc, :as boid} boids]
(let [mboids (distance-map boid boids)
View
@@ -49,4 +49,22 @@
(let [flock (whole-flock)]
(time
(doseq [b flock]
- (doall (distance-map b flock))))))
+ (doall (distance-map b flock))))))
+
+(comment
+ (let [b (get-in @aflock [0 0])
+ bs (distance-map b (whole-flock))]
+ (separation b bs))
+
+ (let [b (get-in @aflock [0 0])
+ bs (distance-map b (whole-flock))]
+ (cohesion b bs))
+
+ (let [b (get-in @aflock [0 0])
+ bs (distance-map b (whole-flock))]
+ (alignment b bs))
+
+ (let [b (get-in @aflock [0 0])
+ bs (distance-map b (whole-flock))]
+ (map :vel (distance-filter bs 0.0 50.0)))
+ )

0 comments on commit c8b3327

Please sign in to comment.