view org/body.org @ 52:00d0e1639d4b

simplified world, got buggy physics ragdoll working
author Robert McIntyre <rlm@mit.edu>
date Mon, 14 Nov 2011 21:30:23 -0700
parents 2a6ede6d8ad8
children bb24106cbd6a
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 (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. (float 0.5))]
116 (dorun
117 (map #(.addBoneName krc %)
118 ["mid1" "mid2" "mid3" "tail" "head"]
119 ;; ["Ulna.L"
120 ;; "Ulna.R"
121 ;; "Chest"
122 ;; "Foot.L"
123 ;; "Foot.R"
124 ;; "Hand.R"
125 ;; "Hand.L"
126 ;; "Neck"
127 ;; "Root"
128 ;; "Stomach"
129 ;; "Waist"
130 ;; "Humerus.L"
131 ;; "Humerus.R"
132 ;; "Thigh.L"
133 ;; "Thigh.R"
134 ;; "Calf.L"
135 ;; "Calf.R"
136 ;; "Clavicle.L"
137 ;; "Clavicle.R"]
138 ))
142 (.addControl node krc)
143 (.setRagdollMode krc)
144 )
145 node
146 )
148 (defn green-x-ray []
149 (doto (Material. (asset-manager)
150 "Common/MatDefs/Misc/Unshaded.j3md")
151 (.setColor "Color" ColorRGBA/Green)
152 (-> (.getAdditionalRenderState)
153 (.setDepthTest false))))
155 (defn view-skeleton [node]
156 (let [sd
158 (doto
159 (SkeletonDebugger. "aurellem-skel-debug"
160 (skel node))
161 (.setMaterial (green-x-ray)))]
162 (view (doto (Node. "view node")
163 (.attachChild sd)
168 ;;(.attachChild node)
169 ))))
172 (defn init-debug-skel-node
173 [f debug-node skeleton]
174 (let [bones
175 (map #(.getBone skeleton %)
176 (range (.getBoneCount skeleton)))]
177 (dorun (map #(.setUserControl % true) bones))
178 (dorun (map (fn [b]
179 (println (.getName b)
180 " -- " (f b)))
181 bones))
183 (dorun
184 (map #(.attachChild
185 debug-node
186 (doto
187 (sphere 0.1
188 :position (f %)
189 :physical? false)
190 (.setMaterial (green-x-ray))))
191 bones)))
192 debug-node)
194 (import jme3test.bullet.PhysicsTestHelper)
196 (defn test-ragdoll []
198 (let [the-worm
199 (body
200 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
201 ;;(worm-blender)
202 (worm)
203 ;;(oto)
204 ;;(sinbad)
205 )
206 ]
209 (.start
210 (world
211 (doto (Node.)
212 (.attachChild the-worm))
213 {"mouse-left" (fire-cannon-ball)}
214 (fn [world]
215 (light-up-everything world)
216 (PhysicsTestHelper/createPhysicsTestWorld
217 (.getRootNode world)
218 (asset-manager)
219 (.getPhysicsSpace
220 (.getState (.getStateManager world) BulletAppState)))
221 ;;(.setTimer world (NanoTimer.))
222 ;;(org.lwjgl.input.Mouse/setGrabbed false)
223 )
224 no-op
225 )
228 )))
239 #+end_src
248 * COMMENT generate Source.
249 #+begin_src clojure :tangle ../src/cortex/body.clj
250 <<body-main>>
251 #+end_src