view org/body.org @ 56:8b571c137f81

bone influence appears to be wrong! Investigating...
author Robert McIntyre <rlm@mit.edu>
date Tue, 15 Nov 2011 23:18:37 -0700
parents 8b95180f5c69
children 37a3256e1ed3
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 * COMMENT 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/simple-worm.blend")))))
61 (defn skel [node]
62 (doto
63 (.getSkeleton
64 (.getControl node SkeletonControl))
65 ;; this is necessary to force the skeleton to have accurate world
66 ;; transforms before it is rendered to the screen.
67 (.resetAndUpdate)))
69 (defprotocol Textual
70 (text [something]
71 "Display a detailed textual analysis of the given object."))
73 (extend-type com.jme3.scene.Node
74 Textual
75 (text [node]
76 (println "Total Vertexes: " (.getVertexCount node))
77 (println "Total Triangles: " (.getTriangleCount node))
78 (println "Controls :")
79 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
80 (println "Has " (.getQuantity node) " Children:")
81 (doall (map text (.getChildren node)))))
83 (extend-type com.jme3.animation.AnimControl
84 Textual
85 (text [control]
86 (let [animations (.getAnimationNames control)]
87 (println "Animation Control with " (count animations) " animation(s):")
88 (dorun (map println animations)))))
90 (extend-type com.jme3.animation.SkeletonControl
91 Textual
92 (text [control]
93 (println "Skeleton Control with the following skeleton:")
94 (println (.getSkeleton control))))
96 (extend-type com.jme3.bullet.control.KinematicRagdollControl
97 Textual
98 (text [control]
99 (println "Ragdoll Control")))
102 (extend-type com.jme3.scene.Geometry
103 Textual
104 (text [control]
105 (println "...geo...")))
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 )
128 (defn green-x-ray []
129 (doto (Material. (asset-manager)
130 "Common/MatDefs/Misc/Unshaded.j3md")
131 (.setColor "Color" ColorRGBA/Green)
132 (-> (.getAdditionalRenderState)
133 (.setDepthTest false))))
135 (defn show-skeleton [node]
136 (let [sd
138 (doto
139 (SkeletonDebugger. "aurellem-skel-debug"
140 (skel node))
141 (.setMaterial (green-x-ray)))]
142 (.attachChild node sd)
143 node))
147 (defn init-debug-skel-node
148 [f debug-node skeleton]
149 (let [bones
150 (map #(.getBone skeleton %)
151 (range (.getBoneCount skeleton)))]
152 (dorun (map #(.setUserControl % true) bones))
153 (dorun (map (fn [b]
154 (println (.getName b)
155 " -- " (f b)))
156 bones))
157 (dorun
158 (map #(.attachChild
159 debug-node
160 (doto
161 (sphere 0.1
162 :position (f %)
163 :physical? false)
164 (.setMaterial (green-x-ray))))
165 bones)))
166 debug-node)
168 (import jme3test.bullet.PhysicsTestHelper)
171 (defn test-zzz [the-worm world value]
172 (if (not value)
173 (let [skeleton (skel the-worm)]
174 (println-repl "enabling bones")
175 (dorun
176 (map
177 #(.setUserControl (.getBone skeleton %) true)
178 (range (.getBoneCount skeleton))))
181 (let [b (.getBone skeleton 2)]
182 (println-repl "moving " (.getName b))
183 (println-repl (.getLocalPosition b))
184 (.setUserTransforms b
185 Vector3f/UNIT_X
186 Quaternion/IDENTITY
187 ;;(doto (Quaternion.)
188 ;; (.fromAngles (/ Math/PI 2)
189 ;; 0
190 ;; 0
192 (Vector3f. 1 1 1))
193 )
195 (println-repl "hi! <3"))))
198 (defn test-ragdoll []
200 (let [the-worm
202 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
203 (doto (show-skeleton (worm-blender))
204 (.setLocalTranslation (Vector3f. 0 10 0))
205 ;;(worm)
206 ;;(oto)
207 ;;(sinbad)
208 )
209 ]
212 (.start
213 (world
214 (doto (Node.)
215 (.attachChild the-worm))
216 {"key-return" (fire-cannon-ball)
217 "key-space" (partial test-zzz the-worm)
218 }
219 (fn [world]
220 (light-up-everything world)
221 (PhysicsTestHelper/createPhysicsTestWorld
222 (.getRootNode world)
223 (asset-manager)
224 (.getPhysicsSpace
225 (.getState (.getStateManager world) BulletAppState)))
226 (set-gravity world Vector3f/ZERO)
227 ;;(.setTimer world (NanoTimer.))
228 ;;(org.lwjgl.input.Mouse/setGrabbed false)
229 )
230 no-op
231 )
234 )))
236 (defn joint-control
237 [joint]
238 (let [physics-space (ref nil)]
239 (reify PhysicsControl
240 (setPhysicsSpace [this space]
241 (dosync
242 (ref-set physics-space space))
243 (.addJoint space joint))
244 (update [this tpf])
245 (setSpatial [this spatial])
246 (render [this rm vp])
247 (getPhysicsSpace [this] (deref physics-space))
248 (isEnabled [this] true)
249 (setEnabled [this state]))))
251 (defn add-joint
252 "Add a joint to a particular object. When the object is added to the
253 PhysicsSpace of a simulation, the joint will also be added"
254 [object joint]
255 (let [control (joint-control joint)]
256 (.addControl object control))
257 object)
259 (defn hinge-world
260 []
261 (let [sphere1 (sphere)
262 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
263 joint (Point2PointJoint.
264 (.getControl sphere1 RigidBodyControl)
265 (.getControl sphere2 RigidBodyControl)
266 Vector3f/ZERO (Vector3f. 3 3 3))]
267 (add-joint sphere1 joint)
268 (doto (Node. "hinge-world")
269 (.attachChild sphere1)
270 (.attachChild sphere2))))
272 (defn test-joint []
273 (view (hinge-world)))
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;;; here is the ragdoll stuff
281 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
282 (def mesh worm-mesh)
284 (.getFloatBuffer mesh VertexBuffer$Type/Position)
285 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
286 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
289 (defn position [index]
290 (.get
291 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
292 index))
294 (defn vec-pos [index]
295 (let [offset (* index 3)]
296 (Vector3f. (position offset)
297 (position (inc offset))
298 (position (inc(inc offset))))))
300 (defn bones [index]
301 (.get
302 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
303 index))
305 (defn bone-control-color [index]
306 (get {[1 0 0 0] ColorRGBA/Red
307 [1 2 0 0] ColorRGBA/Magenta
308 [2 0 0 0] ColorRGBA/Blue}
309 (vec (map (comp int bones) (range (* index 4) (+ (* index 4) 4))))
310 ColorRGBA/White))
312 (defn bone-weights [index]
313 (.get
314 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
315 index))
317 (defn influence-color [index bone-num]
318 (get
319 {(float 0) ColorRGBA/Blue
320 (float 0.5) ColorRGBA/Green
321 (float 1) ColorRGBA/Red}
322 (bone-weights (+ (* 4 index) bone-num))))
324 (defn test-info []
325 (let [points (Node.)]
326 (dorun
327 (map #(.attachChild points %)
328 (map #(sphere 0.01
329 :position (vec-pos %)
330 :color (influence-color % 0)
331 :physical? false)
332 (range 12))))
333 (view points)))
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 #+end_src
360 * COMMENT generate Source.
361 #+begin_src clojure :tangle ../src/cortex/body.clj
362 <<body-main>>
363 #+end_src