annotate 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
rev   line source
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@50 59 "Models/anim2/worm3.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@52 122
rlm@52 123
rlm@52 124
rlm@50 125 (.addControl node krc)
rlm@52 126 (.setRagdollMode krc)
rlm@52 127 )
rlm@50 128 node
rlm@50 129 )
rlm@50 130
rlm@52 131 (defn green-x-ray []
rlm@51 132 (doto (Material. (asset-manager)
rlm@51 133 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@51 134 (.setColor "Color" ColorRGBA/Green)
rlm@52 135 (-> (.getAdditionalRenderState)
rlm@52 136 (.setDepthTest false))))
rlm@51 137
rlm@53 138 (defn show-skeleton [node]
rlm@50 139 (let [sd
rlm@51 140
rlm@50 141 (doto
rlm@51 142 (SkeletonDebugger. "aurellem-skel-debug"
rlm@51 143 (skel node))
rlm@51 144 (.setMaterial (green-x-ray)))]
rlm@53 145 (.attachChild node sd)
rlm@53 146 node))
rlm@50 147
rlm@51 148
rlm@50 149
rlm@50 150 (defn init-debug-skel-node
rlm@50 151 [f debug-node skeleton]
rlm@50 152 (let [bones
rlm@50 153 (map #(.getBone skeleton %)
rlm@50 154 (range (.getBoneCount skeleton)))]
rlm@50 155 (dorun (map #(.setUserControl % true) bones))
rlm@50 156 (dorun (map (fn [b]
rlm@50 157 (println (.getName b)
rlm@50 158 " -- " (f b)))
rlm@50 159 bones))
rlm@51 160
rlm@50 161 (dorun
rlm@50 162 (map #(.attachChild
rlm@50 163 debug-node
rlm@51 164 (doto
rlm@51 165 (sphere 0.1
rlm@51 166 :position (f %)
rlm@51 167 :physical? false)
rlm@51 168 (.setMaterial (green-x-ray))))
rlm@50 169 bones)))
rlm@50 170 debug-node)
rlm@50 171
rlm@52 172 (import jme3test.bullet.PhysicsTestHelper)
rlm@50 173
rlm@53 174
rlm@53 175 (defn test-zzz [the-worm world value]
rlm@53 176 (if (not value)
rlm@53 177 (let [skeleton (skel the-worm)]
rlm@53 178 (println-repl "enabling bones")
rlm@53 179 (dorun
rlm@53 180 (map
rlm@53 181 #(.setUserControl (.getBone skeleton %) true)
rlm@53 182 (range (.getBoneCount skeleton))))
rlm@53 183
rlm@53 184
rlm@53 185 (let [b (.getBone skeleton 6)]
rlm@53 186 (println-repl "moving " (.getName b))
rlm@53 187 (println-repl (.getLocalPosition b))
rlm@53 188 (.setUserTransforms b
rlm@53 189 (.add (.getLocalPosition b) Vector3f/UNIT_X)
rlm@53 190 Quaternion/IDENTITY
rlm@53 191 (Vector3f. 1 1 1))
rlm@53 192
rlm@53 193 )
rlm@53 194
rlm@53 195
rlm@53 196
rlm@53 197 (println-repl "hi! <3"))))
rlm@53 198
rlm@53 199
rlm@50 200 (defn test-ragdoll []
rlm@50 201
rlm@50 202 (let [the-worm
rlm@53 203
rlm@52 204 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
rlm@53 205 (doto (show-skeleton (worm-blender))
rlm@53 206 (.setLocalTranslation (Vector3f. 0 10 0))
rlm@53 207 ;;(worm)
rlm@52 208 ;;(oto)
rlm@52 209 ;;(sinbad)
rlm@50 210 )
rlm@50 211 ]
rlm@50 212
rlm@50 213
rlm@50 214 (.start
rlm@50 215 (world
rlm@50 216 (doto (Node.)
rlm@50 217 (.attachChild the-worm))
rlm@53 218 {"key-return" (fire-cannon-ball)
rlm@53 219 "key-space" (partial test-zzz the-worm)
rlm@53 220 }
rlm@50 221 (fn [world]
rlm@50 222 (light-up-everything world)
rlm@52 223 (PhysicsTestHelper/createPhysicsTestWorld
rlm@52 224 (.getRootNode world)
rlm@52 225 (asset-manager)
rlm@52 226 (.getPhysicsSpace
rlm@52 227 (.getState (.getStateManager world) BulletAppState)))
rlm@53 228 (set-gravity world Vector3f/ZERO)
rlm@50 229 ;;(.setTimer world (NanoTimer.))
rlm@50 230 ;;(org.lwjgl.input.Mouse/setGrabbed false)
rlm@50 231 )
rlm@50 232 no-op
rlm@50 233 )
rlm@50 234
rlm@50 235
rlm@50 236 )))
rlm@50 237
rlm@50 238
rlm@50 239
rlm@49 240
rlm@50 241
rlm@50 242
rlm@50 243
rlm@49 244
rlm@49 245
rlm@49 246
rlm@0 247 #+end_src
rlm@0 248
rlm@0 249
rlm@0 250
rlm@0 251
rlm@0 252
rlm@0 253
rlm@0 254
rlm@0 255
rlm@0 256 * COMMENT generate Source.
rlm@44 257 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@0 258 <<body-main>>
rlm@0 259 #+end_src
rlm@0 260