annotate org/skin.org @ 5:93ff2b4e7e6a

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