@@ -34,7 +34,7 @@ This code uses the [Quil library](https://github.com/quil/quil) to render points
34
34
and lines. In the ` main- ` function, the ` sketch ` macro from the Quil library is
35
35
used to bind the physics domain to the UI.
36
36
37
- ``` clojure
37
+ ``` Clojure
38
38
(defn -main []
39
39
(quil/sketch
40
40
:host -main
@@ -55,7 +55,7 @@ The sketch macro basically asks the developer to configure which 'handler
55
55
functions' it should call when certain events occur. For instance:
56
56
57
57
- a key is pressed -> call ` key-pressed `
58
- - the mouse is pressed -> call ` mouse-press `
58
+ - the mouse is pressed -> call ` mouse-pressed `
59
59
- the state of your program has to be updated -> call ` update-state `
60
60
- it's time to draw the new state -> call ` draw `
61
61
- etc.
@@ -69,7 +69,7 @@ Because I configured Quil to run in functional mode `(quil-mw/fun-mode)`, Quil:
69
69
70
70
The handler function signatures therefore look like this:
71
71
72
- ``` clojure
72
+ ``` Clojure
73
73
(defn draw [state] ... )
74
74
(defn update-state [state] ... )
75
75
(defn key-pressed [state event] ... )
@@ -81,7 +81,7 @@ The handler function signatures therefore look like this:
81
81
What does a snapshot of this world state look like? ` key-pressed ` gives some
82
82
hints:
83
83
84
- ``` clojure
84
+ ``` Clojure
85
85
(defn key-pressed
86
86
[state event]
87
87
(let [raw-key (:raw-key event)
99
99
So on pressing a key, a certain world is loaded. Let's look into the 'sticks'
100
100
world:
101
101
102
- ``` clojure
102
+ ``` Clojure
103
103
{:points {:p0 {:x 10 :y 1 :oldx 0 :oldy 0 }
104
104
:p1 {:x 100 :y 100 :oldx 100 :oldy 100 }
105
105
:p2 {:x 0 :y 500 :oldx 0 :oldy 525 }
@@ -128,7 +128,7 @@ Stick length. The simulation loop boils down to:
128
128
129
129
In code:
130
130
131
- ``` clojure
131
+ ``` Clojure
132
132
(defn update-state
133
133
[state]
134
134
(->> state
@@ -145,7 +145,7 @@ This code can be read like this: 'with `state`, first `update-points`, then
145
145
Points are the main abstraction in this code. I decided to use a record to name
146
146
them:
147
147
148
- ``` clojure
148
+ ``` Clojure
149
149
(defrecord Point [x y oldx oldy pinned])
150
150
```
151
151
@@ -157,7 +157,7 @@ A pinned Point can be unpinned by clicking it with the mouse pointer.
157
157
The ` update-point ` function calculates the velocity of the Point and adds some
158
158
gravity in the mix:
159
159
160
- ``` clojure
160
+ ``` Clojure
161
161
(defn update-point
162
162
[{:keys [x y oldx oldy pinned] :as point}]
163
163
(if pinned
@@ -179,7 +179,7 @@ values in a data structure without explicitly querying the data structure. So
179
179
instead of getting each and every value out of Point and binding it to a 'new'
180
180
name...
181
181
182
- ``` clojure
182
+ ``` Clojure
183
183
(let [x (:x point)
184
184
y (:y point)
185
185
oldx (:oldx point)
@@ -208,7 +208,7 @@ already moving and reacting to gravity in the simulation.
208
208
It's time for Points to meet the harsh reality of life. Walls are harder than
209
209
Points and therefore Points should bounce off of them:
210
210
211
- ``` clojure
211
+ ``` Clojure
212
212
(defn hit-floor? [y] (> y height))
213
213
(defn hit-ceiling? [y] (< y 0 ))
214
214
(defn hit-left-wall? [x] (< x 0 ))
@@ -276,7 +276,7 @@ Instead of trying to calculate the solution that satisfies all Stick constraints
276
276
at once, this code simply looks at one Stick at a time and 'solves' the
277
277
constraint problem by repeatedly solving Stick constraints in isolation.
278
278
279
- ``` clojure
279
+ ``` Clojure
280
280
(defn apply-stick-constraint
281
281
[{length :length :as stick}
282
282
{p0x :x p0y :y oldp0x :oldx oldp0y :oldy pinp0 :pinned :as p0}
@@ -336,7 +336,7 @@ is perceived to behave naturally.
336
336
337
337
Finally, the Points and Sticks have to be drawn on screen:
338
338
339
- ``` clojure
339
+ ``` Clojure
340
340
(defn draw
341
341
[state]
342
342
(quil/background 255 )
0 commit comments