comparison org/body.org @ 61:7b44348af538

cleaning up body code
author Robert McIntyre <rlm@mit.edu>
date Mon, 28 Nov 2011 21:22:35 -0700
parents e5e627f50a3a
children 2b9d81017cb7
comparison
equal deleted inserted replaced
60:e5e627f50a3a 61:7b44348af538
14 14
15 (use 'clojure.contrib.def) 15 (use 'clojure.contrib.def)
16 (cortex.import/mega-import-jme3) 16 (cortex.import/mega-import-jme3)
17 (rlm.rlm-commands/help) 17 (rlm.rlm-commands/help)
18 18
19 ;;(.loadModel
20 ;; (doto (asset-manager)
21 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
22 ;; "Models/person/person.blend")
23
24 (defn view-model [^String model]
25 (view
26 (.loadModel
27 (doto (asset-manager)
28 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
29 model)))
30
31 (defn load-blender-scene [^String model]
32 (.loadModel
33 (doto (asset-manager)
34 (.registerLoader BlenderLoader (into-array String ["blend"])))
35 model))
36
37 (defn load-blender-model 19 (defn load-blender-model
38 [^String model] 20 [^String model]
39 (.loadModel 21 (.loadModel
40 (doto (asset-manager) 22 (doto (asset-manager)
41 (.registerLoader BlenderModelLoader (into-array String ["blend"]))) 23 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
42 model)) 24 model))
43
44 (defn worm
45 []
46 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
47
48 (defn oto
49 []
50 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
51
52 (defn sinbad
53 []
54 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
55
56 (defn worm-blender
57 []
58 (first (seq (.getChildren (load-blender-model
59 "Models/anim2/simple-worm.blend")))))
60 25
61 (defn skel [node] 26 (defn skel [node]
62 (doto 27 (doto
63 (.getSkeleton 28 (.getSkeleton
64 (.getControl node SkeletonControl)) 29 (.getControl node SkeletonControl))
103 Textual 68 Textual
104 (text [control] 69 (text [control]
105 (println "...geo..."))) 70 (println "...geo...")))
106 71
107 72
108
109
110 (defn body
111 "given a node with a SkeletonControl, will produce a body sutiable
112 for AI control with movement and proprioception."
113 [node]
114 (let [skeleton-control (.getControl node SkeletonControl)
115 krc (KinematicRagdollControl.)]
116 (comment
117 (dorun
118 (map #(.addBoneName krc %)
119 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
120 ;;"mid2" "mid3" "tail" "head"]
121 )))
122 (.addControl node krc)
123 (.setRagdollMode krc)
124 )
125 node
126 )
127
128 (defn green-x-ray [] 73 (defn green-x-ray []
129 (doto (Material. (asset-manager) 74 (doto (Material. (asset-manager)
130 "Common/MatDefs/Misc/Unshaded.j3md") 75 "Common/MatDefs/Misc/Unshaded.j3md")
131 (.setColor "Color" ColorRGBA/Green) 76 (.setColor "Color" ColorRGBA/Green)
132 (-> (.getAdditionalRenderState) 77 (-> (.getAdditionalRenderState)
133 (.setDepthTest false)))) 78 (.setDepthTest false))))
134 79
135 (defn show-skeleton [node]
136 (let [sd
137
138 (doto
139 (SkeletonDebugger. "aurellem-skel-debug"
140 (skel node))
141 (.setMaterial (green-x-ray)))]
142 (.attachChild node sd)
143 node))
144
145
146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147
148 ;; this could be a good way to give objects special properties like
149 ;; being eyes and the like
150
151 (.getUserData
152 (.getChild
153 (load-blender-model "Models/property/test.blend") 0)
154 "properties")
155
156 ;; the properties are saved along with the blender file.
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158
159
160
161 (defn init-debug-skel-node
162 [f debug-node skeleton]
163 (let [bones
164 (map #(.getBone skeleton %)
165 (range (.getBoneCount skeleton)))]
166 (dorun (map #(.setUserControl % true) bones))
167 (dorun (map (fn [b]
168 (println (.getName b)
169 " -- " (f b)))
170 bones))
171 (dorun
172 (map #(.attachChild
173 debug-node
174 (doto
175 (sphere 0.1
176 :position (f %)
177 :physical? false)
178 (.setMaterial (green-x-ray))))
179 bones)))
180 debug-node)
181
182 (import jme3test.bullet.PhysicsTestHelper)
183
184
185 (defn test-zzz [the-worm world value]
186 (if (not value)
187 (let [skeleton (skel the-worm)]
188 (println-repl "enabling bones")
189 (dorun
190 (map
191 #(.setUserControl (.getBone skeleton %) true)
192 (range (.getBoneCount skeleton))))
193
194
195 (let [b (.getBone skeleton 2)]
196 (println-repl "moving " (.getName b))
197 (println-repl (.getLocalPosition b))
198 (.setUserTransforms b
199 Vector3f/UNIT_X
200 Quaternion/IDENTITY
201 ;;(doto (Quaternion.)
202 ;; (.fromAngles (/ Math/PI 2)
203 ;; 0
204 ;; 0
205
206 (Vector3f. 1 1 1))
207 )
208
209 (println-repl "hi! <3"))))
210
211
212 (defn test-ragdoll []
213
214 (let [the-worm
215
216 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
217 (doto (show-skeleton (worm-blender))
218 (.setLocalTranslation (Vector3f. 0 10 0))
219 ;;(worm)
220 ;;(oto)
221 ;;(sinbad)
222 )
223 ]
224
225
226 (.start
227 (world
228 (doto (Node.)
229 (.attachChild the-worm))
230 {"key-return" (fire-cannon-ball)
231 "key-space" (partial test-zzz the-worm)
232 }
233 (fn [world]
234 (light-up-everything world)
235 (PhysicsTestHelper/createPhysicsTestWorld
236 (.getRootNode world)
237 (asset-manager)
238 (.getPhysicsSpace
239 (.getState (.getStateManager world) BulletAppState)))
240 (set-gravity world Vector3f/ZERO)
241 ;;(.setTimer world (NanoTimer.))
242 ;;(org.lwjgl.input.Mouse/setGrabbed false)
243 )
244 no-op
245 )
246
247
248 )))
249
250
251
252
253 80
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;; here is the ragdoll stuff
256
257 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
258 (def mesh worm-mesh)
259
260 (.getFloatBuffer mesh VertexBuffer$Type/Position)
261 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
262 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
263
264
265 (defn position [index]
266 (.get
267 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
268 index))
269
270 (defn bones [index]
271 (.get
272 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
273 index))
274
275 (defn bone-weights [index]
276 (.get
277 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
278 index))
279
280
281
282 (defn vertex-bones [vertex]
283 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
284
285 (defn vertex-weights [vertex]
286 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
287
288 (defn vertex-position [index]
289 (let [offset (* index 3)]
290 (Vector3f. (position offset)
291 (position (inc offset))
292 (position (inc(inc offset))))))
293
294 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
295
296 (defn bone-control-color [index]
297 (get {[1 0 0 0] ColorRGBA/Red
298 [1 2 0 0] ColorRGBA/Magenta
299 [2 0 0 0] ColorRGBA/Blue}
300 (vertex-bones index)
301 ColorRGBA/White))
302
303 (defn influence-color [index bone-num]
304 (get
305 {(float 0) ColorRGBA/Blue
306 (float 0.5) ColorRGBA/Green
307 (float 1) ColorRGBA/Red}
308 ;; find the weight of the desired bone
309 ((zipmap (vertex-bones index)(vertex-weights index))
310 bone-num)
311 ColorRGBA/Blue))
312
313 (def worm-vertices (set (map vertex-info (range 60))))
314
315
316 (defn test-info []
317 (let [points (Node.)]
318 (dorun
319 (map #(.attachChild points %)
320 (map #(sphere 0.01
321 :position (vertex-position %)
322 :color (influence-color % 1)
323 :physical? false)
324 (range 60))))
325 (view points)))
326
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 82
329 ;;;;;;;;;;;; eve-style bodies ;;;;;;;; 83 ;;;;;;;;;;;; eve-style bodies ;;;;;;;;
330
331
332 (defrecord JointControl [joint physics-space]
333 PhysicsControl
334 (setPhysicsSpace [this space]
335 (dosync
336 (ref-set (:physics-space this) space))
337 (.addJoint space (:joint this)))
338 (update [this tpf])
339 (setSpatial [this spatial])
340 (render [this rm vp])
341 (getPhysicsSpace [this] (deref (:physics-space this)))
342 (isEnabled [this] true)
343 (setEnabled [this state]))
344
345 (defn add-joint
346 "Add a joint to a particular object. When the object is added to the
347 PhysicsSpace of a simulation, the joint will also be added"
348 [object joint]
349 (let [control (JointControl. joint (ref nil))]
350 (.addControl object control))
351 object)
352
353 (defn hinge-world
354 []
355 (let [sphere1 (sphere)
356 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
357 joint (Point2PointJoint.
358 (.getControl sphere1 RigidBodyControl)
359 (.getControl sphere2 RigidBodyControl)
360 Vector3f/ZERO (Vector3f. 3 3 3))]
361 (add-joint sphere1 joint)
362 (doto (Node. "hinge-world")
363 (.attachChild sphere1)
364 (.attachChild sphere2))))
365
366 (defn test-joint []
367 (view (hinge-world)))
368
369 84
370 (defn worm [segment-length num-segments interstitial-space radius] 85 (defn worm [segment-length num-segments interstitial-space radius]
371 (letfn [(nth-segment 86 (letfn [(nth-segment
372 [n] 87 [n]
373 (box segment-length radius radius :mass 0.1 88 (box segment-length radius radius :mass 0.1
385 (let [node (Node. name)] 100 (let [node (Node. name)]
386 (dorun (map #(.attachChild node %) children)) 101 (dorun (map #(.attachChild node %) children))
387 node)) 102 node))
388 ([children] (nodify "" children))) 103 ([children] (nodify "" children)))
389 104
390
391 (defn connect-at-midpoint 105 (defn connect-at-midpoint
392 [segmentA segmentB] 106 [segmentA segmentB]
393 (let [centerA (.getWorldTranslation segmentA) 107 (let [centerA (.getWorldTranslation segmentA)
394 centerB (.getWorldTranslation segmentB) 108 centerB (.getWorldTranslation segmentB)
395 midpoint (.mult (.add centerA centerB) (float 0.5)) 109 midpoint (.mult (.add centerA centerB) (float 0.5))
396 pivotA (.subtract midpoint centerA) 110 pivotA (.subtract midpoint centerA)
397 pivotB (.subtract midpoint centerB) 111 pivotB (.subtract midpoint centerB)
398
399 joint (Point2PointJoint. 112 joint (Point2PointJoint.
400 (.getControl segmentA RigidBodyControl) 113 (.getControl segmentA RigidBodyControl)
401 (.getControl segmentB RigidBodyControl) 114 (.getControl segmentB RigidBodyControl)
402 pivotA 115 pivotA
403 pivotB)] 116 pivotB)]
404 (add-joint segmentA joint)
405 segmentB)) 117 segmentB))
406
407 118
408 (defn point-worm [] 119 (defn point-worm []
409 (let [segments (worm 0.2 5 0.1 0.1)] 120 (let [segments (worm 0.2 5 0.1 0.1)]
410 (dorun (map (partial apply connect-at-midpoint) 121 (dorun (map (partial apply connect-at-midpoint)
411 (partition 2 1 segments))) 122 (partition 2 1 segments)))
412 (nodify "worm" segments))) 123 (nodify "worm" segments)))
413
414 124
415 (defn test-worm [] 125 (defn test-worm []
416 (.start 126 (.start
417 (world 127 (world
418 (doto (Node.) 128 (doto (Node.)
443 ;; torque applied to their joints. There's not a single straight line 153 ;; torque applied to their joints. There's not a single straight line
444 ;; of force in the human body at all! (a straight line of force would 154 ;; of force in the human body at all! (a straight line of force would
445 ;; correspond to some sort of jet or rocket propulseion) 155 ;; correspond to some sort of jet or rocket propulseion)
446 156
447 (defn node-seq 157 (defn node-seq
448 "take a node and return a seq of all its children 158 "Take a node and return a seq of all its children
449 recursively. There will be no nodes left in the resulting 159 recursively. There will be no nodes left in the resulting
450 structure" 160 structure"
451 [#^Node node] 161 [#^Node node]
452 (tree-seq #(isa? (class %) Node) #(.getChildren %) node)) 162 (tree-seq #(isa? (class %) Node) #(.getChildren %) node))
453 163
475 (let [controls (keep #(.getControl % RigidBodyControl) 185 (let [controls (keep #(.getControl % RigidBodyControl)
476 (node-seq creature)) 186 (node-seq creature))
477 limb-controls (reduce concat (map torque-controls controls)) 187 limb-controls (reduce concat (map torque-controls controls))
478 body-control (partial map #(%1 %2) limb-controls)] 188 body-control (partial map #(%1 %2) limb-controls)]
479 body-control)) 189 body-control))
480
481
482 190
483 (defn test-motor-map 191 (defn test-motor-map
484 "see how torque works." 192 "see how torque works."
485 [] 193 []
486 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0) 194 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
495 (set-gravity world Vector3f/ZERO) 203 (set-gravity world Vector3f/ZERO)
496 (light-up-everything world) 204 (light-up-everything world)
497 (.setTimer world (NanoTimer.))) 205 (.setTimer world (NanoTimer.)))
498 (fn [_ _] 206 (fn [_ _]
499 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0])))))) 207 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
500
501
502 208
503 (defn test-torque 209 (defn test-torque
504 "see how torque works." 210 "see how torque works."
505 [] 211 []
506 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0) 212 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
525 (.mult (.getPhysicsRotation control) 231 (.mult (.getPhysicsRotation control)
526 (Vector3f. -3 20 0)))) 232 (Vector3f. -3 20 0))))
527 (if @move-right? 233 (if @move-right?
528 (.applyTorque control (Vector3f. 0 0 1))))))) 234 (.applyTorque control (Vector3f. 0 0 1)))))))
529 235
530
531 (defn worm-pattern [time] 236 (defn worm-pattern [time]
532 [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 237 [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
533 238
534 0 0 0 0 0 0 0 0 0 0 0 239 0 0 0 0 0 0 0 0 0 0 0
535 240
539 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 244 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
540 0 0 0 0 0 0 0 0 0 0 0 0 0 0 245 0 0 0 0 0 0 0 0 0 0 0 0 0 0
541 246
542 ]) 247 ])
543 248
544
545 ;; (defn copier-gen []
546 ;; (let [count (atom 0)]
547 ;; (fn [in]
548 ;; (swap! count inc)
549 ;; (clojure.contrib.duck-streams/copy
550 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
551 ;; ;;/home/r/tmp/mao-test/clojure-images
552 ;; (format "%08d.png" @count)))))))
553 ;; (defn decrease-framerate []
554 ;; (map
555 ;; (copier-gen)
556 ;; (sort
557 ;; (map first
558 ;; (partition
559 ;; 4
560 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
561 ;; (file-seq
562 ;; (file-str
563 ;; "/home/r/media/anime/mao-temp/images"))))))))
564
565
566
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
568
569
570
571
572 ;;;;;;;;;;;;;;;;;; Proprioception ;;;;;;;;;;;;;;;;;;;;;;;; 249 ;;;;;;;;;;;;;;;;;; Proprioception ;;;;;;;;;;;;;;;;;;;;;;;;
573
574 250
575 ;; this is not used as just getting the rotation would be simpler. 251 ;; this is not used as just getting the rotation would be simpler.
576 (defn proprioception-senses 252 (defn proprioception-senses
577 "given a control , create a sequence of thunks that will report the 253 "given a control , create a sequence of thunks that will report the
578 rotation of the control's object along the same axes as the motor-control map." 254 rotation of the control's object along the same axes as the motor-control map."
606 (let [basis-1 (orthogonal-vect axis) 282 (let [basis-1 (orthogonal-vect axis)
607 basis-2 (.cross axis basis-1) 283 basis-2 (.cross axis basis-1)
608 rotated (.mult q basis-1) 284 rotated (.mult q basis-1)
609 alpha (.dot basis-1 (.project rotated basis-1)) 285 alpha (.dot basis-1 (.project rotated basis-1))
610 beta (.dot basis-2 (.project rotated basis-2))] 286 beta (.dot basis-2 (.project rotated basis-2))]
611 (println-repl alpha)
612 (println-repl beta)
613 (Math/atan2 beta alpha))) 287 (Math/atan2 beta alpha)))
614 288
615 289
616 (defn check-rot [a] 290 (defn check-rot [a]
617 (rot-about-axis 291 (rot-about-axis
636 rotate-a 310 rotate-a
637 (.normalize 311 (.normalize
638 (.subtract 312 (.subtract
639 (.localToWorld object-b (.getPivotB joint) nil) 313 (.localToWorld object-b (.getPivotB joint) nil)
640 (.getWorldTranslation object-b)))) 314 (.getWorldTranslation object-b))))
641 rotate-b
642 (doto (Matrix3f.)
643 (.fromStartEndVectors arm-b Vector3f/UNIT_X))
644
645 pitch 315 pitch
646 (.angleBetween 316 (.angleBetween
647 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b))) 317 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
648 (Vector2f. 1 0)) 318 (Vector2f. 1 0))
649 yaw 319 yaw
650 (.angleBetween 320 (.angleBetween
651 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b))) 321 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
652 (Vector2f. 1 0)) 322 (Vector2f. 1 0))
653 323
654 roll 324 roll
655 (.mult 325 (rot-about-axis
656 (.getLocalRotation object-b) 326 (.mult
657 (doto (Quaternion.) 327 (.getLocalRotation object-b)
658 (.fromRotationMatrix rotate-a))) 328 (doto (Quaternion.)
329 (.fromRotationMatrix rotate-a)))
330 arm-b)
659 ] 331 ]
660 332
661 333
662 334
663 ;;(println-repl 335 ;;(println-repl
671 343
672 344
673 345
674 346
675 347
676 (defn proprioception
677 "Create a proprioception map that reports the rotations of the
678 various limbs of the creature's body"
679 [creature]
680 [#^Node creature]
681 (let [
682 nodes (node-seq creature)
683 joints
684 (map
685 :joint
686 (filter
687 #(isa? (class %) JointControl)
688 (reduce
689 concat
690 (map (fn [node]
691 (map (fn [num] (.getControl node num))
692 (range (.getNumControls node))))
693 nodes))))]
694 (fn []
695 (reduce concat (map relative-positions (list (first joints)))))))
696 348
697 349
698 (defn test-worm-control 350 (defn test-worm-control
699 [] 351 []
700 (let [worm (point-worm) 352 (let [worm (point-worm)
701 time (atom 0) 353 time (atom 0)
702 worm-motor-map (motor-map worm) 354 worm-motor-map (motor-map worm)
703 body-map (proprioception worm) 355 ;;body-map (proprioception worm)
704 debug-segments 356 debug-segments
705 (map 357 (map
706 #(doto 358 #(doto
707 (make-shape 359 (make-shape
708 (assoc base-shape 360 (assoc base-shape
738 debug-segment 390 debug-segment
739 (Quaternion. (float 0) (float 0.05) (float 0) (float 1)))) 391 (Quaternion. (float 0) (float 0.05) (float 0) (float 1))))
740 (drop 1 (node-seq worm)) 392 (drop 1 (node-seq worm))
741 debug-segments)) 393 debug-segments))
742 (swap! time inc) 394 (swap! time inc)
743 (println-repl (with-out-str (clojure.pprint/pprint (doall (body-map))))) 395 ;;(println-repl (with-out-str (clojure.pprint/pprint (doall (body-map)))))
744 (Thread/sleep 200) 396 (Thread/sleep 200)
745 (dorun (worm-motor-map 397 (dorun (worm-motor-map
746 (worm-pattern @time))))))) 398 (worm-pattern @time)))))))
747 399
748 400
753 "see how torque works." 405 "see how torque works."
754 [] 406 []
755 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0) 407 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
756 :mass 0 :color ColorRGBA/Green) 408 :mass 0 :color ColorRGBA/Green)
757 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0) 409 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
758 :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0)) 410 :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
759 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0) 411 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
760 :mass 0 :color ColorRGBA/Gray) 412 :mass 0 :color ColorRGBA/Gray)
761 413
762 move-up? (atom false) 414 move-up? (atom false)
763 move-down? (atom false) 415 move-down? (atom false)
784 "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) 436 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
785 "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) 437 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
786 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) 438 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
787 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) 439 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
788 (fn [world] 440 (fn [world]
789 (set-gravity world Vector3f/ZERO) 441 (set-gravity world (Vector3f. 0 0 0))
790 (.setMoveSpeed (.getFlyByCamera world) 50) 442 (.setMoveSpeed (.getFlyByCamera world) 50)
791 (.setRotationSpeed (.getFlyByCamera world) 50) 443 (.setRotationSpeed (.getFlyByCamera world) 50)
792 (light-up-everything world) 444 (light-up-everything world)
793 (.setTimer world (NanoTimer.)) 445 (.setTimer world (NanoTimer.))
794 ) 446 )
795 (fn [_ _] 447 (fn [_ _]
796 (if @move-up? 448 (if @move-up?
797 (.applyTorque control 449 (.applyTorque control
798 (.mult (.getPhysicsRotation control) 450 (.mult (.getPhysicsRotation control)
799 (Vector3f. 0 0 1)))) 451 (Vector3f. 0 0 10))))
800 (if @move-down? 452 (if @move-down?
801 (.applyTorque control 453 (.applyTorque control
802 (.mult (.getPhysicsRotation control) 454 (.mult (.getPhysicsRotation control)
803 (Vector3f. 0 0 -1)))) 455 (Vector3f. 0 0 -10))))
804 (if @move-left? 456 (if @move-left?
805 (.applyTorque control 457 (.applyTorque control
806 (.mult (.getPhysicsRotation control) 458 (.mult (.getPhysicsRotation control)
807 (Vector3f. 0 1 0)))) 459 (Vector3f. 0 10 0))))
808 (if @move-right? 460 (if @move-right?
809 (.applyTorque control 461 (.applyTorque control
810 (.mult (.getPhysicsRotation control) 462 (.mult (.getPhysicsRotation control)
811 (Vector3f. 0 -1 0)))) 463 (Vector3f. 0 -10 0))))
812 (if @roll-left? 464 (if @roll-left?
813 (.applyTorque control 465 (.applyTorque control
814 (.mult (.getPhysicsRotation control) 466 (.mult (.getPhysicsRotation control)
815 (Vector3f. -0.1 0 0)))) 467 (Vector3f. -1 0 0))))
816 (if @roll-right? 468 (if @roll-right?
817 (.applyTorque control 469 (.applyTorque control
818 (.mult (.getPhysicsRotation control) 470 (.mult (.getPhysicsRotation control)
819 (Vector3f. 0.1 0 0)))) 471 (Vector3f. 1 0 0))))
820 472
821 (if (= 0 (rem (swap! time inc) 2000)) 473 (if (= 0 (rem (swap! time inc) 2000))
822 (do 474 (do
823 ;;(println-repl (.getLocalRotation finger)) 475
824 (println-repl (nth (relative-positions joint) 2)))) 476 (apply
825 477 (comp
826 )))) 478 println-repl
827 479 #(format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n" %1 %2 %3))
828 480 (relative-positions joint))))))))
829
830
831 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
832
833
834
835 481
836 #+end_src 482 #+end_src
837 483
838 #+results: body-main 484
839 : #'cortex.body/test-prop 485 * COMMENT failed-clojure-code
840 486 #+begin_src clojure
487 ;;(.loadModel
488 ;; (doto (asset-manager)
489 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
490 ;; "Models/person/person.blend")
491
492 (defn view-model [^String model]
493 (view
494 (.loadModel
495 (doto (asset-manager)
496 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
497 model)))
498
499 (defn load-blender-scene [^String model]
500 (.loadModel
501 (doto (asset-manager)
502 (.registerLoader BlenderLoader (into-array String ["blend"])))
503 model))
504
505 (defn worm
506 []
507 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
508
509 (defn oto
510 []
511 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
512
513 (defn sinbad
514 []
515 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
516
517 (defn worm-blender
518 []
519 (first (seq (.getChildren (load-blender-model
520 "Models/anim2/simple-worm.blend")))))
521
522 (defn body
523 "given a node with a SkeletonControl, will produce a body sutiable
524 for AI control with movement and proprioception."
525 [node]
526 (let [skeleton-control (.getControl node SkeletonControl)
527 krc (KinematicRagdollControl.)]
528 (comment
529 (dorun
530 (map #(.addBoneName krc %)
531 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
532 ;;"mid2" "mid3" "tail" "head"]
533 )))
534 (.addControl node krc)
535 (.setRagdollMode krc)
536 )
537 node
538 )
539 (defn show-skeleton [node]
540 (let [sd
541
542 (doto
543 (SkeletonDebugger. "aurellem-skel-debug"
544 (skel node))
545 (.setMaterial (green-x-ray)))]
546 (.attachChild node sd)
547 node))
548
549
550
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552
553 ;; this could be a good way to give objects special properties like
554 ;; being eyes and the like
555
556 (.getUserData
557 (.getChild
558 (load-blender-model "Models/property/test.blend") 0)
559 "properties")
560
561 ;; the properties are saved along with the blender file.
562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
563
564
565
566
567 (defn init-debug-skel-node
568 [f debug-node skeleton]
569 (let [bones
570 (map #(.getBone skeleton %)
571 (range (.getBoneCount skeleton)))]
572 (dorun (map #(.setUserControl % true) bones))
573 (dorun (map (fn [b]
574 (println (.getName b)
575 " -- " (f b)))
576 bones))
577 (dorun
578 (map #(.attachChild
579 debug-node
580 (doto
581 (sphere 0.1
582 :position (f %)
583 :physical? false)
584 (.setMaterial (green-x-ray))))
585 bones)))
586 debug-node)
587
588 (import jme3test.bullet.PhysicsTestHelper)
589
590
591 (defn test-zzz [the-worm world value]
592 (if (not value)
593 (let [skeleton (skel the-worm)]
594 (println-repl "enabling bones")
595 (dorun
596 (map
597 #(.setUserControl (.getBone skeleton %) true)
598 (range (.getBoneCount skeleton))))
599
600
601 (let [b (.getBone skeleton 2)]
602 (println-repl "moving " (.getName b))
603 (println-repl (.getLocalPosition b))
604 (.setUserTransforms b
605 Vector3f/UNIT_X
606 Quaternion/IDENTITY
607 ;;(doto (Quaternion.)
608 ;; (.fromAngles (/ Math/PI 2)
609 ;; 0
610 ;; 0
611
612 (Vector3f. 1 1 1))
613 )
614
615 (println-repl "hi! <3"))))
616
617
618 (defn test-ragdoll []
619
620 (let [the-worm
621
622 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
623 (doto (show-skeleton (worm-blender))
624 (.setLocalTranslation (Vector3f. 0 10 0))
625 ;;(worm)
626 ;;(oto)
627 ;;(sinbad)
628 )
629 ]
630
631
632 (.start
633 (world
634 (doto (Node.)
635 (.attachChild the-worm))
636 {"key-return" (fire-cannon-ball)
637 "key-space" (partial test-zzz the-worm)
638 }
639 (fn [world]
640 (light-up-everything world)
641 (PhysicsTestHelper/createPhysicsTestWorld
642 (.getRootNode world)
643 (asset-manager)
644 (.getPhysicsSpace
645 (.getState (.getStateManager world) BulletAppState)))
646 (set-gravity world Vector3f/ZERO)
647 ;;(.setTimer world (NanoTimer.))
648 ;;(org.lwjgl.input.Mouse/setGrabbed false)
649 )
650 no-op
651 )
652
653
654 )))
655
656
657 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658 ;;; here is the ragdoll stuff
659
660 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
661 (def mesh worm-mesh)
662
663 (.getFloatBuffer mesh VertexBuffer$Type/Position)
664 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
665 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
666
667
668 (defn position [index]
669 (.get
670 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
671 index))
672
673 (defn bones [index]
674 (.get
675 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
676 index))
677
678 (defn bone-weights [index]
679 (.get
680 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
681 index))
682
683
684
685 (defn vertex-bones [vertex]
686 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
687
688 (defn vertex-weights [vertex]
689 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
690
691 (defn vertex-position [index]
692 (let [offset (* index 3)]
693 (Vector3f. (position offset)
694 (position (inc offset))
695 (position (inc(inc offset))))))
696
697 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
698
699 (defn bone-control-color [index]
700 (get {[1 0 0 0] ColorRGBA/Red
701 [1 2 0 0] ColorRGBA/Magenta
702 [2 0 0 0] ColorRGBA/Blue}
703 (vertex-bones index)
704 ColorRGBA/White))
705
706 (defn influence-color [index bone-num]
707 (get
708 {(float 0) ColorRGBA/Blue
709 (float 0.5) ColorRGBA/Green
710 (float 1) ColorRGBA/Red}
711 ;; find the weight of the desired bone
712 ((zipmap (vertex-bones index)(vertex-weights index))
713 bone-num)
714 ColorRGBA/Blue))
715
716 (def worm-vertices (set (map vertex-info (range 60))))
717
718
719 (defn test-info []
720 (let [points (Node.)]
721 (dorun
722 (map #(.attachChild points %)
723 (map #(sphere 0.01
724 :position (vertex-position %)
725 :color (influence-color % 1)
726 :physical? false)
727 (range 60))))
728 (view points)))
729
730
731 (defrecord JointControl [joint physics-space]
732 PhysicsControl
733 (setPhysicsSpace [this space]
734 (dosync
735 (ref-set (:physics-space this) space))
736 (.addJoint space (:joint this)))
737 (update [this tpf])
738 (setSpatial [this spatial])
739 (render [this rm vp])
740 (getPhysicsSpace [this] (deref (:physics-space this)))
741 (isEnabled [this] true)
742 (setEnabled [this state]))
743
744 (defn add-joint
745 "Add a joint to a particular object. When the object is added to the
746 PhysicsSpace of a simulation, the joint will also be added"
747 [object joint]
748 (let [control (JointControl. joint (ref nil))]
749 (.addControl object control))
750 object)
751
752
753 (defn hinge-world
754 []
755 (let [sphere1 (sphere)
756 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
757 joint (Point2PointJoint.
758 (.getControl sphere1 RigidBodyControl)
759 (.getControl sphere2 RigidBodyControl)
760 Vector3f/ZERO (Vector3f. 3 3 3))]
761 (add-joint sphere1 joint)
762 (doto (Node. "hinge-world")
763 (.attachChild sphere1)
764 (.attachChild sphere2))))
765
766
767 (defn test-joint []
768 (view (hinge-world)))
769
770 ;; (defn copier-gen []
771 ;; (let [count (atom 0)]
772 ;; (fn [in]
773 ;; (swap! count inc)
774 ;; (clojure.contrib.duck-streams/copy
775 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
776 ;; ;;/home/r/tmp/mao-test/clojure-images
777 ;; (format "%08d.png" @count)))))))
778 ;; (defn decrease-framerate []
779 ;; (map
780 ;; (copier-gen)
781 ;; (sort
782 ;; (map first
783 ;; (partition
784 ;; 4
785 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
786 ;; (file-seq
787 ;; (file-str
788 ;; "/home/r/media/anime/mao-temp/images"))))))))
789
790
791
792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
793
794 (defn proprioception
795 "Create a proprioception map that reports the rotations of the
796 various limbs of the creature's body"
797 [creature]
798 [#^Node creature]
799 (let [
800 nodes (node-seq creature)
801 joints
802 (map
803 :joint
804 (filter
805 #(isa? (class %) JointControl)
806 (reduce
807 concat
808 (map (fn [node]
809 (map (fn [num] (.getControl node num))
810 (range (.getNumControls node))))
811 nodes))))]
812 (fn []
813 (reduce concat (map relative-positions (list (first joints)))))))
814
815
816 #+end_src
841 817
842 818
843 819
844 820
845 821