-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_1_3e.cljs
43 lines (38 loc) · 1013 Bytes
/
chapter_1_3e.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns noc.chapter-1-3e
(:require
[thi.ng.math.core :as tm]
[thi.ng.geom.vector :as v]
[quil.core :as q]))
(def size [640 300])
(defn init-state [{:keys [width height] :as state}]
{:angle 0.0
:pos (v/vec3 0 0 0)
:vel (v/vec3 2 2.5 3)
:thresh 75})
(defn setup! [{:keys [width height]}]
(q/background 255)
(q/no-fill))
(defn tick [{:keys [angle pos vel thresh]}]
(let [[x y z] (tm/+ pos vel)
[vx vy vz] vel
angle (+ angle 0.05)
vx (if (> (abs x) thresh) (* vx -1) vx)
vy (if (> (abs y) thresh) (* vy -1) vy)
vz (if (> (abs z) thresh) (* vz -1) vz)]
{:pos (tm/+ pos vel)
:thresh thresh
:vel (v/vec3 vx vy vz)
:angle angle}))
(defn draw! [{:keys [pos angle]}]
(let [[x y z] pos]
(q/background 255)
(q/directional-light 255 255 255 1 1 -1)
(q/stroke 0)
(q/box 200)
(q/push-matrix)
(q/translate x y z)
(q/rotate-x angle)
(q/rotate-y angle)
(q/stroke 0)
(q/sphere 30)
(q/pop-matrix)))