view org/body.org @ 142:ccd057319c2a

improved proprioception test
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 02:03:19 -0700
parents 22e193b5c60f
children 48f9cba082eb
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 (defn tap [obj direction force]
111 (let [control (.getControl obj RigidBodyControl)]
112 (.applyTorque
113 control
114 (.mult (.getPhysicsRotation control)
115 (.mult (.normalize direction) (float force))))))
118 (defn with-movement
119 [object
120 [up down left right roll-up roll-down :as keyboard]
121 forces
122 [root-node
123 keymap
124 intilization
125 world-loop]]
126 (let [add-keypress
127 (fn [state keymap key]
128 (merge keymap
129 {key
130 (fn [_ pressed?]
131 (reset! state pressed?))}))
132 move-up? (atom false)
133 move-down? (atom false)
134 move-left? (atom false)
135 move-right? (atom false)
136 roll-left? (atom false)
137 roll-right? (atom false)
139 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
140 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
141 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
142 atoms [move-left? move-right? move-up? move-down?
143 roll-left? roll-right?]
145 keymap* (reduce merge
146 (map #(add-keypress %1 keymap %2)
147 atoms
148 keyboard))
150 splice-loop (fn []
151 (dorun
152 (map
153 (fn [sym direction force]
154 (if @sym
155 (tap object direction force)))
156 atoms directions forces)))
158 world-loop* (fn [world tpf]
159 (world-loop world tpf)
160 (splice-loop))]
162 [root-node
163 keymap*
164 intilization
165 world-loop*]))
168 #+end_src
170 #+results: proprioception
171 : #'cortex.body/proprioception
173 * Motor Control
174 #+name: motor-control
175 #+begin_src clojure
176 (in-ns 'cortex.body)
178 ;; surprisingly enough, terristerial creatures only move by using
179 ;; torque applied about their joints. There's not a single straight
180 ;; line of force in the human body at all! (A straight line of force
181 ;; would correspond to some sort of jet or rocket propulseion.)
183 (defn vector-motor-control
184 "Create a function that accepts a sequence of Vector3f objects that
185 describe the torque to be applied to each part of the body."
186 [body]
187 (let [nodes (node-seq body)
188 controls (keep #(.getControl % RigidBodyControl) nodes)]
189 (fn [torques]
190 (map #(.applyTorque %1 %2)
191 controls torques))))
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 #+end_src
195 ## note -- might want to add a lower dimensional, discrete version of
196 ## this if it proves useful from a x-modal clustering perspective.
198 * Examples
200 #+name: test-body
201 #+begin_src clojure
202 (ns cortex.test.body
203 (:use (cortex world util body))
204 (:require cortex.silly)
205 (:import
206 com.jme3.math.Vector3f
207 com.jme3.math.ColorRGBA
208 com.jme3.bullet.joints.Point2PointJoint
209 com.jme3.bullet.control.RigidBodyControl
210 com.jme3.system.NanoTimer))
212 (defn worm-segments
213 "Create multiple evenly spaced box segments. They're fabulous!"
214 [segment-length num-segments interstitial-space radius]
215 (letfn [(nth-segment
216 [n]
217 (box segment-length radius radius :mass 0.1
218 :position
219 (Vector3f.
220 (* 2 n (+ interstitial-space segment-length)) 0 0)
221 :name (str "worm-segment" n)
222 :color (ColorRGBA/randomColor)))]
223 (map nth-segment (range num-segments))))
225 (defn connect-at-midpoint
226 "Connect two physics objects with a Point2Point joint constraint at
227 the point equidistant from both objects' centers."
228 [segmentA segmentB]
229 (let [centerA (.getWorldTranslation segmentA)
230 centerB (.getWorldTranslation segmentB)
231 midpoint (.mult (.add centerA centerB) (float 0.5))
232 pivotA (.subtract midpoint centerA)
233 pivotB (.subtract midpoint centerB)
235 ;; A side-effect of creating a joint registers
236 ;; it with both physics objects which in turn
237 ;; will register the joint with the physics system
238 ;; when the simulation is started.
239 joint (Point2PointJoint.
240 (.getControl segmentA RigidBodyControl)
241 (.getControl segmentB RigidBodyControl)
242 pivotA
243 pivotB)]
244 segmentB))
246 (defn eve-worm
247 "Create a worm-like body bound by invisible joint constraints."
248 []
249 (let [segments (worm-segments 0.2 5 0.1 0.1)]
250 (dorun (map (partial apply connect-at-midpoint)
251 (partition 2 1 segments)))
252 (nodify "worm" segments)))
254 (defn worm-pattern
255 "This is a simple, mindless motor control pattern that drives the
256 second segment of the worm's body at an offset angle with
257 sinusoidally varying strength."
258 [time]
259 (let [angle (* Math/PI (/ 9 20))
260 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
261 [Vector3f/ZERO
262 (.mult
263 direction
264 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
265 Vector3f/ZERO
266 Vector3f/ZERO
267 Vector3f/ZERO]))
269 (defn test-motor-control
270 "Testing motor-control:
271 You should see a multi-segmented worm-like object fall onto the
272 table and begin writhing and moving."
273 []
274 (let [worm (eve-worm)
275 time (atom 0)
276 worm-motor-map (vector-motor-control worm)]
277 (world
278 (nodify [worm
279 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
280 :color ColorRGBA/Gray)])
281 standard-debug-controls
282 (fn [world]
283 (enable-debug world)
284 (light-up-everything world)
285 (comment
286 (com.aurellem.capture.Capture/captureVideo
287 world
288 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
289 )
291 (fn [_ _]
292 (swap! time inc)
293 (Thread/sleep 20)
294 (dorun (worm-motor-map
295 (worm-pattern @time)))))))
299 (defn join-at-point [obj-a obj-b world-pivot]
300 (cortex.silly/joint-dispatch
301 {:type :point}
302 (.getControl obj-a RigidBodyControl)
303 (.getControl obj-b RigidBodyControl)
304 (cortex.silly/world-to-local obj-a world-pivot)
305 (cortex.silly/world-to-local obj-b world-pivot)
306 nil
307 ))
309 (import com.jme3.bullet.collision.PhysicsCollisionObject)
311 (defn blab-* []
312 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
313 :mass 0 :color ColorRGBA/Green)
314 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
315 :mass 1 :color ColorRGBA/Red)
316 connection-point (Vector3f. 1.2 0 0)
317 root (nodify [hand finger])]
319 (join-at-point hand finger (Vector3f. 1.2 0 0))
321 (.setCollisionGroup
322 (.getControl hand RigidBodyControl)
323 PhysicsCollisionObject/COLLISION_GROUP_NONE)
324 (world
325 root
326 standard-debug-controls
327 (fn [world]
328 (enable-debug world)
329 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
330 (set-gravity world Vector3f/ZERO)
331 )
332 no-op)))
333 (import java.awt.image.BufferedImage)
335 (defn draw-sprite [image sprite x y color ]
336 (dorun
337 (for [[u v] sprite]
338 (.setRGB image (+ u x) (+ v y) color))))
340 (defn view-angle
341 "create a debug view of an angle"
342 [color]
343 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
344 previous (atom [25 25])
345 sprite [[0 0] [0 1]
346 [0 -1] [-1 0] [1 0]]]
347 (fn [angle]
348 (let [angle (float angle)]
349 (let [position
350 [(+ 25 (int (* 20 (Math/cos angle))))
351 (+ 25 (int (* 20(Math/sin angle))))]]
352 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
353 (draw-sprite image sprite (position 0) (position 1) color)
354 (reset! previous position))
355 image))))
357 (defn proprioception-debug-window
358 []
359 (let [yaw (view-angle 0xFF0000)
360 roll (view-angle 0x00FF00)
361 pitch (view-angle 0xFFFFFF)
362 v-yaw (view-image)
363 v-roll (view-image)
364 v-pitch (view-image)
365 ]
366 (fn [prop-data]
367 (dorun
368 (map
369 (fn [[y r p]]
370 (v-yaw (yaw y))
371 (v-roll (roll r))
372 (v-pitch (pitch p)))
373 prop-data)))))
374 (comment
376 (defn proprioception-debug-window
377 []
378 (let [time (atom 0)]
379 (fn [prop-data]
380 (if (= 0 (rem (swap! time inc) 40))
381 (println-repl prop-data)))))
382 )
384 (comment
385 (dorun
386 (map
387 (comp
388 println-repl
389 (fn [[p y r]]
390 (format
391 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
392 p y r)))
393 prop-data)))
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 0 :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 "key-period"
439 (fn [world _]
440 (.setEnabled finger-control false)
441 (.setEnabled hand-control false)
442 (.rotate creature (doto (Quaternion.)
443 (.fromAngleAxis
444 (float (/ Math/PI 15))
445 (Vector3f. 0 0 -1))))
447 (.setEnabled finger-control true)
448 (.setEnabled hand-control true)
449 (set-gravity world (Vector3f. 0 0 0))
450 )
453 }
454 )
456 ]
457 (comment
458 (.setCollisionGroup
459 (.getControl hand RigidBodyControl)
460 PhysicsCollisionObject/COLLISION_GROUP_NONE)
461 )
462 (apply
463 world
464 (with-movement
465 hand
466 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]
467 [10 10 10 10 1 1]
468 (with-movement
469 finger
470 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
471 [10 10 10 10 1 1]
472 [root
473 controls
474 (fn [world]
475 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
476 (set-gravity world (Vector3f. 0 0 0))
477 (light-up-everything world))
478 (fn [_ _] (prop-view (list (prop))))])))))
480 #+end_src
482 #+results: test-body
483 : #'cortex.test.body/test-proprioception
486 * COMMENT code-limbo
487 #+begin_src clojure
488 ;;(.loadModel
489 ;; (doto (asset-manager)
490 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
491 ;; "Models/person/person.blend")
494 (defn load-blender-model
495 "Load a .blend file using an asset folder relative path."
496 [^String model]
497 (.loadModel
498 (doto (asset-manager)
499 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
500 model))
503 (defn view-model [^String model]
504 (view
505 (.loadModel
506 (doto (asset-manager)
507 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
508 model)))
510 (defn load-blender-scene [^String model]
511 (.loadModel
512 (doto (asset-manager)
513 (.registerLoader BlenderLoader (into-array String ["blend"])))
514 model))
516 (defn worm
517 []
518 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
520 (defn oto
521 []
522 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
524 (defn sinbad
525 []
526 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
528 (defn worm-blender
529 []
530 (first (seq (.getChildren (load-blender-model
531 "Models/anim2/simple-worm.blend")))))
533 (defn body
534 "given a node with a SkeletonControl, will produce a body sutiable
535 for AI control with movement and proprioception."
536 [node]
537 (let [skeleton-control (.getControl node SkeletonControl)
538 krc (KinematicRagdollControl.)]
539 (comment
540 (dorun
541 (map #(.addBoneName krc %)
542 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
543 ;;"mid2" "mid3" "tail" "head"]
544 )))
545 (.addControl node krc)
546 (.setRagdollMode krc)
547 )
548 node
549 )
550 (defn show-skeleton [node]
551 (let [sd
553 (doto
554 (SkeletonDebugger. "aurellem-skel-debug"
555 (skel node))
556 (.setMaterial (green-x-ray)))]
557 (.attachChild node sd)
558 node))
562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
564 ;; this could be a good way to give objects special properties like
565 ;; being eyes and the like
567 (.getUserData
568 (.getChild
569 (load-blender-model "Models/property/test.blend") 0)
570 "properties")
572 ;; the properties are saved along with the blender file.
573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
578 (defn init-debug-skel-node
579 [f debug-node skeleton]
580 (let [bones
581 (map #(.getBone skeleton %)
582 (range (.getBoneCount skeleton)))]
583 (dorun (map #(.setUserControl % true) bones))
584 (dorun (map (fn [b]
585 (println (.getName b)
586 " -- " (f b)))
587 bones))
588 (dorun
589 (map #(.attachChild
590 debug-node
591 (doto
592 (sphere 0.1
593 :position (f %)
594 :physical? false)
595 (.setMaterial (green-x-ray))))
596 bones)))
597 debug-node)
599 (import jme3test.bullet.PhysicsTestHelper)
602 (defn test-zzz [the-worm world value]
603 (if (not value)
604 (let [skeleton (skel the-worm)]
605 (println-repl "enabling bones")
606 (dorun
607 (map
608 #(.setUserControl (.getBone skeleton %) true)
609 (range (.getBoneCount skeleton))))
612 (let [b (.getBone skeleton 2)]
613 (println-repl "moving " (.getName b))
614 (println-repl (.getLocalPosition b))
615 (.setUserTransforms b
616 Vector3f/UNIT_X
617 Quaternion/IDENTITY
618 ;;(doto (Quaternion.)
619 ;; (.fromAngles (/ Math/PI 2)
620 ;; 0
621 ;; 0
623 (Vector3f. 1 1 1))
624 )
626 (println-repl "hi! <3"))))
629 (defn test-ragdoll []
631 (let [the-worm
633 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
634 (doto (show-skeleton (worm-blender))
635 (.setLocalTranslation (Vector3f. 0 10 0))
636 ;;(worm)
637 ;;(oto)
638 ;;(sinbad)
639 )
640 ]
643 (.start
644 (world
645 (doto (Node.)
646 (.attachChild the-worm))
647 {"key-return" (fire-cannon-ball)
648 "key-space" (partial test-zzz the-worm)
649 }
650 (fn [world]
651 (light-up-everything world)
652 (PhysicsTestHelper/createPhysicsTestWorld
653 (.getRootNode world)
654 (asset-manager)
655 (.getPhysicsSpace
656 (.getState (.getStateManager world) BulletAppState)))
657 (set-gravity world Vector3f/ZERO)
658 ;;(.setTimer world (NanoTimer.))
659 ;;(org.lwjgl.input.Mouse/setGrabbed false)
660 )
661 no-op
662 )
665 )))
668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
669 ;;; here is the ragdoll stuff
671 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
672 (def mesh worm-mesh)
674 (.getFloatBuffer mesh VertexBuffer$Type/Position)
675 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
676 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
679 (defn position [index]
680 (.get
681 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
682 index))
684 (defn bones [index]
685 (.get
686 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
687 index))
689 (defn bone-weights [index]
690 (.get
691 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
692 index))
696 (defn vertex-bones [vertex]
697 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
699 (defn vertex-weights [vertex]
700 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
702 (defn vertex-position [index]
703 (let [offset (* index 3)]
704 (Vector3f. (position offset)
705 (position (inc offset))
706 (position (inc(inc offset))))))
708 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
710 (defn bone-control-color [index]
711 (get {[1 0 0 0] ColorRGBA/Red
712 [1 2 0 0] ColorRGBA/Magenta
713 [2 0 0 0] ColorRGBA/Blue}
714 (vertex-bones index)
715 ColorRGBA/White))
717 (defn influence-color [index bone-num]
718 (get
719 {(float 0) ColorRGBA/Blue
720 (float 0.5) ColorRGBA/Green
721 (float 1) ColorRGBA/Red}
722 ;; find the weight of the desired bone
723 ((zipmap (vertex-bones index)(vertex-weights index))
724 bone-num)
725 ColorRGBA/Blue))
727 (def worm-vertices (set (map vertex-info (range 60))))
730 (defn test-info []
731 (let [points (Node.)]
732 (dorun
733 (map #(.attachChild points %)
734 (map #(sphere 0.01
735 :position (vertex-position %)
736 :color (influence-color % 1)
737 :physical? false)
738 (range 60))))
739 (view points)))
742 (defrecord JointControl [joint physics-space]
743 PhysicsControl
744 (setPhysicsSpace [this space]
745 (dosync
746 (ref-set (:physics-space this) space))
747 (.addJoint space (:joint this)))
748 (update [this tpf])
749 (setSpatial [this spatial])
750 (render [this rm vp])
751 (getPhysicsSpace [this] (deref (:physics-space this)))
752 (isEnabled [this] true)
753 (setEnabled [this state]))
755 (defn add-joint
756 "Add a joint to a particular object. When the object is added to the
757 PhysicsSpace of a simulation, the joint will also be added"
758 [object joint]
759 (let [control (JointControl. joint (ref nil))]
760 (.addControl object control))
761 object)
764 (defn hinge-world
765 []
766 (let [sphere1 (sphere)
767 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
768 joint (Point2PointJoint.
769 (.getControl sphere1 RigidBodyControl)
770 (.getControl sphere2 RigidBodyControl)
771 Vector3f/ZERO (Vector3f. 3 3 3))]
772 (add-joint sphere1 joint)
773 (doto (Node. "hinge-world")
774 (.attachChild sphere1)
775 (.attachChild sphere2))))
778 (defn test-joint []
779 (view (hinge-world)))
781 ;; (defn copier-gen []
782 ;; (let [count (atom 0)]
783 ;; (fn [in]
784 ;; (swap! count inc)
785 ;; (clojure.contrib.duck-streams/copy
786 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
787 ;; ;;/home/r/tmp/mao-test/clojure-images
788 ;; (format "%08d.png" @count)))))))
789 ;; (defn decrease-framerate []
790 ;; (map
791 ;; (copier-gen)
792 ;; (sort
793 ;; (map first
794 ;; (partition
795 ;; 4
796 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
797 ;; (file-seq
798 ;; (file-str
799 ;; "/home/r/media/anime/mao-temp/images"))))))))
803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
805 (defn proprioception
806 "Create a proprioception map that reports the rotations of the
807 various limbs of the creature's body"
808 [creature]
809 [#^Node creature]
810 (let [
811 nodes (node-seq creature)
812 joints
813 (map
814 :joint
815 (filter
816 #(isa? (class %) JointControl)
817 (reduce
818 concat
819 (map (fn [node]
820 (map (fn [num] (.getControl node num))
821 (range (.getNumControls node))))
822 nodes))))]
823 (fn []
824 (reduce concat (map relative-positions (list (first joints)))))))
827 (defn skel [node]
828 (doto
829 (.getSkeleton
830 (.getControl node SkeletonControl))
831 ;; this is necessary to force the skeleton to have accurate world
832 ;; transforms before it is rendered to the screen.
833 (.resetAndUpdate)))
835 (defn green-x-ray []
836 (doto (Material. (asset-manager)
837 "Common/MatDefs/Misc/Unshaded.j3md")
838 (.setColor "Color" ColorRGBA/Green)
839 (-> (.getAdditionalRenderState)
840 (.setDepthTest false))))
842 (defn test-worm []
843 (.start
844 (world
845 (doto (Node.)
846 ;;(.attachChild (point-worm))
847 (.attachChild (load-blender-model
848 "Models/anim2/joint-worm.blend"))
850 (.attachChild (box 10 1 10
851 :position (Vector3f. 0 -2 0) :mass 0
852 :color (ColorRGBA/Gray))))
853 {
854 "key-space" (fire-cannon-ball)
855 }
856 (fn [world]
857 (enable-debug world)
858 (light-up-everything world)
859 ;;(.setTimer world (NanoTimer.))
860 )
861 no-op)))
865 ;; defunct movement stuff
866 (defn torque-controls [control]
867 (let [torques
868 (concat
869 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
870 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
871 [Vector3f/UNIT_X])]
872 (map (fn [torque-axis]
873 (fn [torque]
874 (.applyTorque
875 control
876 (.mult (.mult (.getPhysicsRotation control)
877 torque-axis)
878 (float
879 (* (.getMass control) torque))))))
880 torques)))
882 (defn motor-map
883 "Take a creature and generate a function that will enable fine
884 grained control over all the creature's limbs."
885 [#^Node creature]
886 (let [controls (keep #(.getControl % RigidBodyControl)
887 (node-seq creature))
888 limb-controls (reduce concat (map torque-controls controls))
889 body-control (partial map #(%1 %2) limb-controls)]
890 body-control))
892 (defn test-motor-map
893 "see how torque works."
894 []
895 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
896 :mass 1 :color ColorRGBA/Green)
897 motor-map (motor-map finger)]
898 (world
899 (nodify [finger
900 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
901 :color ColorRGBA/Gray)])
902 standard-debug-controls
903 (fn [world]
904 (set-gravity world Vector3f/ZERO)
905 (light-up-everything world)
906 (.setTimer world (NanoTimer.)))
907 (fn [_ _]
908 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
909 #+end_src
917 * COMMENT generate Source
918 #+begin_src clojure :tangle ../src/cortex/body.clj
919 <<proprioception>>
920 <<motor-control>>
921 #+end_src
923 #+begin_src clojure :tangle ../src/cortex/test/body.clj
924 <<test-body>>
925 #+end_src