annotate org/skin.org @ 8:522cf85fdb57

getting there!
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 00:59:22 -0700
parents 9ccfbcbed90a
children 7d0c0282c3a7
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@6 7 #+babel: :mkdirp yes :noweb yes
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@0 18 #+srcname: skin-main
rlm@0 19 #+begin_src clojure
rlm@0 20 (ns body.skin)
rlm@0 21 (use 'cortex.world)
rlm@0 22 (use 'cortex.import)
rlm@0 23 (use 'clojure.contrib.def)
rlm@0 24 (cortex.import/mega-import-jme3)
rlm@0 25 (rlm.rlm-commands/help)
rlm@0 26
rlm@0 27 (import java.util.logging.Level)
rlm@0 28 (import java.util.logging.Logger)
rlm@0 29
rlm@0 30
rlm@0 31
rlm@0 32 ;; looks like we can use GhostControls for implementing touch.
rlm@0 33 ;; for example:
rlm@0 34 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
rlm@0 35 (def shifted-sphere
rlm@0 36 (doto (CompoundCollisionShape.)
rlm@0 37 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
rlm@0 38
rlm@0 39 (def gc (GhostControl. shifted-sphere))
rlm@0 40 (def b (box 1 1 1 :mass 1))
rlm@0 41 (.addControl b rc)
rlm@0 42 (.addControl b gc)
rlm@0 43
rlm@0 44
rlm@0 45 (defn looksies! []
rlm@0 46 (view b))
rlm@0 47
rlm@0 48 ;; overlapping objects can be gotten via =.getOverlappingCount= and
rlm@0 49 ;; =.getOverlappingObjects=
rlm@0 50
rlm@0 51 ;; looks like I might be able to place a small "touch-sphere" whther
rlm@0 52 ;; on every triangle in the object's mesh, or at the verticies, using
rlm@0 53 ;; .getTriangleCount() on the mesh gotten by .getMesh()
rlm@0 54
rlm@0 55 ;; this way, I can create a mesh and just divide up it's faces using
rlm@0 56 ;; blender, and this will create the touch sensor distribution.
rlm@0 57
rlm@0 58
rlm@0 59 (defn make-touch-sphere [#^Geometry geom]
rlm@0 60 (let [tri (Triangle.)
rlm@0 61 mesh (.getMesh geom)
rlm@0 62 controls! (transient [])]
rlm@0 63 (dorun
rlm@0 64 (for [n (range (.getTriangleCount mesh))]
rlm@0 65 (do
rlm@0 66 (.getTriangle mesh n tri)
rlm@0 67 (.calculateCenter tri)
rlm@0 68 (let [control
rlm@0 69 (doto
rlm@0 70 (GhostControl.
rlm@0 71 (doto (CompoundCollisionShape.)
rlm@0 72 (.addChildShape
rlm@0 73 (SphereCollisionShape. (float 0.1))
rlm@0 74 (.mult (.getCenter tri) (float 1)))
rlm@0 75 (.setMargin -0.1)))
rlm@0 76 (.setApplyPhysicsLocal true))]
rlm@0 77
rlm@0 78 (.addControl geom control)
rlm@0 79 (conj! controls! control)))))
rlm@0 80 (persistent! controls!)))
rlm@6 81
rlm@6 82
rlm@6 83 (defn triangles [#^Geometry geom]
rlm@6 84 (let
rlm@6 85 [mesh (.getMesh geom)
rlm@6 86 triangles (transient [])]
rlm@6 87 (dorun
rlm@6 88 (for [n (range (.getTriangleCount mesh))]
rlm@6 89 (let [tri (Triangle.)]
rlm@6 90 (.getTriangle mesh n tri)
rlm@6 91 (.calculateNormal tri)
rlm@6 92 (.calculateCenter tri)
rlm@6 93 (conj! triangles tri))))
rlm@6 94 (persistent! triangles)))
rlm@6 95
rlm@6 96
rlm@6 97 (defn new-touch [#^Geometry geom]
rlm@6 98 (dorun (map
rlm@6 99 (comp no-op #(.getCenter %))
rlm@6 100 (triangles geom))))
rlm@6 101
rlm@7 102 (defn get-ray-origin
rlm@7 103 [geom tri]
rlm@7 104 (let [new (Vector3f.)]
rlm@7 105 (.calculateCenter tri)
rlm@8 106 (.localToWorld geom (.getCenter tri) new)
rlm@7 107 new))
rlm@6 108
rlm@7 109 (defn get-ray-direction
rlm@7 110 [geom tri]
rlm@7 111 (let [new (Vector3f.)]
rlm@7 112 (.calculateNormal tri)
rlm@8 113
rlm@7 114 (.localToWorld geom (.getNormal tri) new)
rlm@8 115 (.subtract new (get-ray-origin geom tri))
rlm@8 116
rlm@8 117 ;;Vector3f/UNIT_Y
rlm@8 118 ))
rlm@7 119
rlm@7 120 (defn ray-origin-debug
rlm@7 121 [ray]
rlm@7 122 (make-shape
rlm@6 123 (assoc base-shape
rlm@6 124 :shape (Sphere. 5 5 0.05)
rlm@6 125 :name "arrow"
rlm@6 126 :color ColorRGBA/Orange
rlm@6 127 :texture false
rlm@6 128 :physical? false
rlm@6 129 :position
rlm@7 130 (.getOrigin ray))))
rlm@6 131
rlm@6 132 (defn ray-debug [ray]
rlm@6 133 (make-shape
rlm@6 134 (assoc
rlm@6 135 base-shape
rlm@6 136 :name "debug-ray"
rlm@6 137 :physical? false
rlm@6 138 :shape (com.jme3.scene.shape.Line.
rlm@6 139 (.getOrigin ray)
rlm@6 140 (.add
rlm@6 141 (.getOrigin ray)
rlm@6 142 (.mult (.getDirection ray)
rlm@6 143 (float (.getLimit ray))))))))
rlm@6 144
rlm@6 145
rlm@6 146
rlm@6 147
rlm@6 148 (defn normal-rays
rlm@6 149 "returns rays"
rlm@6 150 [limit #^Geometry geom]
rlm@6 151 (vec
rlm@6 152 (map
rlm@6 153 (fn [tri]
rlm@6 154 (doto
rlm@6 155 (Ray. (get-ray-origin geom tri)
rlm@6 156 (get-ray-direction geom tri))
rlm@6 157
rlm@6 158 (.setLimit limit)))
rlm@6 159 (triangles geom))))
rlm@6 160
rlm@6 161
rlm@6 162 (defn collision-debug [node result]
rlm@6 163
rlm@6 164 (println-repl "contact point: " (.getContactPoint result))
rlm@6 165
rlm@6 166
rlm@6 167 )
rlm@6 168
rlm@6 169 (defn touch-percieve [limit geom node debug-node]
rlm@6 170 (let [normals (normal-rays limit geom)]
rlm@6 171 (.detachAllChildren debug-node)
rlm@7 172
rlm@6 173
rlm@6 174 (println-repl "---------")
rlm@6 175 (doall
rlm@6 176 (for [ray normals]
rlm@6 177 (do
rlm@6 178 (let [results (CollisionResults.)]
rlm@6 179 (.attachChild debug-node (ray-debug ray))
rlm@7 180 (.attachChild debug-node (ray-origin-debug ray))
rlm@6 181 (.collideWith geom ray results)
rlm@6 182
rlm@6 183 ;;(println-repl (.size results) "results for " ray)
rlm@6 184 ;;(doall (map (partial collision-debug node) results))
rlm@6 185 (.size results)
rlm@6 186 ))))))
rlm@6 187
rlm@0 188 (defn make-touch [#^Geometry geom]
rlm@0 189 (let [tri (Triangle.)
rlm@0 190 mesh (.getMesh geom)
rlm@0 191 controls! (transient [])]
rlm@0 192 (dorun
rlm@0 193 (for [n (range (.getTriangleCount mesh))]
rlm@0 194 (do
rlm@0 195 (.getTriangle mesh n tri)
rlm@0 196 (.calculateCenter tri)
rlm@0 197 (.calculateNormal tri)
rlm@5 198 ;; (println-repl tri)
rlm@5 199 ;; (println-repl (.get1 tri))
rlm@5 200 ;; (println-repl (.get2 tri))
rlm@5 201 ;; (println-repl (.get3 tri))
rlm@5 202 ;; (println-repl (.getCenter tri))
rlm@5 203 ;; (println-repl (.getNormal tri))
rlm@0 204 (let [control
rlm@0 205 (doto
rlm@0 206 (GhostControl.
rlm@0 207
rlm@0 208 (doto (CompoundCollisionShape.)
rlm@0 209 (.addChildShape
rlm@0 210 (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 211 (.mult (.getCenter tri) (float 1)))
rlm@0 212 (.setMargin 0)
rlm@0 213 ))
rlm@0 214 (.setApplyPhysicsLocal true))]
rlm@0 215 (.addControl geom control)
rlm@0 216 (conj! controls! control)))))
rlm@0 217 (persistent! controls!)))
rlm@0 218
rlm@0 219 (use 'hello.brick-wall)
rlm@0 220
rlm@0 221 (defn touch-reception [controls]
rlm@0 222 (let [control
rlm@0 223 (first
rlm@0 224 (filter
rlm@0 225 #(< 2 (.getOverlappingCount %)) controls))]
rlm@0 226 (if (not (nil? control))
rlm@0 227 (println
rlm@0 228 (seq
rlm@0 229 (.getOverlappingObjects control))))))
rlm@0 230
rlm@0 231 (defn touch-print [controls]
rlm@0 232 (println
rlm@0 233 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
rlm@0 234
rlm@0 235 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
rlm@0 236 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
rlm@0 237
rlm@0 238 (defn html-color [html-str]
rlm@0 239 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
rlm@0 240 (map #(float (/ (Integer/parseInt % 16) 255))
rlm@0 241 (map (partial apply str) (partition 2 html-str)))))
rlm@0 242
rlm@0 243 (defn color-touch [controls]
rlm@0 244 (no-exceptions
rlm@0 245 (dorun
rlm@0 246 (map
rlm@0 247 (fn [control]
rlm@0 248 (case (count (.getNonGhostOverlappingObjects control))
rlm@0 249 0 (set-debug-color ColorRGBA/Gray control)
rlm@0 250 1 (set-debug-color ColorRGBA/Blue control)
rlm@0 251 2 (set-debug-color ColorRGBA/Green control)
rlm@0 252 3 (set-debug-color ColorRGBA/Yellow control)
rlm@0 253 4 (set-debug-color ColorRGBA/Orange control)
rlm@0 254 5 (set-debug-color ColorRGBA/Red control)
rlm@0 255 6 (set-debug-color ColorRGBA/Magenta control)
rlm@0 256 7 (set-debug-color ColorRGBA/Pink control)
rlm@0 257 8 (set-debug-color ColorRGBA/White control)))
rlm@0 258 controls))))
rlm@0 259
rlm@0 260 (defn enable-debug [world]
rlm@0 261 (.enableDebug
rlm@0 262 (.getPhysicsSpace
rlm@0 263 (.getState
rlm@0 264 (.getStateManager world)
rlm@0 265 BulletAppState))
rlm@0 266 (asset-manager)))
rlm@0 267
rlm@0 268 (defn transparent-sphere []
rlm@0 269 (doto
rlm@0 270 (make-shape
rlm@0 271 (merge base-shape
rlm@0 272 {:position (Vector3f. 0 2 0)
rlm@0 273 :name "the blob."
rlm@0 274 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 275 :texture "Textures/purpleWisp.png"
rlm@0 276 :physical? true
rlm@0 277 :mass 70
rlm@0 278 :color ColorRGBA/Blue
rlm@0 279 :shape (Sphere. 10 10 1)}))
rlm@0 280 (-> (.getMaterial)
rlm@0 281 (.getAdditionalRenderState)
rlm@0 282 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 283 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 284
rlm@0 285 (defn transparent-box []
rlm@0 286 (doto
rlm@0 287 (make-shape
rlm@0 288 (merge base-shape
rlm@0 289 {:position (Vector3f. 0 2 0)
rlm@0 290 :name "the blob."
rlm@0 291 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 292 :texture "Textures/purpleWisp.png"
rlm@0 293 :physical? true
rlm@0 294 :mass 70
rlm@0 295 :color ColorRGBA/Blue
rlm@0 296 :shape (Box. 1 1 1)}))
rlm@0 297 (-> (.getMaterial)
rlm@0 298 (.getAdditionalRenderState)
rlm@0 299 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 300 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 301
rlm@6 302
rlm@6 303 (defn transparent-floor []
rlm@6 304 (doto
rlm@6 305 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 306 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@6 307 :texture "Textures/redWisp.png")
rlm@6 308 (-> (.getMaterial)
rlm@6 309 (.getAdditionalRenderState)
rlm@6 310 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 311 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 312
rlm@0 313
rlm@0 314 (defn no-logging []
rlm@0 315 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@0 316
rlm@0 317 (defn set-accuracy [world new-accuracy]
rlm@0 318 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
rlm@0 319 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
rlm@0 320
rlm@0 321 (defn test-skin []
rlm@0 322 (let [b
rlm@6 323 ;;(transparent-sphere)
rlm@6 324 (transparent-box)
rlm@6 325 f (transparent-floor)
rlm@6 326 ;;controls
rlm@0 327 ;;(make-touch-sphere b)
rlm@6 328 ;;(make-touch b)
rlm@6 329 debug-node (Node.)
rlm@6 330 node (doto (Node.) (.attachChild b) (.attachChild f)
rlm@6 331 (.attachChild debug-node))
rlm@6 332
rlm@0 333 ]
rlm@0 334
rlm@0 335 (world
rlm@0 336
rlm@6 337 node
rlm@0 338 {"key-return" (fire-cannon-ball)
rlm@0 339 "key-space" (fn [game value]
rlm@6 340 (println-repl (touch-percieve 1 b node debug-node)))
rlm@6 341 }
rlm@6 342 ;;no-op
rlm@0 343 (fn [world]
rlm@6 344 ;; (Capture/SimpleCaptureVideo
rlm@6 345 ;; world
rlm@6 346 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@6 347 ;; (no-logging)
rlm@6 348 (enable-debug world)
rlm@6 349 ;; (set-accuracy world (/ 1 60))
rlm@0 350 )
rlm@0 351
rlm@0 352 (fn [& _]
rlm@6 353 (Thread/sleep 10)
rlm@5 354 ;;(touch-print controls)
rlm@6 355 ;;(color-touch controls)
rlm@0 356 ))))
rlm@0 357
rlm@0 358 #+end_src
rlm@0 359
rlm@3 360 #+results: skin-main
rlm@3 361 : #'body.skin/test-skin
rlm@3 362
rlm@0 363
rlm@0 364
rlm@0 365
rlm@6 366
rlm@0 367
rlm@0 368 * COMMENT code generation
rlm@6 369 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
rlm@0 370 <<skin-main>>
rlm@0 371 #+end_src
rlm@0 372
rlm@0 373
rlm@0 374
rlm@0 375