view org/test-creature.org @ 176:026f69582022

organized imports in cortex.body
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 06:52:47 -0700
parents 0b9ae09eaec3
children 6fba17a74a57
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
116 (defn gray-scale [num]
117 (+ num
118 (bit-shift-left num 8)
119 (bit-shift-left num 16)))
121 (defn debug-touch-window
122 "creates function that offers a debug view of sensor data"
123 []
124 (let [vi (view-image)]
125 (fn
126 [[coords sensor-data]]
127 (let [image (points->image coords)]
128 (dorun
129 (for [i (range (count coords))]
130 (.setRGB image ((coords i) 0) ((coords i) 1)
131 (gray-scale (sensor-data i)))))
134 (vi image)))))
136 (defn debug-vision-window
137 "creates function that offers a debug view of sensor data"
138 []
139 (let [vi (view-image)]
140 (fn
141 [[coords sensor-data]]
142 (let [image (points->image coords)]
143 (dorun
144 (for [i (range (count coords))]
145 (.setRGB image ((coords i) 0) ((coords i) 1)
146 (sensor-data i))))
147 (vi image)))))
149 (defn debug-hearing-window
150 "view audio data"
151 [height]
152 (let [vi (view-image)]
153 (fn [[coords sensor-data]]
154 (let [image (BufferedImage. (count coords) height
155 BufferedImage/TYPE_INT_RGB)]
156 (dorun
157 (for [x (range (count coords))]
158 (dorun
159 (for [y (range height)]
160 (let [raw-sensor (sensor-data x)]
161 (.setRGB image x y (gray-scale raw-sensor)))))))
163 (vi image)))))
165 (defn test-creature [thing]
166 (let [x-axis
167 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
168 y-axis
169 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
170 z-axis
171 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
172 creature (doto
173 (load-blender-model thing)
174 (body!))
175 touch-nerves (touch creature)
176 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
177 vision-data (vision! creature)
178 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
179 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
180 hearing-senses (hearing! creature)
181 hearing-windows (map (fn [_] (debug-hearing-window 50))
182 hearing-senses)
183 bell (AudioNode. (asset-manager)
184 "Sounds/pure.wav" false)
185 prop (proprioception! creature)
186 prop-debug (proprioception-debug-window)
188 muscle-fns (enable-muscles creature)
189 ;; dream
190 fix-display (runonce
191 (fn [world] (add-camera! world (.getCamera world) no-op)))
192 ]
195 (apply
196 world
197 (with-movement
198 (.getChild creature "worm-21")
199 ["key-r" "key-t"
200 "key-f" "key-g"
201 "key-v" "key-b"]
202 [10 10 10 10 1 1]
203 [(nodify [creature
204 (box 10 2 10 :position (Vector3f. 0 -9 0)
205 :color ColorRGBA/Gray :mass 0)
206 x-axis y-axis z-axis
207 me
208 ])
209 (merge standard-debug-controls
210 {"key-return"
211 (fn [_ value]
212 (if value
213 (do
214 (println-repl "play-sound")
215 (.play bell))))
216 "key-h"
217 (fn [_ value]
218 (if value
219 (do
220 (println-repl "muscle activating!")
221 ((first muscle-fns) 199))))
223 })
224 (fn [world]
225 (light-up-everything world)
226 (enable-debug world)
227 ;;(dorun (map #(% world) init-vision-fns))
228 ;;(dorun (map #(% world) init-hearing-fns))
230 (add-camera! world
231 (add-eye! creature (test-eye))
232 (comp (view-image) BufferedImage!))
235 ;;(set-gravity world (Vector3f. 0 0 0))
236 ;;(com.aurellem.capture.Capture/captureVideo
237 ;; world (file-str "/home/r/proj/ai-videos/hand"))
238 ;;(.setTimer world (RatchetTimer. 60))
239 (speed-up world)
240 (set-gravity world (Vector3f. 0 0 0))
241 )
242 (fn [world tpf]
243 ;;(dorun
244 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
246 (prop-debug (prop))
248 (dorun
249 (map #(%1 (%2 (.getRootNode world)))
250 touch-debug-windows touch-nerves))
252 (dorun
253 (map #(%1 (%2 world))
254 vision-debug vision-data))
255 (dorun
256 (map #(%1 (%2 world)) hearing-windows hearing-senses))
259 ;;(println-repl (vision-data))
260 (.setLocalTranslation me (.getLocation (.getCamera world)))
261 (fix-display world)
263 )]
264 ;;(let [timer (atom 0)]
265 ;; (fn [_ _]
266 ;; (swap! timer inc)
267 ;; (if (= (rem @timer 60) 0)
268 ;; (println-repl (float (/ @timer 60))))))
269 ))))
273 ;; the camera will stay in its initial position/rotation with relation
274 ;; to the spatial.
277 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
278 ;;anchor-qualia, augment-organ, with-organ
280 (defn follow-test
281 "show a camera that stays in the same relative position to a blue cube."
282 []
283 (let [camera-pos (Vector3f. 0 30 0)
284 rock (box 1 1 1 :color ColorRGBA/Blue
285 :position (Vector3f. 0 10 0)
286 :mass 30
287 )
288 rot (.getWorldRotation rock)
290 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
291 :position (Vector3f. 0 -3 0))]
293 (world
294 (nodify [rock table])
295 standard-debug-controls
296 (fn [world]
297 (let
298 [cam (doto (.clone (.getCamera world))
299 (.setLocation camera-pos)
300 (.lookAt Vector3f/ZERO
301 Vector3f/UNIT_X))]
302 (bind-sense rock cam)
304 (.setTimer world (RatchetTimer. 60))
305 (add-camera! world cam (comp (view-image) BufferedImage!))
306 (add-camera! world (.getCamera world) no-op))
307 )
308 (fn [_ _] (println-repl rot)))))
312 #+end_src
314 #+results: body-1
315 : #'cortex.silly/follow-test
318 * COMMENT purgatory
319 #+begin_src clojure
321 (defn bullet-trans* []
322 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
323 :position (Vector3f. 5 0 0)
324 :mass 90)
325 obj-b (sphere 0.5 :color ColorRGBA/Blue
326 :position (Vector3f. -5 0 0)
327 :mass 0)
328 control-a (.getControl obj-a RigidBodyControl)
329 control-b (.getControl obj-b RigidBodyControl)
330 move-up? (atom nil)
331 move-down? (atom nil)
332 move-left? (atom nil)
333 move-right? (atom nil)
334 roll-left? (atom nil)
335 roll-right? (atom nil)
336 force 100
337 swivel
338 (.toRotationMatrix
339 (doto (Quaternion.)
340 (.fromAngleAxis (/ Math/PI 2)
341 Vector3f/UNIT_X)))
342 x-move
343 (doto (Matrix3f.)
344 (.fromStartEndVectors Vector3f/UNIT_X
345 (.normalize (Vector3f. 1 1 0))))
347 timer (atom 0)]
348 (doto
349 (ConeJoint.
350 control-a control-b
351 (Vector3f. -8 0 0)
352 (Vector3f. 2 0 0)
353 ;;swivel swivel
354 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
355 x-move Matrix3f/IDENTITY
356 )
357 (.setCollisionBetweenLinkedBodys false)
358 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
359 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
360 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
361 (world (nodify
362 [obj-a obj-b])
363 (merge standard-debug-controls
364 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
365 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
366 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
367 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
368 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
369 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
371 (fn [world]
372 (enable-debug world)
373 (set-gravity world Vector3f/ZERO)
374 )
376 (fn [world _]
378 (if @move-up?
379 (.applyForce control-a
380 (Vector3f. force 0 0)
381 (Vector3f. 0 0 0)))
382 (if @move-down?
383 (.applyForce control-a
384 (Vector3f. (- force) 0 0)
385 (Vector3f. 0 0 0)))
386 (if @move-left?
387 (.applyForce control-a
388 (Vector3f. 0 force 0)
389 (Vector3f. 0 0 0)))
390 (if @move-right?
391 (.applyForce control-a
392 (Vector3f. 0 (- force) 0)
393 (Vector3f. 0 0 0)))
395 (if @roll-left?
396 (.applyForce control-a
397 (Vector3f. 0 0 force)
398 (Vector3f. 0 0 0)))
399 (if @roll-right?
400 (.applyForce control-a
401 (Vector3f. 0 0 (- force))
402 (Vector3f. 0 0 0)))
404 (if (zero? (rem (swap! timer inc) 100))
405 (.attachChild
406 (.getRootNode world)
407 (sphere 0.05 :color ColorRGBA/Yellow
408 :physical? false :position
409 (.getWorldTranslation obj-a)))))
410 )
411 ))
413 (defn test-joint [joint]
414 (let [[origin top bottom floor] (world-setup joint)
415 control (.getControl top RigidBodyControl)
416 move-up? (atom false)
417 move-down? (atom false)
418 move-left? (atom false)
419 move-right? (atom false)
420 roll-left? (atom false)
421 roll-right? (atom false)
422 timer (atom 0)]
424 (world
425 (nodify [top bottom floor origin])
426 (merge standard-debug-controls
427 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
428 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
429 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
430 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
431 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
432 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
434 (fn [world]
435 (light-up-everything world)
436 (enable-debug world)
437 (set-gravity world (Vector3f. 0 0 0))
438 )
440 (fn [world _]
441 (if (zero? (rem (swap! timer inc) 100))
442 (do
443 ;; (println-repl @timer)
444 (.attachChild (.getRootNode world)
445 (sphere 0.05 :color ColorRGBA/Yellow
446 :position (.getWorldTranslation top)
447 :physical? false))
448 (.attachChild (.getRootNode world)
449 (sphere 0.05 :color ColorRGBA/LightGray
450 :position (.getWorldTranslation bottom)
451 :physical? false))))
453 (if @move-up?
454 (.applyTorque control
455 (.mult (.getPhysicsRotation control)
456 (Vector3f. 0 0 10))))
457 (if @move-down?
458 (.applyTorque control
459 (.mult (.getPhysicsRotation control)
460 (Vector3f. 0 0 -10))))
461 (if @move-left?
462 (.applyTorque control
463 (.mult (.getPhysicsRotation control)
464 (Vector3f. 0 10 0))))
465 (if @move-right?
466 (.applyTorque control
467 (.mult (.getPhysicsRotation control)
468 (Vector3f. 0 -10 0))))
469 (if @roll-left?
470 (.applyTorque control
471 (.mult (.getPhysicsRotation control)
472 (Vector3f. -1 0 0))))
473 (if @roll-right?
474 (.applyTorque control
475 (.mult (.getPhysicsRotation control)
476 (Vector3f. 1 0 0))))))))
477 #+end_src
480 * COMMENT generate source
481 #+begin_src clojure :tangle ../src/cortex/silly.clj
482 <<body-1>>
483 #+end_src