rlm@0
|
1 #+title: The BODY!!!
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@4
|
4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.
|
rlm@4
|
5 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
6 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@4
|
7
|
rlm@53
|
8 * COMMENT Body
|
rlm@0
|
9
|
rlm@0
|
10 #+srcname: body-main
|
rlm@0
|
11 #+begin_src clojure
|
rlm@44
|
12 (ns cortex.body
|
rlm@44
|
13 (use (cortex world util import)))
|
rlm@44
|
14
|
rlm@0
|
15 (use 'clojure.contrib.def)
|
rlm@0
|
16 (cortex.import/mega-import-jme3)
|
rlm@0
|
17 (rlm.rlm-commands/help)
|
rlm@0
|
18
|
rlm@44
|
19 ;;(.loadModel
|
rlm@44
|
20 ;; (doto (asset-manager)
|
rlm@44
|
21 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@44
|
22 ;; "Models/person/person.blend")
|
rlm@44
|
23
|
rlm@44
|
24 (defn view-model [^String model]
|
rlm@44
|
25 (view
|
rlm@44
|
26 (.loadModel
|
rlm@44
|
27 (doto (asset-manager)
|
rlm@44
|
28 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@44
|
29 model)))
|
rlm@49
|
30
|
rlm@49
|
31 (defn load-blender-scene [^String model]
|
rlm@49
|
32 (.loadModel
|
rlm@49
|
33 (doto (asset-manager)
|
rlm@49
|
34 (.registerLoader BlenderLoader (into-array String ["blend"])))
|
rlm@49
|
35 model))
|
rlm@49
|
36
|
rlm@49
|
37 (defn load-blender-model
|
rlm@49
|
38 [^String model]
|
rlm@49
|
39 (.loadModel
|
rlm@49
|
40 (doto (asset-manager)
|
rlm@49
|
41 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@49
|
42 model))
|
rlm@49
|
43
|
rlm@49
|
44 (defn worm
|
rlm@49
|
45 []
|
rlm@50
|
46 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
|
rlm@49
|
47
|
rlm@50
|
48 (defn oto
|
rlm@49
|
49 []
|
rlm@49
|
50 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
|
rlm@49
|
51
|
rlm@50
|
52 (defn sinbad
|
rlm@50
|
53 []
|
rlm@50
|
54 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
|
rlm@50
|
55
|
rlm@50
|
56 (defn worm-blender
|
rlm@50
|
57 []
|
rlm@50
|
58 (first (seq (.getChildren (load-blender-model
|
rlm@56
|
59 "Models/anim2/simple-worm.blend")))))
|
rlm@50
|
60
|
rlm@52
|
61 (defn skel [node]
|
rlm@52
|
62 (doto
|
rlm@52
|
63 (.getSkeleton
|
rlm@52
|
64 (.getControl node SkeletonControl))
|
rlm@52
|
65 ;; this is necessary to force the skeleton to have accurate world
|
rlm@52
|
66 ;; transforms before it is rendered to the screen.
|
rlm@52
|
67 (.resetAndUpdate)))
|
rlm@52
|
68
|
rlm@50
|
69 (defprotocol Textual
|
rlm@50
|
70 (text [something]
|
rlm@50
|
71 "Display a detailed textual analysis of the given object."))
|
rlm@50
|
72
|
rlm@50
|
73 (extend-type com.jme3.scene.Node
|
rlm@50
|
74 Textual
|
rlm@50
|
75 (text [node]
|
rlm@50
|
76 (println "Total Vertexes: " (.getVertexCount node))
|
rlm@50
|
77 (println "Total Triangles: " (.getTriangleCount node))
|
rlm@50
|
78 (println "Controls :")
|
rlm@50
|
79 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
|
rlm@50
|
80 (println "Has " (.getQuantity node) " Children:")
|
rlm@50
|
81 (doall (map text (.getChildren node)))))
|
rlm@50
|
82
|
rlm@50
|
83 (extend-type com.jme3.animation.AnimControl
|
rlm@50
|
84 Textual
|
rlm@50
|
85 (text [control]
|
rlm@50
|
86 (let [animations (.getAnimationNames control)]
|
rlm@52
|
87 (println "Animation Control with " (count animations) " animation(s):")
|
rlm@50
|
88 (dorun (map println animations)))))
|
rlm@50
|
89
|
rlm@50
|
90 (extend-type com.jme3.animation.SkeletonControl
|
rlm@50
|
91 Textual
|
rlm@50
|
92 (text [control]
|
rlm@50
|
93 (println "Skeleton Control with the following skeleton:")
|
rlm@50
|
94 (println (.getSkeleton control))))
|
rlm@50
|
95
|
rlm@52
|
96 (extend-type com.jme3.bullet.control.KinematicRagdollControl
|
rlm@52
|
97 Textual
|
rlm@52
|
98 (text [control]
|
rlm@52
|
99 (println "Ragdoll Control")))
|
rlm@52
|
100
|
rlm@52
|
101
|
rlm@50
|
102 (extend-type com.jme3.scene.Geometry
|
rlm@50
|
103 Textual
|
rlm@50
|
104 (text [control]
|
rlm@50
|
105 (println "...geo...")))
|
rlm@50
|
106
|
rlm@50
|
107
|
rlm@50
|
108
|
rlm@50
|
109
|
rlm@50
|
110 (defn body
|
rlm@52
|
111 "given a node with a SkeletonControl, will produce a body sutiable
|
rlm@52
|
112 for AI control with movement and proprioception."
|
rlm@50
|
113 [node]
|
rlm@50
|
114 (let [skeleton-control (.getControl node SkeletonControl)
|
rlm@53
|
115 krc (KinematicRagdollControl.)]
|
rlm@53
|
116 (comment
|
rlm@50
|
117 (dorun
|
rlm@50
|
118 (map #(.addBoneName krc %)
|
rlm@53
|
119 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
|
rlm@53
|
120 ;;"mid2" "mid3" "tail" "head"]
|
rlm@53
|
121 )))
|
rlm@50
|
122 (.addControl node krc)
|
rlm@52
|
123 (.setRagdollMode krc)
|
rlm@52
|
124 )
|
rlm@50
|
125 node
|
rlm@50
|
126 )
|
rlm@50
|
127
|
rlm@52
|
128 (defn green-x-ray []
|
rlm@51
|
129 (doto (Material. (asset-manager)
|
rlm@51
|
130 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@51
|
131 (.setColor "Color" ColorRGBA/Green)
|
rlm@52
|
132 (-> (.getAdditionalRenderState)
|
rlm@52
|
133 (.setDepthTest false))))
|
rlm@51
|
134
|
rlm@53
|
135 (defn show-skeleton [node]
|
rlm@50
|
136 (let [sd
|
rlm@51
|
137
|
rlm@50
|
138 (doto
|
rlm@51
|
139 (SkeletonDebugger. "aurellem-skel-debug"
|
rlm@51
|
140 (skel node))
|
rlm@51
|
141 (.setMaterial (green-x-ray)))]
|
rlm@53
|
142 (.attachChild node sd)
|
rlm@53
|
143 node))
|
rlm@50
|
144
|
rlm@51
|
145
|
rlm@50
|
146
|
rlm@50
|
147 (defn init-debug-skel-node
|
rlm@50
|
148 [f debug-node skeleton]
|
rlm@50
|
149 (let [bones
|
rlm@50
|
150 (map #(.getBone skeleton %)
|
rlm@50
|
151 (range (.getBoneCount skeleton)))]
|
rlm@50
|
152 (dorun (map #(.setUserControl % true) bones))
|
rlm@50
|
153 (dorun (map (fn [b]
|
rlm@50
|
154 (println (.getName b)
|
rlm@50
|
155 " -- " (f b)))
|
rlm@50
|
156 bones))
|
rlm@50
|
157 (dorun
|
rlm@50
|
158 (map #(.attachChild
|
rlm@50
|
159 debug-node
|
rlm@51
|
160 (doto
|
rlm@51
|
161 (sphere 0.1
|
rlm@51
|
162 :position (f %)
|
rlm@51
|
163 :physical? false)
|
rlm@51
|
164 (.setMaterial (green-x-ray))))
|
rlm@50
|
165 bones)))
|
rlm@50
|
166 debug-node)
|
rlm@50
|
167
|
rlm@52
|
168 (import jme3test.bullet.PhysicsTestHelper)
|
rlm@50
|
169
|
rlm@53
|
170
|
rlm@53
|
171 (defn test-zzz [the-worm world value]
|
rlm@53
|
172 (if (not value)
|
rlm@53
|
173 (let [skeleton (skel the-worm)]
|
rlm@53
|
174 (println-repl "enabling bones")
|
rlm@53
|
175 (dorun
|
rlm@53
|
176 (map
|
rlm@53
|
177 #(.setUserControl (.getBone skeleton %) true)
|
rlm@53
|
178 (range (.getBoneCount skeleton))))
|
rlm@53
|
179
|
rlm@53
|
180
|
rlm@56
|
181 (let [b (.getBone skeleton 2)]
|
rlm@53
|
182 (println-repl "moving " (.getName b))
|
rlm@53
|
183 (println-repl (.getLocalPosition b))
|
rlm@53
|
184 (.setUserTransforms b
|
rlm@54
|
185 Vector3f/UNIT_X
|
rlm@53
|
186 Quaternion/IDENTITY
|
rlm@54
|
187 ;;(doto (Quaternion.)
|
rlm@54
|
188 ;; (.fromAngles (/ Math/PI 2)
|
rlm@54
|
189 ;; 0
|
rlm@54
|
190 ;; 0
|
rlm@54
|
191
|
rlm@53
|
192 (Vector3f. 1 1 1))
|
rlm@53
|
193 )
|
rlm@53
|
194
|
rlm@53
|
195 (println-repl "hi! <3"))))
|
rlm@53
|
196
|
rlm@53
|
197
|
rlm@50
|
198 (defn test-ragdoll []
|
rlm@50
|
199
|
rlm@50
|
200 (let [the-worm
|
rlm@53
|
201
|
rlm@52
|
202 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
|
rlm@53
|
203 (doto (show-skeleton (worm-blender))
|
rlm@53
|
204 (.setLocalTranslation (Vector3f. 0 10 0))
|
rlm@53
|
205 ;;(worm)
|
rlm@52
|
206 ;;(oto)
|
rlm@52
|
207 ;;(sinbad)
|
rlm@50
|
208 )
|
rlm@50
|
209 ]
|
rlm@50
|
210
|
rlm@50
|
211
|
rlm@50
|
212 (.start
|
rlm@50
|
213 (world
|
rlm@50
|
214 (doto (Node.)
|
rlm@50
|
215 (.attachChild the-worm))
|
rlm@53
|
216 {"key-return" (fire-cannon-ball)
|
rlm@53
|
217 "key-space" (partial test-zzz the-worm)
|
rlm@53
|
218 }
|
rlm@50
|
219 (fn [world]
|
rlm@50
|
220 (light-up-everything world)
|
rlm@52
|
221 (PhysicsTestHelper/createPhysicsTestWorld
|
rlm@52
|
222 (.getRootNode world)
|
rlm@52
|
223 (asset-manager)
|
rlm@52
|
224 (.getPhysicsSpace
|
rlm@52
|
225 (.getState (.getStateManager world) BulletAppState)))
|
rlm@53
|
226 (set-gravity world Vector3f/ZERO)
|
rlm@50
|
227 ;;(.setTimer world (NanoTimer.))
|
rlm@50
|
228 ;;(org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@50
|
229 )
|
rlm@50
|
230 no-op
|
rlm@50
|
231 )
|
rlm@50
|
232
|
rlm@50
|
233
|
rlm@50
|
234 )))
|
rlm@50
|
235
|
rlm@55
|
236 (defn joint-control
|
rlm@55
|
237 [joint]
|
rlm@56
|
238 (let [physics-space (ref nil)]
|
rlm@55
|
239 (reify PhysicsControl
|
rlm@55
|
240 (setPhysicsSpace [this space]
|
rlm@55
|
241 (dosync
|
rlm@55
|
242 (ref-set physics-space space))
|
rlm@55
|
243 (.addJoint space joint))
|
rlm@55
|
244 (update [this tpf])
|
rlm@55
|
245 (setSpatial [this spatial])
|
rlm@55
|
246 (render [this rm vp])
|
rlm@55
|
247 (getPhysicsSpace [this] (deref physics-space))
|
rlm@56
|
248 (isEnabled [this] true)
|
rlm@56
|
249 (setEnabled [this state]))))
|
rlm@50
|
250
|
rlm@55
|
251 (defn add-joint
|
rlm@55
|
252 "Add a joint to a particular object. When the object is added to the
|
rlm@55
|
253 PhysicsSpace of a simulation, the joint will also be added"
|
rlm@55
|
254 [object joint]
|
rlm@55
|
255 (let [control (joint-control joint)]
|
rlm@55
|
256 (.addControl object control))
|
rlm@55
|
257 object)
|
rlm@50
|
258
|
rlm@55
|
259 (defn hinge-world
|
rlm@55
|
260 []
|
rlm@55
|
261 (let [sphere1 (sphere)
|
rlm@55
|
262 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
|
rlm@55
|
263 joint (Point2PointJoint.
|
rlm@55
|
264 (.getControl sphere1 RigidBodyControl)
|
rlm@55
|
265 (.getControl sphere2 RigidBodyControl)
|
rlm@55
|
266 Vector3f/ZERO (Vector3f. 3 3 3))]
|
rlm@55
|
267 (add-joint sphere1 joint)
|
rlm@55
|
268 (doto (Node. "hinge-world")
|
rlm@55
|
269 (.attachChild sphere1)
|
rlm@55
|
270 (.attachChild sphere2))))
|
rlm@55
|
271
|
rlm@56
|
272 (defn test-joint []
|
rlm@56
|
273 (view (hinge-world)))
|
rlm@55
|
274
|
rlm@49
|
275
|
rlm@49
|
276
|
rlm@49
|
277
|
rlm@56
|
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@56
|
279 ;;; here is the ragdoll stuff
|
rlm@56
|
280
|
rlm@56
|
281 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
|
rlm@56
|
282 (def mesh worm-mesh)
|
rlm@56
|
283
|
rlm@56
|
284 (.getFloatBuffer mesh VertexBuffer$Type/Position)
|
rlm@56
|
285 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@56
|
286 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@56
|
287
|
rlm@56
|
288
|
rlm@56
|
289 (defn position [index]
|
rlm@56
|
290 (.get
|
rlm@56
|
291 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
|
rlm@56
|
292 index))
|
rlm@56
|
293
|
rlm@56
|
294 (defn bones [index]
|
rlm@56
|
295 (.get
|
rlm@56
|
296 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@56
|
297 index))
|
rlm@56
|
298
|
rlm@56
|
299 (defn bone-weights [index]
|
rlm@56
|
300 (.get
|
rlm@56
|
301 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@56
|
302 index))
|
rlm@56
|
303
|
rlm@57
|
304
|
rlm@57
|
305
|
rlm@57
|
306 (defn vertex-bones [vertex]
|
rlm@57
|
307 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@57
|
308
|
rlm@57
|
309 (defn vertex-weights [vertex]
|
rlm@57
|
310 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@57
|
311
|
rlm@57
|
312 (defn vertex-position [index]
|
rlm@57
|
313 (let [offset (* index 3)]
|
rlm@57
|
314 (Vector3f. (position offset)
|
rlm@57
|
315 (position (inc offset))
|
rlm@57
|
316 (position (inc(inc offset))))))
|
rlm@57
|
317
|
rlm@57
|
318 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
|
rlm@57
|
319
|
rlm@57
|
320 (defn bone-control-color [index]
|
rlm@57
|
321 (get {[1 0 0 0] ColorRGBA/Red
|
rlm@57
|
322 [1 2 0 0] ColorRGBA/Magenta
|
rlm@57
|
323 [2 0 0 0] ColorRGBA/Blue}
|
rlm@57
|
324 (vertex-bones index)
|
rlm@57
|
325 ColorRGBA/White))
|
rlm@57
|
326
|
rlm@56
|
327 (defn influence-color [index bone-num]
|
rlm@56
|
328 (get
|
rlm@56
|
329 {(float 0) ColorRGBA/Blue
|
rlm@56
|
330 (float 0.5) ColorRGBA/Green
|
rlm@56
|
331 (float 1) ColorRGBA/Red}
|
rlm@57
|
332 ;; find the weight of the desired bone
|
rlm@57
|
333 ((zipmap (vertex-bones index)(vertex-weights index))
|
rlm@57
|
334 bone-num)
|
rlm@57
|
335 ColorRGBA/Blue))
|
rlm@57
|
336
|
rlm@57
|
337
|
rlm@57
|
338
|
rlm@57
|
339
|
rlm@57
|
340 (def worm-vertices (set (map vertex-info (range 60))))
|
rlm@57
|
341
|
rlm@56
|
342
|
rlm@56
|
343 (defn test-info []
|
rlm@56
|
344 (let [points (Node.)]
|
rlm@56
|
345 (dorun
|
rlm@56
|
346 (map #(.attachChild points %)
|
rlm@56
|
347 (map #(sphere 0.01
|
rlm@57
|
348 :position (vertex-position %)
|
rlm@57
|
349 :color (influence-color % 1)
|
rlm@56
|
350 :physical? false)
|
rlm@57
|
351 (range 60))))
|
rlm@56
|
352 (view points)))
|
rlm@56
|
353
|
rlm@56
|
354
|
rlm@56
|
355
|
rlm@56
|
356
|
rlm@56
|
357
|
rlm@56
|
358
|
rlm@56
|
359
|
rlm@56
|
360
|
rlm@56
|
361 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@56
|
362
|
rlm@56
|
363
|
rlm@56
|
364
|
rlm@56
|
365
|
rlm@56
|
366
|
rlm@56
|
367
|
rlm@56
|
368
|
rlm@56
|
369
|
rlm@0
|
370 #+end_src
|
rlm@0
|
371
|
rlm@0
|
372
|
rlm@0
|
373
|
rlm@0
|
374
|
rlm@0
|
375
|
rlm@0
|
376
|
rlm@0
|
377
|
rlm@0
|
378
|
rlm@0
|
379 * COMMENT generate Source.
|
rlm@44
|
380 #+begin_src clojure :tangle ../src/cortex/body.clj
|
rlm@0
|
381 <<body-main>>
|
rlm@0
|
382 #+end_src
|
rlm@0
|
383
|