annotate org/skin.org @ 0:92f8d83b5d0b

initial import: I've made hearing and vision, and am working on touch.
author Robert McIntyre <rlm@mit.edu>
date Sun, 16 Oct 2011 05:12:19 -0700
parents
children 4588025678b0
rev   line source
rlm@0 1 #+title: SKIN!
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@0 4 #+MATHJAX: align:"left" mathml:t path:"../aurellem/src/MathJax/MathJax.js"
rlm@0 5 #+STYLE: <link rel="stylesheet" type="text/css" href="../aurellem/src/css/aurellem.css"/>
rlm@0 6 #+BABEL: :exports both :noweb yes :cache no :mkdirp yes
rlm@0 7 #+INCLUDE: ../aurellem/src/templates/level-0.org
rlm@0 8 #+description: Simulating touch in JMonkeyEngine
rlm@0 9
rlm@0 10
rlm@0 11
rlm@0 12
rlm@0 13 * skin!
rlm@0 14
rlm@0 15 #+srcname: skin-main
rlm@0 16 #+begin_src clojure
rlm@0 17 (ns body.skin)
rlm@0 18 (use 'cortex.world)
rlm@0 19 (use 'cortex.import)
rlm@0 20 (use 'clojure.contrib.def)
rlm@0 21 (cortex.import/mega-import-jme3)
rlm@0 22 (rlm.rlm-commands/help)
rlm@0 23
rlm@0 24 (import java.util.logging.Level)
rlm@0 25 (import java.util.logging.Logger)
rlm@0 26
rlm@0 27
rlm@0 28
rlm@0 29 ;; looks like we can use GhostControls for implementing touch.
rlm@0 30 ;; for example:
rlm@0 31 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
rlm@0 32 (def shifted-sphere
rlm@0 33 (doto (CompoundCollisionShape.)
rlm@0 34 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
rlm@0 35
rlm@0 36 (def gc (GhostControl. shifted-sphere))
rlm@0 37 (def b (box 1 1 1 :mass 1))
rlm@0 38 (.addControl b rc)
rlm@0 39 (.addControl b gc)
rlm@0 40
rlm@0 41
rlm@0 42 (defn looksies! []
rlm@0 43 (view b))
rlm@0 44
rlm@0 45 ;; overlapping objects can be gotten via =.getOverlappingCount= and
rlm@0 46 ;; =.getOverlappingObjects=
rlm@0 47
rlm@0 48 ;; looks like I might be able to place a small "touch-sphere" whther
rlm@0 49 ;; on every triangle in the object's mesh, or at the verticies, using
rlm@0 50 ;; .getTriangleCount() on the mesh gotten by .getMesh()
rlm@0 51
rlm@0 52 ;; this way, I can create a mesh and just divide up it's faces using
rlm@0 53 ;; blender, and this will create the touch sensor distribution.
rlm@0 54
rlm@0 55
rlm@0 56 (defn make-touch-sphere [#^Geometry geom]
rlm@0 57 (let [tri (Triangle.)
rlm@0 58 mesh (.getMesh geom)
rlm@0 59 controls! (transient [])]
rlm@0 60 (dorun
rlm@0 61 (for [n (range (.getTriangleCount mesh))]
rlm@0 62 (do
rlm@0 63 (.getTriangle mesh n tri)
rlm@0 64 (.calculateCenter tri)
rlm@0 65 (let [control
rlm@0 66 (doto
rlm@0 67 (GhostControl.
rlm@0 68 (doto (CompoundCollisionShape.)
rlm@0 69 (.addChildShape
rlm@0 70 (SphereCollisionShape. (float 0.1))
rlm@0 71 (.mult (.getCenter tri) (float 1)))
rlm@0 72 (.setMargin -0.1)))
rlm@0 73 (.setApplyPhysicsLocal true))]
rlm@0 74
rlm@0 75 (.addControl geom control)
rlm@0 76 (conj! controls! control)))))
rlm@0 77 (persistent! controls!)))
rlm@0 78
rlm@0 79
rlm@0 80 (defn make-touch [#^Geometry geom]
rlm@0 81 (let [tri (Triangle.)
rlm@0 82 mesh (.getMesh geom)
rlm@0 83 controls! (transient [])]
rlm@0 84 (dorun
rlm@0 85 (for [n (range (.getTriangleCount mesh))]
rlm@0 86 (do
rlm@0 87 (.getTriangle mesh n tri)
rlm@0 88 (.calculateCenter tri)
rlm@0 89 (.calculateNormal tri)
rlm@0 90 (println-repl tri)
rlm@0 91 (println-repl (.get1 tri))
rlm@0 92 (println-repl (.get2 tri))
rlm@0 93 (println-repl (.get3 tri))
rlm@0 94 (println-repl (.getCenter tri))
rlm@0 95 (println-repl (.getNormal tri))
rlm@0 96 (let [control
rlm@0 97 (doto
rlm@0 98 (GhostControl.
rlm@0 99
rlm@0 100 (doto (CompoundCollisionShape.)
rlm@0 101 (.addChildShape
rlm@0 102 (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 103 (.mult (.getCenter tri) (float 1)))
rlm@0 104 (.setMargin 0)
rlm@0 105 ))
rlm@0 106 (.setApplyPhysicsLocal true))]
rlm@0 107
rlm@0 108 (.addControl geom control)
rlm@0 109 (conj! controls! control)))))
rlm@0 110 (persistent! controls!)))
rlm@0 111
rlm@0 112 (defn make-fucked-touch [#^Geometry geom]
rlm@0 113 (let [tri (Triangle.)
rlm@0 114 mesh (.getMesh geom)
rlm@0 115 controls! (transient [])]
rlm@0 116 (dorun
rlm@0 117 (for [n (range (.getTriangleCount mesh))]
rlm@0 118 (do
rlm@0 119 (.getTriangle mesh n tri)
rlm@0 120 (.calculateCenter tri)
rlm@0 121 (.calculateNormal tri)
rlm@0 122 (println-repl tri)
rlm@0 123 (println-repl (.get1 tri))
rlm@0 124 (println-repl (.get2 tri))
rlm@0 125 (println-repl (.get3 tri))
rlm@0 126 (println-repl (.getCenter tri))
rlm@0 127 (println-repl (.getNormal tri))
rlm@0 128 (let [control1
rlm@0 129 (doto
rlm@0 130 (GhostControl.
rlm@0 131 (doto (CompoundCollisionShape.)
rlm@0 132 (.addChildShape
rlm@0 133 (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 134 (.get1 tri))))
rlm@0 135 (.setApplyPhysicsLocal true))]
rlm@0 136
rlm@0 137 (.addControl geom control1)
rlm@0 138 (conj! controls! control1)
rlm@0 139 )
rlm@0 140
rlm@0 141 ;; (let [control1
rlm@0 142 ;; (doto
rlm@0 143 ;; (GhostControl.
rlm@0 144 ;; (doto (CompoundCollisionShape.)
rlm@0 145 ;; (.addChildShape
rlm@0 146 ;; (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 147 ;; (.get2 tri))))
rlm@0 148 ;; (.setApplyPhysicsLocal true))]
rlm@0 149
rlm@0 150 ;; (.addControl geom control1)
rlm@0 151 ;; (conj! controls! control1)
rlm@0 152 ;; )
rlm@0 153
rlm@0 154 ;; (let [control1
rlm@0 155 ;; (doto
rlm@0 156 ;; (GhostControl.
rlm@0 157 ;; (doto (CompoundCollisionShape.)
rlm@0 158 ;; (.addChildShape
rlm@0 159 ;; (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 160 ;; (.get3 tri))))
rlm@0 161 ;; (.setApplyPhysicsLocal true))]
rlm@0 162
rlm@0 163 ;; (.addControl geom control1)
rlm@0 164 ;; (conj! controls! control1)
rlm@0 165
rlm@0 166 )))
rlm@0 167 (persistent! controls!)))
rlm@0 168
rlm@0 169
rlm@0 170
rlm@0 171 (use 'hello.brick-wall)
rlm@0 172
rlm@0 173 (defn touch-reception [controls]
rlm@0 174 (let [control
rlm@0 175 (first
rlm@0 176 (filter
rlm@0 177 #(< 2 (.getOverlappingCount %)) controls))]
rlm@0 178 (if (not (nil? control))
rlm@0 179 (println
rlm@0 180 (seq
rlm@0 181 (.getOverlappingObjects control))))))
rlm@0 182
rlm@0 183 (defn touch-print [controls]
rlm@0 184 (println
rlm@0 185 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
rlm@0 186
rlm@0 187 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
rlm@0 188 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
rlm@0 189
rlm@0 190 (defn html-color [html-str]
rlm@0 191 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
rlm@0 192 (map #(float (/ (Integer/parseInt % 16) 255))
rlm@0 193 (map (partial apply str) (partition 2 html-str)))))
rlm@0 194
rlm@0 195
rlm@0 196 (defn color-touch [controls]
rlm@0 197 (no-exceptions
rlm@0 198 (dorun
rlm@0 199 (map
rlm@0 200 (fn [control]
rlm@0 201 (case (count (.getNonGhostOverlappingObjects control))
rlm@0 202 0 (set-debug-color ColorRGBA/Gray control)
rlm@0 203 1 (set-debug-color ColorRGBA/Blue control)
rlm@0 204 2 (set-debug-color ColorRGBA/Green control)
rlm@0 205 3 (set-debug-color ColorRGBA/Yellow control)
rlm@0 206 4 (set-debug-color ColorRGBA/Orange control)
rlm@0 207 5 (set-debug-color ColorRGBA/Red control)
rlm@0 208 6 (set-debug-color ColorRGBA/Magenta control)
rlm@0 209 7 (set-debug-color ColorRGBA/Pink control)
rlm@0 210 8 (set-debug-color ColorRGBA/White control)))
rlm@0 211 controls))))
rlm@0 212
rlm@0 213 (defn enable-debug [world]
rlm@0 214 (.enableDebug
rlm@0 215 (.getPhysicsSpace
rlm@0 216 (.getState
rlm@0 217 (.getStateManager world)
rlm@0 218 BulletAppState))
rlm@0 219 (asset-manager)))
rlm@0 220
rlm@0 221 (def with-debug
rlm@0 222 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))
rlm@0 223 (def no-debug
rlm@0 224 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))
rlm@0 225
rlm@0 226
rlm@0 227
rlm@0 228 (defn transparent-sphere []
rlm@0 229 (doto
rlm@0 230 (make-shape
rlm@0 231 (merge base-shape
rlm@0 232 {:position (Vector3f. 0 2 0)
rlm@0 233 :name "the blob."
rlm@0 234 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 235 :texture "Textures/purpleWisp.png"
rlm@0 236 :physical? true
rlm@0 237 :mass 70
rlm@0 238 :color ColorRGBA/Blue
rlm@0 239 :shape (Sphere. 10 10 1)}))
rlm@0 240 (-> (.getMaterial)
rlm@0 241 (.getAdditionalRenderState)
rlm@0 242 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 243 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 244
rlm@0 245 (defn transparent-box []
rlm@0 246 (doto
rlm@0 247 (make-shape
rlm@0 248 (merge base-shape
rlm@0 249 {:position (Vector3f. 0 2 0)
rlm@0 250 :name "the blob."
rlm@0 251 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 252 :texture "Textures/purpleWisp.png"
rlm@0 253 :physical? true
rlm@0 254 :mass 70
rlm@0 255 :color ColorRGBA/Blue
rlm@0 256 :shape (Box. 1 1 1)}))
rlm@0 257 (-> (.getMaterial)
rlm@0 258 (.getAdditionalRenderState)
rlm@0 259 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 260 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 261
rlm@0 262
rlm@0 263
rlm@0 264 (defn no-logging []
rlm@0 265 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@0 266
rlm@0 267 (defn set-accuracy [world new-accuracy]
rlm@0 268 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
rlm@0 269 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
rlm@0 270
rlm@0 271 (defn test-skin []
rlm@0 272 (let [b
rlm@0 273 ;;(transparent-box)
rlm@0 274 (transparent-sphere)
rlm@0 275
rlm@0 276 controls
rlm@0 277 ;;(make-touch-sphere b)
rlm@0 278 (make-touch b)
rlm@0 279 ]
rlm@0 280
rlm@0 281 (world
rlm@0 282 (doto (Node.) (.attachChild b)
rlm@0 283 (.attachChild
rlm@0 284 (doto
rlm@0 285 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@0 286 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 287 :texture "Textures/redWisp.png")
rlm@0 288 (-> (.getMaterial)
rlm@0 289 (.getAdditionalRenderState)
rlm@0 290 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 291 (.setQueueBucket RenderQueue$Bucket/Transparent))))
rlm@0 292
rlm@0 293 {"key-return" (fire-cannon-ball)
rlm@0 294 "key-space" (fn [game value]
rlm@0 295 (touch-print controls))}
rlm@0 296 (fn [world]
rlm@0 297 (Capture/SimpleCaptureVideo world (file-str "~/out-temp/blob.avi"))
rlm@0 298 (no-logging)
rlm@0 299 (enable-debug world)
rlm@0 300 (set-accuracy world (/ 1 60))
rlm@0 301 )
rlm@0 302
rlm@0 303 (fn [& _]
rlm@0 304 (touch-print controls)
rlm@0 305 (color-touch controls)
rlm@0 306 ))))
rlm@0 307
rlm@0 308 #+end_src
rlm@0 309
rlm@0 310
rlm@0 311
rlm@0 312
rlm@0 313
rlm@0 314
rlm@0 315 * COMMENT code generation
rlm@0 316 #+begin_src clojure :tangle ../src/body/skin.clj
rlm@0 317 <<skin-main>>
rlm@0 318 #+end_src
rlm@0 319
rlm@0 320
rlm@0 321
rlm@0 322