view org/body.org @ 141:22e193b5c60f

moved with-movement to cortex.body
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 01:35:21 -0700
parents 22444eb20ecc
children ccd057319c2a
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 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