annotate org/skin.org @ 20:67d508a1e34d

going to update website/documentation
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 12:32:56 -0700
parents 5a371057078f
children 6372c108c5c6
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@11 29 (use 'hello.brick-wall)
rlm@0 30
rlm@6 31 (defn triangles [#^Geometry geom]
rlm@6 32 (let
rlm@6 33 [mesh (.getMesh geom)
rlm@6 34 triangles (transient [])]
rlm@6 35 (dorun
rlm@6 36 (for [n (range (.getTriangleCount mesh))]
rlm@6 37 (let [tri (Triangle.)]
rlm@6 38 (.getTriangle mesh n tri)
rlm@6 39 (.calculateNormal tri)
rlm@6 40 (.calculateCenter tri)
rlm@6 41 (conj! triangles tri))))
rlm@6 42 (persistent! triangles)))
rlm@6 43
rlm@7 44 (defn get-ray-origin
rlm@7 45 [geom tri]
rlm@7 46 (let [new (Vector3f.)]
rlm@7 47 (.calculateCenter tri)
rlm@8 48 (.localToWorld geom (.getCenter tri) new)
rlm@7 49 new))
rlm@6 50
rlm@7 51 (defn get-ray-direction
rlm@7 52 [geom tri]
rlm@9 53 (let [n+c (Vector3f.)]
rlm@7 54 (.calculateNormal tri)
rlm@9 55 (.calculateCenter tri)
rlm@9 56 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@9 57 (.subtract n+c (get-ray-origin geom tri))
rlm@8 58 ))
rlm@7 59
rlm@7 60 (defn ray-origin-debug
rlm@9 61 [ray color]
rlm@7 62 (make-shape
rlm@20 63 (assoc base-shape
rlm@20 64 :shape (Sphere. 5 5 0.05)
rlm@20 65 :name "arrow"
rlm@20 66 :color color
rlm@20 67 :texture false
rlm@20 68 :physical? false
rlm@20 69 :position
rlm@20 70 (.getOrigin ray))))
rlm@6 71
rlm@9 72 (defn ray-debug [ray color]
rlm@6 73 (make-shape
rlm@6 74 (assoc
rlm@6 75 base-shape
rlm@6 76 :name "debug-ray"
rlm@6 77 :physical? false
rlm@6 78 :shape (com.jme3.scene.shape.Line.
rlm@6 79 (.getOrigin ray)
rlm@6 80 (.add
rlm@6 81 (.getOrigin ray)
rlm@6 82 (.mult (.getDirection ray)
rlm@6 83 (float (.getLimit ray))))))))
rlm@6 84
rlm@6 85
rlm@10 86 (defn contact-color [contacts]
rlm@10 87 (case contacts
rlm@10 88 0 ColorRGBA/Gray
rlm@10 89 1 ColorRGBA/Blue
rlm@10 90 2 ColorRGBA/Green
rlm@10 91 3 ColorRGBA/Yellow
rlm@10 92 4 ColorRGBA/Orange
rlm@10 93 5 ColorRGBA/Red
rlm@10 94 6 ColorRGBA/Magenta
rlm@10 95 7 ColorRGBA/Pink
rlm@10 96 8 ColorRGBA/White))
rlm@6 97
rlm@6 98 (defn normal-rays
rlm@6 99 "returns rays"
rlm@6 100 [limit #^Geometry geom]
rlm@6 101 (vec
rlm@6 102 (map
rlm@6 103 (fn [tri]
rlm@6 104 (doto
rlm@6 105 (Ray. (get-ray-origin geom tri)
rlm@6 106 (get-ray-direction geom tri))
rlm@12 107 (.setLimit limit)))
rlm@6 108 (triangles geom))))
rlm@6 109
rlm@14 110 (defn update-ray-debug [node ray contacts]
rlm@14 111 (let [origin (.getChild node 0)]
rlm@14 112 (.setLocalTranslation origin (.getOrigin ray))
rlm@14 113 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
rlm@14 114
rlm@13 115 (defn init-node
rlm@13 116 [debug-node rays]
rlm@13 117 (println-repl "Init touch debug node.")
rlm@12 118 (.detachAllChildren debug-node)
rlm@13 119 (dorun
rlm@13 120 (for [ray rays]
rlm@13 121 (do
rlm@13 122 (.attachChild
rlm@13 123 debug-node
rlm@13 124 (doto (Node.)
rlm@14 125 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
rlm@20 126 (.attachChild (ray-debug ray ColorRGBA/Gray))
rlm@14 127 ))))))
rlm@14 128
rlm@13 129 (defn manage-ray-debug-node [debug-node geom touch-data limit]
rlm@13 130 (let [rays (normal-rays limit geom)]
rlm@13 131 (if (not= (count (.getChildren debug-node)) (count touch-data))
rlm@13 132 (init-node debug-node rays))
rlm@13 133 (dorun
rlm@13 134 (for [n (range (count touch-data))]
rlm@14 135 (update-ray-debug
rlm@14 136 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
rlm@12 137
rlm@13 138 (defn touch-percieve [limit geom node]
rlm@6 139 (let [normals (normal-rays limit geom)]
rlm@12 140
rlm@6 141 (doall
rlm@6 142 (for [ray normals]
rlm@6 143 (do
rlm@6 144 (let [results (CollisionResults.)]
rlm@10 145 (.collideWith node ray results)
rlm@19 146 (let [touch-objects (set (filter #(not (= geom %))
rlm@19 147 (map #(.getGeometry %) results)))]
rlm@19 148 ;;(dorun (map #(println-repl (.getName %)) touch-objects))
rlm@19 149 (count touch-objects))))))))
rlm@6 150
rlm@0 151 (defn enable-debug [world]
rlm@0 152 (.enableDebug
rlm@0 153 (.getPhysicsSpace
rlm@0 154 (.getState
rlm@0 155 (.getStateManager world)
rlm@0 156 BulletAppState))
rlm@0 157 (asset-manager)))
rlm@0 158
rlm@11 159 (defn no-logging []
rlm@11 160 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@11 161
rlm@11 162 (defn set-accuracy [world new-accuracy]
rlm@11 163 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
rlm@11 164 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
rlm@11 165
rlm@0 166 (defn transparent-sphere []
rlm@0 167 (doto
rlm@0 168 (make-shape
rlm@0 169 (merge base-shape
rlm@0 170 {:position (Vector3f. 0 2 0)
rlm@0 171 :name "the blob."
rlm@0 172 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 173 :texture "Textures/purpleWisp.png"
rlm@0 174 :physical? true
rlm@0 175 :mass 70
rlm@0 176 :color ColorRGBA/Blue
rlm@0 177 :shape (Sphere. 10 10 1)}))
rlm@0 178 (-> (.getMaterial)
rlm@0 179 (.getAdditionalRenderState)
rlm@0 180 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 181 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 182
rlm@0 183 (defn transparent-box []
rlm@0 184 (doto
rlm@0 185 (make-shape
rlm@0 186 (merge base-shape
rlm@0 187 {:position (Vector3f. 0 2 0)
rlm@10 188 :name "box"
rlm@0 189 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 190 :texture "Textures/purpleWisp.png"
rlm@0 191 :physical? true
rlm@0 192 :mass 70
rlm@0 193 :color ColorRGBA/Blue
rlm@0 194 :shape (Box. 1 1 1)}))
rlm@0 195 (-> (.getMaterial)
rlm@0 196 (.getAdditionalRenderState)
rlm@0 197 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 198 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 199
rlm@6 200 (defn transparent-floor []
rlm@6 201 (doto
rlm@6 202 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 203 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@10 204 :texture "Textures/redWisp.png"
rlm@10 205 :name "floor")
rlm@6 206 (-> (.getMaterial)
rlm@6 207 (.getAdditionalRenderState)
rlm@6 208 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 209 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 210
rlm@0 211 (defn test-skin []
rlm@0 212 (let [b
rlm@18 213 ;;(transparent-box)
rlm@18 214 (transparent-sphere)
rlm@10 215 ;;(sphere)
rlm@6 216 f (transparent-floor)
rlm@6 217 debug-node (Node.)
rlm@12 218 node (doto (Node.) (.attachChild b) (.attachChild f))
rlm@12 219 root-node (doto (Node.) (.attachChild node)
rlm@12 220 (.attachChild debug-node))
rlm@12 221 ]
rlm@0 222
rlm@0 223 (world
rlm@12 224 root-node
rlm@15 225 {"key-return" (fire-cannon-ball node)}
rlm@0 226 (fn [world]
rlm@20 227 ;; (Capture/SimpleCaptureVideo
rlm@20 228 ;; world
rlm@20 229 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@20 230 ;; (no-logging)
rlm@20 231 ;;(enable-debug world)
rlm@20 232 ;; (set-accuracy world (/ 1 60))
rlm@11 233 )
rlm@0 234
rlm@0 235 (fn [& _]
rlm@19 236 (let [sensitivity 0.2
rlm@18 237 touch-data (touch-percieve sensitivity b node)]
rlm@18 238 (manage-ray-debug-node debug-node b touch-data sensitivity)
rlm@18 239 )
rlm@11 240 (Thread/sleep 10)
rlm@0 241 ))))
rlm@0 242
rlm@0 243 #+end_src
rlm@0 244
rlm@3 245 #+results: skin-main
rlm@3 246 : #'body.skin/test-skin
rlm@3 247
rlm@0 248
rlm@0 249
rlm@0 250
rlm@10 251
rlm@10 252
rlm@6 253
rlm@0 254
rlm@0 255 * COMMENT code generation
rlm@6 256 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
rlm@0 257 <<skin-main>>
rlm@0 258 #+end_src
rlm@0 259
rlm@0 260
rlm@0 261
rlm@0 262