annotate org/skin.org @ 9:7d0c0282c3a7

got positioning right, but there is still a problem
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 01:27:20 -0700
parents 522cf85fdb57
children 0251b7e7609f
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@9 111 (let [n+c (Vector3f.)]
rlm@7 112 (.calculateNormal tri)
rlm@9 113 (.calculateCenter tri)
rlm@9 114 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@9 115 (.subtract n+c (get-ray-origin geom tri))
rlm@8 116 ))
rlm@7 117
rlm@7 118 (defn ray-origin-debug
rlm@9 119 [ray color]
rlm@7 120 (make-shape
rlm@6 121 (assoc base-shape
rlm@6 122 :shape (Sphere. 5 5 0.05)
rlm@6 123 :name "arrow"
rlm@9 124 :color color
rlm@6 125 :texture false
rlm@6 126 :physical? false
rlm@6 127 :position
rlm@7 128 (.getOrigin ray))))
rlm@6 129
rlm@9 130 (defn ray-debug [ray color]
rlm@6 131 (make-shape
rlm@6 132 (assoc
rlm@6 133 base-shape
rlm@6 134 :name "debug-ray"
rlm@6 135 :physical? false
rlm@6 136 :shape (com.jme3.scene.shape.Line.
rlm@6 137 (.getOrigin ray)
rlm@6 138 (.add
rlm@6 139 (.getOrigin ray)
rlm@6 140 (.mult (.getDirection ray)
rlm@6 141 (float (.getLimit ray))))))))
rlm@6 142
rlm@6 143
rlm@6 144
rlm@6 145
rlm@6 146 (defn normal-rays
rlm@6 147 "returns rays"
rlm@6 148 [limit #^Geometry geom]
rlm@6 149 (vec
rlm@6 150 (map
rlm@6 151 (fn [tri]
rlm@6 152 (doto
rlm@6 153 (Ray. (get-ray-origin geom tri)
rlm@6 154 (get-ray-direction geom tri))
rlm@6 155
rlm@6 156 (.setLimit limit)))
rlm@6 157 (triangles geom))))
rlm@6 158
rlm@6 159
rlm@6 160 (defn collision-debug [node result]
rlm@6 161 (println-repl "contact point: " (.getContactPoint result))
rlm@6 162 )
rlm@6 163
rlm@6 164 (defn touch-percieve [limit geom node debug-node]
rlm@6 165 (let [normals (normal-rays limit geom)]
rlm@6 166 (.detachAllChildren debug-node)
rlm@6 167 (doall
rlm@6 168 (for [ray normals]
rlm@6 169 (do
rlm@6 170 (let [results (CollisionResults.)]
rlm@6 171 (.collideWith geom ray results)
rlm@9 172 (let [color (contact-color (.size results))]
rlm@9 173
rlm@9 174 (.attachChild debug-node (ray-debug ray color))
rlm@9 175 (.attachChild debug-node (ray-origin-debug ray color))
rlm@9 176 )
rlm@6 177
rlm@9 178 (println-repl (.size results) "results for " ray)
rlm@9 179 (doall (map (partial collision-debug node) results))
rlm@6 180 (.size results)
rlm@6 181 ))))))
rlm@6 182
rlm@0 183 (defn make-touch [#^Geometry geom]
rlm@0 184 (let [tri (Triangle.)
rlm@0 185 mesh (.getMesh geom)
rlm@0 186 controls! (transient [])]
rlm@0 187 (dorun
rlm@0 188 (for [n (range (.getTriangleCount mesh))]
rlm@0 189 (do
rlm@0 190 (.getTriangle mesh n tri)
rlm@0 191 (.calculateCenter tri)
rlm@0 192 (.calculateNormal tri)
rlm@5 193 ;; (println-repl tri)
rlm@5 194 ;; (println-repl (.get1 tri))
rlm@5 195 ;; (println-repl (.get2 tri))
rlm@5 196 ;; (println-repl (.get3 tri))
rlm@5 197 ;; (println-repl (.getCenter tri))
rlm@5 198 ;; (println-repl (.getNormal tri))
rlm@0 199 (let [control
rlm@0 200 (doto
rlm@0 201 (GhostControl.
rlm@0 202
rlm@0 203 (doto (CompoundCollisionShape.)
rlm@0 204 (.addChildShape
rlm@0 205 (SimplexCollisionShape. Vector3f/ZERO)
rlm@0 206 (.mult (.getCenter tri) (float 1)))
rlm@0 207 (.setMargin 0)
rlm@0 208 ))
rlm@0 209 (.setApplyPhysicsLocal true))]
rlm@0 210 (.addControl geom control)
rlm@0 211 (conj! controls! control)))))
rlm@0 212 (persistent! controls!)))
rlm@0 213
rlm@0 214 (use 'hello.brick-wall)
rlm@0 215
rlm@0 216 (defn touch-reception [controls]
rlm@0 217 (let [control
rlm@0 218 (first
rlm@0 219 (filter
rlm@0 220 #(< 2 (.getOverlappingCount %)) controls))]
rlm@0 221 (if (not (nil? control))
rlm@0 222 (println
rlm@0 223 (seq
rlm@0 224 (.getOverlappingObjects control))))))
rlm@0 225
rlm@0 226 (defn touch-print [controls]
rlm@0 227 (println
rlm@0 228 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
rlm@0 229
rlm@0 230 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
rlm@0 231 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
rlm@0 232
rlm@0 233 (defn html-color [html-str]
rlm@0 234 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
rlm@0 235 (map #(float (/ (Integer/parseInt % 16) 255))
rlm@0 236 (map (partial apply str) (partition 2 html-str)))))
rlm@0 237
rlm@0 238 (defn color-touch [controls]
rlm@0 239 (no-exceptions
rlm@0 240 (dorun
rlm@0 241 (map
rlm@0 242 (fn [control]
rlm@0 243 (case (count (.getNonGhostOverlappingObjects control))
rlm@0 244 0 (set-debug-color ColorRGBA/Gray control)
rlm@0 245 1 (set-debug-color ColorRGBA/Blue control)
rlm@0 246 2 (set-debug-color ColorRGBA/Green control)
rlm@0 247 3 (set-debug-color ColorRGBA/Yellow control)
rlm@0 248 4 (set-debug-color ColorRGBA/Orange control)
rlm@0 249 5 (set-debug-color ColorRGBA/Red control)
rlm@0 250 6 (set-debug-color ColorRGBA/Magenta control)
rlm@0 251 7 (set-debug-color ColorRGBA/Pink control)
rlm@0 252 8 (set-debug-color ColorRGBA/White control)))
rlm@0 253 controls))))
rlm@0 254
rlm@0 255 (defn enable-debug [world]
rlm@0 256 (.enableDebug
rlm@0 257 (.getPhysicsSpace
rlm@0 258 (.getState
rlm@0 259 (.getStateManager world)
rlm@0 260 BulletAppState))
rlm@0 261 (asset-manager)))
rlm@0 262
rlm@0 263 (defn transparent-sphere []
rlm@0 264 (doto
rlm@0 265 (make-shape
rlm@0 266 (merge base-shape
rlm@0 267 {:position (Vector3f. 0 2 0)
rlm@0 268 :name "the blob."
rlm@0 269 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 270 :texture "Textures/purpleWisp.png"
rlm@0 271 :physical? true
rlm@0 272 :mass 70
rlm@0 273 :color ColorRGBA/Blue
rlm@0 274 :shape (Sphere. 10 10 1)}))
rlm@0 275 (-> (.getMaterial)
rlm@0 276 (.getAdditionalRenderState)
rlm@0 277 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 278 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 279
rlm@0 280 (defn transparent-box []
rlm@0 281 (doto
rlm@0 282 (make-shape
rlm@0 283 (merge base-shape
rlm@0 284 {:position (Vector3f. 0 2 0)
rlm@0 285 :name "the blob."
rlm@0 286 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 287 :texture "Textures/purpleWisp.png"
rlm@0 288 :physical? true
rlm@0 289 :mass 70
rlm@0 290 :color ColorRGBA/Blue
rlm@0 291 :shape (Box. 1 1 1)}))
rlm@0 292 (-> (.getMaterial)
rlm@0 293 (.getAdditionalRenderState)
rlm@0 294 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 295 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 296
rlm@6 297
rlm@6 298 (defn transparent-floor []
rlm@6 299 (doto
rlm@6 300 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 301 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@6 302 :texture "Textures/redWisp.png")
rlm@6 303 (-> (.getMaterial)
rlm@6 304 (.getAdditionalRenderState)
rlm@6 305 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 306 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 307
rlm@0 308
rlm@0 309 (defn no-logging []
rlm@0 310 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@0 311
rlm@0 312 (defn set-accuracy [world new-accuracy]
rlm@0 313 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
rlm@0 314 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
rlm@0 315
rlm@0 316 (defn test-skin []
rlm@0 317 (let [b
rlm@6 318 ;;(transparent-sphere)
rlm@6 319 (transparent-box)
rlm@6 320 f (transparent-floor)
rlm@6 321 ;;controls
rlm@0 322 ;;(make-touch-sphere b)
rlm@6 323 ;;(make-touch b)
rlm@6 324 debug-node (Node.)
rlm@6 325 node (doto (Node.) (.attachChild b) (.attachChild f)
rlm@6 326 (.attachChild debug-node))
rlm@6 327
rlm@0 328 ]
rlm@0 329
rlm@0 330 (world
rlm@0 331
rlm@6 332 node
rlm@0 333 {"key-return" (fire-cannon-ball)
rlm@0 334 "key-space" (fn [game value]
rlm@6 335 (println-repl (touch-percieve 1 b node debug-node)))
rlm@6 336 }
rlm@6 337 ;;no-op
rlm@0 338 (fn [world]
rlm@6 339 ;; (Capture/SimpleCaptureVideo
rlm@6 340 ;; world
rlm@6 341 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@6 342 ;; (no-logging)
rlm@6 343 (enable-debug world)
rlm@6 344 ;; (set-accuracy world (/ 1 60))
rlm@0 345 )
rlm@0 346
rlm@0 347 (fn [& _]
rlm@6 348 (Thread/sleep 10)
rlm@5 349 ;;(touch-print controls)
rlm@6 350 ;;(color-touch controls)
rlm@0 351 ))))
rlm@0 352
rlm@0 353 #+end_src
rlm@0 354
rlm@3 355 #+results: skin-main
rlm@3 356 : #'body.skin/test-skin
rlm@3 357
rlm@0 358
rlm@0 359
rlm@0 360
rlm@6 361
rlm@0 362
rlm@0 363 * COMMENT code generation
rlm@6 364 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
rlm@0 365 <<skin-main>>
rlm@0 366 #+end_src
rlm@0 367
rlm@0 368
rlm@0 369
rlm@0 370