rlm@73
|
1 #+title: First attempt at a creature!
|
rlm@73
|
2 #+author: Robert McIntyre
|
rlm@73
|
3 #+email: rlm@mit.edu
|
rlm@73
|
4 #+description:
|
rlm@73
|
5 #+keywords: simulation, jMonkeyEngine3, clojure
|
rlm@73
|
6 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@73
|
7 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@73
|
8
|
rlm@124
|
9 * ideas
|
rlm@125
|
10
|
rlm@125
|
11 ** have to get done before winston
|
rlm@126
|
12 - [ ] write an explination for why greyscale bitmaps for senses is
|
rlm@126
|
13 appropiate -- 1/2 day
|
rlm@126
|
14 - [ ] muscle control -- day
|
rlm@126
|
15 - [ ] proprioception sensor map in the style of the other senses -- day
|
rlm@125
|
16 - [ ] refactor integration code to distribute to each of the senses
|
rlm@126
|
17 -- day
|
rlm@126
|
18 - [ ] create video showing all the senses for Winston -- 2 days
|
rlm@126
|
19 - [ ] write summary of project for Winston \
|
rlm@126
|
20 - [ ] project proposals for Winston \
|
rlm@126
|
21 - [ ] additional senses to be implemented for Winston | -- 2 days
|
rlm@126
|
22 - [ ] send Winston package /
|
rlm@125
|
23
|
rlm@125
|
24 ** would be cool to get done before winston
|
rlm@126
|
25 - [X] enable greyscale bitmaps for touch -- 2 hours
|
rlm@126
|
26 - [X] use sawfish to auto-tile sense windows -- 6 hours
|
rlm@126
|
27 - [X] sawfish keybinding to automatically delete all sense windows
|
rlm@126
|
28 - [ ] directly change the UV-pixels to show sensor activation -- 2
|
rlm@126
|
29 days
|
rlm@126
|
30 - [ ] proof of concept C sense manipulation -- 2 days
|
rlm@126
|
31 - [ ] proof of concept GPU sense manipulation -- week
|
rlm@126
|
32 - [ ] fourier view of sound -- 2 or 3 days
|
rlm@127
|
33 - [ ] dancing music generator -- 1 day, depends on fourier
|
rlm@125
|
34
|
rlm@125
|
35 ** don't have to get done before winston
|
rlm@126
|
36 - [ ] write tests for integration -- 3 days
|
rlm@126
|
37 - [ ] usertime/gametime clock HUD display -- day
|
rlm@126
|
38 - [ ] find papers for each of the senses justifying my own
|
rlm@126
|
39 representation -- week
|
rlm@126
|
40 - [ ] show sensor maps in HUD display? -- 4 days
|
rlm@126
|
41 - [ ] show sensor maps in AWT display? -- 2 days
|
rlm@124
|
42
|
rlm@99
|
43
|
rlm@73
|
44 * Intro
|
rlm@73
|
45 So far, I've made the following senses --
|
rlm@73
|
46 - Vision
|
rlm@73
|
47 - Hearing
|
rlm@73
|
48 - Touch
|
rlm@73
|
49 - Proprioception
|
rlm@73
|
50
|
rlm@73
|
51 And one effector:
|
rlm@73
|
52 - Movement
|
rlm@73
|
53
|
rlm@73
|
54 However, the code so far has only enabled these senses, but has not
|
rlm@73
|
55 actually implemented them. For example, there is still a lot of work
|
rlm@73
|
56 to be done for vision. I need to be able to create an /eyeball/ in
|
rlm@73
|
57 simulation that can be moved around and see the world from different
|
rlm@73
|
58 angles. I also need to determine weather to use log-polar or cartesian
|
rlm@73
|
59 for the visual input, and I need to determine how/wether to
|
rlm@73
|
60 disceritise the visual input.
|
rlm@73
|
61
|
rlm@73
|
62 I also want to be able to visualize both the sensors and the
|
rlm@104
|
63 effectors in pretty pictures. This semi-retarted creature will be my
|
rlm@73
|
64 first attempt at bringing everything together.
|
rlm@73
|
65
|
rlm@73
|
66 * The creature's body
|
rlm@73
|
67
|
rlm@73
|
68 Still going to do an eve-like body in blender, but due to problems
|
rlm@104
|
69 importing the joints, etc into jMonkeyEngine3, I'm going to do all
|
rlm@73
|
70 the connecting here in clojure code, using the names of the individual
|
rlm@73
|
71 components and trial and error. Later, I'll maybe make some sort of
|
rlm@73
|
72 creature-building modifications to blender that support whatever
|
rlm@73
|
73 discreitized senses I'm going to make.
|
rlm@73
|
74
|
rlm@73
|
75 #+name: body-1
|
rlm@73
|
76 #+begin_src clojure
|
rlm@73
|
77 (ns cortex.silly
|
rlm@73
|
78 "let's play!"
|
rlm@73
|
79 {:author "Robert McIntyre"})
|
rlm@73
|
80
|
rlm@73
|
81 ;; TODO remove this!
|
rlm@73
|
82 (require 'cortex.import)
|
rlm@73
|
83 (cortex.import/mega-import-jme3)
|
rlm@73
|
84 (use '(cortex world util body hearing touch vision))
|
rlm@73
|
85
|
rlm@73
|
86 (rlm.rlm-commands/help)
|
rlm@99
|
87 (import java.awt.image.BufferedImage)
|
rlm@99
|
88 (import javax.swing.JPanel)
|
rlm@99
|
89 (import javax.swing.SwingUtilities)
|
rlm@99
|
90 (import java.awt.Dimension)
|
rlm@99
|
91 (import javax.swing.JFrame)
|
rlm@99
|
92 (import java.awt.Dimension)
|
rlm@106
|
93 (import com.aurellem.capture.RatchetTimer)
|
rlm@99
|
94 (declare joint-create)
|
rlm@108
|
95 (use 'clojure.contrib.def)
|
rlm@73
|
96
|
rlm@100
|
97 (defn points->image
|
rlm@100
|
98 "Take a sparse collection of points and visuliaze it as a
|
rlm@100
|
99 BufferedImage."
|
rlm@102
|
100
|
rlm@102
|
101 ;; TODO maybe parallelize this since it's easy
|
rlm@102
|
102
|
rlm@100
|
103 [points]
|
rlm@106
|
104 (if (empty? points)
|
rlm@106
|
105 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
|
rlm@106
|
106 (let [xs (vec (map first points))
|
rlm@106
|
107 ys (vec (map second points))
|
rlm@106
|
108 x0 (apply min xs)
|
rlm@106
|
109 y0 (apply min ys)
|
rlm@106
|
110 width (- (apply max xs) x0)
|
rlm@106
|
111 height (- (apply max ys) y0)
|
rlm@106
|
112 image (BufferedImage. (inc width) (inc height)
|
rlm@119
|
113 BufferedImage/TYPE_INT_RGB)]
|
rlm@118
|
114 (dorun
|
rlm@118
|
115 (for [x (range (.getWidth image))
|
rlm@118
|
116 y (range (.getHeight image))]
|
rlm@119
|
117 (.setRGB image x y 0xFF0000)))
|
rlm@106
|
118 (dorun
|
rlm@106
|
119 (for [index (range (count points))]
|
rlm@106
|
120 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
|
rlm@106
|
121
|
rlm@106
|
122 image)))
|
rlm@100
|
123
|
rlm@101
|
124 (defn average [coll]
|
rlm@101
|
125 (/ (reduce + coll) (count coll)))
|
rlm@101
|
126
|
rlm@101
|
127 (defn collapse-1d
|
rlm@101
|
128 "One dimensional analogue of collapse"
|
rlm@101
|
129 [center line]
|
rlm@101
|
130 (let [length (count line)
|
rlm@101
|
131 num-above (count (filter (partial < center) line))
|
rlm@101
|
132 num-below (- length num-above)]
|
rlm@101
|
133 (range (- center num-below)
|
rlm@115
|
134 (+ center num-above))))
|
rlm@99
|
135
|
rlm@99
|
136 (defn collapse
|
rlm@99
|
137 "Take a set of pairs of integers and collapse them into a
|
rlm@99
|
138 contigous bitmap."
|
rlm@99
|
139 [points]
|
rlm@108
|
140 (if (empty? points) []
|
rlm@108
|
141 (let
|
rlm@108
|
142 [num-points (count points)
|
rlm@108
|
143 center (vector
|
rlm@108
|
144 (int (average (map first points)))
|
rlm@108
|
145 (int (average (map first points))))
|
rlm@108
|
146 flattened
|
rlm@108
|
147 (reduce
|
rlm@108
|
148 concat
|
rlm@108
|
149 (map
|
rlm@108
|
150 (fn [column]
|
rlm@108
|
151 (map vector
|
rlm@108
|
152 (map first column)
|
rlm@108
|
153 (collapse-1d (second center)
|
rlm@108
|
154 (map second column))))
|
rlm@108
|
155 (partition-by first (sort-by first points))))
|
rlm@108
|
156 squeezed
|
rlm@108
|
157 (reduce
|
rlm@108
|
158 concat
|
rlm@108
|
159 (map
|
rlm@108
|
160 (fn [row]
|
rlm@108
|
161 (map vector
|
rlm@108
|
162 (collapse-1d (first center)
|
rlm@108
|
163 (map first row))
|
rlm@108
|
164 (map second row)))
|
rlm@108
|
165 (partition-by second (sort-by second flattened))))
|
rlm@108
|
166 relocate
|
rlm@108
|
167 (let [min-x (apply min (map first squeezed))
|
rlm@108
|
168 min-y (apply min (map second squeezed))]
|
rlm@108
|
169 (map (fn [[x y]]
|
rlm@108
|
170 [(- x min-x)
|
rlm@108
|
171 (- y min-y)])
|
rlm@108
|
172 squeezed))]
|
rlm@115
|
173 relocate)))
|
rlm@83
|
174
|
rlm@83
|
175 (defn load-bullet []
|
rlm@84
|
176 (let [sim (world (Node.) {} no-op no-op)]
|
rlm@102
|
177 (doto sim
|
rlm@102
|
178 (.enqueue
|
rlm@102
|
179 (fn []
|
rlm@102
|
180 (.stop sim)))
|
rlm@102
|
181 (.start))))
|
rlm@83
|
182
|
rlm@73
|
183 (defn load-blender-model
|
rlm@73
|
184 "Load a .blend file using an asset folder relative path."
|
rlm@73
|
185 [^String model]
|
rlm@73
|
186 (.loadModel
|
rlm@73
|
187 (doto (asset-manager)
|
rlm@73
|
188 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@73
|
189 model))
|
rlm@73
|
190
|
rlm@74
|
191 (defn meta-data [blender-node key]
|
rlm@74
|
192 (if-let [data (.getUserData blender-node "properties")]
|
rlm@74
|
193 (.findValue data key)
|
rlm@74
|
194 nil))
|
rlm@73
|
195
|
rlm@78
|
196 (defn blender-to-jme
|
rlm@78
|
197 "Convert from Blender coordinates to JME coordinates"
|
rlm@78
|
198 [#^Vector3f in]
|
rlm@78
|
199 (Vector3f. (.getX in)
|
rlm@78
|
200 (.getZ in)
|
rlm@78
|
201 (- (.getY in))))
|
rlm@74
|
202
|
rlm@79
|
203 (defn jme-to-blender
|
rlm@79
|
204 "Convert from JME coordinates to Blender coordinates"
|
rlm@79
|
205 [#^Vector3f in]
|
rlm@79
|
206 (Vector3f. (.getX in)
|
rlm@79
|
207 (- (.getZ in))
|
rlm@79
|
208 (.getY in)))
|
rlm@79
|
209
|
rlm@78
|
210 (defn joint-targets
|
rlm@78
|
211 "Return the two closest two objects to the joint object, ordered
|
rlm@78
|
212 from bottom to top according to the joint's rotation."
|
rlm@78
|
213 [#^Node parts #^Node joint]
|
rlm@78
|
214 (loop [radius (float 0.01)]
|
rlm@78
|
215 (let [results (CollisionResults.)]
|
rlm@78
|
216 (.collideWith
|
rlm@78
|
217 parts
|
rlm@78
|
218 (BoundingBox. (.getWorldTranslation joint)
|
rlm@78
|
219 radius radius radius)
|
rlm@78
|
220 results)
|
rlm@78
|
221 (let [targets
|
rlm@78
|
222 (distinct
|
rlm@78
|
223 (map #(.getGeometry %) results))]
|
rlm@78
|
224 (if (>= (count targets) 2)
|
rlm@78
|
225 (sort-by
|
rlm@79
|
226 #(let [v
|
rlm@79
|
227 (jme-to-blender
|
rlm@79
|
228 (.mult
|
rlm@79
|
229 (.inverse (.getWorldRotation joint))
|
rlm@79
|
230 (.subtract (.getWorldTranslation %)
|
rlm@79
|
231 (.getWorldTranslation joint))))]
|
rlm@79
|
232 (println-repl (.getName %) ":" v)
|
rlm@79
|
233 (.dot (Vector3f. 1 1 1)
|
rlm@79
|
234 v))
|
rlm@78
|
235 (take 2 targets))
|
rlm@78
|
236 (recur (float (* radius 2))))))))
|
rlm@74
|
237
|
rlm@87
|
238 (defn world-to-local
|
rlm@87
|
239 "Convert the world coordinates into coordinates relative to the
|
rlm@87
|
240 object (i.e. local coordinates), taking into account the rotation
|
rlm@87
|
241 of object."
|
rlm@87
|
242 [#^Spatial object world-coordinate]
|
rlm@87
|
243 (let [out (Vector3f.)]
|
rlm@88
|
244 (.worldToLocal object world-coordinate out) out))
|
rlm@87
|
245
|
rlm@96
|
246 (defn local-to-world
|
rlm@96
|
247 "Convert the local coordinates into coordinates into world relative
|
rlm@96
|
248 coordinates"
|
rlm@96
|
249 [#^Spatial object local-coordinate]
|
rlm@96
|
250 (let [world-coordinate (Vector3f.)]
|
rlm@96
|
251 (.localToWorld object local-coordinate world-coordinate)
|
rlm@96
|
252 world-coordinate))
|
rlm@96
|
253
|
rlm@87
|
254 (defmulti joint-dispatch
|
rlm@87
|
255 "Translate blender pseudo-joints into real JME joints."
|
rlm@88
|
256 (fn [constraints & _]
|
rlm@87
|
257 (:type constraints)))
|
rlm@87
|
258
|
rlm@87
|
259 (defmethod joint-dispatch :point
|
rlm@87
|
260 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
261 (println-repl "creating POINT2POINT joint")
|
rlm@87
|
262 (Point2PointJoint.
|
rlm@87
|
263 control-a
|
rlm@87
|
264 control-b
|
rlm@87
|
265 pivot-a
|
rlm@87
|
266 pivot-b))
|
rlm@87
|
267
|
rlm@87
|
268 (defmethod joint-dispatch :hinge
|
rlm@87
|
269 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
270 (println-repl "creating HINGE joint")
|
rlm@87
|
271 (let [axis
|
rlm@87
|
272 (if-let
|
rlm@87
|
273 [axis (:axis constraints)]
|
rlm@87
|
274 axis
|
rlm@87
|
275 Vector3f/UNIT_X)
|
rlm@87
|
276 [limit-1 limit-2] (:limit constraints)
|
rlm@87
|
277 hinge-axis
|
rlm@87
|
278 (.mult
|
rlm@87
|
279 rotation
|
rlm@87
|
280 (blender-to-jme axis))]
|
rlm@87
|
281 (doto
|
rlm@87
|
282 (HingeJoint.
|
rlm@87
|
283 control-a
|
rlm@87
|
284 control-b
|
rlm@87
|
285 pivot-a
|
rlm@87
|
286 pivot-b
|
rlm@87
|
287 hinge-axis
|
rlm@87
|
288 hinge-axis)
|
rlm@87
|
289 (.setLimit limit-1 limit-2))))
|
rlm@87
|
290
|
rlm@87
|
291 (defmethod joint-dispatch :cone
|
rlm@87
|
292 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
293 (let [limit-xz (:limit-xz constraints)
|
rlm@87
|
294 limit-xy (:limit-xy constraints)
|
rlm@87
|
295 twist (:twist constraints)]
|
rlm@87
|
296
|
rlm@87
|
297 (println-repl "creating CONE joint")
|
rlm@87
|
298 (println-repl rotation)
|
rlm@87
|
299 (println-repl
|
rlm@87
|
300 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
|
rlm@87
|
301 (println-repl
|
rlm@87
|
302 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
|
rlm@87
|
303 (println-repl
|
rlm@87
|
304 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
|
rlm@87
|
305 (doto
|
rlm@87
|
306 (ConeJoint.
|
rlm@87
|
307 control-a
|
rlm@87
|
308 control-b
|
rlm@87
|
309 pivot-a
|
rlm@87
|
310 pivot-b
|
rlm@87
|
311 rotation
|
rlm@87
|
312 rotation)
|
rlm@87
|
313 (.setLimit (float limit-xz)
|
rlm@87
|
314 (float limit-xy)
|
rlm@87
|
315 (float twist)))))
|
rlm@87
|
316
|
rlm@88
|
317 (defn connect
|
rlm@87
|
318 "here are some examples:
|
rlm@87
|
319 {:type :point}
|
rlm@87
|
320 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@87
|
321 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@87
|
322
|
rlm@89
|
323 {:type :cone :limit-xz 0]
|
rlm@89
|
324 :limit-xy 0]
|
rlm@89
|
325 :twist 0]} (use XZY rotation mode in blender!)"
|
rlm@87
|
326 [#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@87
|
327 (let [control-a (.getControl obj-a RigidBodyControl)
|
rlm@87
|
328 control-b (.getControl obj-b RigidBodyControl)
|
rlm@87
|
329 joint-center (.getWorldTranslation joint)
|
rlm@87
|
330 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
|
rlm@87
|
331 pivot-a (world-to-local obj-a joint-center)
|
rlm@87
|
332 pivot-b (world-to-local obj-b joint-center)]
|
rlm@89
|
333
|
rlm@87
|
334 (if-let [constraints
|
rlm@87
|
335 (map-vals
|
rlm@87
|
336 eval
|
rlm@87
|
337 (read-string
|
rlm@87
|
338 (meta-data joint "joint")))]
|
rlm@89
|
339 ;; A side-effect of creating a joint registers
|
rlm@89
|
340 ;; it with both physics objects which in turn
|
rlm@89
|
341 ;; will register the joint with the physics system
|
rlm@89
|
342 ;; when the simulation is started.
|
rlm@87
|
343 (do
|
rlm@87
|
344 (println-repl "creating joint between"
|
rlm@87
|
345 (.getName obj-a) "and" (.getName obj-b))
|
rlm@87
|
346 (joint-dispatch constraints
|
rlm@87
|
347 control-a control-b
|
rlm@87
|
348 pivot-a pivot-b
|
rlm@87
|
349 joint-rotation))
|
rlm@87
|
350 (println-repl "could not find joint meta-data!"))))
|
rlm@87
|
351
|
rlm@78
|
352 (defn assemble-creature [#^Node pieces joints]
|
rlm@78
|
353 (dorun
|
rlm@78
|
354 (map
|
rlm@78
|
355 (fn [geom]
|
rlm@78
|
356 (let [physics-control
|
rlm@78
|
357 (RigidBodyControl.
|
rlm@78
|
358 (HullCollisionShape.
|
rlm@78
|
359 (.getMesh geom))
|
rlm@78
|
360 (if-let [mass (meta-data geom "mass")]
|
rlm@78
|
361 (do
|
rlm@78
|
362 (println-repl
|
rlm@78
|
363 "setting" (.getName geom) "mass to" (float mass))
|
rlm@78
|
364 (float mass))
|
rlm@78
|
365 (float 1)))]
|
rlm@78
|
366
|
rlm@78
|
367 (.addControl geom physics-control)))
|
rlm@78
|
368 (filter #(isa? (class %) Geometry )
|
rlm@78
|
369 (node-seq pieces))))
|
rlm@78
|
370 (dorun
|
rlm@78
|
371 (map
|
rlm@78
|
372 (fn [joint]
|
rlm@78
|
373 (let [[obj-a obj-b]
|
rlm@78
|
374 (joint-targets pieces joint)]
|
rlm@88
|
375 (connect obj-a obj-b joint)))
|
rlm@78
|
376 joints))
|
rlm@78
|
377 pieces)
|
rlm@74
|
378
|
rlm@116
|
379 (declare blender-creature)
|
rlm@74
|
380
|
rlm@78
|
381 (def hand "Models/creature1/one.blend")
|
rlm@74
|
382
|
rlm@78
|
383 (def worm "Models/creature1/try-again.blend")
|
rlm@78
|
384
|
rlm@90
|
385 (def touch "Models/creature1/touch.blend")
|
rlm@90
|
386
|
rlm@90
|
387 (defn worm-model [] (load-blender-model worm))
|
rlm@90
|
388
|
rlm@80
|
389 (defn x-ray [#^ColorRGBA color]
|
rlm@80
|
390 (doto (Material. (asset-manager)
|
rlm@80
|
391 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@80
|
392 (.setColor "Color" color)
|
rlm@80
|
393 (-> (.getAdditionalRenderState)
|
rlm@80
|
394 (.setDepthTest false))))
|
rlm@80
|
395
|
rlm@91
|
396 (defn colorful []
|
rlm@91
|
397 (.getChild (worm-model) "worm-21"))
|
rlm@90
|
398
|
rlm@90
|
399 (import jme3tools.converters.ImageToAwt)
|
rlm@90
|
400
|
rlm@90
|
401 (import ij.ImagePlus)
|
rlm@90
|
402
|
rlm@108
|
403 ;; Every Mesh has many triangles, each with its own index.
|
rlm@108
|
404 ;; Every vertex has its own index as well.
|
rlm@90
|
405
|
rlm@108
|
406 (defn tactile-sensor-image
|
rlm@110
|
407 "Return the touch-sensor distribution image in BufferedImage format,
|
rlm@110
|
408 or nil if it does not exist."
|
rlm@91
|
409 [#^Geometry obj]
|
rlm@110
|
410 (if-let [image-path (meta-data obj "touch")]
|
rlm@110
|
411 (ImageToAwt/convert
|
rlm@110
|
412 (.getImage
|
rlm@110
|
413 (.loadTexture
|
rlm@110
|
414 (asset-manager)
|
rlm@110
|
415 image-path))
|
rlm@110
|
416 false false 0)))
|
rlm@110
|
417
|
rlm@91
|
418 (import ij.process.ImageProcessor)
|
rlm@91
|
419 (import java.awt.image.BufferedImage)
|
rlm@91
|
420
|
rlm@92
|
421 (def white -1)
|
rlm@94
|
422
|
rlm@91
|
423 (defn filter-pixels
|
rlm@108
|
424 "List the coordinates of all pixels matching pred, within the bounds
|
rlm@108
|
425 provided. Bounds -> [x0 y0 width height]"
|
rlm@92
|
426 {:author "Dylan Holmes"}
|
rlm@108
|
427 ([pred #^BufferedImage image]
|
rlm@108
|
428 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
|
rlm@108
|
429 ([pred #^BufferedImage image [x0 y0 width height]]
|
rlm@108
|
430 ((fn accumulate [x y matches]
|
rlm@108
|
431 (cond
|
rlm@108
|
432 (>= y (+ height y0)) matches
|
rlm@108
|
433 (>= x (+ width x0)) (recur 0 (inc y) matches)
|
rlm@108
|
434 (pred (.getRGB image x y))
|
rlm@108
|
435 (recur (inc x) y (conj matches [x y]))
|
rlm@108
|
436 :else (recur (inc x) y matches)))
|
rlm@108
|
437 x0 y0 [])))
|
rlm@91
|
438
|
rlm@91
|
439 (defn white-coordinates
|
rlm@108
|
440 "Coordinates of all the white pixels in a subset of the image."
|
rlm@112
|
441 ([#^BufferedImage image bounds]
|
rlm@112
|
442 (filter-pixels #(= % white) image bounds))
|
rlm@112
|
443 ([#^BufferedImage image]
|
rlm@112
|
444 (filter-pixels #(= % white) image)))
|
rlm@108
|
445
|
rlm@108
|
446 (defn triangle
|
rlm@112
|
447 "Get the triangle specified by triangle-index from the mesh within
|
rlm@112
|
448 bounds."
|
rlm@108
|
449 [#^Mesh mesh triangle-index]
|
rlm@108
|
450 (let [scratch (Triangle.)]
|
rlm@108
|
451 (.getTriangle mesh triangle-index scratch)
|
rlm@108
|
452 scratch))
|
rlm@108
|
453
|
rlm@108
|
454 (defn triangle-vertex-indices
|
rlm@108
|
455 "Get the triangle vertex indices of a given triangle from a given
|
rlm@108
|
456 mesh."
|
rlm@108
|
457 [#^Mesh mesh triangle-index]
|
rlm@108
|
458 (let [indices (int-array 3)]
|
rlm@108
|
459 (.getTriangle mesh triangle-index indices)
|
rlm@108
|
460 (vec indices)))
|
rlm@108
|
461
|
rlm@108
|
462 (defn vertex-UV-coord
|
rlm@108
|
463 "Get the uv-coordinates of the vertex named by vertex-index"
|
rlm@108
|
464 [#^Mesh mesh vertex-index]
|
rlm@108
|
465 (let [UV-buffer
|
rlm@108
|
466 (.getData
|
rlm@108
|
467 (.getBuffer
|
rlm@108
|
468 mesh
|
rlm@108
|
469 VertexBuffer$Type/TexCoord))]
|
rlm@108
|
470 [(.get UV-buffer (* vertex-index 2))
|
rlm@108
|
471 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
|
rlm@108
|
472
|
rlm@108
|
473 (defn triangle-UV-coord
|
rlm@108
|
474 "Get the uv-cooridnates of the triangle's verticies."
|
rlm@108
|
475 [#^Mesh mesh width height triangle-index]
|
rlm@108
|
476 (map (fn [[u v]] (vector (* width u) (* height v)))
|
rlm@108
|
477 (map (partial vertex-UV-coord mesh)
|
rlm@108
|
478 (triangle-vertex-indices mesh triangle-index))))
|
rlm@91
|
479
|
rlm@102
|
480 (defn same-side?
|
rlm@102
|
481 "Given the points p1 and p2 and the reference point ref, is point p
|
rlm@102
|
482 on the same side of the line that goes through p1 and p2 as ref is?"
|
rlm@102
|
483 [p1 p2 ref p]
|
rlm@91
|
484 (<=
|
rlm@91
|
485 0
|
rlm@91
|
486 (.dot
|
rlm@91
|
487 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@91
|
488 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@91
|
489
|
rlm@108
|
490 (defn triangle-seq [#^Triangle tri]
|
rlm@108
|
491 [(.get1 tri) (.get2 tri) (.get3 tri)])
|
rlm@108
|
492
|
rlm@108
|
493 (defn vector3f-seq [#^Vector3f v]
|
rlm@108
|
494 [(.getX v) (.getY v) (.getZ v)])
|
rlm@108
|
495
|
rlm@108
|
496 (defn inside-triangle?
|
rlm@108
|
497 "Is the point inside the triangle?"
|
rlm@108
|
498 {:author "Dylan Holmes"}
|
rlm@108
|
499 [#^Triangle tri #^Vector3f p]
|
rlm@108
|
500 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
|
rlm@108
|
501 (and
|
rlm@108
|
502 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@108
|
503 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@108
|
504 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@108
|
505
|
rlm@94
|
506 (defn triangle->matrix4f
|
rlm@108
|
507 "Converts the triangle into a 4x4 matrix: The first three columns
|
rlm@108
|
508 contain the vertices of the triangle; the last contains the unit
|
rlm@108
|
509 normal of the triangle. The bottom row is filled with 1s."
|
rlm@94
|
510 [#^Triangle t]
|
rlm@94
|
511 (let [mat (Matrix4f.)
|
rlm@94
|
512 [vert-1 vert-2 vert-3]
|
rlm@94
|
513 ((comp vec map) #(.get t %) (range 3))
|
rlm@94
|
514 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@94
|
515 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@94
|
516 (dorun
|
rlm@94
|
517 (for [row (range 4) col (range 3)]
|
rlm@94
|
518 (do
|
rlm@94
|
519 (.set mat col row (.get (vertices row)col))
|
rlm@94
|
520 (.set mat 3 row 1))))
|
rlm@94
|
521 mat))
|
rlm@94
|
522
|
rlm@94
|
523 (defn triangle-transformation
|
rlm@94
|
524 "Returns the affine transformation that converts each vertex in the
|
rlm@94
|
525 first triangle into the corresponding vertex in the second
|
rlm@94
|
526 triangle."
|
rlm@94
|
527 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
528 (.mult
|
rlm@94
|
529 (triangle->matrix4f tri-2)
|
rlm@94
|
530 (.invert (triangle->matrix4f tri-1))))
|
rlm@94
|
531
|
rlm@108
|
532 (defn point->vector2f [[u v]]
|
rlm@108
|
533 (Vector2f. u v))
|
rlm@94
|
534
|
rlm@94
|
535 (defn vector2f->vector3f [v]
|
rlm@94
|
536 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@94
|
537
|
rlm@94
|
538 (defn map-triangle [f #^Triangle tri]
|
rlm@94
|
539 (Triangle.
|
rlm@94
|
540 (f 0 (.get1 tri))
|
rlm@94
|
541 (f 1 (.get2 tri))
|
rlm@94
|
542 (f 2 (.get3 tri))))
|
rlm@94
|
543
|
rlm@108
|
544 (defn points->triangle
|
rlm@108
|
545 "Convert a list of points into a triangle."
|
rlm@108
|
546 [points]
|
rlm@108
|
547 (apply #(Triangle. %1 %2 %3)
|
rlm@108
|
548 (map (fn [point]
|
rlm@108
|
549 (let [point (vec point)]
|
rlm@108
|
550 (Vector3f. (get point 0 0)
|
rlm@108
|
551 (get point 1 0)
|
rlm@108
|
552 (get point 2 0))))
|
rlm@108
|
553 (take 3 points))))
|
rlm@94
|
554
|
rlm@108
|
555 (defn convex-bounds
|
rlm@108
|
556 "Dimensions of the smallest integer bounding square of the list of
|
rlm@108
|
557 2D verticies in the form: [x y width height]."
|
rlm@108
|
558 [uv-verts]
|
rlm@108
|
559 (let [xs (map first uv-verts)
|
rlm@108
|
560 ys (map second uv-verts)
|
rlm@108
|
561 x0 (Math/floor (apply min xs))
|
rlm@108
|
562 y0 (Math/floor (apply min ys))
|
rlm@108
|
563 x1 (Math/ceil (apply max xs))
|
rlm@108
|
564 y1 (Math/ceil (apply max ys))]
|
rlm@108
|
565 [x0 y0 (- x1 x0) (- y1 y0)]))
|
rlm@93
|
566
|
rlm@106
|
567 (defn sensors-in-triangle
|
rlm@107
|
568 "Find the locations of the touch sensors within a triangle in both
|
rlm@107
|
569 UV and gemoetry relative coordinates."
|
rlm@107
|
570 [image mesh tri-index]
|
rlm@107
|
571 (let [width (.getWidth image)
|
rlm@108
|
572 height (.getHeight image)
|
rlm@108
|
573 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
|
rlm@108
|
574 bounds (convex-bounds UV-vertex-coords)
|
rlm@108
|
575
|
rlm@108
|
576 cutout-triangle (points->triangle UV-vertex-coords)
|
rlm@108
|
577 UV-sensor-coords
|
rlm@108
|
578 (filter (comp (partial inside-triangle? cutout-triangle)
|
rlm@108
|
579 (fn [[u v]] (Vector3f. u v 0)))
|
rlm@108
|
580 (white-coordinates image bounds))
|
rlm@108
|
581 UV->geometry (triangle-transformation
|
rlm@108
|
582 cutout-triangle
|
rlm@108
|
583 (triangle mesh tri-index))
|
rlm@108
|
584 geometry-sensor-coords
|
rlm@108
|
585 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
|
rlm@108
|
586 UV-sensor-coords)]
|
rlm@108
|
587 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
|
rlm@107
|
588
|
rlm@108
|
589 (defn-memo locate-feelers
|
rlm@94
|
590 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@94
|
591 their positions in geometry-relative coordinates."
|
rlm@94
|
592 [#^Geometry geo]
|
rlm@108
|
593 (let [mesh (.getMesh geo)
|
rlm@108
|
594 num-triangles (.getTriangleCount mesh)]
|
rlm@108
|
595 (if-let [image (tactile-sensor-image geo)]
|
rlm@108
|
596 (map
|
rlm@108
|
597 (partial sensors-in-triangle image mesh)
|
rlm@108
|
598 (range num-triangles))
|
rlm@108
|
599 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
|
rlm@102
|
600
|
rlm@102
|
601 (use 'clojure.contrib.def)
|
rlm@102
|
602
|
rlm@102
|
603 (defn-memo touch-topology [#^Gemoetry geo]
|
rlm@108
|
604 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
|
rlm@108
|
605
|
rlm@108
|
606 (defn-memo feeler-coordinates [#^Geometry geo]
|
rlm@108
|
607 (vec (map :geometry (locate-feelers geo))))
|
rlm@102
|
608
|
rlm@97
|
609 (defn enable-touch [#^Geometry geo]
|
rlm@108
|
610 (let [feeler-coords (feeler-coordinates geo)
|
rlm@96
|
611 tris (triangles geo)
|
rlm@109
|
612 limit 0.1
|
rlm@109
|
613 ;;results (CollisionResults.)
|
rlm@109
|
614 ]
|
rlm@111
|
615 (if (empty? (touch-topology geo))
|
rlm@111
|
616 nil
|
rlm@111
|
617 (fn [node]
|
rlm@111
|
618 (let [sensor-origins
|
rlm@111
|
619 (map
|
rlm@111
|
620 #(map (partial local-to-world geo) %)
|
rlm@111
|
621 feeler-coords)
|
rlm@111
|
622 triangle-normals
|
rlm@111
|
623 (map (partial get-ray-direction geo)
|
rlm@111
|
624 tris)
|
rlm@111
|
625 rays
|
rlm@111
|
626 (flatten
|
rlm@111
|
627 (map (fn [origins norm]
|
rlm@111
|
628 (map #(doto (Ray. % norm)
|
rlm@97
|
629 (.setLimit limit)) origins))
|
rlm@111
|
630 sensor-origins triangle-normals))]
|
rlm@111
|
631 (vector
|
rlm@111
|
632 (touch-topology geo)
|
rlm@111
|
633 (vec
|
rlm@111
|
634 (for [ray rays]
|
rlm@111
|
635 (do
|
rlm@111
|
636 (let [results (CollisionResults.)]
|
rlm@111
|
637 (.collideWith node ray results)
|
rlm@111
|
638 (let [touch-objects
|
rlm@126
|
639 (filter #(not (= geo (.getGeometry %)))
|
rlm@126
|
640 results)]
|
rlm@126
|
641 (- 255
|
rlm@126
|
642 (if (empty? touch-objects) 255
|
rlm@126
|
643 (rem
|
rlm@126
|
644 (int
|
rlm@126
|
645 (* 255 (/ (.getDistance
|
rlm@126
|
646 (first touch-objects)) limit)))
|
rlm@126
|
647 256))))))))))))))
|
rlm@126
|
648
|
rlm@111
|
649
|
rlm@111
|
650 (defn touch [#^Node pieces]
|
rlm@111
|
651 (filter (comp not nil?)
|
rlm@111
|
652 (map enable-touch
|
rlm@111
|
653 (filter #(isa? (class %) Geometry)
|
rlm@111
|
654 (node-seq pieces)))))
|
rlm@94
|
655
|
rlm@109
|
656
|
rlm@111
|
657 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
|
rlm@111
|
658 ;; http://en.wikipedia.org/wiki/Retina
|
rlm@109
|
659
|
rlm@111
|
660 (defn test-eye []
|
rlm@117
|
661 (.getChild
|
rlm@117
|
662 (.getChild (worm-model) "eyes")
|
rlm@117
|
663 "eye"))
|
rlm@111
|
664
|
rlm@111
|
665
|
rlm@111
|
666 (defn retina-sensor-image
|
rlm@111
|
667 "Return a map of pixel selection functions to BufferedImages
|
rlm@111
|
668 describing the distribution of light-sensitive components on this
|
rlm@111
|
669 geometry's surface. Each function creates an integer from the rgb
|
rlm@111
|
670 values found in the pixel. :red, :green, :blue, :gray are already
|
rlm@111
|
671 defined as extracting the red green blue and average components
|
rlm@111
|
672 respectively."
|
rlm@117
|
673 [#^Spatial eye]
|
rlm@111
|
674 (if-let [eye-map (meta-data eye "eye")]
|
rlm@111
|
675 (map-vals
|
rlm@111
|
676 #(ImageToAwt/convert
|
rlm@111
|
677 (.getImage (.loadTexture (asset-manager) %))
|
rlm@111
|
678 false false 0)
|
rlm@120
|
679 (eval (read-string eye-map)))))
|
rlm@111
|
680
|
rlm@117
|
681 (defn eye-dimensions
|
rlm@117
|
682 "returns the width and height specified in the metadata of the eye"
|
rlm@117
|
683 [#^Spatial eye]
|
rlm@117
|
684 (let [dimensions
|
rlm@117
|
685 (map #(vector (.getWidth %) (.getHeight %))
|
rlm@117
|
686 (vals (retina-sensor-image eye)))]
|
rlm@117
|
687 [(apply max (map first dimensions))
|
rlm@117
|
688 (apply max (map second dimensions))]))
|
rlm@117
|
689
|
rlm@116
|
690 (defn creature-eyes
|
rlm@116
|
691 "The eye nodes which are children of the \"eyes\" node in the
|
rlm@116
|
692 creature."
|
rlm@116
|
693 [#^Node creature]
|
rlm@116
|
694 (if-let [eye-node (.getChild creature "eyes")]
|
rlm@116
|
695 (seq (.getChildren eye-node))
|
rlm@116
|
696 (do (println-repl "could not find eyes node") [])))
|
rlm@111
|
697
|
rlm@123
|
698 ;; Here's how vision will work.
|
rlm@112
|
699
|
rlm@123
|
700 ;; Make the continuation in scene-processor take FrameBuffer,
|
rlm@123
|
701 ;; byte-buffer, BufferedImage already sized to the correct
|
rlm@123
|
702 ;; dimensions. the continuation will decide wether to "mix" them
|
rlm@123
|
703 ;; into the BufferedImage, lazily ignore them, or mix them halfway
|
rlm@123
|
704 ;; and call c/graphics card routines.
|
rlm@112
|
705
|
rlm@123
|
706 ;; (vision creature) will take an optional :skip argument which will
|
rlm@123
|
707 ;; inform the continuations in scene processor to skip the given
|
rlm@123
|
708 ;; number of cycles; 0 means that no cycles will be skipped.
|
rlm@112
|
709
|
rlm@123
|
710 ;; (vision creature) will return [init-functions sensor-functions].
|
rlm@123
|
711 ;; The init-functions are each single-arg functions that take the
|
rlm@123
|
712 ;; world and register the cameras and must each be called before the
|
rlm@123
|
713 ;; corresponding sensor-functions. Each init-function returns the
|
rlm@123
|
714 ;; viewport for that eye which can be manipulated, saved, etc. Each
|
rlm@123
|
715 ;; sensor-function is a thunk and will return data in the same
|
rlm@123
|
716 ;; format as the tactile-sensor functions; the structure is
|
rlm@123
|
717 ;; [topology, sensor-data]. Internally, these sensor-functions
|
rlm@123
|
718 ;; maintain a reference to sensor-data which is periodically updated
|
rlm@123
|
719 ;; by the continuation function established by its init-function.
|
rlm@123
|
720 ;; They can be queried every cycle, but their information may not
|
rlm@123
|
721 ;; necessairly be different every cycle.
|
rlm@112
|
722
|
rlm@123
|
723 ;; Each eye in the creature in blender will work the same way as
|
rlm@123
|
724 ;; joints -- a zero dimensional object with no geometry whose local
|
rlm@123
|
725 ;; coordinate system determines the orientation of the resulting
|
rlm@123
|
726 ;; eye. All eyes will have a parent named "eyes" just as all joints
|
rlm@123
|
727 ;; have a parent named "joints". The resulting camera will be a
|
rlm@123
|
728 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
|
rlm@123
|
729 ;; the eye marker. The eye marker will contain the metadata for the
|
rlm@123
|
730 ;; eye, and will be moved by it's bound geometry. The dimensions of
|
rlm@123
|
731 ;; the eye's camera are equal to the dimensions of the eye's "UV"
|
rlm@123
|
732 ;; map.
|
rlm@116
|
733
|
rlm@123
|
734
|
rlm@123
|
735 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@123
|
736
|
rlm@123
|
737 ;; Ears work the same way as vision.
|
rlm@123
|
738
|
rlm@123
|
739 ;; (hearing creature) will return [init-functions
|
rlm@123
|
740 ;; sensor-functions]. The init functions each take the world and
|
rlm@123
|
741 ;; register a SoundProcessor that does foureier transforms on the
|
rlm@123
|
742 ;; incommong sound data, making it available to each sensor function.
|
rlm@123
|
743
|
rlm@123
|
744 (defn creature-ears
|
rlm@123
|
745 "The ear nodes which are children of the \"ears\" node in the
|
rlm@123
|
746 creature."
|
rlm@123
|
747 [#^Node creature]
|
rlm@123
|
748 (if-let [ear-node (.getChild creature "ears")]
|
rlm@123
|
749 (seq (.getChildren ear-node))
|
rlm@123
|
750 (do (println-repl "could not find ears node") [])))
|
rlm@123
|
751
|
rlm@123
|
752 (defn closest-node
|
rlm@123
|
753 "The closest object in creature to the given node."
|
rlm@116
|
754 [#^Node creature #^Node eye]
|
rlm@116
|
755 (loop [radius (float 0.01)]
|
rlm@116
|
756 (let [results (CollisionResults.)]
|
rlm@116
|
757 (.collideWith
|
rlm@116
|
758 creature
|
rlm@116
|
759 (BoundingBox. (.getWorldTranslation eye)
|
rlm@116
|
760 radius radius radius)
|
rlm@116
|
761 results)
|
rlm@116
|
762 (if-let [target (first results)]
|
rlm@116
|
763 (.getGeometry target)
|
rlm@116
|
764 (recur (float (* 2 radius)))))))
|
rlm@116
|
765
|
rlm@123
|
766 (defn bind-sense
|
rlm@123
|
767 "Bind the sense to the Spatial such that it will maintain its
|
rlm@117
|
768 current position relative to the Spatial no matter how the spatial
|
rlm@123
|
769 moves. 'sense can be either a Camera or Listener object."
|
rlm@123
|
770 [#^Spatial obj sense]
|
rlm@123
|
771 (let [sense-offset (.subtract (.getLocation sense)
|
rlm@123
|
772 (.getWorldTranslation obj))
|
rlm@123
|
773 initial-sense-rotation (Quaternion. (.getRotation sense))
|
rlm@117
|
774 base-anti-rotation (.inverse (.getWorldRotation obj))]
|
rlm@117
|
775 (.addControl
|
rlm@117
|
776 obj
|
rlm@117
|
777 (proxy [AbstractControl] []
|
rlm@117
|
778 (controlUpdate [tpf]
|
rlm@117
|
779 (let [total-rotation
|
rlm@117
|
780 (.mult base-anti-rotation (.getWorldRotation obj))]
|
rlm@123
|
781 (.setLocation sense
|
rlm@117
|
782 (.add
|
rlm@123
|
783 (.mult total-rotation sense-offset)
|
rlm@117
|
784 (.getWorldTranslation obj)))
|
rlm@123
|
785 (.setRotation sense
|
rlm@123
|
786 (.mult total-rotation initial-sense-rotation))))
|
rlm@117
|
787 (controlRender [_ _])))))
|
rlm@117
|
788
|
rlm@117
|
789
|
rlm@123
|
790 (defn update-listener-velocity
|
rlm@123
|
791 "Update the listener's velocity every update loop."
|
rlm@123
|
792 [#^Spatial obj #^Listener lis]
|
rlm@123
|
793 (let [old-position (atom (.getLocation lis))]
|
rlm@123
|
794 (.addControl
|
rlm@123
|
795 obj
|
rlm@123
|
796 (proxy [AbstractControl] []
|
rlm@123
|
797 (controlUpdate [tpf]
|
rlm@123
|
798 (let [new-position (.getLocation lis)]
|
rlm@123
|
799 (.setVelocity
|
rlm@123
|
800 lis
|
rlm@123
|
801 (.mult (.subtract new-position @old-position)
|
rlm@123
|
802 (float (/ tpf))))
|
rlm@123
|
803 (reset! old-position new-position)))
|
rlm@123
|
804 (controlRender [_ _])))))
|
rlm@123
|
805
|
rlm@123
|
806 (import com.aurellem.capture.audio.AudioSendRenderer)
|
rlm@123
|
807
|
rlm@123
|
808 (defn attach-ear
|
rlm@123
|
809 [#^Application world #^Node creature #^Spatial ear continuation]
|
rlm@123
|
810 (let [target (closest-node creature ear)
|
rlm@123
|
811 lis (Listener.)
|
rlm@123
|
812 audio-renderer (.getAudioRenderer world)
|
rlm@123
|
813 sp (sound-processor continuation)]
|
rlm@123
|
814 (.setLocation lis (.getWorldTranslation ear))
|
rlm@123
|
815 (.setRotation lis (.getWorldRotation ear))
|
rlm@123
|
816 (bind-sense target lis)
|
rlm@123
|
817 (update-listener-velocity target lis)
|
rlm@123
|
818 (.addListener audio-renderer lis)
|
rlm@123
|
819 (.registerSoundProcessor audio-renderer lis sp)))
|
rlm@123
|
820
|
rlm@123
|
821 (defn enable-hearing
|
rlm@123
|
822 [#^Node creature #^Spatial ear]
|
rlm@123
|
823 (let [hearing-data (atom [])]
|
rlm@123
|
824 [(fn [world]
|
rlm@123
|
825 (attach-ear world creature ear
|
rlm@123
|
826 (fn [data]
|
rlm@123
|
827 (reset! hearing-data (vec data)))))
|
rlm@123
|
828 [(fn []
|
rlm@123
|
829 (let [data @hearing-data
|
rlm@123
|
830 topology
|
rlm@123
|
831 (vec (map #(vector % 0) (range 0 (count data))))
|
rlm@123
|
832 scaled-data
|
rlm@123
|
833 (vec
|
rlm@123
|
834 (map
|
rlm@123
|
835 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)
|
rlm@123
|
836 data))]
|
rlm@123
|
837 [topology scaled-data]))
|
rlm@123
|
838 ]]))
|
rlm@123
|
839
|
rlm@123
|
840 (defn hearing
|
rlm@123
|
841 [#^Node creature]
|
rlm@123
|
842 (reduce
|
rlm@123
|
843 (fn [[init-a senses-a]
|
rlm@123
|
844 [init-b senses-b]]
|
rlm@123
|
845 [(conj init-a init-b)
|
rlm@123
|
846 (into senses-a senses-b)])
|
rlm@123
|
847 [[][]]
|
rlm@123
|
848 (for [ear (creature-ears creature)]
|
rlm@123
|
849 (enable-hearing creature ear))))
|
rlm@123
|
850
|
rlm@118
|
851 (defn attach-eye
|
rlm@118
|
852 "Attach a Camera to the appropiate area and return the Camera."
|
rlm@118
|
853 [#^Node creature #^Spatial eye]
|
rlm@123
|
854 (let [target (closest-node creature eye)
|
rlm@118
|
855 [cam-width cam-height] (eye-dimensions eye)
|
rlm@118
|
856 cam (Camera. cam-width cam-height)]
|
rlm@118
|
857 (.setLocation cam (.getWorldTranslation eye))
|
rlm@118
|
858 (.setRotation cam (.getWorldRotation eye))
|
rlm@119
|
859 (.setFrustumPerspective
|
rlm@119
|
860 cam 45 (/ (.getWidth cam) (.getHeight cam))
|
rlm@119
|
861 1 1000)
|
rlm@123
|
862 (bind-sense target cam)
|
rlm@118
|
863 cam))
|
rlm@118
|
864
|
rlm@118
|
865 (def presets
|
rlm@121
|
866 {:all 0xFFFFFF
|
rlm@119
|
867 :red 0xFF0000
|
rlm@119
|
868 :blue 0x0000FF
|
rlm@119
|
869 :green 0x00FF00})
|
rlm@119
|
870
|
rlm@118
|
871 (defn enable-vision
|
rlm@118
|
872 "return [init-function sensor-functions] for a particular eye"
|
rlm@118
|
873 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
|
rlm@118
|
874 (let [retinal-map (retina-sensor-image eye)
|
rlm@123
|
875 camera (attach-eye creature eye)
|
rlm@123
|
876 vision-image
|
rlm@123
|
877 (atom
|
rlm@123
|
878 (BufferedImage. (.getWidth camera)
|
rlm@123
|
879 (.getHeight camera)
|
rlm@123
|
880 BufferedImage/TYPE_BYTE_BINARY))]
|
rlm@123
|
881 [(fn [world]
|
rlm@123
|
882 (add-eye
|
rlm@123
|
883 world camera
|
rlm@123
|
884 (let [counter (atom 0)]
|
rlm@123
|
885 (fn [r fb bb bi]
|
rlm@123
|
886 (if (zero? (rem (swap! counter inc) (inc skip)))
|
rlm@123
|
887 (reset! vision-image (BufferedImage! r fb bb bi)))))))
|
rlm@123
|
888 (vec
|
rlm@123
|
889 (map
|
rlm@123
|
890 (fn [[key image]]
|
rlm@123
|
891 (let [whites (white-coordinates image)
|
rlm@123
|
892 topology (vec (collapse whites))
|
rlm@123
|
893 mask (presets key)]
|
rlm@123
|
894 (fn []
|
rlm@123
|
895 (vector
|
rlm@123
|
896 topology
|
rlm@123
|
897 (vec
|
rlm@123
|
898 (for [[x y] whites]
|
rlm@123
|
899 (bit-and
|
rlm@123
|
900 mask (.getRGB @vision-image x y))))))))
|
rlm@123
|
901 retinal-map))]))
|
rlm@118
|
902
|
rlm@116
|
903 (defn vision
|
rlm@121
|
904 [#^Node creature & {skip :skip :or {skip 0}}]
|
rlm@121
|
905 (reduce
|
rlm@121
|
906 (fn [[init-a senses-a]
|
rlm@121
|
907 [init-b senses-b]]
|
rlm@121
|
908 [(conj init-a init-b)
|
rlm@121
|
909 (into senses-a senses-b)])
|
rlm@121
|
910 [[][]]
|
rlm@121
|
911 (for [eye (creature-eyes creature)]
|
rlm@121
|
912 (enable-vision creature eye))))
|
rlm@121
|
913
|
rlm@116
|
914
|
rlm@116
|
915 (defn blender-creature
|
rlm@116
|
916 "Return a creature with all joints in place."
|
rlm@116
|
917 [blender-path]
|
rlm@116
|
918 (let [model (load-blender-model blender-path)
|
rlm@116
|
919 joints
|
rlm@116
|
920 (if-let [joint-node (.getChild model "joints")]
|
rlm@116
|
921 (seq (.getChildren joint-node))
|
rlm@116
|
922 (do (println-repl "could not find joints node") []))]
|
rlm@116
|
923 (assemble-creature model joints)))
|
rlm@116
|
924
|
rlm@126
|
925 (defn gray-scale [num]
|
rlm@126
|
926 (+ num
|
rlm@126
|
927 (bit-shift-left num 8)
|
rlm@126
|
928 (bit-shift-left num 16)))
|
rlm@126
|
929
|
rlm@103
|
930 (defn debug-window
|
rlm@103
|
931 "creates function that offers a debug view of sensor data"
|
rlm@103
|
932 []
|
rlm@103
|
933 (let [vi (view-image)]
|
rlm@103
|
934 (fn
|
rlm@103
|
935 [[coords sensor-data]]
|
rlm@103
|
936 (let [image (points->image coords)]
|
rlm@103
|
937 (dorun
|
rlm@103
|
938 (for [i (range (count coords))]
|
rlm@103
|
939 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@126
|
940 (gray-scale (sensor-data i)))))
|
rlm@126
|
941
|
rlm@126
|
942
|
rlm@103
|
943 (vi image)))))
|
rlm@103
|
944
|
rlm@118
|
945 (defn debug-vision-window
|
rlm@118
|
946 "creates function that offers a debug view of sensor data"
|
rlm@118
|
947 []
|
rlm@118
|
948 (let [vi (view-image)]
|
rlm@118
|
949 (fn
|
rlm@118
|
950 [[coords sensor-data]]
|
rlm@118
|
951 (let [image (points->image coords)]
|
rlm@118
|
952 (dorun
|
rlm@118
|
953 (for [i (range (count coords))]
|
rlm@118
|
954 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@118
|
955 (sensor-data i))))
|
rlm@118
|
956 (vi image)))))
|
rlm@118
|
957
|
rlm@123
|
958 (defn debug-hearing-window
|
rlm@123
|
959 "view audio data"
|
rlm@123
|
960 [height]
|
rlm@123
|
961 (let [vi (view-image)]
|
rlm@123
|
962 (fn [[coords sensor-data]]
|
rlm@123
|
963 (let [image (BufferedImage. (count coords) height
|
rlm@123
|
964 BufferedImage/TYPE_INT_RGB)]
|
rlm@123
|
965 (dorun
|
rlm@123
|
966 (for [x (range (count coords))]
|
rlm@123
|
967 (dorun
|
rlm@123
|
968 (for [y (range height)]
|
rlm@123
|
969 (let [raw-sensor (sensor-data x)]
|
rlm@126
|
970 (.setRGB image x y (gray-scale raw-sensor)))))))
|
rlm@126
|
971
|
rlm@123
|
972 (vi image)))))
|
rlm@123
|
973
|
rlm@123
|
974
|
rlm@123
|
975
|
rlm@106
|
976 ;;(defn test-touch [world creature]
|
rlm@83
|
977
|
rlm@78
|
978
|
rlm@123
|
979
|
rlm@123
|
980
|
rlm@123
|
981
|
rlm@123
|
982
|
rlm@106
|
983 (defn test-creature [thing]
|
rlm@106
|
984 (let [x-axis
|
rlm@106
|
985 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
|
rlm@106
|
986 y-axis
|
rlm@106
|
987 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
|
rlm@106
|
988 z-axis
|
rlm@106
|
989 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
|
rlm@106
|
990 creature (blender-creature thing)
|
rlm@106
|
991 touch-nerves (touch creature)
|
rlm@106
|
992 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
|
rlm@121
|
993 [init-vision-fns vision-data] (vision creature)
|
rlm@121
|
994 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
|
rlm@118
|
995 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
|
rlm@123
|
996 [init-hearing-fns hearing-senses] (hearing creature)
|
rlm@123
|
997 hearing-windows (map (fn [_] (debug-hearing-window 50))
|
rlm@123
|
998 hearing-senses)
|
rlm@124
|
999 bell (AudioNode. (asset-manager)
|
rlm@124
|
1000 "Sounds/ear-and-eye.wav" false)
|
rlm@123
|
1001 ;; dream
|
rlm@123
|
1002
|
rlm@106
|
1003 ]
|
rlm@106
|
1004 (world
|
rlm@106
|
1005 (nodify [creature
|
rlm@106
|
1006 (box 10 2 10 :position (Vector3f. 0 -9 0)
|
rlm@106
|
1007 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
1008 x-axis y-axis z-axis
|
rlm@118
|
1009 me
|
rlm@106
|
1010 ])
|
rlm@123
|
1011 (merge standard-debug-controls
|
rlm@123
|
1012 {"key-return"
|
rlm@123
|
1013 (fn [_ value]
|
rlm@123
|
1014 (if value
|
rlm@123
|
1015 (do
|
rlm@123
|
1016 (println-repl "play-sound")
|
rlm@124
|
1017 (.play bell))))})
|
rlm@106
|
1018 (fn [world]
|
rlm@106
|
1019 (light-up-everything world)
|
rlm@106
|
1020 (enable-debug world)
|
rlm@122
|
1021 (dorun (map #(% world) init-vision-fns))
|
rlm@123
|
1022 (dorun (map #(% world) init-hearing-fns))
|
rlm@118
|
1023
|
rlm@118
|
1024 (add-eye world
|
rlm@118
|
1025 (attach-eye creature (test-eye))
|
rlm@118
|
1026 (comp (view-image) BufferedImage!))
|
rlm@118
|
1027
|
rlm@118
|
1028 (add-eye world (.getCamera world) no-op)
|
rlm@118
|
1029
|
rlm@106
|
1030 ;;(com.aurellem.capture.Capture/captureVideo
|
rlm@106
|
1031 ;; world (file-str "/home/r/proj/ai-videos/hand"))
|
rlm@110
|
1032 ;;(.setTimer world (RatchetTimer. 60))
|
rlm@119
|
1033 (speed-up world)
|
rlm@106
|
1034 ;;(set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
1035 )
|
rlm@106
|
1036 (fn [world tpf]
|
rlm@109
|
1037 ;;(dorun
|
rlm@109
|
1038 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
|
rlm@123
|
1039
|
rlm@123
|
1040
|
rlm@123
|
1041
|
rlm@106
|
1042 (dorun
|
rlm@109
|
1043 (map #(%1 (%2 (.getRootNode world)))
|
rlm@121
|
1044 touch-debug-windows touch-nerves))
|
rlm@123
|
1045
|
rlm@121
|
1046 (dorun
|
rlm@121
|
1047 (map #(%1 (%2))
|
rlm@121
|
1048 vision-debug vision-data))
|
rlm@123
|
1049 (dorun
|
rlm@123
|
1050 (map #(%1 (%2)) hearing-windows hearing-senses))
|
rlm@123
|
1051
|
rlm@123
|
1052
|
rlm@118
|
1053 ;;(println-repl (vision-data))
|
rlm@118
|
1054 (.setLocalTranslation me (.getLocation (.getCamera world)))
|
rlm@118
|
1055
|
rlm@121
|
1056
|
rlm@106
|
1057 )
|
rlm@106
|
1058 ;;(let [timer (atom 0)]
|
rlm@106
|
1059 ;; (fn [_ _]
|
rlm@106
|
1060 ;; (swap! timer inc)
|
rlm@106
|
1061 ;; (if (= (rem @timer 60) 0)
|
rlm@106
|
1062 ;; (println-repl (float (/ @timer 60))))))
|
rlm@106
|
1063 )))
|
rlm@83
|
1064
|
rlm@109
|
1065
|
rlm@109
|
1066
|
rlm@109
|
1067
|
rlm@109
|
1068
|
rlm@109
|
1069
|
rlm@109
|
1070
|
rlm@109
|
1071
|
rlm@109
|
1072
|
rlm@109
|
1073 ;;; experiments in collisions
|
rlm@109
|
1074
|
rlm@109
|
1075
|
rlm@109
|
1076
|
rlm@109
|
1077 (defn collision-test []
|
rlm@110
|
1078 (let [b-radius 1
|
rlm@110
|
1079 b-position (Vector3f. 0 0 0)
|
rlm@109
|
1080 obj-b (box 1 1 1 :color ColorRGBA/Blue
|
rlm@109
|
1081 :position b-position
|
rlm@110
|
1082 :mass 0)
|
rlm@110
|
1083 node (nodify [obj-b])
|
rlm@110
|
1084 bounds-b
|
rlm@110
|
1085 (doto (Picture.)
|
rlm@110
|
1086 (.setHeight 50)
|
rlm@110
|
1087 (.setWidth 50)
|
rlm@110
|
1088 (.setImage (asset-manager)
|
rlm@110
|
1089 "Models/creature1/hand.png"
|
rlm@110
|
1090 false
|
rlm@110
|
1091 ))
|
rlm@110
|
1092
|
rlm@110
|
1093 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
|
rlm@110
|
1094
|
rlm@110
|
1095 collisions
|
rlm@110
|
1096 (let [cr (CollisionResults.)]
|
rlm@110
|
1097 (.collideWith node bounds-b cr)
|
rlm@110
|
1098 (println (map #(.getContactPoint %) cr))
|
rlm@110
|
1099 cr)
|
rlm@110
|
1100
|
rlm@110
|
1101 ;;collision-points
|
rlm@110
|
1102 ;;(map #(sphere 0.1 :position (.getContactPoint %))
|
rlm@110
|
1103 ;; collisions)
|
rlm@110
|
1104
|
rlm@110
|
1105 ;;node (nodify (conj collision-points obj-b))
|
rlm@110
|
1106
|
rlm@109
|
1107 sim
|
rlm@109
|
1108 (world node
|
rlm@110
|
1109 {"key-space"
|
rlm@110
|
1110 (fn [_ value]
|
rlm@110
|
1111 (if value
|
rlm@110
|
1112 (let [cr (CollisionResults.)]
|
rlm@110
|
1113 (.collideWith node bounds-b cr)
|
rlm@110
|
1114 (println-repl (map #(.getContactPoint %) cr))
|
rlm@110
|
1115 cr)))}
|
rlm@109
|
1116 no-op
|
rlm@109
|
1117 no-op)
|
rlm@109
|
1118
|
rlm@109
|
1119 ]
|
rlm@110
|
1120 sim
|
rlm@109
|
1121
|
rlm@109
|
1122 ))
|
rlm@109
|
1123
|
rlm@116
|
1124
|
rlm@116
|
1125 ;; the camera will stay in its initial position/rotation with relation
|
rlm@116
|
1126 ;; to the spatial.
|
rlm@116
|
1127
|
rlm@116
|
1128
|
rlm@117
|
1129 (defn follow-test
|
rlm@117
|
1130 "show a camera that stays in the same relative position to a blue cube."
|
rlm@117
|
1131 []
|
rlm@116
|
1132 (let [camera-pos (Vector3f. 0 30 0)
|
rlm@116
|
1133 rock (box 1 1 1 :color ColorRGBA/Blue
|
rlm@116
|
1134 :position (Vector3f. 0 10 0)
|
rlm@116
|
1135 :mass 30
|
rlm@116
|
1136 )
|
rlm@118
|
1137 rot (.getWorldRotation rock)
|
rlm@116
|
1138
|
rlm@116
|
1139 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
|
rlm@116
|
1140 :position (Vector3f. 0 -3 0))]
|
rlm@116
|
1141
|
rlm@116
|
1142 (world
|
rlm@116
|
1143 (nodify [rock table])
|
rlm@116
|
1144 standard-debug-controls
|
rlm@116
|
1145 (fn [world]
|
rlm@116
|
1146 (let
|
rlm@116
|
1147 [cam (doto (.clone (.getCamera world))
|
rlm@116
|
1148 (.setLocation camera-pos)
|
rlm@116
|
1149 (.lookAt Vector3f/ZERO
|
rlm@116
|
1150 Vector3f/UNIT_X))]
|
rlm@123
|
1151 (bind-sense rock cam)
|
rlm@116
|
1152
|
rlm@116
|
1153 (.setTimer world (RatchetTimer. 60))
|
rlm@116
|
1154 (add-eye world cam (comp (view-image) BufferedImage!))
|
rlm@116
|
1155 (add-eye world (.getCamera world) no-op))
|
rlm@116
|
1156 )
|
rlm@118
|
1157 (fn [_ _] (println-repl rot)))))
|
rlm@116
|
1158
|
rlm@118
|
1159
|
rlm@123
|
1160
|
rlm@87
|
1161 #+end_src
|
rlm@83
|
1162
|
rlm@87
|
1163 #+results: body-1
|
rlm@109
|
1164 : #'cortex.silly/test-creature
|
rlm@78
|
1165
|
rlm@78
|
1166
|
rlm@78
|
1167 * COMMENT purgatory
|
rlm@78
|
1168 #+begin_src clojure
|
rlm@77
|
1169 (defn bullet-trans []
|
rlm@77
|
1170 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
1171 :position (Vector3f. -10 5 0))
|
rlm@77
|
1172 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
1173 :position (Vector3f. -10 -5 0)
|
rlm@77
|
1174 :mass 0)
|
rlm@77
|
1175 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
1176 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
1177 swivel
|
rlm@77
|
1178 (.toRotationMatrix
|
rlm@77
|
1179 (doto (Quaternion.)
|
rlm@77
|
1180 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
1181 Vector3f/UNIT_X)))]
|
rlm@77
|
1182 (doto
|
rlm@77
|
1183 (ConeJoint.
|
rlm@77
|
1184 control-a control-b
|
rlm@77
|
1185 (Vector3f. 0 5 0)
|
rlm@77
|
1186 (Vector3f. 0 -5 0)
|
rlm@77
|
1187 swivel swivel)
|
rlm@77
|
1188 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
1189 (/ Math/PI 4)
|
rlm@77
|
1190 (* Math/PI 0.8)))
|
rlm@77
|
1191 (world (nodify
|
rlm@77
|
1192 [obj-a obj-b])
|
rlm@77
|
1193 standard-debug-controls
|
rlm@77
|
1194 enable-debug
|
rlm@77
|
1195 no-op)))
|
rlm@74
|
1196
|
rlm@74
|
1197
|
rlm@77
|
1198 (defn bullet-trans* []
|
rlm@77
|
1199 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
1200 :position (Vector3f. 5 0 0)
|
rlm@77
|
1201 :mass 90)
|
rlm@77
|
1202 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
1203 :position (Vector3f. -5 0 0)
|
rlm@77
|
1204 :mass 0)
|
rlm@77
|
1205 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
1206 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
1207 move-up? (atom nil)
|
rlm@77
|
1208 move-down? (atom nil)
|
rlm@77
|
1209 move-left? (atom nil)
|
rlm@77
|
1210 move-right? (atom nil)
|
rlm@77
|
1211 roll-left? (atom nil)
|
rlm@77
|
1212 roll-right? (atom nil)
|
rlm@77
|
1213 force 100
|
rlm@77
|
1214 swivel
|
rlm@77
|
1215 (.toRotationMatrix
|
rlm@77
|
1216 (doto (Quaternion.)
|
rlm@77
|
1217 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
1218 Vector3f/UNIT_X)))
|
rlm@77
|
1219 x-move
|
rlm@77
|
1220 (doto (Matrix3f.)
|
rlm@77
|
1221 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
1222 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
1223
|
rlm@77
|
1224 timer (atom 0)]
|
rlm@77
|
1225 (doto
|
rlm@77
|
1226 (ConeJoint.
|
rlm@77
|
1227 control-a control-b
|
rlm@77
|
1228 (Vector3f. -8 0 0)
|
rlm@77
|
1229 (Vector3f. 2 0 0)
|
rlm@77
|
1230 ;;swivel swivel
|
rlm@77
|
1231 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
1232 x-move Matrix3f/IDENTITY
|
rlm@77
|
1233 )
|
rlm@77
|
1234 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
1235 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
1236 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
1237 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
1238 (world (nodify
|
rlm@77
|
1239 [obj-a obj-b])
|
rlm@77
|
1240 (merge standard-debug-controls
|
rlm@77
|
1241 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
1242 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
1243 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
1244 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
1245 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
1246 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
1247
|
rlm@77
|
1248 (fn [world]
|
rlm@77
|
1249 (enable-debug world)
|
rlm@77
|
1250 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
1251 )
|
rlm@77
|
1252
|
rlm@77
|
1253 (fn [world _]
|
rlm@77
|
1254
|
rlm@77
|
1255 (if @move-up?
|
rlm@77
|
1256 (.applyForce control-a
|
rlm@77
|
1257 (Vector3f. force 0 0)
|
rlm@77
|
1258 (Vector3f. 0 0 0)))
|
rlm@77
|
1259 (if @move-down?
|
rlm@77
|
1260 (.applyForce control-a
|
rlm@77
|
1261 (Vector3f. (- force) 0 0)
|
rlm@77
|
1262 (Vector3f. 0 0 0)))
|
rlm@77
|
1263 (if @move-left?
|
rlm@77
|
1264 (.applyForce control-a
|
rlm@77
|
1265 (Vector3f. 0 force 0)
|
rlm@77
|
1266 (Vector3f. 0 0 0)))
|
rlm@77
|
1267 (if @move-right?
|
rlm@77
|
1268 (.applyForce control-a
|
rlm@77
|
1269 (Vector3f. 0 (- force) 0)
|
rlm@77
|
1270 (Vector3f. 0 0 0)))
|
rlm@77
|
1271
|
rlm@77
|
1272 (if @roll-left?
|
rlm@77
|
1273 (.applyForce control-a
|
rlm@77
|
1274 (Vector3f. 0 0 force)
|
rlm@77
|
1275 (Vector3f. 0 0 0)))
|
rlm@77
|
1276 (if @roll-right?
|
rlm@77
|
1277 (.applyForce control-a
|
rlm@77
|
1278 (Vector3f. 0 0 (- force))
|
rlm@77
|
1279 (Vector3f. 0 0 0)))
|
rlm@77
|
1280
|
rlm@77
|
1281 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
1282 (.attachChild
|
rlm@77
|
1283 (.getRootNode world)
|
rlm@77
|
1284 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
1285 :physical? false :position
|
rlm@77
|
1286 (.getWorldTranslation obj-a)))))
|
rlm@77
|
1287 )
|
rlm@77
|
1288 ))
|
rlm@77
|
1289
|
rlm@94
|
1290 (defn transform-trianglesdsd
|
rlm@94
|
1291 "Transform that converts each vertex in the first triangle
|
rlm@94
|
1292 into the corresponding vertex in the second triangle."
|
rlm@94
|
1293 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
1294 (let [in [(.get1 tri-1)
|
rlm@94
|
1295 (.get2 tri-1)
|
rlm@94
|
1296 (.get3 tri-1)]
|
rlm@94
|
1297 out [(.get1 tri-2)
|
rlm@94
|
1298 (.get2 tri-2)
|
rlm@94
|
1299 (.get3 tri-2)]]
|
rlm@94
|
1300 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
|
rlm@94
|
1301 in* [(.mult translate (in 0))
|
rlm@94
|
1302 (.mult translate (in 1))
|
rlm@94
|
1303 (.mult translate (in 2))]
|
rlm@94
|
1304 final-translation
|
rlm@94
|
1305 (doto (Matrix4f.)
|
rlm@94
|
1306 (.setTranslation (out 1)))
|
rlm@94
|
1307
|
rlm@94
|
1308 rotate-1
|
rlm@94
|
1309 (doto (Matrix3f.)
|
rlm@94
|
1310 (.fromStartEndVectors
|
rlm@94
|
1311 (.normalize
|
rlm@94
|
1312 (.subtract
|
rlm@94
|
1313 (in* 1) (in* 0)))
|
rlm@94
|
1314 (.normalize
|
rlm@94
|
1315 (.subtract
|
rlm@94
|
1316 (out 1) (out 0)))))
|
rlm@94
|
1317 in** [(.mult rotate-1 (in* 0))
|
rlm@94
|
1318 (.mult rotate-1 (in* 1))
|
rlm@94
|
1319 (.mult rotate-1 (in* 2))]
|
rlm@94
|
1320 scale-factor-1
|
rlm@94
|
1321 (.mult
|
rlm@94
|
1322 (.normalize
|
rlm@94
|
1323 (.subtract
|
rlm@94
|
1324 (out 1)
|
rlm@94
|
1325 (out 0)))
|
rlm@94
|
1326 (/ (.length
|
rlm@94
|
1327 (.subtract (out 1)
|
rlm@94
|
1328 (out 0)))
|
rlm@94
|
1329 (.length
|
rlm@94
|
1330 (.subtract (in** 1)
|
rlm@94
|
1331 (in** 0)))))
|
rlm@94
|
1332 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
|
rlm@94
|
1333 in*** [(.mult scale-1 (in** 0))
|
rlm@94
|
1334 (.mult scale-1 (in** 1))
|
rlm@94
|
1335 (.mult scale-1 (in** 2))]
|
rlm@94
|
1336
|
rlm@94
|
1337
|
rlm@94
|
1338
|
rlm@94
|
1339
|
rlm@94
|
1340
|
rlm@94
|
1341 ]
|
rlm@94
|
1342
|
rlm@94
|
1343 (dorun (map println in))
|
rlm@94
|
1344 (println)
|
rlm@94
|
1345 (dorun (map println in*))
|
rlm@94
|
1346 (println)
|
rlm@94
|
1347 (dorun (map println in**))
|
rlm@94
|
1348 (println)
|
rlm@94
|
1349 (dorun (map println in***))
|
rlm@94
|
1350 (println)
|
rlm@94
|
1351
|
rlm@99
|
1352 ))))
|
rlm@94
|
1353
|
rlm@94
|
1354
|
rlm@106
|
1355 (defn world-setup [joint]
|
rlm@106
|
1356 (let [joint-position (Vector3f. 0 0 0)
|
rlm@106
|
1357 joint-rotation
|
rlm@106
|
1358 (.toRotationMatrix
|
rlm@106
|
1359 (.mult
|
rlm@106
|
1360 (doto (Quaternion.)
|
rlm@106
|
1361 (.fromAngleAxis
|
rlm@106
|
1362 (* 1 (/ Math/PI 4))
|
rlm@106
|
1363 (Vector3f. -1 0 0)))
|
rlm@106
|
1364 (doto (Quaternion.)
|
rlm@106
|
1365 (.fromAngleAxis
|
rlm@106
|
1366 (* 1 (/ Math/PI 2))
|
rlm@106
|
1367 (Vector3f. 0 0 1)))))
|
rlm@106
|
1368 top-position (.mult joint-rotation (Vector3f. 8 0 0))
|
rlm@106
|
1369
|
rlm@106
|
1370 origin (doto
|
rlm@106
|
1371 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
|
rlm@106
|
1372 :position top-position))
|
rlm@106
|
1373 top (doto
|
rlm@106
|
1374 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@106
|
1375 :position top-position)
|
rlm@106
|
1376
|
rlm@106
|
1377 (.addControl
|
rlm@106
|
1378 (RigidBodyControl.
|
rlm@106
|
1379 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
|
rlm@106
|
1380 bottom (doto
|
rlm@106
|
1381 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@106
|
1382 :position (Vector3f. 0 0 0))
|
rlm@106
|
1383 (.addControl
|
rlm@106
|
1384 (RigidBodyControl.
|
rlm@106
|
1385 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@106
|
1386 table (box 10 2 10 :position (Vector3f. 0 -20 0)
|
rlm@106
|
1387 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
1388 a (.getControl top RigidBodyControl)
|
rlm@106
|
1389 b (.getControl bottom RigidBodyControl)]
|
rlm@106
|
1390
|
rlm@106
|
1391 (cond
|
rlm@106
|
1392 (= joint :cone)
|
rlm@106
|
1393
|
rlm@106
|
1394 (doto (ConeJoint.
|
rlm@106
|
1395 a b
|
rlm@106
|
1396 (world-to-local top joint-position)
|
rlm@106
|
1397 (world-to-local bottom joint-position)
|
rlm@106
|
1398 joint-rotation
|
rlm@106
|
1399 joint-rotation
|
rlm@106
|
1400 )
|
rlm@106
|
1401
|
rlm@106
|
1402
|
rlm@106
|
1403 (.setLimit (* (/ 10) Math/PI)
|
rlm@106
|
1404 (* (/ 4) Math/PI)
|
rlm@106
|
1405 0)))
|
rlm@106
|
1406 [origin top bottom table]))
|
rlm@106
|
1407
|
rlm@106
|
1408 (defn test-joint [joint]
|
rlm@106
|
1409 (let [[origin top bottom floor] (world-setup joint)
|
rlm@106
|
1410 control (.getControl top RigidBodyControl)
|
rlm@106
|
1411 move-up? (atom false)
|
rlm@106
|
1412 move-down? (atom false)
|
rlm@106
|
1413 move-left? (atom false)
|
rlm@106
|
1414 move-right? (atom false)
|
rlm@106
|
1415 roll-left? (atom false)
|
rlm@106
|
1416 roll-right? (atom false)
|
rlm@106
|
1417 timer (atom 0)]
|
rlm@106
|
1418
|
rlm@106
|
1419 (world
|
rlm@106
|
1420 (nodify [top bottom floor origin])
|
rlm@106
|
1421 (merge standard-debug-controls
|
rlm@106
|
1422 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@106
|
1423 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@106
|
1424 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@106
|
1425 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@106
|
1426 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@106
|
1427 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@106
|
1428
|
rlm@106
|
1429 (fn [world]
|
rlm@106
|
1430 (light-up-everything world)
|
rlm@106
|
1431 (enable-debug world)
|
rlm@106
|
1432 (set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
1433 )
|
rlm@106
|
1434
|
rlm@106
|
1435 (fn [world _]
|
rlm@106
|
1436 (if (zero? (rem (swap! timer inc) 100))
|
rlm@106
|
1437 (do
|
rlm@106
|
1438 ;; (println-repl @timer)
|
rlm@106
|
1439 (.attachChild (.getRootNode world)
|
rlm@106
|
1440 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@106
|
1441 :position (.getWorldTranslation top)
|
rlm@106
|
1442 :physical? false))
|
rlm@106
|
1443 (.attachChild (.getRootNode world)
|
rlm@106
|
1444 (sphere 0.05 :color ColorRGBA/LightGray
|
rlm@106
|
1445 :position (.getWorldTranslation bottom)
|
rlm@106
|
1446 :physical? false))))
|
rlm@106
|
1447
|
rlm@106
|
1448 (if @move-up?
|
rlm@106
|
1449 (.applyTorque control
|
rlm@106
|
1450 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1451 (Vector3f. 0 0 10))))
|
rlm@106
|
1452 (if @move-down?
|
rlm@106
|
1453 (.applyTorque control
|
rlm@106
|
1454 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1455 (Vector3f. 0 0 -10))))
|
rlm@106
|
1456 (if @move-left?
|
rlm@106
|
1457 (.applyTorque control
|
rlm@106
|
1458 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1459 (Vector3f. 0 10 0))))
|
rlm@106
|
1460 (if @move-right?
|
rlm@106
|
1461 (.applyTorque control
|
rlm@106
|
1462 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1463 (Vector3f. 0 -10 0))))
|
rlm@106
|
1464 (if @roll-left?
|
rlm@106
|
1465 (.applyTorque control
|
rlm@106
|
1466 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1467 (Vector3f. -1 0 0))))
|
rlm@106
|
1468 (if @roll-right?
|
rlm@106
|
1469 (.applyTorque control
|
rlm@106
|
1470 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1471 (Vector3f. 1 0 0))))))))
|
rlm@106
|
1472
|
rlm@99
|
1473
|
rlm@99
|
1474
|
rlm@107
|
1475 (defprotocol Frame
|
rlm@107
|
1476 (frame [this]))
|
rlm@107
|
1477
|
rlm@107
|
1478 (extend-type BufferedImage
|
rlm@107
|
1479 Frame
|
rlm@107
|
1480 (frame [image]
|
rlm@107
|
1481 (merge
|
rlm@107
|
1482 (apply
|
rlm@107
|
1483 hash-map
|
rlm@107
|
1484 (interleave
|
rlm@107
|
1485 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1486 (vector x y)))
|
rlm@107
|
1487 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1488 (let [data (.getRGB image x y)]
|
rlm@107
|
1489 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
|
rlm@107
|
1490 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
|
rlm@107
|
1491 :b (bit-and 0x0000ff data)))))))
|
rlm@107
|
1492 {:width (.getWidth image) :height (.getHeight image)})))
|
rlm@107
|
1493
|
rlm@107
|
1494
|
rlm@107
|
1495 (extend-type ImagePlus
|
rlm@107
|
1496 Frame
|
rlm@107
|
1497 (frame [image+]
|
rlm@107
|
1498 (frame (.getBufferedImage image+))))
|
rlm@107
|
1499
|
rlm@107
|
1500
|
rlm@99
|
1501 #+end_src
|
rlm@99
|
1502
|
rlm@99
|
1503
|
rlm@99
|
1504 * COMMENT generate source
|
rlm@99
|
1505 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@99
|
1506 <<body-1>>
|
rlm@99
|
1507 #+end_src
|
rlm@99
|
1508
|
rlm@99
|
1509
|
rlm@94
|
1510
|
rlm@94
|
1511
|