Mercurial > cortex
view 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 |
line wrap: on
line source
1 #+title: SKIN!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating touch in JMonkeyEngine5 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org7 #+babel: :mkdirp yes :noweb yes9 let's see what checkboxes look like:11 * test [1/2]12 - [ ] item 113 - [X] item 216 * skin!18 #+srcname: skin-main19 #+begin_src clojure20 (ns body.skin)21 (use 'cortex.world)22 (use 'cortex.import)23 (use 'clojure.contrib.def)24 (cortex.import/mega-import-jme3)25 (rlm.rlm-commands/help)27 (import java.util.logging.Level)28 (import java.util.logging.Logger)32 ;; looks like we can use GhostControls for implementing touch.33 ;; for example:34 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))35 (def shifted-sphere36 (doto (CompoundCollisionShape.)37 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))39 (def gc (GhostControl. shifted-sphere))40 (def b (box 1 1 1 :mass 1))41 (.addControl b rc)42 (.addControl b gc)45 (defn looksies! []46 (view b))48 ;; overlapping objects can be gotten via =.getOverlappingCount= and49 ;; =.getOverlappingObjects=51 ;; looks like I might be able to place a small "touch-sphere" whther52 ;; on every triangle in the object's mesh, or at the verticies, using53 ;; .getTriangleCount() on the mesh gotten by .getMesh()55 ;; this way, I can create a mesh and just divide up it's faces using56 ;; blender, and this will create the touch sensor distribution.59 (defn make-touch-sphere [#^Geometry geom]60 (let [tri (Triangle.)61 mesh (.getMesh geom)62 controls! (transient [])]63 (dorun64 (for [n (range (.getTriangleCount mesh))]65 (do66 (.getTriangle mesh n tri)67 (.calculateCenter tri)68 (let [control69 (doto70 (GhostControl.71 (doto (CompoundCollisionShape.)72 (.addChildShape73 (SphereCollisionShape. (float 0.1))74 (.mult (.getCenter tri) (float 1)))75 (.setMargin -0.1)))76 (.setApplyPhysicsLocal true))]78 (.addControl geom control)79 (conj! controls! control)))))80 (persistent! controls!)))83 (defn triangles [#^Geometry geom]84 (let85 [mesh (.getMesh geom)86 triangles (transient [])]87 (dorun88 (for [n (range (.getTriangleCount mesh))]89 (let [tri (Triangle.)]90 (.getTriangle mesh n tri)91 (.calculateNormal tri)92 (.calculateCenter tri)93 (conj! triangles tri))))94 (persistent! triangles)))97 (defn new-touch [#^Geometry geom]98 (dorun (map99 (comp no-op #(.getCenter %))100 (triangles geom))))102 (defn get-ray-origin103 [geom tri]104 (let [new (Vector3f.)]105 (.calculateCenter tri)106 (.localToWorld geom (.getCenter tri) new)107 new))109 (defn get-ray-direction110 [geom tri]111 (let [new (Vector3f.)]112 (.calculateNormal tri)114 (.localToWorld geom (.getNormal tri) new)115 (.subtract new (get-ray-origin geom tri))117 ;;Vector3f/UNIT_Y118 ))120 (defn ray-origin-debug121 [ray]122 (make-shape123 (assoc base-shape124 :shape (Sphere. 5 5 0.05)125 :name "arrow"126 :color ColorRGBA/Orange127 :texture false128 :physical? false129 :position130 (.getOrigin ray))))132 (defn ray-debug [ray]133 (make-shape134 (assoc135 base-shape136 :name "debug-ray"137 :physical? false138 :shape (com.jme3.scene.shape.Line.139 (.getOrigin ray)140 (.add141 (.getOrigin ray)142 (.mult (.getDirection ray)143 (float (.getLimit ray))))))))148 (defn normal-rays149 "returns rays"150 [limit #^Geometry geom]151 (vec152 (map153 (fn [tri]154 (doto155 (Ray. (get-ray-origin geom tri)156 (get-ray-direction geom tri))158 (.setLimit limit)))159 (triangles geom))))162 (defn collision-debug [node result]164 (println-repl "contact point: " (.getContactPoint result))167 )169 (defn touch-percieve [limit geom node debug-node]170 (let [normals (normal-rays limit geom)]171 (.detachAllChildren debug-node)174 (println-repl "---------")175 (doall176 (for [ray normals]177 (do178 (let [results (CollisionResults.)]179 (.attachChild debug-node (ray-debug ray))180 (.attachChild debug-node (ray-origin-debug ray))181 (.collideWith geom ray results)183 ;;(println-repl (.size results) "results for " ray)184 ;;(doall (map (partial collision-debug node) results))185 (.size results)186 ))))))188 (defn make-touch [#^Geometry geom]189 (let [tri (Triangle.)190 mesh (.getMesh geom)191 controls! (transient [])]192 (dorun193 (for [n (range (.getTriangleCount mesh))]194 (do195 (.getTriangle mesh n tri)196 (.calculateCenter tri)197 (.calculateNormal tri)198 ;; (println-repl tri)199 ;; (println-repl (.get1 tri))200 ;; (println-repl (.get2 tri))201 ;; (println-repl (.get3 tri))202 ;; (println-repl (.getCenter tri))203 ;; (println-repl (.getNormal tri))204 (let [control205 (doto206 (GhostControl.208 (doto (CompoundCollisionShape.)209 (.addChildShape210 (SimplexCollisionShape. Vector3f/ZERO)211 (.mult (.getCenter tri) (float 1)))212 (.setMargin 0)213 ))214 (.setApplyPhysicsLocal true))]215 (.addControl geom control)216 (conj! controls! control)))))217 (persistent! controls!)))219 (use 'hello.brick-wall)221 (defn touch-reception [controls]222 (let [control223 (first224 (filter225 #(< 2 (.getOverlappingCount %)) controls))]226 (if (not (nil? control))227 (println228 (seq229 (.getOverlappingObjects control))))))231 (defn touch-print [controls]232 (println233 (map #(count (.getNonGhostOverlappingObjects %)) controls)))235 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]236 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))238 (defn html-color [html-str]239 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))240 (map #(float (/ (Integer/parseInt % 16) 255))241 (map (partial apply str) (partition 2 html-str)))))243 (defn color-touch [controls]244 (no-exceptions245 (dorun246 (map247 (fn [control]248 (case (count (.getNonGhostOverlappingObjects control))249 0 (set-debug-color ColorRGBA/Gray control)250 1 (set-debug-color ColorRGBA/Blue control)251 2 (set-debug-color ColorRGBA/Green control)252 3 (set-debug-color ColorRGBA/Yellow control)253 4 (set-debug-color ColorRGBA/Orange control)254 5 (set-debug-color ColorRGBA/Red control)255 6 (set-debug-color ColorRGBA/Magenta control)256 7 (set-debug-color ColorRGBA/Pink control)257 8 (set-debug-color ColorRGBA/White control)))258 controls))))260 (defn enable-debug [world]261 (.enableDebug262 (.getPhysicsSpace263 (.getState264 (.getStateManager world)265 BulletAppState))266 (asset-manager)))268 (defn transparent-sphere []269 (doto270 (make-shape271 (merge base-shape272 {:position (Vector3f. 0 2 0)273 :name "the blob."274 :material "Common/MatDefs/Misc/Unshaded.j3md"275 :texture "Textures/purpleWisp.png"276 :physical? true277 :mass 70278 :color ColorRGBA/Blue279 :shape (Sphere. 10 10 1)}))280 (-> (.getMaterial)281 (.getAdditionalRenderState)282 (.setBlendMode RenderState$BlendMode/Alpha))283 (.setQueueBucket RenderQueue$Bucket/Transparent)))285 (defn transparent-box []286 (doto287 (make-shape288 (merge base-shape289 {:position (Vector3f. 0 2 0)290 :name "the blob."291 :material "Common/MatDefs/Misc/Unshaded.j3md"292 :texture "Textures/purpleWisp.png"293 :physical? true294 :mass 70295 :color ColorRGBA/Blue296 :shape (Box. 1 1 1)}))297 (-> (.getMaterial)298 (.getAdditionalRenderState)299 (.setBlendMode RenderState$BlendMode/Alpha))300 (.setQueueBucket RenderQueue$Bucket/Transparent)))303 (defn transparent-floor []304 (doto305 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)306 :material "Common/MatDefs/Misc/Unshaded.j3md"307 :texture "Textures/redWisp.png")308 (-> (.getMaterial)309 (.getAdditionalRenderState)310 (.setBlendMode RenderState$BlendMode/Alpha))311 (.setQueueBucket RenderQueue$Bucket/Transparent)))314 (defn no-logging []315 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))317 (defn set-accuracy [world new-accuracy]318 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]319 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))321 (defn test-skin []322 (let [b323 ;;(transparent-sphere)324 (transparent-box)325 f (transparent-floor)326 ;;controls327 ;;(make-touch-sphere b)328 ;;(make-touch b)329 debug-node (Node.)330 node (doto (Node.) (.attachChild b) (.attachChild f)331 (.attachChild debug-node))333 ]335 (world337 node338 {"key-return" (fire-cannon-ball)339 "key-space" (fn [game value]340 (println-repl (touch-percieve 1 b node debug-node)))341 }342 ;;no-op343 (fn [world]344 ;; (Capture/SimpleCaptureVideo345 ;; world346 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))347 ;; (no-logging)348 (enable-debug world)349 ;; (set-accuracy world (/ 1 60))350 )352 (fn [& _]353 (Thread/sleep 10)354 ;;(touch-print controls)355 ;;(color-touch controls)356 ))))358 #+end_src360 #+results: skin-main361 : #'body.skin/test-skin368 * COMMENT code generation369 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes370 <<skin-main>>371 #+end_src