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