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