-
Notifications
You must be signed in to change notification settings - Fork 5
/
bounce.clj
211 lines (191 loc) · 7.43 KB
/
bounce.clj
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
(ns bounce
(:require [org.nfrac.cljbox2d.testbed :as bed]
[org.nfrac.cljbox2d.core :refer :all]
[org.nfrac.cljbox2d.vec2d :as vec2d]
[quil.core :as quil]
[quil.middleware])
(:use [clojure.data.priority-map]))
(def circ-attr {:density 1.0
:category-bits 0x0008
:mask-bits 0xFFFF})
(defn sanitize
"ensure that input pairs have type vec2"
[bumper-positions]
(mapv (fn [pos]
(if (= (type pos) org.jbox2d.common.Vec2)
pos (vec2 pos)))
bumper-positions))
(defn create-world
"creates a world given bumper positions and ball bodies,
which are used to copy bodies for post-inference display"
([bumper-positions] (create-world bumper-positions nil))
([bumper-positions balls]
(let [bumper-positions (sanitize bumper-positions)
world (new-world)
; creates target cup
cup_center 15
cup_floor (body! world {:type :static}
{:shape (edge [(- cup_center 1) 0] [(+ cup_center 1) 0])
:friction 0.3 :restitution 0.0})
cup_left_wall (body! world {:type :static}
{:shape (edge [(- cup_center 1) 0] [(- cup_center 1) 2])
:friction 0.3 :restitution 0.0})
cup_right_wall (body! world {:type :static}
{:shape (edge [(+ cup_center 1) 0] [(+ cup_center 1) 2])
:friction 0.3 :restitution 0.0})
; creates ramp
ramp (body! world {:type :static}
{:shape (edge [-6 9] [-4 7])
:friction 0.2 :restitution 0.0})
; if a list of balls are passed in re-create
; new balls at the old balls' positions
_ (if (not (nil? balls))
(loop [bs balls]
(if (empty? bs)
nil
(let [b (first bs)]
(body! world {:position (position b)}
; TODO generalize to copy body (or attrs),
; not just create a new ball at the same
; location
(assoc circ-attr
:shape (circle 0.25) :restitution 0.5
:group-index 1))
(recur (rest bs))))))
; creates bumpers
_ (loop [x bumper-positions]
(if (empty? x)
nil
(let
[[xx yy] (v2xy (first x))
;_ (prn [xx yy])
lpos [(- xx 0.5) yy]
;_ (prn lpos)
rpos [(+ xx 0.5) yy]
;_ (prn rpos)
]
(body! world {:type :static}
{:shape (edge lpos rpos)
:friction 0.3 :restitution 0.9})
(recur (rest x)))))]
world)))
(defn setup [bumper-positions balls]
"creates world and sets up state for running quil simulation"
(quil/frame-rate 30)
(let [world (create-world bumper-positions balls)]
(assoc bed/initial-state
:last 0.0
:numballs 0
:world world)))
(defn step
"steps the simulation one timestep forward (in quil simluation)
creating 20 balls/bodies as the simulation runs forward"
[state]
(let [now (:time state)
contacts (all-current-contacts (:world state))]
(do
(if (and (> now (+ (:last state)
(/ 20.0 30.0)))
(< (:numballs state) 20))
(do
(body! (:world state)
;;{:position [-5 (+ 8.25 0.1)]};(sample (gamma 0.2 0.2)))]}
{:position [-5 (+ 8.25 0.1 (* 0.1 (rand)))]}
(assoc circ-attr
:shape (circle 0.25)
:restitution 0.5
:group-index 1))
(-> (update-in state [:last] (fn [a b] b) now)
(update-in [:numballs] + 1)
(bed/world-step)
(bed/record-snapshot true)))
(-> (bed/world-step state)
(bed/record-snapshot true))))))
(defn draw
"quil state drawing function"
[state]
(bed/draw state)
(quil/fill 255)
(quil/text (str "Bounce.\n")
10 10))
(defn simulate-world
"simulates the world for 20 sec. outside of quil
creating 20 balls, one every 2/3's of a sec."
[world]
(let [dt (/ 1.0 30.0)
duration 20.0
dts-between-balls 20
bodies (loop [t 0.0 c 0 bodies []]
(if (> t duration)
bodies
(do
(step! world dt)
(if (and (= c dts-between-balls)
(< (count bodies) 20))
(let [new-ball
(body! world
{:position
;[-5 (+ 8.25 0.1)]};(sample (gamma 0.2 0.2)))]}
[-5 (+ 8.25 0.1 (* 0.1 (rand)))]}
(assoc circ-attr
:shape (circle 0.25)
:restitution 0.5
:group-index 1))]
(recur (+ t dt)
0
(conj bodies new-ball)))
(recur (+ t dt)
(+ c 1)
bodies)))))]
{:balls bodies}))
(defn display-static-world [bumper-positions balls]
"displays a static world with bumpers and balls"
(quil/sketch
:title "Bounce"
:setup (partial setup bumper-positions balls)
:update identity
:draw draw
:key-typed bed/key-press
:mouse-pressed bed/mouse-pressed
:mouse-released bed/mouse-released
:mouse-dragged bed/mouse-dragged
:mouse-wheel bed/mouse-wheel
:size [600 500]
:features [:resizable :keep-on-top]
:middleware [quil.middleware/fun-mode]))
(defn show-world-simulation [bumper-positions]
"simulates and displays a world with bumpers
creating 20 balls during the simulation"
(quil/sketch
:title "Bounce"
:setup (partial setup bumper-positions nil)
:update (fn [s] (if (:paused? s) s (step s)))
:draw draw
:key-typed bed/key-press
:mouse-pressed bed/mouse-pressed
:mouse-released bed/mouse-released
:mouse-dragged bed/mouse-dragged
:mouse-wheel bed/mouse-wheel
:size [600 500]
:features [:resizable :keep-on-top]
:middleware [quil.middleware/fun-mode]))
(defn linspace
"creates a list of n values
linearly distributed between min
and max inclusive"
[min max n]
(let [step (/ (- max min) (dec n))]
(range min (+ max step) step)))
;; we need an observable signal and the number
;; of balls in the box after 20 sec. of simulation
;; is something we can count; here's a function
;; to do that
(defn balls-in-box [world]
"computes number of balls that
are in the box in the given
world";
;(-ish, problem specific hack)
(->> (:balls world)
(map position)
(filter #(and (< 0 (second %)) (< (second %) 3) (< 14 (first %))))
(count)))