view org/integration.org @ 297:d1206b11ae2d

creating final video
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 12:48:51 -0700
parents 1eed471e2ebf
children 85f3ff3e3f26
line wrap: on
line source
1 #+title:
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
9 * Intro
11 This is the ultimate test which features all of the senses that I've
12 made so far. The blender file for the creature serves as an example of
13 a fully equipped creature in terms of senses. You can find it [[../assets/Models/test-creature/hand.blend][here]].
15 #+name: integration
16 #+begin_src clojure
17 (ns cortex.integration
18 "let's play!"
19 {:author "Robert McIntyre"}
20 (:use (cortex world util body sense
21 hearing touch vision proprioception movement))
22 (:import (com.jme3.math ColorRGBA Vector3f))
23 (:import java.io.File)
24 (:import com.jme3.audio.AudioNode)
25 (:import com.aurellem.capture.RatchetTimer))
27 (dorun (cortex.import/mega-import-jme3))
28 (rlm.rlm-commands/help)
30 (def hand "Models/test-creature/hand.blend")
32 (def output-base (File. "/home/r/proj/cortex/render/hand"))
35 ;; Let the hand fall palm-up
37 ;; it curls its phalanges, starting with the pinky.
39 ;; it lets these fall back down?
41 ;; block falls down onto the hand, accompanied by a sound. The block
42 ;; can be seen by the hand's eye.
44 ;; hand FORCEFULLY catapults the block so that it hits the camera.
47 (defn motor-control-program
48 "Create a function which will execute the motor script"
49 [muscle-positions
50 script]
51 (let [current-frame (atom -1)
52 keyed-script (group-by first script)
53 current-forces (atom {}) ]
54 (fn [effectors]
55 (let [indexed-effectors (vec effectors)]
56 (dorun
57 (for [[_ part force] (keyed-script (swap! current-frame inc))]
58 (swap! current-forces (fn [m] (assoc m part force)))))
59 (doall (map (fn [effector power]
60 (effector (int power)))
61 effectors
62 (map #(@current-forces % 0) muscle-positions)))))))
64 (def muscle-positions
65 [:pointer-2-e
66 :pointer-2-f
67 :thumb-1
68 :thumb-1
69 :pointer-1-e
70 :pointer-1-f
71 :thumb-2-e
72 :thumb-2-f
73 :middle-1-e
74 :middle-1-f
75 :pointer-3-f
76 :pointer-3-e
77 :middle-2-e
78 :middle-2-f
79 :middle-3-f
80 :middle-3-e
81 :pinky-2-e
82 :pinky-2-f
83 :pinky-3-f
84 :pinky-3-e
85 :ring-3-e
86 :ring-3-f
87 :ring-2-f
88 :ring-2-e
89 :ring-1-e
90 :ring-1-f
91 :thumb-1-e
92 :thumb-1-f
93 :pinky-1-f
94 :pinky-1-e])
96 (def full 9001)
97 ;; the systax here is [keyframe body-part force]
98 (def wiggle-each-finger-tip
99 [[300 :pointer-3-f 50]
102 [400 :middle-3-f full]
103 [401 :middle-3-f 50]
105 [600 :ring-3-f full]
106 [601 :ring-3-f 40]
108 [800 :pinky-3-f full]
109 [801 :pinky-3-f 50]
111 [1300 :pointer-1-f full]
112 [1300 :pointer-2-f full]
113 [1300 :pointer-3-f 0]
115 [1289 :middle-1-f full]
116 [1289 :middle-2-f full]
117 [1289 :middle-3-f 0]
119 [1310 :pointer-1-f 0]
120 [1310 :pointer-2-f 0]
121 [1310 :pointer-3-f 0]
123 [1310 :middle-1-f 0]
124 [1310 :middle-2-f 0]
125 [1310 :middle-3-f 0]
129 ])
131 (defn gen-summon-ball []
132 (let [wait (atom 20)]
133 (fn [world]
134 (if (= 0 (swap! wait dec))
135 (let [brick
136 (box 0.8 0.8 0.8 :mass 0.05
137 :position (Vector3f. 0 0 0.5)
138 :color (ColorRGBA/Red))]
140 (.addControl
141 brick
142 (proxy [AbstractControl] []
143 (controlUpdate [tpf]
144 (println-repl (.getWorldTranslation brick)))
145 (controlRender [_ _])))
146 (add-element world brick))))))
149 (def control-list
150 [
151 0 ;;pointer-21 #<Vector3f (0.99999994, 0.0, 0.0)>
152 0 ;;pointer-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
153 0 ;;thumb-11 #<Vector3f (-0.8802276, -0.39781287, -0.25873658)>
154 0 ;;thumb-11 #<Vector3f (0.8485723, 0.46149826, 0.2587364)>
155 0 ;;pointer-11 #<Vector3f (0.99999994, 0.0, 0.0)>
156 0 ;;pointer-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
157 0 ;;thumb-2.0011 #<Vector3f (-0.71705645, -0.44753736, -0.5343599)>
158 0 ;;thumb-2.0011 #<Vector3f (-0.10567085, 0.83862597, 0.53435963)>
159 0 ;;middle-11 #<Vector3f (0.99999994, 0.0, 0.0)>
160 0 ;;middle-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
161 1 ;;pointer-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
162 0 ;;pointer-31 #<Vector3f (0.99999994, 0.0, 0.0)>
163 0 ;;middle-21 #<Vector3f (0.99999994, 0.0, 0.0)>
164 0 ;;middle-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
165 0 ;;middle-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
166 0 ;;middle-31 #<Vector3f (0.99999994, 0.0, 0.0)>
167 0 ;;pinky-21 #<Vector3f (0.99999994, 0.0, 0.0)>
168 0 ;;pinky-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
169 0 ;;pinky-31 #<Vector3f (-1, 0.0, 0.0)>
170 0 ;;pinky-31 #<Vector3f (1.0, 0.0, 0.0)>
171 0 ;;ring-31 #<Vector3f (0.99999994, 0.0, 0.0)>
172 0 ;;ring-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
173 0 ;;ring-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
174 0 ;;ring-21 #<Vector3f (0.99999994, 0.0, 0.0)>
175 0 ;;ring-11 #<Vector3f (0.99999994, 0.0, 0.0)>
176 0 ;;ring-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
177 0 ;;thumb-11 #<Vector3f (-0.43154645, 0.7302033, 0.5296894)>
178 0 ;;thumb-11 #<Vector3f (-0.8032993, -0.2722854, -0.5296895)>
179 0 ;;pinky-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
180 0 ;;pinky-11 #<Vector3f (0.99999994, 0.0, 0.0)>
181 ])
183 (defn test-everything!
184 ([] (test-everything! false))
185 ([record?]
186 (let [me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
188 bell (AudioNode. (asset-manager)
189 "Sounds/pure.wav" false)
190 creature (doto (load-blender-model hand)
191 (body!))
193 summon-ball (gen-summon-ball)
194 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;;touch (touch! creature)
196 ;;touch-display (view-touch)
198 ;;vision (vision! creature)
199 ;;vision-display (view-vision)
201 ;;hearing (hearing! creature)
202 ;;hearing-display (view-hearing)
204 prop (proprioception! creature)
205 prop-display (view-proprioception)
207 control-script (motor-control-program
208 muscle-positions wiggle-each-finger-tip)
209 muscles (movement! creature)
210 muscle-display (view-movement)
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 fix-display (gen-fix-display)]
214 (world
215 (nodify [creature
216 (box 10 2 10 :position (Vector3f. 0 -9 0)
217 :color ColorRGBA/Gray :mass 0)
218 me])
219 (merge standard-debug-controls
220 {"key-return"
221 (fn [_ value]
222 (if value
223 (.play bell)))})
225 (fn [world]
226 (.setTimer world (RatchetTimer. 60))
227 (position-camera
228 world (Vector3f. 7.7101235, 6.3628435, -14.15047)
229 (Quaternion. 0.3644328, -0.2558489, 0.104854375, 0.8892332))
230 (light-up-everything world)
231 (enable-debug world)
232 (add-camera! world
233 (add-eye! creature
234 (.getChild
235 (.getChild creature "eyes") "eye"))
236 (comp (view-image) BufferedImage!))
237 (speed-up world)
238 )
239 (fn [world tpf]
240 ;;(prop-display (prop))
241 ;;(touch-display (map #(% (.getRootNode world)) touch))
242 ;;(vision-display (map #(% world) vision))
243 ;;(hearing-display (map #(% world) hearing))
244 (summon-ball world)
245 ;;(muscle-display
246 (control-script muscles)
247 ;;)
249 (.setLocalTranslation me (.getLocation (.getCamera world)))
250 (fix-display world))))))
251 #+end_src
253 #+results: integration
254 : #'cortex.integration/test-everything!
256 * COMMENT purgatory
257 #+begin_src clojure
258 (defn bullet-trans* []
259 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
260 :position (Vector3f. 5 0 0)
261 :mass 90)
262 obj-b (sphere 0.5 :color ColorRGBA/Blue
263 :position (Vector3f. -5 0 0)
264 :mass 0)
265 control-a (.getControl obj-a RigidBodyControl)
266 control-b (.getControl obj-b RigidBodyControl)
267 move-up? (atom nil)
268 move-down? (atom nil)
269 move-left? (atom nil)
270 move-right? (atom nil)
271 roll-left? (atom nil)
272 roll-right? (atom nil)
273 force 100
274 swivel
275 (.toRotationMatrix
276 (doto (Quaternion.)
277 (.fromAngleAxis (/ Math/PI 2)
278 Vector3f/UNIT_X)))
279 x-move
280 (doto (Matrix3f.)
281 (.fromStartEndVectors Vector3f/UNIT_X
282 (.normalize (Vector3f. 1 1 0))))
284 timer (atom 0)]
285 (doto
286 (ConeJoint.
287 control-a control-b
288 (Vector3f. -8 0 0)
289 (Vector3f. 2 0 0)
290 ;;swivel swivel
291 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
292 x-move Matrix3f/IDENTITY
293 )
294 (.setCollisionBetweenLinkedBodys false)
295 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
296 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
297 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
298 (world (nodify
299 [obj-a obj-b])
300 (merge standard-debug-controls
301 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
302 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
303 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
304 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
305 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
306 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
308 (fn [world]
309 (enable-debug world)
310 (set-gravity world Vector3f/ZERO)
311 )
313 (fn [world _]
315 (if @move-up?
316 (.applyForce control-a
317 (Vector3f. force 0 0)
318 (Vector3f. 0 0 0)))
319 (if @move-down?
320 (.applyForce control-a
321 (Vector3f. (- force) 0 0)
322 (Vector3f. 0 0 0)))
323 (if @move-left?
324 (.applyForce control-a
325 (Vector3f. 0 force 0)
326 (Vector3f. 0 0 0)))
327 (if @move-right?
328 (.applyForce control-a
329 (Vector3f. 0 (- force) 0)
330 (Vector3f. 0 0 0)))
332 (if @roll-left?
333 (.applyForce control-a
334 (Vector3f. 0 0 force)
335 (Vector3f. 0 0 0)))
336 (if @roll-right?
337 (.applyForce control-a
338 (Vector3f. 0 0 (- force))
339 (Vector3f. 0 0 0)))
341 (if (zero? (rem (swap! timer inc) 100))
342 (.attachChild
343 (.getRootNode world)
344 (sphere 0.05 :color ColorRGBA/Yellow
345 :physical? false :position
346 (.getWorldTranslation obj-a)))))
347 )
348 ))
350 (defn test-joint [joint]
351 (let [[origin top bottom floor] (world-setup joint)
352 control (.getControl top RigidBodyControl)
353 move-up? (atom false)
354 move-down? (atom false)
355 move-left? (atom false)
356 move-right? (atom false)
357 roll-left? (atom false)
358 roll-right? (atom false)
359 timer (atom 0)]
361 (world
362 (nodify [top bottom floor origin])
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 (light-up-everything world)
373 (enable-debug world)
374 (set-gravity world (Vector3f. 0 0 0))
375 )
377 (fn [world _]
378 (if (zero? (rem (swap! timer inc) 100))
379 (do
380 ;; (println-repl @timer)
381 (.attachChild (.getRootNode world)
382 (sphere 0.05 :color ColorRGBA/Yellow
383 :position (.getWorldTranslation top)
384 :physical? false))
385 (.attachChild (.getRootNode world)
386 (sphere 0.05 :color ColorRGBA/LightGray
387 :position (.getWorldTranslation bottom)
388 :physical? false))))
390 (if @move-up?
391 (.applyTorque control
392 (.mult (.getPhysicsRotation control)
393 (Vector3f. 0 0 10))))
394 (if @move-down?
395 (.applyTorque control
396 (.mult (.getPhysicsRotation control)
397 (Vector3f. 0 0 -10))))
398 (if @move-left?
399 (.applyTorque control
400 (.mult (.getPhysicsRotation control)
401 (Vector3f. 0 10 0))))
402 (if @move-right?
403 (.applyTorque control
404 (.mult (.getPhysicsRotation control)
405 (Vector3f. 0 -10 0))))
406 (if @roll-left?
407 (.applyTorque control
408 (.mult (.getPhysicsRotation control)
409 (Vector3f. -1 0 0))))
410 (if @roll-right?
411 (.applyTorque control
412 (.mult (.getPhysicsRotation control)
413 (Vector3f. 1 0 0))))))))
414 #+end_src
417 * COMMENT generate source
418 #+begin_src clojure :tangle ../src/cortex/integration.clj
419 <<integration>>
420 #+end_src