view org/test-creature.org @ 187:6142e85f5825

extracted common elements of display code
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 09:42:19 -0700
parents cfb71209ddc6
children 22548d48cc85
line wrap: on
line source
1 #+title: First attempt at a creature!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
12 * Intro
13 So far, I've made the following senses --
14 - Vision
15 - Hearing
16 - Touch
17 - Proprioception
19 And one effector:
20 - Movement
22 However, the code so far has only enabled these senses, but has not
23 actually implemented them. For example, there is still a lot of work
24 to be done for vision. I need to be able to create an /eyeball/ in
25 simulation that can be moved around and see the world from different
26 angles. I also need to determine weather to use log-polar or cartesian
27 for the visual input, and I need to determine how/wether to
28 disceritise the visual input.
30 I also want to be able to visualize both the sensors and the
31 effectors in pretty pictures. This semi-retarted creature will be my
32 first attempt at bringing everything together.
34 * The creature's body
36 Still going to do an eve-like body in blender, but due to problems
37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
38 the connecting here in clojure code, using the names of the individual
39 components and trial and error. Later, I'll maybe make some sort of
40 creature-building modifications to blender that support whatever
41 discritized senses I'm going to make.
43 #+name: body-1
44 #+begin_src clojure
45 (ns cortex.silly
46 "let's play!"
47 {:author "Robert McIntyre"})
49 ;; TODO remove this!
50 (require 'cortex.import)
51 (cortex.import/mega-import-jme3)
52 (use '(cortex world util body hearing touch vision sense
53 proprioception movement))
55 (rlm.rlm-commands/help)
56 (import java.awt.image.BufferedImage)
57 (import javax.swing.JPanel)
58 (import javax.swing.SwingUtilities)
59 (import java.awt.Dimension)
60 (import javax.swing.JFrame)
61 (import java.awt.Dimension)
62 (import com.aurellem.capture.RatchetTimer)
64 (use 'clojure.contrib.def)
66 (defn load-blender-model
67 "Load a .blend file using an asset folder relative path."
68 [^String model]
69 (.loadModel
70 (doto (asset-manager)
71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
72 model))
74 (def hand "Models/creature1/one.blend")
76 (def worm "Models/creature1/try-again.blend")
78 (defn worm-model [] (load-blender-model worm))
80 (defn x-ray [#^ColorRGBA color]
81 (doto (Material. (asset-manager)
82 "Common/MatDefs/Misc/Unshaded.j3md")
83 (.setColor "Color" color)
84 (-> (.getAdditionalRenderState)
85 (.setDepthTest false))))
87 (defn colorful []
88 (.getChild (worm-model) "worm-21"))
90 (import jme3tools.converters.ImageToAwt)
92 (import ij.ImagePlus)
96 (defn test-eye []
97 (.getChild
98 (.getChild (worm-model) "eyes")
99 "eye"))
103 ;; lower level --- nodes
104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
106 ;; higher level -- organs
107 ;;
109 ;; higher level --- sense/effector
110 ;; these are the functions that provide world i/o, chinese-room style
113 (defn debug-vision-window
114 "creates function that offers a debug view of sensor data"
115 []
116 (let [vi (view-image)]
117 (fn
118 [[coords sensor-data]]
119 (let [image (points->image coords)]
120 (dorun
121 (for [i (range (count coords))]
122 (.setRGB image ((coords i) 0) ((coords i) 1)
123 (sensor-data i))))
124 (vi image)))))
126 (defn debug-hearing-window
127 "view audio data"
128 [height]
129 (let [vi (view-image)]
130 (fn [[coords sensor-data]]
131 (let [image (BufferedImage. (count coords) height
132 BufferedImage/TYPE_INT_RGB)]
133 (dorun
134 (for [x (range (count coords))]
135 (dorun
136 (for [y (range height)]
137 (let [raw-sensor (sensor-data x)]
138 (.setRGB image x y (gray-scale raw-sensor)))))))
140 (vi image)))))
142 (defn test-creature [thing]
143 (let [x-axis
144 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
145 y-axis
146 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
147 z-axis
148 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
149 creature (doto
150 (load-blender-model thing)
151 (body!))
152 touch (touch! creature)
153 touch-view (view-touch)
154 vision-data (vision! creature)
155 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
156 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
157 hearing-senses (hearing! creature)
158 hearing-windows (map (fn [_] (debug-hearing-window 50))
159 hearing-senses)
160 bell (AudioNode. (asset-manager)
161 "Sounds/pure.wav" false)
162 prop (proprioception! creature)
163 prop-debug (proprioception-debug-window)
165 muscle-fns (movement! creature)
166 ;; dream
167 fix-display (runonce
168 (fn [world] (add-camera! world (.getCamera world) no-op)))
169 ]
172 (apply
173 world
174 (with-movement
175 (.getChild creature "worm-21")
176 ["key-r" "key-t"
177 "key-f" "key-g"
178 "key-v" "key-b"]
179 [10 10 10 10 1 1]
180 [(nodify [creature
181 (box 10 2 10 :position (Vector3f. 0 -9 0)
182 :color ColorRGBA/Gray :mass 0)
183 x-axis y-axis z-axis
184 me
185 ])
186 (merge standard-debug-controls
187 {"key-return"
188 (fn [_ value]
189 (if value
190 (do
191 (println-repl "play-sound")
192 (.play bell))))
193 "key-h"
194 (fn [_ value]
195 (if value
196 (do
197 (println-repl "muscle activating!")
198 ((first muscle-fns) 199))))
200 })
201 (fn [world]
202 (light-up-everything world)
203 (enable-debug world)
204 ;;(dorun (map #(% world) init-vision-fns))
205 ;;(dorun (map #(% world) init-hearing-fns))
207 (add-camera! world
208 (add-eye! creature (test-eye))
209 (comp (view-image) BufferedImage!))
212 ;;(set-gravity world (Vector3f. 0 0 0))
213 ;;(com.aurellem.capture.Capture/captureVideo
214 ;; world (file-str "/home/r/proj/ai-videos/hand"))
215 ;;(.setTimer world (RatchetTimer. 60))
216 (speed-up world)
217 (set-gravity world (Vector3f. 0 0 0))
218 )
219 (fn [world tpf]
220 ;;(dorun
221 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
223 (prop-debug (prop))
225 (touch-view (map #(% (.getRootNode world)) touch))
227 (dorun
228 (map #(%1 (%2 world))
229 vision-debug vision-data))
230 (dorun
231 (map #(%1 (%2 world)) hearing-windows hearing-senses))
234 ;;(println-repl (vision-data))
235 (.setLocalTranslation me (.getLocation (.getCamera world)))
236 (fix-display world)
238 )]
239 ;;(let [timer (atom 0)]
240 ;; (fn [_ _]
241 ;; (swap! timer inc)
242 ;; (if (= (rem @timer 60) 0)
243 ;; (println-repl (float (/ @timer 60))))))
244 ))))
248 ;; the camera will stay in its initial position/rotation with relation
249 ;; to the spatial.
252 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
253 ;;anchor-qualia, augment-organ, with-organ
255 (defn follow-test
256 "show a camera that stays in the same relative position to a blue cube."
257 []
258 (let [camera-pos (Vector3f. 0 30 0)
259 rock (box 1 1 1 :color ColorRGBA/Blue
260 :position (Vector3f. 0 10 0)
261 :mass 30
262 )
263 rot (.getWorldRotation rock)
265 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
266 :position (Vector3f. 0 -3 0))]
268 (world
269 (nodify [rock table])
270 standard-debug-controls
271 (fn [world]
272 (let
273 [cam (doto (.clone (.getCamera world))
274 (.setLocation camera-pos)
275 (.lookAt Vector3f/ZERO
276 Vector3f/UNIT_X))]
277 (bind-sense rock cam)
279 (.setTimer world (RatchetTimer. 60))
280 (add-camera! world cam (comp (view-image) BufferedImage!))
281 (add-camera! world (.getCamera world) no-op))
282 )
283 (fn [_ _] (println-repl rot)))))
287 #+end_src
289 #+results: body-1
290 : #'cortex.silly/follow-test
293 * COMMENT purgatory
294 #+begin_src clojure
296 (defn bullet-trans* []
297 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
298 :position (Vector3f. 5 0 0)
299 :mass 90)
300 obj-b (sphere 0.5 :color ColorRGBA/Blue
301 :position (Vector3f. -5 0 0)
302 :mass 0)
303 control-a (.getControl obj-a RigidBodyControl)
304 control-b (.getControl obj-b RigidBodyControl)
305 move-up? (atom nil)
306 move-down? (atom nil)
307 move-left? (atom nil)
308 move-right? (atom nil)
309 roll-left? (atom nil)
310 roll-right? (atom nil)
311 force 100
312 swivel
313 (.toRotationMatrix
314 (doto (Quaternion.)
315 (.fromAngleAxis (/ Math/PI 2)
316 Vector3f/UNIT_X)))
317 x-move
318 (doto (Matrix3f.)
319 (.fromStartEndVectors Vector3f/UNIT_X
320 (.normalize (Vector3f. 1 1 0))))
322 timer (atom 0)]
323 (doto
324 (ConeJoint.
325 control-a control-b
326 (Vector3f. -8 0 0)
327 (Vector3f. 2 0 0)
328 ;;swivel swivel
329 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
330 x-move Matrix3f/IDENTITY
331 )
332 (.setCollisionBetweenLinkedBodys false)
333 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
334 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
335 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
336 (world (nodify
337 [obj-a obj-b])
338 (merge standard-debug-controls
339 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
340 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
341 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
342 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
343 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
344 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
346 (fn [world]
347 (enable-debug world)
348 (set-gravity world Vector3f/ZERO)
349 )
351 (fn [world _]
353 (if @move-up?
354 (.applyForce control-a
355 (Vector3f. force 0 0)
356 (Vector3f. 0 0 0)))
357 (if @move-down?
358 (.applyForce control-a
359 (Vector3f. (- force) 0 0)
360 (Vector3f. 0 0 0)))
361 (if @move-left?
362 (.applyForce control-a
363 (Vector3f. 0 force 0)
364 (Vector3f. 0 0 0)))
365 (if @move-right?
366 (.applyForce control-a
367 (Vector3f. 0 (- force) 0)
368 (Vector3f. 0 0 0)))
370 (if @roll-left?
371 (.applyForce control-a
372 (Vector3f. 0 0 force)
373 (Vector3f. 0 0 0)))
374 (if @roll-right?
375 (.applyForce control-a
376 (Vector3f. 0 0 (- force))
377 (Vector3f. 0 0 0)))
379 (if (zero? (rem (swap! timer inc) 100))
380 (.attachChild
381 (.getRootNode world)
382 (sphere 0.05 :color ColorRGBA/Yellow
383 :physical? false :position
384 (.getWorldTranslation obj-a)))))
385 )
386 ))
388 (defn test-joint [joint]
389 (let [[origin top bottom floor] (world-setup joint)
390 control (.getControl top RigidBodyControl)
391 move-up? (atom false)
392 move-down? (atom false)
393 move-left? (atom false)
394 move-right? (atom false)
395 roll-left? (atom false)
396 roll-right? (atom false)
397 timer (atom 0)]
399 (world
400 (nodify [top bottom floor origin])
401 (merge standard-debug-controls
402 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
403 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
404 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
405 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
406 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
407 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
409 (fn [world]
410 (light-up-everything world)
411 (enable-debug world)
412 (set-gravity world (Vector3f. 0 0 0))
413 )
415 (fn [world _]
416 (if (zero? (rem (swap! timer inc) 100))
417 (do
418 ;; (println-repl @timer)
419 (.attachChild (.getRootNode world)
420 (sphere 0.05 :color ColorRGBA/Yellow
421 :position (.getWorldTranslation top)
422 :physical? false))
423 (.attachChild (.getRootNode world)
424 (sphere 0.05 :color ColorRGBA/LightGray
425 :position (.getWorldTranslation bottom)
426 :physical? false))))
428 (if @move-up?
429 (.applyTorque control
430 (.mult (.getPhysicsRotation control)
431 (Vector3f. 0 0 10))))
432 (if @move-down?
433 (.applyTorque control
434 (.mult (.getPhysicsRotation control)
435 (Vector3f. 0 0 -10))))
436 (if @move-left?
437 (.applyTorque control
438 (.mult (.getPhysicsRotation control)
439 (Vector3f. 0 10 0))))
440 (if @move-right?
441 (.applyTorque control
442 (.mult (.getPhysicsRotation control)
443 (Vector3f. 0 -10 0))))
444 (if @roll-left?
445 (.applyTorque control
446 (.mult (.getPhysicsRotation control)
447 (Vector3f. -1 0 0))))
448 (if @roll-right?
449 (.applyTorque control
450 (.mult (.getPhysicsRotation control)
451 (Vector3f. 1 0 0))))))))
452 #+end_src
455 * COMMENT generate source
456 #+begin_src clojure :tangle ../src/cortex/silly.clj
457 <<body-1>>
458 #+end_src