annotate org/skin.org @ 35:5eb5c6f0590b

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