view org/body.org @ 53:bb24106cbd6a

many problems solved by tightening up the vertex groups in the blender model
author Robert McIntyre <rlm@mit.edu>
date Tue, 15 Nov 2011 00:24:46 -0700
parents 00d0e1639d4b
children 7bc3b22c507b
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 (.add (.getLocalPosition b) Vector3f/UNIT_X)
190 Quaternion/IDENTITY
191 (Vector3f. 1 1 1))
193 )
197 (println-repl "hi! <3"))))
200 (defn test-ragdoll []
202 (let [the-worm
204 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
205 (doto (show-skeleton (worm-blender))
206 (.setLocalTranslation (Vector3f. 0 10 0))
207 ;;(worm)
208 ;;(oto)
209 ;;(sinbad)
210 )
211 ]
214 (.start
215 (world
216 (doto (Node.)
217 (.attachChild the-worm))
218 {"key-return" (fire-cannon-ball)
219 "key-space" (partial test-zzz the-worm)
220 }
221 (fn [world]
222 (light-up-everything world)
223 (PhysicsTestHelper/createPhysicsTestWorld
224 (.getRootNode world)
225 (asset-manager)
226 (.getPhysicsSpace
227 (.getState (.getStateManager world) BulletAppState)))
228 (set-gravity world Vector3f/ZERO)
229 ;;(.setTimer world (NanoTimer.))
230 ;;(org.lwjgl.input.Mouse/setGrabbed false)
231 )
232 no-op
233 )
236 )))
247 #+end_src
256 * COMMENT generate Source.
257 #+begin_src clojure :tangle ../src/cortex/body.clj
258 <<body-main>>
259 #+end_src