 (defn render
   []
   (motion-tick!)
-  ;(handle-collisions!)
+  (handle-collisions!)
 
   (doseq [i (range (count @particles))]
     (let [p (nth @particles i)
           m (nth @particle-meshes i)]
+      ;; static rotation for blinginess
+      (rotate-angles! m [0 2 0])
       (set-position! m (:pos p))))
 
   (render-scene))

(ns physim.core
  (:require [THREE :as THREE]))

(def particle-count 30)
(def gravity-multiplier 100)
(def velocity-limit 10)

(def particles (atom []))
(def particle-meshes (atom []))

(defn rand-x
  []
  (- (rand-int window/innerWidth)
     (/ window/innerWidth 2)))

(defn rand-y
  []
  (- (rand-int window/innerHeight)
     (/ window/innerHeight 2)))

(defn generate-particles
  [n]
  (for [i (range n)]
    { :pos [(rand-x) (rand-y) (rand-y)]
      :vel [(/ (* 0 (rand-x)) window/innerWidth)
            (/ (* 0 (rand-y)) window/innerWidth)
            (/ (* 0 (rand-y)) window/innerWidth)] }))

(defn set-position!
  [mesh [x y z]]
  (set! (.-x (.-position mesh)) x)
  (set! (.-y (.-position mesh)) y)
  (set! (.-z (.-position mesh)) z))

(defn mesh-from-particle
  [p]

  (let [a (calc-accel p all)
        v (map + v a)
        magv (magnitude v)
        v (if (> magv speed-limit)
            (map #(* (/ % magv) speed-limit) v)
            v)
        p (map + p v)]
    [p v a]))
