comparison org/integration.org @ 192:deac7b708750

cleaned up test-creature.org and renamed to integreation.org.
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 11:29:45 -0700
parents org/test-creature.org@66fbab414d45
children 305439cec54d
comparison
equal deleted inserted replaced
191:66fbab414d45 192:deac7b708750
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
8
9
10
11
12 * Intro
13 So far, I've made the following senses --
14 - Vision
15 - Hearing
16 - Touch
17 - Proprioception
18
19 And one effector:
20 - Movement
21
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.
29
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.
33
34 * The creature's body
35
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.
42
43 #+name: integration
44 #+begin_src clojure
45 (ns cortex.integration
46 "let's play!"
47 {:author "Robert McIntyre"}
48 (:use (cortex world util body
49 hearing touch vision sense proprioception movement))
50 (:import (com.jme3.math ColorRGBA Vector3f))
51 (:import com.jme3.audio.AudioNode)
52 (:import com.aurellem.capture.RatchetTimer))
53
54 (def hand "Models/creature1/one.blend")
55
56 (def worm "Models/creature1/try-again.blend")
57
58 (defn test-creature [thing]
59 (let [x-axis
60 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
61 y-axis
62 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
63 z-axis
64 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
65
66 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
67 bell (AudioNode. (asset-manager)
68 "Sounds/pure.wav" false)
69
70 fix-display
71 (runonce (fn [world]
72 (add-camera! world (.getCamera world) no-op)))
73 creature (doto (load-blender-model thing) (body!))
74
75 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 touch (touch! creature)
77 touch-display (view-touch)
78
79 vision (vision! creature)
80 vision-display (view-vision)
81
82 hearing (hearing! creature)
83 hearing-display (view-hearing)
84
85 prop (proprioception! creature)
86 prop-display (view-proprioception)
87
88 muscle-exertion (atom 0)
89 muscles (movement! creature)
90 muscle-display (view-movement)]
91
92 (apply
93 world
94 (with-movement
95 (.getChild creature "worm-21")
96 ["key-r" "key-t"
97 "key-f" "key-g"
98 "key-v" "key-b"]
99 [10 10 10 10 1 1]
100 [(nodify [creature
101 (box 10 2 10 :position (Vector3f. 0 -9 0)
102 :color ColorRGBA/Gray :mass 0)
103 x-axis y-axis z-axis
104 me])
105 (merge standard-debug-controls
106 {"key-return"
107 (fn [_ value]
108 (if value
109 (do
110 (println-repl "play-sound")
111 (.play bell))))
112 "key-h"
113 (fn [_ value]
114 (if value
115 (swap! muscle-exertion (partial + 20))))
116 "key-n"
117 (fn [_ value]
118 (if value
119 (swap! muscle-exertion (fn [v] (- v 20)))))})
120 (fn [world]
121 (light-up-everything world)
122 (enable-debug world)
123 (add-camera! world
124 (add-eye! creature
125 (.getChild
126 (.getChild creature "eyes") "eye"))
127 (comp (view-image) BufferedImage!))
128 (.setTimer world (RatchetTimer. 60))
129 (speed-up world)
130 (set-gravity world (Vector3f. 0 0 0))
131 (comment
132 (com.aurellem.capture.Capture/captureVideo
133 world (file-str "/home/r/proj/ai-videos/hand"))))
134 (fn [world tpf]
135 (prop-display (prop))
136 (touch-display (map #(% (.getRootNode world)) touch))
137 (vision-display (map #(% world) vision))
138 (hearing-display (map #(% world) hearing))
139 (muscle-display (map #(% @muscle-exertion) muscles))
140 (.setLocalTranslation me (.getLocation (.getCamera world)))
141 (fix-display world))]))))
142 #+end_src
143
144 #+results: body-1
145 : #'cortex.silly/follow-test
146
147
148 * COMMENT purgatory
149 #+begin_src clojure
150
151 (defn bullet-trans* []
152 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
153 :position (Vector3f. 5 0 0)
154 :mass 90)
155 obj-b (sphere 0.5 :color ColorRGBA/Blue
156 :position (Vector3f. -5 0 0)
157 :mass 0)
158 control-a (.getControl obj-a RigidBodyControl)
159 control-b (.getControl obj-b RigidBodyControl)
160 move-up? (atom nil)
161 move-down? (atom nil)
162 move-left? (atom nil)
163 move-right? (atom nil)
164 roll-left? (atom nil)
165 roll-right? (atom nil)
166 force 100
167 swivel
168 (.toRotationMatrix
169 (doto (Quaternion.)
170 (.fromAngleAxis (/ Math/PI 2)
171 Vector3f/UNIT_X)))
172 x-move
173 (doto (Matrix3f.)
174 (.fromStartEndVectors Vector3f/UNIT_X
175 (.normalize (Vector3f. 1 1 0))))
176
177 timer (atom 0)]
178 (doto
179 (ConeJoint.
180 control-a control-b
181 (Vector3f. -8 0 0)
182 (Vector3f. 2 0 0)
183 ;;swivel swivel
184 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
185 x-move Matrix3f/IDENTITY
186 )
187 (.setCollisionBetweenLinkedBodys false)
188 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
189 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
190 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
191 (world (nodify
192 [obj-a obj-b])
193 (merge standard-debug-controls
194 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
195 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
196 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
197 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
198 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
199 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
200
201 (fn [world]
202 (enable-debug world)
203 (set-gravity world Vector3f/ZERO)
204 )
205
206 (fn [world _]
207
208 (if @move-up?
209 (.applyForce control-a
210 (Vector3f. force 0 0)
211 (Vector3f. 0 0 0)))
212 (if @move-down?
213 (.applyForce control-a
214 (Vector3f. (- force) 0 0)
215 (Vector3f. 0 0 0)))
216 (if @move-left?
217 (.applyForce control-a
218 (Vector3f. 0 force 0)
219 (Vector3f. 0 0 0)))
220 (if @move-right?
221 (.applyForce control-a
222 (Vector3f. 0 (- force) 0)
223 (Vector3f. 0 0 0)))
224
225 (if @roll-left?
226 (.applyForce control-a
227 (Vector3f. 0 0 force)
228 (Vector3f. 0 0 0)))
229 (if @roll-right?
230 (.applyForce control-a
231 (Vector3f. 0 0 (- force))
232 (Vector3f. 0 0 0)))
233
234 (if (zero? (rem (swap! timer inc) 100))
235 (.attachChild
236 (.getRootNode world)
237 (sphere 0.05 :color ColorRGBA/Yellow
238 :physical? false :position
239 (.getWorldTranslation obj-a)))))
240 )
241 ))
242
243 (defn test-joint [joint]
244 (let [[origin top bottom floor] (world-setup joint)
245 control (.getControl top RigidBodyControl)
246 move-up? (atom false)
247 move-down? (atom false)
248 move-left? (atom false)
249 move-right? (atom false)
250 roll-left? (atom false)
251 roll-right? (atom false)
252 timer (atom 0)]
253
254 (world
255 (nodify [top bottom floor origin])
256 (merge standard-debug-controls
257 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
258 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
259 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
260 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
261 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
262 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
263
264 (fn [world]
265 (light-up-everything world)
266 (enable-debug world)
267 (set-gravity world (Vector3f. 0 0 0))
268 )
269
270 (fn [world _]
271 (if (zero? (rem (swap! timer inc) 100))
272 (do
273 ;; (println-repl @timer)
274 (.attachChild (.getRootNode world)
275 (sphere 0.05 :color ColorRGBA/Yellow
276 :position (.getWorldTranslation top)
277 :physical? false))
278 (.attachChild (.getRootNode world)
279 (sphere 0.05 :color ColorRGBA/LightGray
280 :position (.getWorldTranslation bottom)
281 :physical? false))))
282
283 (if @move-up?
284 (.applyTorque control
285 (.mult (.getPhysicsRotation control)
286 (Vector3f. 0 0 10))))
287 (if @move-down?
288 (.applyTorque control
289 (.mult (.getPhysicsRotation control)
290 (Vector3f. 0 0 -10))))
291 (if @move-left?
292 (.applyTorque control
293 (.mult (.getPhysicsRotation control)
294 (Vector3f. 0 10 0))))
295 (if @move-right?
296 (.applyTorque control
297 (.mult (.getPhysicsRotation control)
298 (Vector3f. 0 -10 0))))
299 (if @roll-left?
300 (.applyTorque control
301 (.mult (.getPhysicsRotation control)
302 (Vector3f. -1 0 0))))
303 (if @roll-right?
304 (.applyTorque control
305 (.mult (.getPhysicsRotation control)
306 (Vector3f. 1 0 0))))))))
307
308 (defn follow-test
309 "Show a camera that stays in the same relative position to a blue cube."
310 []
311 (let [camera-pos (Vector3f. 0 30 0)
312 rock (box 1 1 1 :color ColorRGBA/Blue
313 :position (Vector3f. 0 10 0)
314 :mass 30
315 )
316 rot (.getWorldRotation rock)
317
318 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
319 :position (Vector3f. 0 -3 0))]
320
321 (world
322 (nodify [rock table])
323 standard-debug-controls
324 (fn [world]
325 (let
326 [cam (doto (.clone (.getCamera world))
327 (.setLocation camera-pos)
328 (.lookAt Vector3f/ZERO
329 Vector3f/UNIT_X))]
330 (bind-sense rock cam)
331
332 (.setTimer world (RatchetTimer. 60))
333 (add-camera! world cam (comp (view-image) BufferedImage!))
334 (add-camera! world (.getCamera world) no-op))
335 )
336 (fn [_ _] (println-repl rot)))))
337 #+end_src
338
339
340 * COMMENT generate source
341 #+begin_src clojure :tangle ../src/cortex/integration.clj
342 <<integration>>
343 #+end_src
344
345
346
347