view org/body.org @ 140:22444eb20ecc

de-macrofication complete.
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 01:32:31 -0700
parents ffbab4199c0d
children 22e193b5c60f
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 (defn with-movement
349 [object
350 [up down left right roll-up roll-down :as keyboard]
351 forces
352 [root-node
353 keymap
354 intilization
355 world-loop]]
356 (let [add-keypress
357 (fn [state keymap key]
358 (merge keymap
359 {key
360 (fn [_ pressed?]
361 (reset! state pressed?))}))
362 move-up? (atom false)
363 move-down? (atom false)
364 move-left? (atom false)
365 move-right? (atom false)
366 roll-left? (atom false)
367 roll-right? (atom false)
369 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
370 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
371 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
372 atoms [move-left? move-right? move-up? move-down?
373 roll-left? roll-right?]
375 keymap* (reduce merge
376 (map #(add-keypress %1 keymap %2)
377 atoms
378 keyboard))
380 splice-loop (fn []
381 (dorun
382 (map
383 (fn [sym direction force]
384 (if @sym
385 (tap object direction force)))
386 atoms directions forces)))
388 world-loop* (fn [world tpf]
389 (world-loop world tpf)
390 (splice-loop))]
392 [root-node
393 keymap*
394 intilization
395 world-loop*]))
398 (defn test-proprioception
399 "Testing proprioception:
400 You should see two foating bars, and a printout of pitch, yaw, and
401 roll. Pressing key-r/key-t should move the blue bar up and down and
402 change only the value of pitch. key-f/key-g moves it side to side
403 and changes yaw. key-v/key-b will spin the blue segment clockwise
404 and counterclockwise, and only affect roll."
405 []
406 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
407 :mass 1 :color ColorRGBA/Green :name "hand")
408 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
409 :mass 1 :color ColorRGBA/Red :name "finger")
410 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
411 :position (Vector3f. 1.2 2 0)
412 :physical? false)
413 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
414 creature (nodify [hand finger joint-node])
415 ;; *******************************************
417 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
418 :mass 0 :color ColorRGBA/Gray)
420 root (nodify [creature floor])
421 prop (joint-proprioception creature joint-node)
422 prop-view (proprioception-debug-window)
423 finger-control (.getControl finger RigidBodyControl)
424 hand-control (.getControl hand RigidBodyControl)
426 controls
427 (merge standard-debug-controls
428 {"key-o"
429 (fn [_ _] (.setEnabled finger-control true))
430 "key-p"
431 (fn [_ _] (.setEnabled finger-control false))
432 "key-k"
433 (fn [_ _] (.setEnabled hand-control true))
434 "key-l"
435 (fn [_ _] (.setEnabled hand-control false))
436 "key-i"
437 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))
438 }
439 )
441 ]
442 (comment
443 (.setCollisionGroup
444 (.getControl hand RigidBodyControl)
445 PhysicsCollisionObject/COLLISION_GROUP_NONE)
446 )
447 (apply
448 world
449 (with-movement
450 hand
451 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]
452 [10 10 10 10 1 1]
453 (with-movement
454 finger
455 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
456 [10 10 10 10 1 1]
457 [root
458 controls
459 (fn [world]
460 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
461 (set-gravity world (Vector3f. 0 0 0))
462 (light-up-everything world))
463 (fn [_ _] (prop-view (list (prop))))])))))
465 #+end_src
467 #+results: test-body
468 : #'cortex.test.body/test-proprioception
471 * COMMENT code-limbo
472 #+begin_src clojure
473 ;;(.loadModel
474 ;; (doto (asset-manager)
475 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
476 ;; "Models/person/person.blend")
479 (defn load-blender-model
480 "Load a .blend file using an asset folder relative path."
481 [^String model]
482 (.loadModel
483 (doto (asset-manager)
484 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
485 model))
488 (defn view-model [^String model]
489 (view
490 (.loadModel
491 (doto (asset-manager)
492 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
493 model)))
495 (defn load-blender-scene [^String model]
496 (.loadModel
497 (doto (asset-manager)
498 (.registerLoader BlenderLoader (into-array String ["blend"])))
499 model))
501 (defn worm
502 []
503 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
505 (defn oto
506 []
507 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
509 (defn sinbad
510 []
511 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
513 (defn worm-blender
514 []
515 (first (seq (.getChildren (load-blender-model
516 "Models/anim2/simple-worm.blend")))))
518 (defn body
519 "given a node with a SkeletonControl, will produce a body sutiable
520 for AI control with movement and proprioception."
521 [node]
522 (let [skeleton-control (.getControl node SkeletonControl)
523 krc (KinematicRagdollControl.)]
524 (comment
525 (dorun
526 (map #(.addBoneName krc %)
527 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
528 ;;"mid2" "mid3" "tail" "head"]
529 )))
530 (.addControl node krc)
531 (.setRagdollMode krc)
532 )
533 node
534 )
535 (defn show-skeleton [node]
536 (let [sd
538 (doto
539 (SkeletonDebugger. "aurellem-skel-debug"
540 (skel node))
541 (.setMaterial (green-x-ray)))]
542 (.attachChild node sd)
543 node))
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ;; this could be a good way to give objects special properties like
550 ;; being eyes and the like
552 (.getUserData
553 (.getChild
554 (load-blender-model "Models/property/test.blend") 0)
555 "properties")
557 ;; the properties are saved along with the blender file.
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
563 (defn init-debug-skel-node
564 [f debug-node skeleton]
565 (let [bones
566 (map #(.getBone skeleton %)
567 (range (.getBoneCount skeleton)))]
568 (dorun (map #(.setUserControl % true) bones))
569 (dorun (map (fn [b]
570 (println (.getName b)
571 " -- " (f b)))
572 bones))
573 (dorun
574 (map #(.attachChild
575 debug-node
576 (doto
577 (sphere 0.1
578 :position (f %)
579 :physical? false)
580 (.setMaterial (green-x-ray))))
581 bones)))
582 debug-node)
584 (import jme3test.bullet.PhysicsTestHelper)
587 (defn test-zzz [the-worm world value]
588 (if (not value)
589 (let [skeleton (skel the-worm)]
590 (println-repl "enabling bones")
591 (dorun
592 (map
593 #(.setUserControl (.getBone skeleton %) true)
594 (range (.getBoneCount skeleton))))
597 (let [b (.getBone skeleton 2)]
598 (println-repl "moving " (.getName b))
599 (println-repl (.getLocalPosition b))
600 (.setUserTransforms b
601 Vector3f/UNIT_X
602 Quaternion/IDENTITY
603 ;;(doto (Quaternion.)
604 ;; (.fromAngles (/ Math/PI 2)
605 ;; 0
606 ;; 0
608 (Vector3f. 1 1 1))
609 )
611 (println-repl "hi! <3"))))
614 (defn test-ragdoll []
616 (let [the-worm
618 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
619 (doto (show-skeleton (worm-blender))
620 (.setLocalTranslation (Vector3f. 0 10 0))
621 ;;(worm)
622 ;;(oto)
623 ;;(sinbad)
624 )
625 ]
628 (.start
629 (world
630 (doto (Node.)
631 (.attachChild the-worm))
632 {"key-return" (fire-cannon-ball)
633 "key-space" (partial test-zzz the-worm)
634 }
635 (fn [world]
636 (light-up-everything world)
637 (PhysicsTestHelper/createPhysicsTestWorld
638 (.getRootNode world)
639 (asset-manager)
640 (.getPhysicsSpace
641 (.getState (.getStateManager world) BulletAppState)))
642 (set-gravity world Vector3f/ZERO)
643 ;;(.setTimer world (NanoTimer.))
644 ;;(org.lwjgl.input.Mouse/setGrabbed false)
645 )
646 no-op
647 )
650 )))
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 ;;; here is the ragdoll stuff
656 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
657 (def mesh worm-mesh)
659 (.getFloatBuffer mesh VertexBuffer$Type/Position)
660 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
661 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
664 (defn position [index]
665 (.get
666 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
667 index))
669 (defn bones [index]
670 (.get
671 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
672 index))
674 (defn bone-weights [index]
675 (.get
676 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
677 index))
681 (defn vertex-bones [vertex]
682 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
684 (defn vertex-weights [vertex]
685 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
687 (defn vertex-position [index]
688 (let [offset (* index 3)]
689 (Vector3f. (position offset)
690 (position (inc offset))
691 (position (inc(inc offset))))))
693 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
695 (defn bone-control-color [index]
696 (get {[1 0 0 0] ColorRGBA/Red
697 [1 2 0 0] ColorRGBA/Magenta
698 [2 0 0 0] ColorRGBA/Blue}
699 (vertex-bones index)
700 ColorRGBA/White))
702 (defn influence-color [index bone-num]
703 (get
704 {(float 0) ColorRGBA/Blue
705 (float 0.5) ColorRGBA/Green
706 (float 1) ColorRGBA/Red}
707 ;; find the weight of the desired bone
708 ((zipmap (vertex-bones index)(vertex-weights index))
709 bone-num)
710 ColorRGBA/Blue))
712 (def worm-vertices (set (map vertex-info (range 60))))
715 (defn test-info []
716 (let [points (Node.)]
717 (dorun
718 (map #(.attachChild points %)
719 (map #(sphere 0.01
720 :position (vertex-position %)
721 :color (influence-color % 1)
722 :physical? false)
723 (range 60))))
724 (view points)))
727 (defrecord JointControl [joint physics-space]
728 PhysicsControl
729 (setPhysicsSpace [this space]
730 (dosync
731 (ref-set (:physics-space this) space))
732 (.addJoint space (:joint this)))
733 (update [this tpf])
734 (setSpatial [this spatial])
735 (render [this rm vp])
736 (getPhysicsSpace [this] (deref (:physics-space this)))
737 (isEnabled [this] true)
738 (setEnabled [this state]))
740 (defn add-joint
741 "Add a joint to a particular object. When the object is added to the
742 PhysicsSpace of a simulation, the joint will also be added"
743 [object joint]
744 (let [control (JointControl. joint (ref nil))]
745 (.addControl object control))
746 object)
749 (defn hinge-world
750 []
751 (let [sphere1 (sphere)
752 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
753 joint (Point2PointJoint.
754 (.getControl sphere1 RigidBodyControl)
755 (.getControl sphere2 RigidBodyControl)
756 Vector3f/ZERO (Vector3f. 3 3 3))]
757 (add-joint sphere1 joint)
758 (doto (Node. "hinge-world")
759 (.attachChild sphere1)
760 (.attachChild sphere2))))
763 (defn test-joint []
764 (view (hinge-world)))
766 ;; (defn copier-gen []
767 ;; (let [count (atom 0)]
768 ;; (fn [in]
769 ;; (swap! count inc)
770 ;; (clojure.contrib.duck-streams/copy
771 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
772 ;; ;;/home/r/tmp/mao-test/clojure-images
773 ;; (format "%08d.png" @count)))))))
774 ;; (defn decrease-framerate []
775 ;; (map
776 ;; (copier-gen)
777 ;; (sort
778 ;; (map first
779 ;; (partition
780 ;; 4
781 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
782 ;; (file-seq
783 ;; (file-str
784 ;; "/home/r/media/anime/mao-temp/images"))))))))
788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 (defn proprioception
791 "Create a proprioception map that reports the rotations of the
792 various limbs of the creature's body"
793 [creature]
794 [#^Node creature]
795 (let [
796 nodes (node-seq creature)
797 joints
798 (map
799 :joint
800 (filter
801 #(isa? (class %) JointControl)
802 (reduce
803 concat
804 (map (fn [node]
805 (map (fn [num] (.getControl node num))
806 (range (.getNumControls node))))
807 nodes))))]
808 (fn []
809 (reduce concat (map relative-positions (list (first joints)))))))
812 (defn skel [node]
813 (doto
814 (.getSkeleton
815 (.getControl node SkeletonControl))
816 ;; this is necessary to force the skeleton to have accurate world
817 ;; transforms before it is rendered to the screen.
818 (.resetAndUpdate)))
820 (defn green-x-ray []
821 (doto (Material. (asset-manager)
822 "Common/MatDefs/Misc/Unshaded.j3md")
823 (.setColor "Color" ColorRGBA/Green)
824 (-> (.getAdditionalRenderState)
825 (.setDepthTest false))))
827 (defn test-worm []
828 (.start
829 (world
830 (doto (Node.)
831 ;;(.attachChild (point-worm))
832 (.attachChild (load-blender-model
833 "Models/anim2/joint-worm.blend"))
835 (.attachChild (box 10 1 10
836 :position (Vector3f. 0 -2 0) :mass 0
837 :color (ColorRGBA/Gray))))
838 {
839 "key-space" (fire-cannon-ball)
840 }
841 (fn [world]
842 (enable-debug world)
843 (light-up-everything world)
844 ;;(.setTimer world (NanoTimer.))
845 )
846 no-op)))
850 ;; defunct movement stuff
851 (defn torque-controls [control]
852 (let [torques
853 (concat
854 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
855 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
856 [Vector3f/UNIT_X])]
857 (map (fn [torque-axis]
858 (fn [torque]
859 (.applyTorque
860 control
861 (.mult (.mult (.getPhysicsRotation control)
862 torque-axis)
863 (float
864 (* (.getMass control) torque))))))
865 torques)))
867 (defn motor-map
868 "Take a creature and generate a function that will enable fine
869 grained control over all the creature's limbs."
870 [#^Node creature]
871 (let [controls (keep #(.getControl % RigidBodyControl)
872 (node-seq creature))
873 limb-controls (reduce concat (map torque-controls controls))
874 body-control (partial map #(%1 %2) limb-controls)]
875 body-control))
877 (defn test-motor-map
878 "see how torque works."
879 []
880 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
881 :mass 1 :color ColorRGBA/Green)
882 motor-map (motor-map finger)]
883 (world
884 (nodify [finger
885 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
886 :color ColorRGBA/Gray)])
887 standard-debug-controls
888 (fn [world]
889 (set-gravity world Vector3f/ZERO)
890 (light-up-everything world)
891 (.setTimer world (NanoTimer.)))
892 (fn [_ _]
893 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
894 #+end_src
902 * COMMENT generate Source
903 #+begin_src clojure :tangle ../src/cortex/body.clj
904 <<proprioception>>
905 <<motor-control>>
906 #+end_src
908 #+begin_src clojure :tangle ../src/cortex/test/body.clj
909 <<test-body>>
910 #+end_src