view org/body.org @ 139:ffbab4199c0d

going to de-macroify debug code
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 00:57:18 -0700
parents 16bdf9e80daf
children 22444eb20ecc
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)]
387 `(let [~move-up? (atom false)
388 ~move-down? (atom false)
389 ~move-left? (atom false)
390 ~move-right? (atom false)
391 ~roll-left? (atom false)
392 ~roll-right? (atom false)]
393 (~world-invocation
394 ~root-node
395 (reduce merge ~keymap*)
396 ~intilization
397 ~world-loop*))))
400 (defn test-proprioception
401 "Testing proprioception:
402 You should see two foating bars, and a printout of pitch, yaw, and
403 roll. Pressing key-r/key-t should move the blue bar up and down and
404 change only the value of pitch. key-f/key-g moves it side to side
405 and changes yaw. key-v/key-b will spin the blue segment clockwise
406 and counterclockwise, and only affect roll."
407 []
408 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
409 :mass 1 :color ColorRGBA/Green :name "hand")
410 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
411 :mass 1 :color ColorRGBA/Red :name "finger")
412 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
413 :position (Vector3f. 1.2 2 0)
414 :physical? false)
415 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
416 creature (nodify [hand finger joint-node])
417 ;; *******************************************
419 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
420 :mass 0 :color ColorRGBA/Gray)
422 root (nodify [creature floor])
423 prop (joint-proprioception creature joint-node)
424 prop-view (proprioception-debug-window)
425 finger-control (.getControl finger RigidBodyControl)
426 hand-control (.getControl hand RigidBodyControl)
428 controls
429 (merge standard-debug-controls
430 {"key-y"
431 (fn [_ _] (.setEnabled finger-control true))
432 "key-u"
433 (fn [_ _] (.setEnabled finger-control false))
434 "key-i"
435 (fn [_ _] (.setEnabled hand-control true))
436 "key-o"
437 (fn [_ _] (.setEnabled hand-control false))
438 "key-q"
439 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))
440 }
441 )
443 ]
444 (comment
445 (.setCollisionGroup
446 (.getControl hand RigidBodyControl)
447 PhysicsCollisionObject/COLLISION_GROUP_NONE)
448 )
450 (with-movement
451 hand
452 ["key-w" "key-e" "key-s" "key-d" "key-x" "key-c"]
453 [10 10 10 10 1 1]
454 (with-movement
455 finger
456 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
457 [10 10 10 10 1 1]
458 (world
459 root
460 controls
461 (fn [world]
462 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
463 (set-gravity world (Vector3f. 0 0 0))
464 (light-up-everything world))
465 (fn [_ _] (prop-view (list (prop)))))))))
470 #+end_src
472 #+results: test-body
473 : #'cortex.test.body/test-proprioception
476 * COMMENT code-limbo
477 #+begin_src clojure
478 ;;(.loadModel
479 ;; (doto (asset-manager)
480 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
481 ;; "Models/person/person.blend")
484 (defn load-blender-model
485 "Load a .blend file using an asset folder relative path."
486 [^String model]
487 (.loadModel
488 (doto (asset-manager)
489 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
490 model))
493 (defn view-model [^String model]
494 (view
495 (.loadModel
496 (doto (asset-manager)
497 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
498 model)))
500 (defn load-blender-scene [^String model]
501 (.loadModel
502 (doto (asset-manager)
503 (.registerLoader BlenderLoader (into-array String ["blend"])))
504 model))
506 (defn worm
507 []
508 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
510 (defn oto
511 []
512 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
514 (defn sinbad
515 []
516 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
518 (defn worm-blender
519 []
520 (first (seq (.getChildren (load-blender-model
521 "Models/anim2/simple-worm.blend")))))
523 (defn body
524 "given a node with a SkeletonControl, will produce a body sutiable
525 for AI control with movement and proprioception."
526 [node]
527 (let [skeleton-control (.getControl node SkeletonControl)
528 krc (KinematicRagdollControl.)]
529 (comment
530 (dorun
531 (map #(.addBoneName krc %)
532 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
533 ;;"mid2" "mid3" "tail" "head"]
534 )))
535 (.addControl node krc)
536 (.setRagdollMode krc)
537 )
538 node
539 )
540 (defn show-skeleton [node]
541 (let [sd
543 (doto
544 (SkeletonDebugger. "aurellem-skel-debug"
545 (skel node))
546 (.setMaterial (green-x-ray)))]
547 (.attachChild node sd)
548 node))
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 ;; this could be a good way to give objects special properties like
555 ;; being eyes and the like
557 (.getUserData
558 (.getChild
559 (load-blender-model "Models/property/test.blend") 0)
560 "properties")
562 ;; the properties are saved along with the blender file.
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
568 (defn init-debug-skel-node
569 [f debug-node skeleton]
570 (let [bones
571 (map #(.getBone skeleton %)
572 (range (.getBoneCount skeleton)))]
573 (dorun (map #(.setUserControl % true) bones))
574 (dorun (map (fn [b]
575 (println (.getName b)
576 " -- " (f b)))
577 bones))
578 (dorun
579 (map #(.attachChild
580 debug-node
581 (doto
582 (sphere 0.1
583 :position (f %)
584 :physical? false)
585 (.setMaterial (green-x-ray))))
586 bones)))
587 debug-node)
589 (import jme3test.bullet.PhysicsTestHelper)
592 (defn test-zzz [the-worm world value]
593 (if (not value)
594 (let [skeleton (skel the-worm)]
595 (println-repl "enabling bones")
596 (dorun
597 (map
598 #(.setUserControl (.getBone skeleton %) true)
599 (range (.getBoneCount skeleton))))
602 (let [b (.getBone skeleton 2)]
603 (println-repl "moving " (.getName b))
604 (println-repl (.getLocalPosition b))
605 (.setUserTransforms b
606 Vector3f/UNIT_X
607 Quaternion/IDENTITY
608 ;;(doto (Quaternion.)
609 ;; (.fromAngles (/ Math/PI 2)
610 ;; 0
611 ;; 0
613 (Vector3f. 1 1 1))
614 )
616 (println-repl "hi! <3"))))
619 (defn test-ragdoll []
621 (let [the-worm
623 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
624 (doto (show-skeleton (worm-blender))
625 (.setLocalTranslation (Vector3f. 0 10 0))
626 ;;(worm)
627 ;;(oto)
628 ;;(sinbad)
629 )
630 ]
633 (.start
634 (world
635 (doto (Node.)
636 (.attachChild the-worm))
637 {"key-return" (fire-cannon-ball)
638 "key-space" (partial test-zzz the-worm)
639 }
640 (fn [world]
641 (light-up-everything world)
642 (PhysicsTestHelper/createPhysicsTestWorld
643 (.getRootNode world)
644 (asset-manager)
645 (.getPhysicsSpace
646 (.getState (.getStateManager world) BulletAppState)))
647 (set-gravity world Vector3f/ZERO)
648 ;;(.setTimer world (NanoTimer.))
649 ;;(org.lwjgl.input.Mouse/setGrabbed false)
650 )
651 no-op
652 )
655 )))
658 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
659 ;;; here is the ragdoll stuff
661 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
662 (def mesh worm-mesh)
664 (.getFloatBuffer mesh VertexBuffer$Type/Position)
665 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
666 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
669 (defn position [index]
670 (.get
671 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
672 index))
674 (defn bones [index]
675 (.get
676 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
677 index))
679 (defn bone-weights [index]
680 (.get
681 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
682 index))
686 (defn vertex-bones [vertex]
687 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
689 (defn vertex-weights [vertex]
690 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
692 (defn vertex-position [index]
693 (let [offset (* index 3)]
694 (Vector3f. (position offset)
695 (position (inc offset))
696 (position (inc(inc offset))))))
698 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
700 (defn bone-control-color [index]
701 (get {[1 0 0 0] ColorRGBA/Red
702 [1 2 0 0] ColorRGBA/Magenta
703 [2 0 0 0] ColorRGBA/Blue}
704 (vertex-bones index)
705 ColorRGBA/White))
707 (defn influence-color [index bone-num]
708 (get
709 {(float 0) ColorRGBA/Blue
710 (float 0.5) ColorRGBA/Green
711 (float 1) ColorRGBA/Red}
712 ;; find the weight of the desired bone
713 ((zipmap (vertex-bones index)(vertex-weights index))
714 bone-num)
715 ColorRGBA/Blue))
717 (def worm-vertices (set (map vertex-info (range 60))))
720 (defn test-info []
721 (let [points (Node.)]
722 (dorun
723 (map #(.attachChild points %)
724 (map #(sphere 0.01
725 :position (vertex-position %)
726 :color (influence-color % 1)
727 :physical? false)
728 (range 60))))
729 (view points)))
732 (defrecord JointControl [joint physics-space]
733 PhysicsControl
734 (setPhysicsSpace [this space]
735 (dosync
736 (ref-set (:physics-space this) space))
737 (.addJoint space (:joint this)))
738 (update [this tpf])
739 (setSpatial [this spatial])
740 (render [this rm vp])
741 (getPhysicsSpace [this] (deref (:physics-space this)))
742 (isEnabled [this] true)
743 (setEnabled [this state]))
745 (defn add-joint
746 "Add a joint to a particular object. When the object is added to the
747 PhysicsSpace of a simulation, the joint will also be added"
748 [object joint]
749 (let [control (JointControl. joint (ref nil))]
750 (.addControl object control))
751 object)
754 (defn hinge-world
755 []
756 (let [sphere1 (sphere)
757 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
758 joint (Point2PointJoint.
759 (.getControl sphere1 RigidBodyControl)
760 (.getControl sphere2 RigidBodyControl)
761 Vector3f/ZERO (Vector3f. 3 3 3))]
762 (add-joint sphere1 joint)
763 (doto (Node. "hinge-world")
764 (.attachChild sphere1)
765 (.attachChild sphere2))))
768 (defn test-joint []
769 (view (hinge-world)))
771 ;; (defn copier-gen []
772 ;; (let [count (atom 0)]
773 ;; (fn [in]
774 ;; (swap! count inc)
775 ;; (clojure.contrib.duck-streams/copy
776 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
777 ;; ;;/home/r/tmp/mao-test/clojure-images
778 ;; (format "%08d.png" @count)))))))
779 ;; (defn decrease-framerate []
780 ;; (map
781 ;; (copier-gen)
782 ;; (sort
783 ;; (map first
784 ;; (partition
785 ;; 4
786 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
787 ;; (file-seq
788 ;; (file-str
789 ;; "/home/r/media/anime/mao-temp/images"))))))))
793 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
795 (defn proprioception
796 "Create a proprioception map that reports the rotations of the
797 various limbs of the creature's body"
798 [creature]
799 [#^Node creature]
800 (let [
801 nodes (node-seq creature)
802 joints
803 (map
804 :joint
805 (filter
806 #(isa? (class %) JointControl)
807 (reduce
808 concat
809 (map (fn [node]
810 (map (fn [num] (.getControl node num))
811 (range (.getNumControls node))))
812 nodes))))]
813 (fn []
814 (reduce concat (map relative-positions (list (first joints)))))))
817 (defn skel [node]
818 (doto
819 (.getSkeleton
820 (.getControl node SkeletonControl))
821 ;; this is necessary to force the skeleton to have accurate world
822 ;; transforms before it is rendered to the screen.
823 (.resetAndUpdate)))
825 (defn green-x-ray []
826 (doto (Material. (asset-manager)
827 "Common/MatDefs/Misc/Unshaded.j3md")
828 (.setColor "Color" ColorRGBA/Green)
829 (-> (.getAdditionalRenderState)
830 (.setDepthTest false))))
832 (defn test-worm []
833 (.start
834 (world
835 (doto (Node.)
836 ;;(.attachChild (point-worm))
837 (.attachChild (load-blender-model
838 "Models/anim2/joint-worm.blend"))
840 (.attachChild (box 10 1 10
841 :position (Vector3f. 0 -2 0) :mass 0
842 :color (ColorRGBA/Gray))))
843 {
844 "key-space" (fire-cannon-ball)
845 }
846 (fn [world]
847 (enable-debug world)
848 (light-up-everything world)
849 ;;(.setTimer world (NanoTimer.))
850 )
851 no-op)))
855 ;; defunct movement stuff
856 (defn torque-controls [control]
857 (let [torques
858 (concat
859 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
860 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
861 [Vector3f/UNIT_X])]
862 (map (fn [torque-axis]
863 (fn [torque]
864 (.applyTorque
865 control
866 (.mult (.mult (.getPhysicsRotation control)
867 torque-axis)
868 (float
869 (* (.getMass control) torque))))))
870 torques)))
872 (defn motor-map
873 "Take a creature and generate a function that will enable fine
874 grained control over all the creature's limbs."
875 [#^Node creature]
876 (let [controls (keep #(.getControl % RigidBodyControl)
877 (node-seq creature))
878 limb-controls (reduce concat (map torque-controls controls))
879 body-control (partial map #(%1 %2) limb-controls)]
880 body-control))
882 (defn test-motor-map
883 "see how torque works."
884 []
885 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
886 :mass 1 :color ColorRGBA/Green)
887 motor-map (motor-map finger)]
888 (world
889 (nodify [finger
890 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
891 :color ColorRGBA/Gray)])
892 standard-debug-controls
893 (fn [world]
894 (set-gravity world Vector3f/ZERO)
895 (light-up-everything world)
896 (.setTimer world (NanoTimer.)))
897 (fn [_ _]
898 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
899 #+end_src
907 * COMMENT generate Source
908 #+begin_src clojure :tangle ../src/cortex/body.clj
909 <<proprioception>>
910 <<motor-control>>
911 #+end_src
913 #+begin_src clojure :tangle ../src/cortex/test/body.clj
914 <<test-body>>
915 #+end_src