annotate org/skin.org @ 4:50c92af2018e

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