view org/body.org @ 138:16bdf9e80daf

working movement debug framework
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 00:05:17 -0700
parents 39c89ae5c7d0
children ffbab4199c0d
line wrap: on
line source
1 #+title: The BODY!!!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.
5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org
8 * Proprioception
9 #+name: proprioception
10 #+begin_src clojure
11 (ns cortex.body
12 (:use (cortex world util))
13 (:import
14 com.jme3.math.Vector3f
15 com.jme3.math.Quaternion
16 com.jme3.math.Vector2f
17 com.jme3.math.Matrix3f
18 com.jme3.bullet.control.RigidBodyControl
19 com.jme3.collision.CollisionResults
20 com.jme3.bounding.BoundingBox))
22 (import com.jme3.scene.Node)
24 (defn jme-to-blender
25 "Convert from JME coordinates to Blender coordinates"
26 [#^Vector3f in]
27 (Vector3f. (.getX in)
28 (- (.getZ in))
29 (.getY in)))
31 (defn joint-targets
32 "Return the two closest two objects to the joint object, ordered
33 from bottom to top according to the joint's rotation."
34 [#^Node parts #^Node joint]
35 (loop [radius (float 0.01)]
36 (let [results (CollisionResults.)]
37 (.collideWith
38 parts
39 (BoundingBox. (.getWorldTranslation joint)
40 radius radius radius)
41 results)
42 (let [targets
43 (distinct
44 (map #(.getGeometry %) results))]
45 (if (>= (count targets) 2)
46 (sort-by
47 #(let [v
48 (jme-to-blender
49 (.mult
50 (.inverse (.getWorldRotation joint))
51 (.subtract (.getWorldTranslation %)
52 (.getWorldTranslation joint))))]
53 (println-repl (.getName %) ":" v)
54 (.dot (Vector3f. 1 1 1)
55 v))
56 (take 2 targets))
57 (recur (float (* radius 2))))))))
59 (defn creature-joints
60 "Return the children of the creature's \"joints\" node."
61 [#^Node creature]
62 (if-let [joint-node (.getChild creature "joints")]
63 (seq (.getChildren joint-node))
64 (do (println-repl "could not find JOINTS node") [])))
66 (defn joint-proprioception [#^Node parts #^Node joint]
67 (let [[obj-a obj-b] (joint-targets parts joint)
68 joint-rot (.getWorldRotation joint)
69 pre-inv-a (.inverse (.getWorldRotation obj-a))
70 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))
71 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))
72 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))]
73 (println-repl "x:" x)
74 (println-repl "y:" y)
75 (println-repl "z:" z)
76 ;; this function will report proprioceptive information for the
77 ;; joint.
78 (fn []
79 ;; x is the "twist" axis, y and z are the "bend" axes
80 (let [rot-a (.getWorldRotation obj-a)
81 ;;inv-a (.inverse rot-a)
82 rot-b (.getWorldRotation obj-b)
83 ;;relative (.mult rot-b inv-a)
84 basis (doto (Matrix3f.)
85 (.setColumn 0 (.mult rot-a x))
86 (.setColumn 1 (.mult rot-a y))
87 (.setColumn 2 (.mult rot-a z)))
88 rotation-about-joint
89 (doto (Quaternion.)
90 (.fromRotationMatrix
91 (.mult (.invert basis)
92 (.toRotationMatrix rot-b))))
93 [yaw roll pitch]
94 (seq (.toAngles rotation-about-joint nil))]
95 ;;return euler angles of the quaternion around the new basis
96 [yaw roll pitch]
97 ))))
100 (defn proprioception
101 "Create a function that provides proprioceptive information about an
102 entire body."
103 [#^Node creature]
104 ;; extract the body's joints
105 (let [joints (creature-joints creature)
106 senses (map (partial joint-proprioception creature) joints)]
107 (fn []
108 (map #(%) senses))))
110 #+end_src
112 #+results: proprioception
113 : #'cortex.body/proprioception
115 * Motor Control
116 #+name: motor-control
117 #+begin_src clojure
118 (in-ns 'cortex.body)
120 ;; surprisingly enough, terristerial creatures only move by using
121 ;; torque applied about their joints. There's not a single straight
122 ;; line of force in the human body at all! (A straight line of force
123 ;; would correspond to some sort of jet or rocket propulseion.)
125 (defn vector-motor-control
126 "Create a function that accepts a sequence of Vector3f objects that
127 describe the torque to be applied to each part of the body."
128 [body]
129 (let [nodes (node-seq body)
130 controls (keep #(.getControl % RigidBodyControl) nodes)]
131 (fn [torques]
132 (map #(.applyTorque %1 %2)
133 controls torques))))
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 #+end_src
137 ## note -- might want to add a lower dimensional, discrete version of
138 ## this if it proves useful from a x-modal clustering perspective.
140 * Examples
142 #+name: test-body
143 #+begin_src clojure
144 (ns cortex.test.body
145 (:use (cortex world util body))
146 (:require cortex.silly)
147 (:import
148 com.jme3.math.Vector3f
149 com.jme3.math.ColorRGBA
150 com.jme3.bullet.joints.Point2PointJoint
151 com.jme3.bullet.control.RigidBodyControl
152 com.jme3.system.NanoTimer))
154 (defn worm-segments
155 "Create multiple evenly spaced box segments. They're fabulous!"
156 [segment-length num-segments interstitial-space radius]
157 (letfn [(nth-segment
158 [n]
159 (box segment-length radius radius :mass 0.1
160 :position
161 (Vector3f.
162 (* 2 n (+ interstitial-space segment-length)) 0 0)
163 :name (str "worm-segment" n)
164 :color (ColorRGBA/randomColor)))]
165 (map nth-segment (range num-segments))))
167 (defn connect-at-midpoint
168 "Connect two physics objects with a Point2Point joint constraint at
169 the point equidistant from both objects' centers."
170 [segmentA segmentB]
171 (let [centerA (.getWorldTranslation segmentA)
172 centerB (.getWorldTranslation segmentB)
173 midpoint (.mult (.add centerA centerB) (float 0.5))
174 pivotA (.subtract midpoint centerA)
175 pivotB (.subtract midpoint centerB)
177 ;; A side-effect of creating a joint registers
178 ;; it with both physics objects which in turn
179 ;; will register the joint with the physics system
180 ;; when the simulation is started.
181 joint (Point2PointJoint.
182 (.getControl segmentA RigidBodyControl)
183 (.getControl segmentB RigidBodyControl)
184 pivotA
185 pivotB)]
186 segmentB))
188 (defn eve-worm
189 "Create a worm-like body bound by invisible joint constraints."
190 []
191 (let [segments (worm-segments 0.2 5 0.1 0.1)]
192 (dorun (map (partial apply connect-at-midpoint)
193 (partition 2 1 segments)))
194 (nodify "worm" segments)))
196 (defn worm-pattern
197 "This is a simple, mindless motor control pattern that drives the
198 second segment of the worm's body at an offset angle with
199 sinusoidally varying strength."
200 [time]
201 (let [angle (* Math/PI (/ 9 20))
202 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
203 [Vector3f/ZERO
204 (.mult
205 direction
206 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
207 Vector3f/ZERO
208 Vector3f/ZERO
209 Vector3f/ZERO]))
211 (defn test-motor-control
212 "Testing motor-control:
213 You should see a multi-segmented worm-like object fall onto the
214 table and begin writhing and moving."
215 []
216 (let [worm (eve-worm)
217 time (atom 0)
218 worm-motor-map (vector-motor-control worm)]
219 (world
220 (nodify [worm
221 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
222 :color ColorRGBA/Gray)])
223 standard-debug-controls
224 (fn [world]
225 (enable-debug world)
226 (light-up-everything world)
227 (comment
228 (com.aurellem.capture.Capture/captureVideo
229 world
230 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
231 )
233 (fn [_ _]
234 (swap! time inc)
235 (Thread/sleep 20)
236 (dorun (worm-motor-map
237 (worm-pattern @time)))))))
241 (defn join-at-point [obj-a obj-b world-pivot]
242 (cortex.silly/joint-dispatch
243 {:type :point}
244 (.getControl obj-a RigidBodyControl)
245 (.getControl obj-b RigidBodyControl)
246 (cortex.silly/world-to-local obj-a world-pivot)
247 (cortex.silly/world-to-local obj-b world-pivot)
248 nil
249 ))
251 (import com.jme3.bullet.collision.PhysicsCollisionObject)
253 (defn blab-* []
254 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
255 :mass 0 :color ColorRGBA/Green)
256 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
257 :mass 1 :color ColorRGBA/Red)
258 connection-point (Vector3f. 1.2 0 0)
259 root (nodify [hand finger])]
261 (join-at-point hand finger (Vector3f. 1.2 0 0))
263 (.setCollisionGroup
264 (.getControl hand RigidBodyControl)
265 PhysicsCollisionObject/COLLISION_GROUP_NONE)
266 (world
267 root
268 standard-debug-controls
269 (fn [world]
270 (enable-debug world)
271 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
272 (set-gravity world Vector3f/ZERO)
273 )
274 no-op)))
275 (import java.awt.image.BufferedImage)
277 (defn draw-sprite [image sprite x y color ]
278 (dorun
279 (for [[u v] sprite]
280 (.setRGB image (+ u x) (+ v y) color))))
282 (defn view-angle
283 "create a debug view of an angle"
284 [color]
285 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
286 previous (atom [25 25])
287 sprite [[0 0] [0 1]
288 [0 -1] [-1 0] [1 0]]]
289 (fn [angle]
290 (let [angle (float angle)]
291 (let [position
292 [(+ 25 (int (* 20 (Math/cos angle))))
293 (+ 25 (int (* 20(Math/sin angle))))]]
294 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
295 (draw-sprite image sprite (position 0) (position 1) color)
296 (reset! previous position))
297 image))))
299 (defn proprioception-debug-window
300 []
301 (let [yaw (view-angle 0xFF0000)
302 roll (view-angle 0x00FF00)
303 pitch (view-angle 0xFFFFFF)
304 v-yaw (view-image)
305 v-roll (view-image)
306 v-pitch (view-image)
307 ]
308 (fn [prop-data]
309 (dorun
310 (map
311 (fn [[y r p]]
312 (v-yaw (yaw y))
313 (v-roll (roll r))
314 (v-pitch (pitch p)))
315 prop-data)))))
316 (comment
318 (defn proprioception-debug-window
319 []
320 (let [time (atom 0)]
321 (fn [prop-data]
322 (if (= 0 (rem (swap! time inc) 40))
323 (println-repl prop-data)))))
324 )
326 (comment
327 (dorun
328 (map
329 (comp
330 println-repl
331 (fn [[p y r]]
332 (format
333 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
334 p y r)))
335 prop-data)))
339 (defn tap [obj direction force]
340 (let [control (.getControl obj RigidBodyControl)]
341 (.applyTorque
342 control
343 (.mult (.getPhysicsRotation control)
344 (.mult (.normalize direction) (float force))))))
348 (defmacro with-movement
349 [object
350 [up down left right roll-up roll-down :as keyboard]
351 forces
352 [world-invocation
353 root-node
354 keymap
355 intilization
356 world-loop]]
357 (let [add-keypress
358 (fn [state keymap key]
359 `(merge ~keymap
360 {~key
361 (fn [_# pressed?#]
362 (reset! ~state pressed?#))}))
363 move-left? (gensym "move-left?")
364 move-right? (gensym "move-right?")
365 move-up? (gensym "move-up?")
366 move-down? (gensym "move-down?")
367 roll-left? (gensym "roll-left?")
368 roll-right? (gensym "roll-right?")
369 directions [[0 1 0][0 -1 0][0 0 1][0 0 -1][-1 0 0][1 0 0]]
370 symbols [move-left? move-right? move-up? move-down?
371 roll-left? roll-right?]
373 keymap* (vec (map #(add-keypress %1 keymap %2)
374 symbols
375 keyboard))
377 splice-loop (map (fn [sym direction force]
378 `(if (deref ~sym)
379 (tap ~object
380 (Vector3f. ~@direction)
381 ~force)))
382 symbols directions forces)
384 world-loop* `(fn [world# tpf#]
385 (~world-loop world# tpf#)
386 ~@splice-loop)]
388 `(let [~move-up? (atom false)
389 ~move-down? (atom false)
390 ~move-left? (atom false)
391 ~move-right? (atom false)
392 ~roll-left? (atom false)
393 ~roll-right? (atom false)]
394 (~world-invocation
395 ~root-node
396 (reduce merge ~keymap*)
397 ~intilization
398 ~world-loop*))))
401 (defn test-proprioception
402 "Testing proprioception:
403 You should see two foating bars, and a printout of pitch, yaw, and
404 roll. Pressing key-r/key-t should move the blue bar up and down and
405 change only the value of pitch. key-f/key-g moves it side to side
406 and changes yaw. key-v/key-b will spin the blue segment clockwise
407 and counterclockwise, and only affect roll."
408 []
409 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
410 :mass 0 :color ColorRGBA/Green :name "hand")
411 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
412 :mass 1 :color ColorRGBA/Red :name "finger")
413 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
414 :position (Vector3f. 1.2 2 0)
415 :physical? false)
416 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
417 creature (nodify [hand finger joint-node])
418 ;; *******************************************
420 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
421 :mass 0 :color ColorRGBA/Gray)
423 root (nodify [creature floor])
424 prop (joint-proprioception creature joint-node)
425 prop-view (proprioception-debug-window)]
427 (.setCollisionGroup
428 (.getControl hand RigidBodyControl)
429 PhysicsCollisionObject/COLLISION_GROUP_NONE)
431 (with-movement
432 finger
433 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
434 [10 10 10 10 1 1]
435 (world
436 root
437 standard-debug-controls
438 (fn [world]
439 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
440 (set-gravity world (Vector3f. 0 0 0))
441 (light-up-everything world))
442 (fn [_ _] (prop-view (list (prop))))))))
447 #+end_src
449 #+results: test-body
450 : #'cortex.test.body/test-proprioception
453 * COMMENT code-limbo
454 #+begin_src clojure
455 ;;(.loadModel
456 ;; (doto (asset-manager)
457 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
458 ;; "Models/person/person.blend")
461 (defn load-blender-model
462 "Load a .blend file using an asset folder relative path."
463 [^String model]
464 (.loadModel
465 (doto (asset-manager)
466 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
467 model))
470 (defn view-model [^String model]
471 (view
472 (.loadModel
473 (doto (asset-manager)
474 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
475 model)))
477 (defn load-blender-scene [^String model]
478 (.loadModel
479 (doto (asset-manager)
480 (.registerLoader BlenderLoader (into-array String ["blend"])))
481 model))
483 (defn worm
484 []
485 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
487 (defn oto
488 []
489 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
491 (defn sinbad
492 []
493 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
495 (defn worm-blender
496 []
497 (first (seq (.getChildren (load-blender-model
498 "Models/anim2/simple-worm.blend")))))
500 (defn body
501 "given a node with a SkeletonControl, will produce a body sutiable
502 for AI control with movement and proprioception."
503 [node]
504 (let [skeleton-control (.getControl node SkeletonControl)
505 krc (KinematicRagdollControl.)]
506 (comment
507 (dorun
508 (map #(.addBoneName krc %)
509 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
510 ;;"mid2" "mid3" "tail" "head"]
511 )))
512 (.addControl node krc)
513 (.setRagdollMode krc)
514 )
515 node
516 )
517 (defn show-skeleton [node]
518 (let [sd
520 (doto
521 (SkeletonDebugger. "aurellem-skel-debug"
522 (skel node))
523 (.setMaterial (green-x-ray)))]
524 (.attachChild node sd)
525 node))
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 ;; this could be a good way to give objects special properties like
532 ;; being eyes and the like
534 (.getUserData
535 (.getChild
536 (load-blender-model "Models/property/test.blend") 0)
537 "properties")
539 ;; the properties are saved along with the blender file.
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
545 (defn init-debug-skel-node
546 [f debug-node skeleton]
547 (let [bones
548 (map #(.getBone skeleton %)
549 (range (.getBoneCount skeleton)))]
550 (dorun (map #(.setUserControl % true) bones))
551 (dorun (map (fn [b]
552 (println (.getName b)
553 " -- " (f b)))
554 bones))
555 (dorun
556 (map #(.attachChild
557 debug-node
558 (doto
559 (sphere 0.1
560 :position (f %)
561 :physical? false)
562 (.setMaterial (green-x-ray))))
563 bones)))
564 debug-node)
566 (import jme3test.bullet.PhysicsTestHelper)
569 (defn test-zzz [the-worm world value]
570 (if (not value)
571 (let [skeleton (skel the-worm)]
572 (println-repl "enabling bones")
573 (dorun
574 (map
575 #(.setUserControl (.getBone skeleton %) true)
576 (range (.getBoneCount skeleton))))
579 (let [b (.getBone skeleton 2)]
580 (println-repl "moving " (.getName b))
581 (println-repl (.getLocalPosition b))
582 (.setUserTransforms b
583 Vector3f/UNIT_X
584 Quaternion/IDENTITY
585 ;;(doto (Quaternion.)
586 ;; (.fromAngles (/ Math/PI 2)
587 ;; 0
588 ;; 0
590 (Vector3f. 1 1 1))
591 )
593 (println-repl "hi! <3"))))
596 (defn test-ragdoll []
598 (let [the-worm
600 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
601 (doto (show-skeleton (worm-blender))
602 (.setLocalTranslation (Vector3f. 0 10 0))
603 ;;(worm)
604 ;;(oto)
605 ;;(sinbad)
606 )
607 ]
610 (.start
611 (world
612 (doto (Node.)
613 (.attachChild the-worm))
614 {"key-return" (fire-cannon-ball)
615 "key-space" (partial test-zzz the-worm)
616 }
617 (fn [world]
618 (light-up-everything world)
619 (PhysicsTestHelper/createPhysicsTestWorld
620 (.getRootNode world)
621 (asset-manager)
622 (.getPhysicsSpace
623 (.getState (.getStateManager world) BulletAppState)))
624 (set-gravity world Vector3f/ZERO)
625 ;;(.setTimer world (NanoTimer.))
626 ;;(org.lwjgl.input.Mouse/setGrabbed false)
627 )
628 no-op
629 )
632 )))
635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
636 ;;; here is the ragdoll stuff
638 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
639 (def mesh worm-mesh)
641 (.getFloatBuffer mesh VertexBuffer$Type/Position)
642 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
643 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
646 (defn position [index]
647 (.get
648 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
649 index))
651 (defn bones [index]
652 (.get
653 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
654 index))
656 (defn bone-weights [index]
657 (.get
658 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
659 index))
663 (defn vertex-bones [vertex]
664 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
666 (defn vertex-weights [vertex]
667 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
669 (defn vertex-position [index]
670 (let [offset (* index 3)]
671 (Vector3f. (position offset)
672 (position (inc offset))
673 (position (inc(inc offset))))))
675 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
677 (defn bone-control-color [index]
678 (get {[1 0 0 0] ColorRGBA/Red
679 [1 2 0 0] ColorRGBA/Magenta
680 [2 0 0 0] ColorRGBA/Blue}
681 (vertex-bones index)
682 ColorRGBA/White))
684 (defn influence-color [index bone-num]
685 (get
686 {(float 0) ColorRGBA/Blue
687 (float 0.5) ColorRGBA/Green
688 (float 1) ColorRGBA/Red}
689 ;; find the weight of the desired bone
690 ((zipmap (vertex-bones index)(vertex-weights index))
691 bone-num)
692 ColorRGBA/Blue))
694 (def worm-vertices (set (map vertex-info (range 60))))
697 (defn test-info []
698 (let [points (Node.)]
699 (dorun
700 (map #(.attachChild points %)
701 (map #(sphere 0.01
702 :position (vertex-position %)
703 :color (influence-color % 1)
704 :physical? false)
705 (range 60))))
706 (view points)))
709 (defrecord JointControl [joint physics-space]
710 PhysicsControl
711 (setPhysicsSpace [this space]
712 (dosync
713 (ref-set (:physics-space this) space))
714 (.addJoint space (:joint this)))
715 (update [this tpf])
716 (setSpatial [this spatial])
717 (render [this rm vp])
718 (getPhysicsSpace [this] (deref (:physics-space this)))
719 (isEnabled [this] true)
720 (setEnabled [this state]))
722 (defn add-joint
723 "Add a joint to a particular object. When the object is added to the
724 PhysicsSpace of a simulation, the joint will also be added"
725 [object joint]
726 (let [control (JointControl. joint (ref nil))]
727 (.addControl object control))
728 object)
731 (defn hinge-world
732 []
733 (let [sphere1 (sphere)
734 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
735 joint (Point2PointJoint.
736 (.getControl sphere1 RigidBodyControl)
737 (.getControl sphere2 RigidBodyControl)
738 Vector3f/ZERO (Vector3f. 3 3 3))]
739 (add-joint sphere1 joint)
740 (doto (Node. "hinge-world")
741 (.attachChild sphere1)
742 (.attachChild sphere2))))
745 (defn test-joint []
746 (view (hinge-world)))
748 ;; (defn copier-gen []
749 ;; (let [count (atom 0)]
750 ;; (fn [in]
751 ;; (swap! count inc)
752 ;; (clojure.contrib.duck-streams/copy
753 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
754 ;; ;;/home/r/tmp/mao-test/clojure-images
755 ;; (format "%08d.png" @count)))))))
756 ;; (defn decrease-framerate []
757 ;; (map
758 ;; (copier-gen)
759 ;; (sort
760 ;; (map first
761 ;; (partition
762 ;; 4
763 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
764 ;; (file-seq
765 ;; (file-str
766 ;; "/home/r/media/anime/mao-temp/images"))))))))
770 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
772 (defn proprioception
773 "Create a proprioception map that reports the rotations of the
774 various limbs of the creature's body"
775 [creature]
776 [#^Node creature]
777 (let [
778 nodes (node-seq creature)
779 joints
780 (map
781 :joint
782 (filter
783 #(isa? (class %) JointControl)
784 (reduce
785 concat
786 (map (fn [node]
787 (map (fn [num] (.getControl node num))
788 (range (.getNumControls node))))
789 nodes))))]
790 (fn []
791 (reduce concat (map relative-positions (list (first joints)))))))
794 (defn skel [node]
795 (doto
796 (.getSkeleton
797 (.getControl node SkeletonControl))
798 ;; this is necessary to force the skeleton to have accurate world
799 ;; transforms before it is rendered to the screen.
800 (.resetAndUpdate)))
802 (defn green-x-ray []
803 (doto (Material. (asset-manager)
804 "Common/MatDefs/Misc/Unshaded.j3md")
805 (.setColor "Color" ColorRGBA/Green)
806 (-> (.getAdditionalRenderState)
807 (.setDepthTest false))))
809 (defn test-worm []
810 (.start
811 (world
812 (doto (Node.)
813 ;;(.attachChild (point-worm))
814 (.attachChild (load-blender-model
815 "Models/anim2/joint-worm.blend"))
817 (.attachChild (box 10 1 10
818 :position (Vector3f. 0 -2 0) :mass 0
819 :color (ColorRGBA/Gray))))
820 {
821 "key-space" (fire-cannon-ball)
822 }
823 (fn [world]
824 (enable-debug world)
825 (light-up-everything world)
826 ;;(.setTimer world (NanoTimer.))
827 )
828 no-op)))
832 ;; defunct movement stuff
833 (defn torque-controls [control]
834 (let [torques
835 (concat
836 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
837 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
838 [Vector3f/UNIT_X])]
839 (map (fn [torque-axis]
840 (fn [torque]
841 (.applyTorque
842 control
843 (.mult (.mult (.getPhysicsRotation control)
844 torque-axis)
845 (float
846 (* (.getMass control) torque))))))
847 torques)))
849 (defn motor-map
850 "Take a creature and generate a function that will enable fine
851 grained control over all the creature's limbs."
852 [#^Node creature]
853 (let [controls (keep #(.getControl % RigidBodyControl)
854 (node-seq creature))
855 limb-controls (reduce concat (map torque-controls controls))
856 body-control (partial map #(%1 %2) limb-controls)]
857 body-control))
859 (defn test-motor-map
860 "see how torque works."
861 []
862 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
863 :mass 1 :color ColorRGBA/Green)
864 motor-map (motor-map finger)]
865 (world
866 (nodify [finger
867 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
868 :color ColorRGBA/Gray)])
869 standard-debug-controls
870 (fn [world]
871 (set-gravity world Vector3f/ZERO)
872 (light-up-everything world)
873 (.setTimer world (NanoTimer.)))
874 (fn [_ _]
875 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
876 #+end_src
884 * COMMENT generate Source
885 #+begin_src clojure :tangle ../src/cortex/body.clj
886 <<proprioception>>
887 <<motor-control>>
888 #+end_src
890 #+begin_src clojure :tangle ../src/cortex/test/body.clj
891 <<test-body>>
892 #+end_src