view org/body.org @ 54:7bc3b22c507b

translation behaves as expected, but the debug mesh does seems to be reversed from the actual bone layout
author Robert McIntyre <rlm@mit.edu>
date Tue, 15 Nov 2011 00:53:06 -0700
parents bb24106cbd6a
children 8b95180f5c69
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/worm3.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 )))
125 (.addControl node krc)
126 (.setRagdollMode krc)
127 )
128 node
129 )
131 (defn green-x-ray []
132 (doto (Material. (asset-manager)
133 "Common/MatDefs/Misc/Unshaded.j3md")
134 (.setColor "Color" ColorRGBA/Green)
135 (-> (.getAdditionalRenderState)
136 (.setDepthTest false))))
138 (defn show-skeleton [node]
139 (let [sd
141 (doto
142 (SkeletonDebugger. "aurellem-skel-debug"
143 (skel node))
144 (.setMaterial (green-x-ray)))]
145 (.attachChild node sd)
146 node))
150 (defn init-debug-skel-node
151 [f debug-node skeleton]
152 (let [bones
153 (map #(.getBone skeleton %)
154 (range (.getBoneCount skeleton)))]
155 (dorun (map #(.setUserControl % true) bones))
156 (dorun (map (fn [b]
157 (println (.getName b)
158 " -- " (f b)))
159 bones))
161 (dorun
162 (map #(.attachChild
163 debug-node
164 (doto
165 (sphere 0.1
166 :position (f %)
167 :physical? false)
168 (.setMaterial (green-x-ray))))
169 bones)))
170 debug-node)
172 (import jme3test.bullet.PhysicsTestHelper)
175 (defn test-zzz [the-worm world value]
176 (if (not value)
177 (let [skeleton (skel the-worm)]
178 (println-repl "enabling bones")
179 (dorun
180 (map
181 #(.setUserControl (.getBone skeleton %) true)
182 (range (.getBoneCount skeleton))))
185 (let [b (.getBone skeleton 6)]
186 (println-repl "moving " (.getName b))
187 (println-repl (.getLocalPosition b))
188 (.setUserTransforms b
189 Vector3f/UNIT_X
190 Quaternion/IDENTITY
191 ;;(doto (Quaternion.)
192 ;; (.fromAngles (/ Math/PI 2)
193 ;; 0
194 ;; 0
196 (Vector3f. 1 1 1))
198 )
202 (println-repl "hi! <3"))))
205 (defn test-ragdoll []
207 (let [the-worm
209 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
210 (doto (show-skeleton (worm-blender))
211 (.setLocalTranslation (Vector3f. 0 10 0))
212 ;;(worm)
213 ;;(oto)
214 ;;(sinbad)
215 )
216 ]
219 (.start
220 (world
221 (doto (Node.)
222 (.attachChild the-worm))
223 {"key-return" (fire-cannon-ball)
224 "key-space" (partial test-zzz the-worm)
225 }
226 (fn [world]
227 (light-up-everything world)
228 (PhysicsTestHelper/createPhysicsTestWorld
229 (.getRootNode world)
230 (asset-manager)
231 (.getPhysicsSpace
232 (.getState (.getStateManager world) BulletAppState)))
233 (set-gravity world Vector3f/ZERO)
234 ;;(.setTimer world (NanoTimer.))
235 ;;(org.lwjgl.input.Mouse/setGrabbed false)
236 )
237 no-op
238 )
241 )))
252 #+end_src
261 * COMMENT generate Source.
262 #+begin_src clojure :tangle ../src/cortex/body.clj
263 <<body-main>>
264 #+end_src