view org/body.org @ 144:48f9cba082eb

proprioception is position-dependent, investigating...
author Robert McIntyre <rlm@mit.edu>
date Thu, 02 Feb 2012 03:17:11 -0700
parents ccd057319c2a
children 7a49b81ca1bf
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 tmp-rot-a (.getWorldRotation obj-a)]
74 (println-repl "x:" (.mult tmp-rot-a x))
75 (println-repl "y:" (.mult tmp-rot-a y))
76 (println-repl "z:" (.mult tmp-rot-a z))
77 (println-repl "rot-a" (.getWorldRotation obj-a))
78 (println-repl "rot-b" (.getWorldRotation obj-b))
79 ;; this function will report proprioceptive information for the
80 ;; joint.
81 (fn []
82 ;; x is the "twist" axis, y and z are the "bend" axes
83 (let [rot-a (.getWorldRotation obj-a)
84 ;;inv-a (.inverse rot-a)
85 rot-b (.getWorldRotation obj-b)
86 ;;relative (.mult rot-b inv-a)
87 basis (doto (Matrix3f.)
88 (.setColumn 0 (.mult rot-a x))
89 (.setColumn 1 (.mult rot-a y))
90 (.setColumn 2 (.mult rot-a z)))
91 rotation-about-joint
92 (doto (Quaternion.)
93 (.fromRotationMatrix
94 (.mult (.invert basis)
95 (.toRotationMatrix rot-b))))
96 [yaw roll pitch]
97 (seq (.toAngles rotation-about-joint nil))]
98 ;;return euler angles of the quaternion around the new basis
99 [yaw roll pitch]
100 ))))
103 (defn proprioception
104 "Create a function that provides proprioceptive information about an
105 entire body."
106 [#^Node creature]
107 ;; extract the body's joints
108 (let [joints (creature-joints creature)
109 senses (map (partial joint-proprioception creature) joints)]
110 (fn []
111 (map #(%) senses))))
113 (defn tap [obj direction force]
114 (let [control (.getControl obj RigidBodyControl)]
115 (.applyTorque
116 control
117 (.mult (.getPhysicsRotation control)
118 (.mult (.normalize direction) (float force))))))
121 (defn with-movement
122 [object
123 [up down left right roll-up roll-down :as keyboard]
124 forces
125 [root-node
126 keymap
127 intilization
128 world-loop]]
129 (let [add-keypress
130 (fn [state keymap key]
131 (merge keymap
132 {key
133 (fn [_ pressed?]
134 (reset! state pressed?))}))
135 move-up? (atom false)
136 move-down? (atom false)
137 move-left? (atom false)
138 move-right? (atom false)
139 roll-left? (atom false)
140 roll-right? (atom false)
142 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
143 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
144 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
145 atoms [move-left? move-right? move-up? move-down?
146 roll-left? roll-right?]
148 keymap* (reduce merge
149 (map #(add-keypress %1 keymap %2)
150 atoms
151 keyboard))
153 splice-loop (fn []
154 (dorun
155 (map
156 (fn [sym direction force]
157 (if @sym
158 (tap object direction force)))
159 atoms directions forces)))
161 world-loop* (fn [world tpf]
162 (world-loop world tpf)
163 (splice-loop))]
165 [root-node
166 keymap*
167 intilization
168 world-loop*]))
171 #+end_src
173 #+results: proprioception
174 : #'cortex.body/proprioception
176 * Motor Control
177 #+name: motor-control
178 #+begin_src clojure
179 (in-ns 'cortex.body)
181 ;; surprisingly enough, terristerial creatures only move by using
182 ;; torque applied about their joints. There's not a single straight
183 ;; line of force in the human body at all! (A straight line of force
184 ;; would correspond to some sort of jet or rocket propulseion.)
186 (defn vector-motor-control
187 "Create a function that accepts a sequence of Vector3f objects that
188 describe the torque to be applied to each part of the body."
189 [body]
190 (let [nodes (node-seq body)
191 controls (keep #(.getControl % RigidBodyControl) nodes)]
192 (fn [torques]
193 (map #(.applyTorque %1 %2)
194 controls torques))))
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 #+end_src
198 ## note -- might want to add a lower dimensional, discrete version of
199 ## this if it proves useful from a x-modal clustering perspective.
201 * Examples
203 #+name: test-body
204 #+begin_src clojure
205 (ns cortex.test.body
206 (:use (cortex world util body))
207 (:require cortex.silly)
208 (:import
209 com.jme3.math.Vector3f
210 com.jme3.math.ColorRGBA
211 com.jme3.bullet.joints.Point2PointJoint
212 com.jme3.bullet.control.RigidBodyControl
213 com.jme3.system.NanoTimer))
215 (defn worm-segments
216 "Create multiple evenly spaced box segments. They're fabulous!"
217 [segment-length num-segments interstitial-space radius]
218 (letfn [(nth-segment
219 [n]
220 (box segment-length radius radius :mass 0.1
221 :position
222 (Vector3f.
223 (* 2 n (+ interstitial-space segment-length)) 0 0)
224 :name (str "worm-segment" n)
225 :color (ColorRGBA/randomColor)))]
226 (map nth-segment (range num-segments))))
228 (defn connect-at-midpoint
229 "Connect two physics objects with a Point2Point joint constraint at
230 the point equidistant from both objects' centers."
231 [segmentA segmentB]
232 (let [centerA (.getWorldTranslation segmentA)
233 centerB (.getWorldTranslation segmentB)
234 midpoint (.mult (.add centerA centerB) (float 0.5))
235 pivotA (.subtract midpoint centerA)
236 pivotB (.subtract midpoint centerB)
238 ;; A side-effect of creating a joint registers
239 ;; it with both physics objects which in turn
240 ;; will register the joint with the physics system
241 ;; when the simulation is started.
242 joint (Point2PointJoint.
243 (.getControl segmentA RigidBodyControl)
244 (.getControl segmentB RigidBodyControl)
245 pivotA
246 pivotB)]
247 segmentB))
249 (defn eve-worm
250 "Create a worm-like body bound by invisible joint constraints."
251 []
252 (let [segments (worm-segments 0.2 5 0.1 0.1)]
253 (dorun (map (partial apply connect-at-midpoint)
254 (partition 2 1 segments)))
255 (nodify "worm" segments)))
257 (defn worm-pattern
258 "This is a simple, mindless motor control pattern that drives the
259 second segment of the worm's body at an offset angle with
260 sinusoidally varying strength."
261 [time]
262 (let [angle (* Math/PI (/ 9 20))
263 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
264 [Vector3f/ZERO
265 (.mult
266 direction
267 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
268 Vector3f/ZERO
269 Vector3f/ZERO
270 Vector3f/ZERO]))
272 (defn test-motor-control
273 "Testing motor-control:
274 You should see a multi-segmented worm-like object fall onto the
275 table and begin writhing and moving."
276 []
277 (let [worm (eve-worm)
278 time (atom 0)
279 worm-motor-map (vector-motor-control worm)]
280 (world
281 (nodify [worm
282 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
283 :color ColorRGBA/Gray)])
284 standard-debug-controls
285 (fn [world]
286 (enable-debug world)
287 (light-up-everything world)
288 (comment
289 (com.aurellem.capture.Capture/captureVideo
290 world
291 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
292 )
294 (fn [_ _]
295 (swap! time inc)
296 (Thread/sleep 20)
297 (dorun (worm-motor-map
298 (worm-pattern @time)))))))
302 (defn join-at-point [obj-a obj-b world-pivot]
303 (cortex.silly/joint-dispatch
304 {:type :point}
305 (.getControl obj-a RigidBodyControl)
306 (.getControl obj-b RigidBodyControl)
307 (cortex.silly/world-to-local obj-a world-pivot)
308 (cortex.silly/world-to-local obj-b world-pivot)
309 nil
310 ))
312 (import com.jme3.bullet.collision.PhysicsCollisionObject)
314 (defn blab-* []
315 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
316 :mass 0 :color ColorRGBA/Green)
317 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
318 :mass 1 :color ColorRGBA/Red)
319 connection-point (Vector3f. 1.2 0 0)
320 root (nodify [hand finger])]
322 (join-at-point hand finger (Vector3f. 1.2 0 0))
324 (.setCollisionGroup
325 (.getControl hand RigidBodyControl)
326 PhysicsCollisionObject/COLLISION_GROUP_NONE)
327 (world
328 root
329 standard-debug-controls
330 (fn [world]
331 (enable-debug world)
332 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
333 (set-gravity world Vector3f/ZERO)
334 )
335 no-op)))
336 (import java.awt.image.BufferedImage)
338 (defn draw-sprite [image sprite x y color ]
339 (dorun
340 (for [[u v] sprite]
341 (.setRGB image (+ u x) (+ v y) color))))
343 (defn view-angle
344 "create a debug view of an angle"
345 [color]
346 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
347 previous (atom [25 25])
348 sprite [[0 0] [0 1]
349 [0 -1] [-1 0] [1 0]]]
350 (fn [angle]
351 (let [angle (float angle)]
352 (let [position
353 [(+ 25 (int (* 20 (Math/cos angle))))
354 (+ 25 (int (* 20(Math/sin angle))))]]
355 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
356 (draw-sprite image sprite (position 0) (position 1) color)
357 (reset! previous position))
358 image))))
360 (defn proprioception-debug-window
361 []
362 (let [yaw (view-angle 0xFF0000)
363 roll (view-angle 0x00FF00)
364 pitch (view-angle 0xFFFFFF)
365 v-yaw (view-image)
366 v-roll (view-image)
367 v-pitch (view-image)
368 ]
369 (fn [prop-data]
370 (dorun
371 (map
372 (fn [[y r p]]
373 (v-yaw (yaw y))
374 (v-roll (roll r))
375 (v-pitch (pitch p)))
376 prop-data)))))
377 (comment
379 (defn proprioception-debug-window
380 []
381 (let [time (atom 0)]
382 (fn [prop-data]
383 (if (= 0 (rem (swap! time inc) 40))
384 (println-repl prop-data)))))
385 )
387 (comment
388 (dorun
389 (map
390 (comp
391 println-repl
392 (fn [[p y r]]
393 (format
394 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
395 p y r)))
396 prop-data)))
401 (defn test-proprioception
402 "Testing proprioception:
403 You should see two foating bars, and a printout of pitch, yaw, and
404 roll. Pressing key-r/key-t should move the blue bar up and down and
405 change only the value of pitch. key-f/key-g moves it side to side
406 and changes yaw. key-v/key-b will spin the blue segment clockwise
407 and counterclockwise, and only affect roll."
408 []
409 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
410 :mass 0 :color ColorRGBA/Green :name "hand")
411 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
412 :mass 1 :color ColorRGBA/Red :name "finger")
413 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
414 :position (Vector3f. 1.2 2 0)
415 :physical? false)
416 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
417 creature (nodify [hand finger joint-node])
418 ;; *******************************************
420 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
421 :mass 0 :color ColorRGBA/Gray)
423 root (nodify [creature floor])
424 prop (joint-proprioception creature joint-node)
425 prop-view (proprioception-debug-window)
426 finger-control (.getControl finger RigidBodyControl)
427 hand-control (.getControl hand RigidBodyControl)
429 controls
430 (merge standard-debug-controls
431 {"key-o"
432 (fn [_ _] (.setEnabled finger-control true))
433 "key-p"
434 (fn [_ _] (.setEnabled finger-control false))
435 "key-k"
436 (fn [_ _] (.setEnabled hand-control true))
437 "key-l"
438 (fn [_ _] (.setEnabled hand-control false))
439 "key-i"
440 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))
441 "key-period"
442 (fn [world _]
443 (.setEnabled finger-control false)
444 (.setEnabled hand-control false)
445 (.rotate creature (doto (Quaternion.)
446 (.fromAngleAxis
447 (float (/ Math/PI 15))
448 (Vector3f. 0 0 -1))))
450 (.setEnabled finger-control true)
451 (.setEnabled hand-control true)
452 (set-gravity world (Vector3f. 0 0 0))
453 )
456 }
457 )
459 ]
460 (comment
461 (.setCollisionGroup
462 (.getControl hand RigidBodyControl)
463 PhysicsCollisionObject/COLLISION_GROUP_NONE)
464 )
465 (apply
466 world
467 (with-movement
468 hand
469 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]
470 [10 10 10 10 1 1]
471 (with-movement
472 finger
473 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
474 [10 10 10 10 1 1]
475 [root
476 controls
477 (fn [world]
478 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
479 (set-gravity world (Vector3f. 0 0 0))
480 (light-up-everything world))
481 (fn [_ _] (prop-view (list (prop))))])))))
483 #+end_src
485 #+results: test-body
486 : #'cortex.test.body/test-proprioception
489 * COMMENT code-limbo
490 #+begin_src clojure
491 ;;(.loadModel
492 ;; (doto (asset-manager)
493 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
494 ;; "Models/person/person.blend")
497 (defn load-blender-model
498 "Load a .blend file using an asset folder relative path."
499 [^String model]
500 (.loadModel
501 (doto (asset-manager)
502 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
503 model))
506 (defn view-model [^String model]
507 (view
508 (.loadModel
509 (doto (asset-manager)
510 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
511 model)))
513 (defn load-blender-scene [^String model]
514 (.loadModel
515 (doto (asset-manager)
516 (.registerLoader BlenderLoader (into-array String ["blend"])))
517 model))
519 (defn worm
520 []
521 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
523 (defn oto
524 []
525 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
527 (defn sinbad
528 []
529 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
531 (defn worm-blender
532 []
533 (first (seq (.getChildren (load-blender-model
534 "Models/anim2/simple-worm.blend")))))
536 (defn body
537 "given a node with a SkeletonControl, will produce a body sutiable
538 for AI control with movement and proprioception."
539 [node]
540 (let [skeleton-control (.getControl node SkeletonControl)
541 krc (KinematicRagdollControl.)]
542 (comment
543 (dorun
544 (map #(.addBoneName krc %)
545 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
546 ;;"mid2" "mid3" "tail" "head"]
547 )))
548 (.addControl node krc)
549 (.setRagdollMode krc)
550 )
551 node
552 )
553 (defn show-skeleton [node]
554 (let [sd
556 (doto
557 (SkeletonDebugger. "aurellem-skel-debug"
558 (skel node))
559 (.setMaterial (green-x-ray)))]
560 (.attachChild node sd)
561 node))
565 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
567 ;; this could be a good way to give objects special properties like
568 ;; being eyes and the like
570 (.getUserData
571 (.getChild
572 (load-blender-model "Models/property/test.blend") 0)
573 "properties")
575 ;; the properties are saved along with the blender file.
576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
581 (defn init-debug-skel-node
582 [f debug-node skeleton]
583 (let [bones
584 (map #(.getBone skeleton %)
585 (range (.getBoneCount skeleton)))]
586 (dorun (map #(.setUserControl % true) bones))
587 (dorun (map (fn [b]
588 (println (.getName b)
589 " -- " (f b)))
590 bones))
591 (dorun
592 (map #(.attachChild
593 debug-node
594 (doto
595 (sphere 0.1
596 :position (f %)
597 :physical? false)
598 (.setMaterial (green-x-ray))))
599 bones)))
600 debug-node)
602 (import jme3test.bullet.PhysicsTestHelper)
605 (defn test-zzz [the-worm world value]
606 (if (not value)
607 (let [skeleton (skel the-worm)]
608 (println-repl "enabling bones")
609 (dorun
610 (map
611 #(.setUserControl (.getBone skeleton %) true)
612 (range (.getBoneCount skeleton))))
615 (let [b (.getBone skeleton 2)]
616 (println-repl "moving " (.getName b))
617 (println-repl (.getLocalPosition b))
618 (.setUserTransforms b
619 Vector3f/UNIT_X
620 Quaternion/IDENTITY
621 ;;(doto (Quaternion.)
622 ;; (.fromAngles (/ Math/PI 2)
623 ;; 0
624 ;; 0
626 (Vector3f. 1 1 1))
627 )
629 (println-repl "hi! <3"))))
632 (defn test-ragdoll []
634 (let [the-worm
636 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
637 (doto (show-skeleton (worm-blender))
638 (.setLocalTranslation (Vector3f. 0 10 0))
639 ;;(worm)
640 ;;(oto)
641 ;;(sinbad)
642 )
643 ]
646 (.start
647 (world
648 (doto (Node.)
649 (.attachChild the-worm))
650 {"key-return" (fire-cannon-ball)
651 "key-space" (partial test-zzz the-worm)
652 }
653 (fn [world]
654 (light-up-everything world)
655 (PhysicsTestHelper/createPhysicsTestWorld
656 (.getRootNode world)
657 (asset-manager)
658 (.getPhysicsSpace
659 (.getState (.getStateManager world) BulletAppState)))
660 (set-gravity world Vector3f/ZERO)
661 ;;(.setTimer world (NanoTimer.))
662 ;;(org.lwjgl.input.Mouse/setGrabbed false)
663 )
664 no-op
665 )
668 )))
671 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
672 ;;; here is the ragdoll stuff
674 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
675 (def mesh worm-mesh)
677 (.getFloatBuffer mesh VertexBuffer$Type/Position)
678 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
679 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
682 (defn position [index]
683 (.get
684 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
685 index))
687 (defn bones [index]
688 (.get
689 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
690 index))
692 (defn bone-weights [index]
693 (.get
694 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
695 index))
699 (defn vertex-bones [vertex]
700 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
702 (defn vertex-weights [vertex]
703 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
705 (defn vertex-position [index]
706 (let [offset (* index 3)]
707 (Vector3f. (position offset)
708 (position (inc offset))
709 (position (inc(inc offset))))))
711 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
713 (defn bone-control-color [index]
714 (get {[1 0 0 0] ColorRGBA/Red
715 [1 2 0 0] ColorRGBA/Magenta
716 [2 0 0 0] ColorRGBA/Blue}
717 (vertex-bones index)
718 ColorRGBA/White))
720 (defn influence-color [index bone-num]
721 (get
722 {(float 0) ColorRGBA/Blue
723 (float 0.5) ColorRGBA/Green
724 (float 1) ColorRGBA/Red}
725 ;; find the weight of the desired bone
726 ((zipmap (vertex-bones index)(vertex-weights index))
727 bone-num)
728 ColorRGBA/Blue))
730 (def worm-vertices (set (map vertex-info (range 60))))
733 (defn test-info []
734 (let [points (Node.)]
735 (dorun
736 (map #(.attachChild points %)
737 (map #(sphere 0.01
738 :position (vertex-position %)
739 :color (influence-color % 1)
740 :physical? false)
741 (range 60))))
742 (view points)))
745 (defrecord JointControl [joint physics-space]
746 PhysicsControl
747 (setPhysicsSpace [this space]
748 (dosync
749 (ref-set (:physics-space this) space))
750 (.addJoint space (:joint this)))
751 (update [this tpf])
752 (setSpatial [this spatial])
753 (render [this rm vp])
754 (getPhysicsSpace [this] (deref (:physics-space this)))
755 (isEnabled [this] true)
756 (setEnabled [this state]))
758 (defn add-joint
759 "Add a joint to a particular object. When the object is added to the
760 PhysicsSpace of a simulation, the joint will also be added"
761 [object joint]
762 (let [control (JointControl. joint (ref nil))]
763 (.addControl object control))
764 object)
767 (defn hinge-world
768 []
769 (let [sphere1 (sphere)
770 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
771 joint (Point2PointJoint.
772 (.getControl sphere1 RigidBodyControl)
773 (.getControl sphere2 RigidBodyControl)
774 Vector3f/ZERO (Vector3f. 3 3 3))]
775 (add-joint sphere1 joint)
776 (doto (Node. "hinge-world")
777 (.attachChild sphere1)
778 (.attachChild sphere2))))
781 (defn test-joint []
782 (view (hinge-world)))
784 ;; (defn copier-gen []
785 ;; (let [count (atom 0)]
786 ;; (fn [in]
787 ;; (swap! count inc)
788 ;; (clojure.contrib.duck-streams/copy
789 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
790 ;; ;;/home/r/tmp/mao-test/clojure-images
791 ;; (format "%08d.png" @count)))))))
792 ;; (defn decrease-framerate []
793 ;; (map
794 ;; (copier-gen)
795 ;; (sort
796 ;; (map first
797 ;; (partition
798 ;; 4
799 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
800 ;; (file-seq
801 ;; (file-str
802 ;; "/home/r/media/anime/mao-temp/images"))))))))
806 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
808 (defn proprioception
809 "Create a proprioception map that reports the rotations of the
810 various limbs of the creature's body"
811 [creature]
812 [#^Node creature]
813 (let [
814 nodes (node-seq creature)
815 joints
816 (map
817 :joint
818 (filter
819 #(isa? (class %) JointControl)
820 (reduce
821 concat
822 (map (fn [node]
823 (map (fn [num] (.getControl node num))
824 (range (.getNumControls node))))
825 nodes))))]
826 (fn []
827 (reduce concat (map relative-positions (list (first joints)))))))
830 (defn skel [node]
831 (doto
832 (.getSkeleton
833 (.getControl node SkeletonControl))
834 ;; this is necessary to force the skeleton to have accurate world
835 ;; transforms before it is rendered to the screen.
836 (.resetAndUpdate)))
838 (defn green-x-ray []
839 (doto (Material. (asset-manager)
840 "Common/MatDefs/Misc/Unshaded.j3md")
841 (.setColor "Color" ColorRGBA/Green)
842 (-> (.getAdditionalRenderState)
843 (.setDepthTest false))))
845 (defn test-worm []
846 (.start
847 (world
848 (doto (Node.)
849 ;;(.attachChild (point-worm))
850 (.attachChild (load-blender-model
851 "Models/anim2/joint-worm.blend"))
853 (.attachChild (box 10 1 10
854 :position (Vector3f. 0 -2 0) :mass 0
855 :color (ColorRGBA/Gray))))
856 {
857 "key-space" (fire-cannon-ball)
858 }
859 (fn [world]
860 (enable-debug world)
861 (light-up-everything world)
862 ;;(.setTimer world (NanoTimer.))
863 )
864 no-op)))
868 ;; defunct movement stuff
869 (defn torque-controls [control]
870 (let [torques
871 (concat
872 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
873 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
874 [Vector3f/UNIT_X])]
875 (map (fn [torque-axis]
876 (fn [torque]
877 (.applyTorque
878 control
879 (.mult (.mult (.getPhysicsRotation control)
880 torque-axis)
881 (float
882 (* (.getMass control) torque))))))
883 torques)))
885 (defn motor-map
886 "Take a creature and generate a function that will enable fine
887 grained control over all the creature's limbs."
888 [#^Node creature]
889 (let [controls (keep #(.getControl % RigidBodyControl)
890 (node-seq creature))
891 limb-controls (reduce concat (map torque-controls controls))
892 body-control (partial map #(%1 %2) limb-controls)]
893 body-control))
895 (defn test-motor-map
896 "see how torque works."
897 []
898 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
899 :mass 1 :color ColorRGBA/Green)
900 motor-map (motor-map finger)]
901 (world
902 (nodify [finger
903 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
904 :color ColorRGBA/Gray)])
905 standard-debug-controls
906 (fn [world]
907 (set-gravity world Vector3f/ZERO)
908 (light-up-everything world)
909 (.setTimer world (NanoTimer.)))
910 (fn [_ _]
911 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
912 #+end_src
920 * COMMENT generate Source
921 #+begin_src clojure :tangle ../src/cortex/body.clj
922 <<proprioception>>
923 <<motor-control>>
924 #+end_src
926 #+begin_src clojure :tangle ../src/cortex/test/body.clj
927 <<test-body>>
928 #+end_src