Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active June 4, 2021 19:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrusher/32be28e6fabff06b60b2 to your computer and use it in GitHub Desktop.
Save jackrusher/32be28e6fabff06b60b2 to your computer and use it in GitHub Desktop.
The simplest possible @thing_clj physics + @Quilist demo
(ns gumball-machine.core
(:require [thi.ng.color.core :as col]
[thi.ng.math.core :as m]
[thi.ng.geom.core :as g]
[thi.ng.geom.core.vector :refer [vec3]]
[thi.ng.geom.physics.core :as ph]
[thi.ng.geom.sphere :refer [sphere]]
[quil.core :as q :include-macros true]))
(def particles
(repeatedly 20 #(ph/particle (vec3 (m/random -50 50)
(m/random -50 50)
(m/random -50 50)) 1)))
(def engine (ph/physics {:particles particles
:drag 0.05
:constraints {:w (ph/shape-constraint-inside
(g/center (sphere 200)))}
:behaviors {:g (ph/gravity (vec3 0 0.5 0))}}))
(defn setup []
(q/smooth)
(q/frame-rate 55)
(q/color-mode :hsb)
(q/no-stroke))
(defn draw []
;; tick the world clock, recalculate particle positions
(ph/timestep engine 1)
;; if all the particles have come to rest...
(when (every? #(< (second (ph/velocity %)) 0.08) particles)
(doseq [p particles]
;; apply a randomized force to them
(ph/add-force p (vec3 (m/random -10 10) -10 (m/random -10 10)))))
;; set up bg + lighting conditions
(q/background 51)
(q/ambient-light 0 0 80)
(q/directional-light 0 0 150 0 0 -1)
(q/light-specular 0 0 200)
(q/specular 0 0 70)
(q/shininess 0.5)
;; draw the particles
(q/with-translation [(/ (q/width) 2) (/ (q/height) 2) 100]
(doseq [[i p] (map-indexed vector particles)]
(let [[x y z] (ph/position p)]
(q/fill (* i 12) 255 255)
(q/with-translation [x y z]
(q/sphere 6))))))
(q/defsketch gumball-machine
:host "gumball-machine"
:size [600 600]
:renderer :p3d
:setup setup
:draw draw)
(defproject gumball-machine "0.1.0-SNAPSHOT"
:description "Using thi.ng from quil + clojurescript."
:dependencies [[org.clojure/clojure "1.7.0"]
[quil "2.3.0"]
[org.clojure/clojurescript "1.7.170"]
[thi.ng/geom "0.0.908"]
[thi.ng/color "1.0.0"]]
:plugins [[lein-cljsbuild "1.1.1"]]
:hooks [leiningen.cljsbuild]
:cljsbuild {:builds [{:source-paths ["src"]
:compiler {:output-to "js/main.js"
:output-dir "out"
:main "gumball_machine.core"
:optimizations :advanced
:pretty-print false}}]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment