view org/body.org @ 51:2a6ede6d8ad8

problem was that the skeleton was not 'ready'
author Robert McIntyre <rlm@mit.edu>
date Mon, 14 Nov 2011 19:44:30 -0700
parents b1b90c4ab0bf
children 00d0e1639d4b
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 * Body
10 #+srcname: body-main
11 #+begin_src clojure
12 (ns cortex.body
13 (use (cortex world util import)))
15 (use 'clojure.contrib.def)
16 (cortex.import/mega-import-jme3)
17 (rlm.rlm-commands/help)
19 ;;(.loadModel
20 ;; (doto (asset-manager)
21 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
22 ;; "Models/person/person.blend")
24 (defn view-model [^String model]
25 (view
26 (.loadModel
27 (doto (asset-manager)
28 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
29 model)))
31 (defn load-blender-scene [^String model]
32 (.loadModel
33 (doto (asset-manager)
34 (.registerLoader BlenderLoader (into-array String ["blend"])))
35 model))
37 (defn load-blender-model
38 [^String model]
39 (.loadModel
40 (doto (asset-manager)
41 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
42 model))
44 (defn worm
45 []
46 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
48 (defn oto
49 []
50 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
52 (defn sinbad
53 []
54 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
56 (defn worm-blender
57 []
58 (first (seq (.getChildren (load-blender-model
59 "Models/anim2/worm3.blend")))))
61 (defprotocol Textual
62 (text [something]
63 "Display a detailed textual analysis of the given object."))
65 (extend-type com.jme3.scene.Node
66 Textual
67 (text [node]
68 (println "Total Vertexes: " (.getVertexCount node))
69 (println "Total Triangles: " (.getTriangleCount node))
70 (println "Controls :")
71 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
72 (println "Has " (.getQuantity node) " Children:")
73 (doall (map text (.getChildren node)))))
75 (extend-type com.jme3.animation.AnimControl
76 Textual
77 (text [control]
78 (let [animations (.getAnimationNames control)]
79 (println "Animation Control with " (count animations) " animations:")
80 (dorun (map println animations)))))
82 (extend-type com.jme3.animation.SkeletonControl
83 Textual
84 (text [control]
85 (println "Skeleton Control with the following skeleton:")
86 (println (.getSkeleton control))))
88 (extend-type com.jme3.scene.Geometry
89 Textual
90 (text [control]
91 (println "...geo...")))
96 (defn body
97 "given a node with a skeleton, will produce a body sutiable for AI
98 control with movement and proprioception."
99 [node]
100 (let [skeleton-control (.getControl node SkeletonControl)
101 krc (KinematicRagdollControl. (float 0.5))]
102 (dorun
103 (map #(.addBoneName krc %)
104 ["mid1" "mid2" "mid3" "tail" "head"]))
105 (.addControl node krc)
106 (.setRagdollMode krc))
107 node
108 )
110 (defn
111 green-x-ray []
112 (doto (Material. (asset-manager)
113 "Common/MatDefs/Misc/Unshaded.j3md")
114 (.setColor "Color" ColorRGBA/Green)
115 (-> (.getAdditionalRenderState) (.setDepthTest
116 false))))
118 (defn view-skeleton [node]
119 (let [sd
121 (doto
122 (SkeletonDebugger. "aurellem-skel-debug"
123 (skel node))
124 (.setMaterial (green-x-ray)))]
125 (view (doto (Node. "view node")
126 (.attachChild sd)
131 ;;(.attachChild node)
132 ))))
135 (defn init-debug-skel-node
136 [f debug-node skeleton]
137 (let [bones
138 (map #(.getBone skeleton %)
139 (range (.getBoneCount skeleton)))]
140 (dorun (map #(.setUserControl % true) bones))
141 (dorun (map (fn [b]
142 (println (.getName b)
143 " -- " (f b)))
144 bones))
146 (dorun
147 (map #(.attachChild
148 debug-node
149 (doto
150 (sphere 0.1
151 :position (f %)
152 :physical? false)
153 (.setMaterial (green-x-ray))))
154 bones)))
155 debug-node)
157 (defn skel [node]
158 (doto
159 (.getSkeleton
160 (.getControl node SkeletonControl))
161 ;; this is necessary to force the skeleton to have accurate world
162 ;; transforms before it is rendered to the screen.
163 (.resetAndUpdate)))
169 (view
170 (init-debug-skel-node
171 (fn [b] (.getWorldBindPosition b))
172 (Node. "skel-debug") (skel (worm))))
178 (defn test-ragdoll []
180 (let [the-worm
181 (body
182 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
183 ;;(worm-blender)
184 )
185 ]
188 (.start
189 (world
190 (doto (Node.)
191 (.attachChild the-worm))
192 {"mouse-left" (fire-cannon-ball)}
193 (fn [world]
194 (light-up-everything world)
195 ;;(.setTimer world (NanoTimer.))
196 ;;(org.lwjgl.input.Mouse/setGrabbed false)
197 )
198 no-op
199 )
202 )))
221 (defn test-worm-anim []
223 (let [the-worm
224 ;; (oto)
225 (worm-blender)
226 anim-control (.getControl the-worm AnimControl)
227 channel (doto (.createChannel anim-control)
228 ;; (.setAnim "Walk")
229 (.setAnim "moveHead")
230 )
232 play-anim
233 no-op
234 ;; (fn [world pressed]
235 ;; (if (not pressed)
236 ;; (do
237 ;; (println-repl "space")
238 ;; ;; (.setAnim channel "moveHead")
239 ;; (.setAnim channel "Walk" (float 0.5))
240 ;; )))
242 ]
244 (.start
245 (world
246 (doto (Node.)
247 (.attachChild the-worm))
248 {"key-space" play-anim
249 "mouse-left" (fire-cannon-ball)}
250 (fn [world]
251 (light-up-everything world)
252 ;;(.setTimer world (NanoTimer.))
253 (org.lwjgl.input.Mouse/setGrabbed false)
254 )
255 no-op
256 )
259 )))
267 #+end_src
276 * COMMENT generate Source.
277 #+begin_src clojure :tangle ../src/cortex/body.clj
278 <<body-main>>
279 #+end_src