view org/body.org @ 133:2ed7e60d3821

FINALLY got proprioception working
author Robert McIntyre <rlm@mit.edu>
date Wed, 01 Feb 2012 02:27:18 -0700
parents 3206d5e20bee
children ac350a0ac6b0
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))
20 (comment
21 (defn joint-proprioception
22 "Relative position information for a two-part system connected by a
23 joint. Gives the pitch, yaw, and roll of the 'B' object relative to
24 the 'A' object, as determined by the joint."
25 [joint]
26 (let [object-a (.getUserObject (.getBodyA joint))
27 object-b (.getUserObject (.getBodyB joint))
28 arm-a
29 (.normalize
30 (.subtract
31 (.localToWorld object-a (.getPivotA joint) nil)
32 (.getWorldTranslation object-a)))
34 ;; this is probably wrong!
35 rotate-a
36 (doto (Matrix3f.)
37 (.fromStartEndVectors arm-a Vector3f/UNIT_X))
39 arm-b
40 (.mult
41 rotate-a
42 (.normalize
43 (.subtract
44 (.localToWorld object-b (.getPivotB joint) nil)
45 (.getWorldTranslation object-b))))
46 pitch
47 (.angleBetween
48 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
49 (Vector2f. 1 0))
50 yaw
51 (.angleBetween
52 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
53 (Vector2f. 1 0))
55 roll
56 (project-quaternion
57 (.mult
58 (.getLocalRotation object-b)
59 (doto (Quaternion.)
60 (.fromRotationMatrix rotate-a)))
61 arm-b)]
62 ;;(println-repl (.getName object-a) (.getName object-b))
63 [pitch yaw roll]))
64 )
66 (defn any-orthogonal
67 "Generate an arbitray (but stable) orthogonal vector to a given
68 vector."
69 [vector]
70 (let [x (.getX vector)
71 y (.getY vector)
72 z (.getZ vector)]
73 (cond
74 (not= x (float 0)) (Vector3f. (- z) 0 x)
75 (not= y (float 0)) (Vector3f. 0 (- z) y)
76 (not= z (float 0)) (Vector3f. 0 (- z) y)
77 true Vector3f/ZERO)))
79 (comment
80 (defn project-quaternion
81 "From http://stackoverflow.com/questions/3684269/
82 component-of-a-quaternion-rotation-around-an-axis.
84 Determine the amount of rotation a quaternion will
85 cause about a given axis."
86 [#^Quaternion q #^Vector3f axis]
87 (let [basis-1 (any-orthogonal axis)
88 basis-2 (.cross axis basis-1)
89 rotated (.mult q basis-1)
90 alpha (.dot basis-1 (.project rotated basis-1))
91 beta (.dot basis-2 (.project rotated basis-2))]
92 (Math/atan2 beta alpha)))
93 )
95 (defn right-handed? [vec1 vec2 vec3]
96 (< 0 (.dot (.cross vec1 vec2) vec3)))
98 (defn absolute-angle [vec1 vec2 axis]
99 (let [angle (.angleBetween vec1 vec2)]
100 (if (right-handed? vec1 vec2 axis)
101 angle (- (* 2 Math/PI) angle))))
103 (defn angle-min [& angles]
104 (first
105 (sort-by
106 (fn [angle]
107 (let [in-circle (Math/abs (rem angle (* 2 Math/PI)))]
108 (min in-circle
109 (- (* Math/PI 2) in-circle))))
110 angles)))
112 (defn project-quaternion
113 "From http://stackoverflow.com/questions/3684269/
114 component-of-a-quaternion-rotation-around-an-axis.
116 Determine the amount of rotation a quaternion will
117 cause about a given axis."
118 [#^Quaternion q #^Vector3f axis]
119 (let [axis (.normalize axis)
120 basis-1 (.normalize (any-orthogonal axis))
121 basis-2 (.cross axis basis-1)
122 rotated-1 (.mult q basis-1)
123 basis-1* (.normalize
124 (.add (.project rotated-1 basis-1)
125 (.project rotated-1 basis-2)))
126 rotated-2 (.mult q basis-2)
127 basis-2* (.normalize
128 (.add (.project rotated-2 basis-1)
129 (.project rotated-2 basis-2)))
130 angle-1
131 (absolute-angle basis-1 basis-1* axis)
132 angle-2
133 (absolute-angle basis-2 basis-2* axis)
136 angle (angle-min angle-1 angle-2)
137 ]
140 ;; be sure to get sign from cross product
141 (if false
142 (do
143 (println-repl "axis" axis)
144 (println-repl "basis-1" basis-1)
145 (println-repl "basis-2" basis-2)
146 (println-repl "rotated-1" rotated-1)
147 (println-repl "rotated-2" rotated-2)
148 (println-repl "basis-1*" basis-1*)
149 (println-repl "basis-2*" basis-2*)
150 (println-repl "angle-1" angle-1)
151 (println-repl "angle-2" angle-2)
153 (println-repl "angle" angle)
154 (println-repl "")))
155 angle))
158 (import com.jme3.scene.Node)
160 (defn joint-proprioception [#^Node parts #^Node joint]
161 (let [[obj-a obj-b] (cortex.silly/joint-targets parts joint)
162 joint-rot (.getWorldRotation joint)
163 x (.mult joint-rot Vector3f/UNIT_X)
164 y (.mult joint-rot Vector3f/UNIT_Y)
165 z (.mult joint-rot Vector3f/UNIT_Z)]
166 ;; this function will report proprioceptive information for the
167 ;; joint
168 (fn []
169 ;; x is the "twist" axis, y and z are the "bend" axes
170 (let [rot-a (.getWorldRotation obj-a)
171 rot-b (.getWorldRotation obj-b)
172 relative (.mult (.inverse rot-a) rot-b)
173 basis (doto (Matrix3f.)
174 (.setColumn 0 x)
175 (.setColumn 1 y)
176 (.setColumn 2 z))
177 rotation-about-joint
178 (doto (Quaternion.)
179 (.fromRotationMatrix
180 (.mult (.invert basis)
181 (.toRotationMatrix relative))))
182 [yaw roll pitch]
183 (seq (.toAngles rotation-about-joint nil))]
184 ;;return euler angles of the quaternion around the new basis
185 ;;[yaw pitch roll]
186 [yaw roll pitch]
187 ))))
190 (comment
192 (defn joint-proprioception
193 [joint]
194 (let [object-a (.getUserObject (.getBodyA joint))
195 object-b (.getUserObject (.getBodyB joint))
196 rot-a (.clone (.getWorldRotation object-a))
197 rot-b (.clone (.getWorldRotation object-b))
198 ]
200 (.mult rot-b (.inverse rot-a))
202 ;; object-a == hand
203 ;; object-b == finger
204 ))
205 )
206 ;; (defn joint-proprioception*
207 ;; [joint]
208 ;; (let [object-a (.getUserObject (.getBodyA joint))
209 ;; object-b (.getUserObject (.getBodyB joint))
211 ;; rotate-a (.clone (.getWorldRotation object-a))
212 ;; rotate-b (.clone (.getWorldRotation object-b))
214 ;; rotate-rel (.mult rotate-b (.inverse rotate-a))
215 ;; ]
216 ;; ((comp vec map) (partial project-quaternion rotate-rel)
217 ;; [Vector3f/UNIT_X
218 ;; Vector3f/UNIT_Y
219 ;; Vector3f/UNIT_Z])))
222 (defn proprioception
223 "Create a function that provides proprioceptive information about an
224 entire body."
225 [body]
226 ;; extract the body's joints
227 (let [joints
228 (distinct
229 (reduce
230 concat
231 (map #(.getJoints %)
232 (keep
233 #(.getControl % RigidBodyControl)
234 (node-seq body)))))]
235 (fn []
236 (map joint-proprioception joints))))
238 #+end_src
240 #+results: proprioception
241 : #'cortex.body/proprioception
243 * Motor Control
244 #+name: motor-control
245 #+begin_src clojure
246 (in-ns 'cortex.body)
248 ;; surprisingly enough, terristerial creatures only move by using
249 ;; torque applied about their joints. There's not a single straight
250 ;; line of force in the human body at all! (A straight line of force
251 ;; would correspond to some sort of jet or rocket propulseion.)
253 (defn vector-motor-control
254 "Create a function that accepts a sequence of Vector3f objects that
255 describe the torque to be applied to each part of the body."
256 [body]
257 (let [nodes (node-seq body)
258 controls (keep #(.getControl % RigidBodyControl) nodes)]
259 (fn [torques]
260 (map #(.applyTorque %1 %2)
261 controls torques))))
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 #+end_src
265 ## note -- might want to add a lower dimensional, discrete version of
266 ## this if it proves useful from a x-modal clustering perspective.
268 * Examples
270 #+name: test-body
271 #+begin_src clojure
272 (ns cortex.test.body
273 (:use (cortex world util body))
274 (:import
275 com.jme3.math.Vector3f
276 com.jme3.math.ColorRGBA
277 com.jme3.bullet.joints.Point2PointJoint
278 com.jme3.bullet.control.RigidBodyControl
279 com.jme3.system.NanoTimer))
281 (defn worm-segments
282 "Create multiple evenly spaced box segments. They're fabulous!"
283 [segment-length num-segments interstitial-space radius]
284 (letfn [(nth-segment
285 [n]
286 (box segment-length radius radius :mass 0.1
287 :position
288 (Vector3f.
289 (* 2 n (+ interstitial-space segment-length)) 0 0)
290 :name (str "worm-segment" n)
291 :color (ColorRGBA/randomColor)))]
292 (map nth-segment (range num-segments))))
294 (defn connect-at-midpoint
295 "Connect two physics objects with a Point2Point joint constraint at
296 the point equidistant from both objects' centers."
297 [segmentA segmentB]
298 (let [centerA (.getWorldTranslation segmentA)
299 centerB (.getWorldTranslation segmentB)
300 midpoint (.mult (.add centerA centerB) (float 0.5))
301 pivotA (.subtract midpoint centerA)
302 pivotB (.subtract midpoint centerB)
304 ;; A side-effect of creating a joint registers
305 ;; it with both physics objects which in turn
306 ;; will register the joint with the physics system
307 ;; when the simulation is started.
308 joint (Point2PointJoint.
309 (.getControl segmentA RigidBodyControl)
310 (.getControl segmentB RigidBodyControl)
311 pivotA
312 pivotB)]
313 segmentB))
315 (defn eve-worm
316 "Create a worm-like body bound by invisible joint constraints."
317 []
318 (let [segments (worm-segments 0.2 5 0.1 0.1)]
319 (dorun (map (partial apply connect-at-midpoint)
320 (partition 2 1 segments)))
321 (nodify "worm" segments)))
323 (defn worm-pattern
324 "This is a simple, mindless motor control pattern that drives the
325 second segment of the worm's body at an offset angle with
326 sinusoidally varying strength."
327 [time]
328 (let [angle (* Math/PI (/ 9 20))
329 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
330 [Vector3f/ZERO
331 (.mult
332 direction
333 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
334 Vector3f/ZERO
335 Vector3f/ZERO
336 Vector3f/ZERO]))
338 (defn test-motor-control
339 "Testing motor-control:
340 You should see a multi-segmented worm-like object fall onto the
341 table and begin writhing and moving."
342 []
343 (let [worm (eve-worm)
344 time (atom 0)
345 worm-motor-map (vector-motor-control worm)]
346 (world
347 (nodify [worm
348 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
349 :color ColorRGBA/Gray)])
350 standard-debug-controls
351 (fn [world]
352 (enable-debug world)
353 (light-up-everything world)
354 (comment
355 (com.aurellem.capture.Capture/captureVideo
356 world
357 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
358 )
360 (fn [_ _]
361 (swap! time inc)
362 (Thread/sleep 20)
363 (dorun (worm-motor-map
364 (worm-pattern @time)))))))
367 (require 'cortex.silly)
368 (defn join-at-point [obj-a obj-b world-pivot]
369 (cortex.silly/joint-dispatch
370 {:type :point}
371 (.getControl obj-a RigidBodyControl)
372 (.getControl obj-b RigidBodyControl)
373 (cortex.silly/world-to-local obj-a world-pivot)
374 (cortex.silly/world-to-local obj-b world-pivot)
375 nil
376 ))
378 (import com.jme3.bullet.collision.PhysicsCollisionObject)
380 (defn blab-* []
381 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
382 :mass 0 :color ColorRGBA/Green)
383 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
384 :mass 1 :color ColorRGBA/Red)
385 connection-point (Vector3f. 1.2 0 0)
386 root (nodify [hand finger])]
388 (join-at-point hand finger (Vector3f. 1.2 0 0))
390 (.setCollisionGroup
391 (.getControl hand RigidBodyControl)
392 PhysicsCollisionObject/COLLISION_GROUP_NONE)
393 (world
394 root
395 standard-debug-controls
396 (fn [world]
397 (enable-debug world)
398 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
399 (set-gravity world Vector3f/ZERO)
400 )
401 no-op)))
402 (import java.awt.image.BufferedImage)
404 (defn draw-sprite [image sprite x y color ]
405 (dorun
406 (for [[u v] sprite]
407 (.setRGB image (+ u x) (+ v y) color))))
409 (defn view-angle
410 "create a debug view of an angle"
411 [color]
412 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
413 previous (atom [25 25])
414 sprite [[0 0] [0 1]
415 [0 -1] [-1 0] [1 0]]]
416 (fn [angle]
417 (let [angle (float angle)]
418 (let [position
419 [(+ 25 (int (* 20 (Math/cos angle))))
420 (+ 25 (int (* 20(Math/sin angle))))]]
421 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
422 (draw-sprite image sprite (position 0) (position 1) color)
423 (reset! previous position))
424 image))))
426 (defn proprioception-debug-window
427 []
428 (let [yaw (view-angle 0xFF0000)
429 roll (view-angle 0x00FF00)
430 pitch (view-angle 0xFFFFFF)
431 v-yaw (view-image)
432 v-roll (view-image)
433 v-pitch (view-image)
434 ]
435 (fn [prop-data]
436 (dorun
437 (map
438 (fn [[y r p]]
439 (v-yaw (yaw y))
440 (v-roll (roll r))
441 (v-pitch (pitch p)))
442 prop-data)))))
443 (comment
445 (defn proprioception-debug-window
446 []
447 (let [time (atom 0)]
448 (fn [prop-data]
449 (if (= 0 (rem (swap! time inc) 40))
450 (println-repl prop-data)))))
451 )
453 (comment
454 (dorun
455 (map
456 (comp
457 println-repl
458 (fn [[p y r]]
459 (format
460 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
461 p y r)))
462 prop-data)))
468 (defn test-proprioception
469 "Testing proprioception:
470 You should see two foating bars, and a printout of pitch, yaw, and
471 roll. Pressing key-r/key-t should move the blue bar up and down and
472 change only the value of pitch. key-f/key-g moves it side to side
473 and changes yaw. key-v/key-b will spin the blue segment clockwise
474 and counterclockwise, and only affect roll."
475 []
476 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
477 :mass 0 :color ColorRGBA/Green :name "hand")
478 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
479 :mass 1 :color ColorRGBA/Red :name "finger")
480 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
481 :mass 0 :color ColorRGBA/Gray)
482 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
483 :position (Vector3f. 1.2 2 0)
484 :physical? false)
486 move-up? (atom false)
487 move-down? (atom false)
488 move-left? (atom false)
489 move-right? (atom false)
490 roll-left? (atom false)
491 roll-right? (atom false)
492 control (.getControl finger RigidBodyControl)
493 time (atom 0)
494 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
495 creature (nodify [hand finger joint-node])
496 prop (joint-proprioception creature joint-node)
498 prop-view (proprioception-debug-window)
501 ]
506 (.setCollisionGroup
507 (.getControl hand RigidBodyControl)
508 PhysicsCollisionObject/COLLISION_GROUP_NONE)
511 (world
512 (nodify [hand finger floor joint-node])
513 (merge standard-debug-controls
514 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
515 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
516 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
517 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
518 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
519 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
520 (fn [world]
521 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
522 (set-gravity world (Vector3f. 0 0 0))
523 (light-up-everything world))
524 (fn [_ _]
525 (if @move-up?
526 (.applyTorque control
527 (.mult (.getPhysicsRotation control)
528 (Vector3f. 0 0 10))))
529 (if @move-down?
530 (.applyTorque control
531 (.mult (.getPhysicsRotation control)
532 (Vector3f. 0 0 -10))))
533 (if @move-left?
534 (.applyTorque control
535 (.mult (.getPhysicsRotation control)
536 (Vector3f. 0 10 0))))
537 (if @move-right?
538 (.applyTorque control
539 (.mult (.getPhysicsRotation control)
540 (Vector3f. 0 -10 0))))
541 (if @roll-left?
542 (.applyTorque control
543 (.mult (.getPhysicsRotation control)
544 (Vector3f. -1 0 0))))
545 (if @roll-right?
546 (.applyTorque control
547 (.mult (.getPhysicsRotation control)
548 (Vector3f. 1 0 0))))
550 ;;(if (= 0 (rem (swap! time inc) 20))
551 (prop-view (list (prop)))))))
553 #+end_src
555 #+results: test-body
556 : #'cortex.test.body/test-proprioception
559 * COMMENT code-limbo
560 #+begin_src clojure
561 ;;(.loadModel
562 ;; (doto (asset-manager)
563 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
564 ;; "Models/person/person.blend")
567 (defn load-blender-model
568 "Load a .blend file using an asset folder relative path."
569 [^String model]
570 (.loadModel
571 (doto (asset-manager)
572 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
573 model))
576 (defn view-model [^String model]
577 (view
578 (.loadModel
579 (doto (asset-manager)
580 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
581 model)))
583 (defn load-blender-scene [^String model]
584 (.loadModel
585 (doto (asset-manager)
586 (.registerLoader BlenderLoader (into-array String ["blend"])))
587 model))
589 (defn worm
590 []
591 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
593 (defn oto
594 []
595 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
597 (defn sinbad
598 []
599 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
601 (defn worm-blender
602 []
603 (first (seq (.getChildren (load-blender-model
604 "Models/anim2/simple-worm.blend")))))
606 (defn body
607 "given a node with a SkeletonControl, will produce a body sutiable
608 for AI control with movement and proprioception."
609 [node]
610 (let [skeleton-control (.getControl node SkeletonControl)
611 krc (KinematicRagdollControl.)]
612 (comment
613 (dorun
614 (map #(.addBoneName krc %)
615 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
616 ;;"mid2" "mid3" "tail" "head"]
617 )))
618 (.addControl node krc)
619 (.setRagdollMode krc)
620 )
621 node
622 )
623 (defn show-skeleton [node]
624 (let [sd
626 (doto
627 (SkeletonDebugger. "aurellem-skel-debug"
628 (skel node))
629 (.setMaterial (green-x-ray)))]
630 (.attachChild node sd)
631 node))
635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 ;; this could be a good way to give objects special properties like
638 ;; being eyes and the like
640 (.getUserData
641 (.getChild
642 (load-blender-model "Models/property/test.blend") 0)
643 "properties")
645 ;; the properties are saved along with the blender file.
646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
651 (defn init-debug-skel-node
652 [f debug-node skeleton]
653 (let [bones
654 (map #(.getBone skeleton %)
655 (range (.getBoneCount skeleton)))]
656 (dorun (map #(.setUserControl % true) bones))
657 (dorun (map (fn [b]
658 (println (.getName b)
659 " -- " (f b)))
660 bones))
661 (dorun
662 (map #(.attachChild
663 debug-node
664 (doto
665 (sphere 0.1
666 :position (f %)
667 :physical? false)
668 (.setMaterial (green-x-ray))))
669 bones)))
670 debug-node)
672 (import jme3test.bullet.PhysicsTestHelper)
675 (defn test-zzz [the-worm world value]
676 (if (not value)
677 (let [skeleton (skel the-worm)]
678 (println-repl "enabling bones")
679 (dorun
680 (map
681 #(.setUserControl (.getBone skeleton %) true)
682 (range (.getBoneCount skeleton))))
685 (let [b (.getBone skeleton 2)]
686 (println-repl "moving " (.getName b))
687 (println-repl (.getLocalPosition b))
688 (.setUserTransforms b
689 Vector3f/UNIT_X
690 Quaternion/IDENTITY
691 ;;(doto (Quaternion.)
692 ;; (.fromAngles (/ Math/PI 2)
693 ;; 0
694 ;; 0
696 (Vector3f. 1 1 1))
697 )
699 (println-repl "hi! <3"))))
702 (defn test-ragdoll []
704 (let [the-worm
706 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
707 (doto (show-skeleton (worm-blender))
708 (.setLocalTranslation (Vector3f. 0 10 0))
709 ;;(worm)
710 ;;(oto)
711 ;;(sinbad)
712 )
713 ]
716 (.start
717 (world
718 (doto (Node.)
719 (.attachChild the-worm))
720 {"key-return" (fire-cannon-ball)
721 "key-space" (partial test-zzz the-worm)
722 }
723 (fn [world]
724 (light-up-everything world)
725 (PhysicsTestHelper/createPhysicsTestWorld
726 (.getRootNode world)
727 (asset-manager)
728 (.getPhysicsSpace
729 (.getState (.getStateManager world) BulletAppState)))
730 (set-gravity world Vector3f/ZERO)
731 ;;(.setTimer world (NanoTimer.))
732 ;;(org.lwjgl.input.Mouse/setGrabbed false)
733 )
734 no-op
735 )
738 )))
741 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
742 ;;; here is the ragdoll stuff
744 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
745 (def mesh worm-mesh)
747 (.getFloatBuffer mesh VertexBuffer$Type/Position)
748 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
749 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
752 (defn position [index]
753 (.get
754 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
755 index))
757 (defn bones [index]
758 (.get
759 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
760 index))
762 (defn bone-weights [index]
763 (.get
764 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
765 index))
769 (defn vertex-bones [vertex]
770 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
772 (defn vertex-weights [vertex]
773 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
775 (defn vertex-position [index]
776 (let [offset (* index 3)]
777 (Vector3f. (position offset)
778 (position (inc offset))
779 (position (inc(inc offset))))))
781 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
783 (defn bone-control-color [index]
784 (get {[1 0 0 0] ColorRGBA/Red
785 [1 2 0 0] ColorRGBA/Magenta
786 [2 0 0 0] ColorRGBA/Blue}
787 (vertex-bones index)
788 ColorRGBA/White))
790 (defn influence-color [index bone-num]
791 (get
792 {(float 0) ColorRGBA/Blue
793 (float 0.5) ColorRGBA/Green
794 (float 1) ColorRGBA/Red}
795 ;; find the weight of the desired bone
796 ((zipmap (vertex-bones index)(vertex-weights index))
797 bone-num)
798 ColorRGBA/Blue))
800 (def worm-vertices (set (map vertex-info (range 60))))
803 (defn test-info []
804 (let [points (Node.)]
805 (dorun
806 (map #(.attachChild points %)
807 (map #(sphere 0.01
808 :position (vertex-position %)
809 :color (influence-color % 1)
810 :physical? false)
811 (range 60))))
812 (view points)))
815 (defrecord JointControl [joint physics-space]
816 PhysicsControl
817 (setPhysicsSpace [this space]
818 (dosync
819 (ref-set (:physics-space this) space))
820 (.addJoint space (:joint this)))
821 (update [this tpf])
822 (setSpatial [this spatial])
823 (render [this rm vp])
824 (getPhysicsSpace [this] (deref (:physics-space this)))
825 (isEnabled [this] true)
826 (setEnabled [this state]))
828 (defn add-joint
829 "Add a joint to a particular object. When the object is added to the
830 PhysicsSpace of a simulation, the joint will also be added"
831 [object joint]
832 (let [control (JointControl. joint (ref nil))]
833 (.addControl object control))
834 object)
837 (defn hinge-world
838 []
839 (let [sphere1 (sphere)
840 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
841 joint (Point2PointJoint.
842 (.getControl sphere1 RigidBodyControl)
843 (.getControl sphere2 RigidBodyControl)
844 Vector3f/ZERO (Vector3f. 3 3 3))]
845 (add-joint sphere1 joint)
846 (doto (Node. "hinge-world")
847 (.attachChild sphere1)
848 (.attachChild sphere2))))
851 (defn test-joint []
852 (view (hinge-world)))
854 ;; (defn copier-gen []
855 ;; (let [count (atom 0)]
856 ;; (fn [in]
857 ;; (swap! count inc)
858 ;; (clojure.contrib.duck-streams/copy
859 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
860 ;; ;;/home/r/tmp/mao-test/clojure-images
861 ;; (format "%08d.png" @count)))))))
862 ;; (defn decrease-framerate []
863 ;; (map
864 ;; (copier-gen)
865 ;; (sort
866 ;; (map first
867 ;; (partition
868 ;; 4
869 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
870 ;; (file-seq
871 ;; (file-str
872 ;; "/home/r/media/anime/mao-temp/images"))))))))
876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
878 (defn proprioception
879 "Create a proprioception map that reports the rotations of the
880 various limbs of the creature's body"
881 [creature]
882 [#^Node creature]
883 (let [
884 nodes (node-seq creature)
885 joints
886 (map
887 :joint
888 (filter
889 #(isa? (class %) JointControl)
890 (reduce
891 concat
892 (map (fn [node]
893 (map (fn [num] (.getControl node num))
894 (range (.getNumControls node))))
895 nodes))))]
896 (fn []
897 (reduce concat (map relative-positions (list (first joints)))))))
900 (defn skel [node]
901 (doto
902 (.getSkeleton
903 (.getControl node SkeletonControl))
904 ;; this is necessary to force the skeleton to have accurate world
905 ;; transforms before it is rendered to the screen.
906 (.resetAndUpdate)))
908 (defn green-x-ray []
909 (doto (Material. (asset-manager)
910 "Common/MatDefs/Misc/Unshaded.j3md")
911 (.setColor "Color" ColorRGBA/Green)
912 (-> (.getAdditionalRenderState)
913 (.setDepthTest false))))
915 (defn test-worm []
916 (.start
917 (world
918 (doto (Node.)
919 ;;(.attachChild (point-worm))
920 (.attachChild (load-blender-model
921 "Models/anim2/joint-worm.blend"))
923 (.attachChild (box 10 1 10
924 :position (Vector3f. 0 -2 0) :mass 0
925 :color (ColorRGBA/Gray))))
926 {
927 "key-space" (fire-cannon-ball)
928 }
929 (fn [world]
930 (enable-debug world)
931 (light-up-everything world)
932 ;;(.setTimer world (NanoTimer.))
933 )
934 no-op)))
938 ;; defunct movement stuff
939 (defn torque-controls [control]
940 (let [torques
941 (concat
942 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
943 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
944 [Vector3f/UNIT_X])]
945 (map (fn [torque-axis]
946 (fn [torque]
947 (.applyTorque
948 control
949 (.mult (.mult (.getPhysicsRotation control)
950 torque-axis)
951 (float
952 (* (.getMass control) torque))))))
953 torques)))
955 (defn motor-map
956 "Take a creature and generate a function that will enable fine
957 grained control over all the creature's limbs."
958 [#^Node creature]
959 (let [controls (keep #(.getControl % RigidBodyControl)
960 (node-seq creature))
961 limb-controls (reduce concat (map torque-controls controls))
962 body-control (partial map #(%1 %2) limb-controls)]
963 body-control))
965 (defn test-motor-map
966 "see how torque works."
967 []
968 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
969 :mass 1 :color ColorRGBA/Green)
970 motor-map (motor-map finger)]
971 (world
972 (nodify [finger
973 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
974 :color ColorRGBA/Gray)])
975 standard-debug-controls
976 (fn [world]
977 (set-gravity world Vector3f/ZERO)
978 (light-up-everything world)
979 (.setTimer world (NanoTimer.)))
980 (fn [_ _]
981 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
982 #+end_src
990 * COMMENT generate Source
991 #+begin_src clojure :tangle ../src/cortex/body.clj
992 <<proprioception>>
993 <<motor-control>>
994 #+end_src
996 #+begin_src clojure :tangle ../src/cortex/test/body.clj
997 <<test-body>>
998 #+end_src