Mercurial > cortex
view org/skin.org @ 29:6372c108c5c6
cleaned up util.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 24 Oct 2011 12:35:15 -0700 |
parents | 67d508a1e34d |
children | 97703c7f020e |
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)29 (use 'hello.brick-wall)31 (defn triangles [#^Geometry geom]32 (let33 [mesh (.getMesh geom)34 triangles (transient [])]35 (dorun36 (for [n (range (.getTriangleCount mesh))]37 (let [tri (Triangle.)]38 (.getTriangle mesh n tri)39 (.calculateNormal tri)40 (.calculateCenter tri)41 (conj! triangles tri))))42 (persistent! triangles)))44 (defn get-ray-origin45 [geom tri]46 (let [new (Vector3f.)]47 (.calculateCenter tri)48 (.localToWorld geom (.getCenter tri) new)49 new))51 (defn get-ray-direction52 [geom tri]53 (let [n+c (Vector3f.)]54 (.calculateNormal tri)55 (.calculateCenter tri)56 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)57 (.subtract n+c (get-ray-origin geom tri))58 ))60 (defn ray-origin-debug61 [ray color]62 (make-shape63 (assoc base-shape64 :shape (Sphere. 5 5 0.05)65 :name "arrow"66 :color color67 :texture false68 :physical? false69 :position70 (.getOrigin ray))))72 (defn ray-debug [ray color]73 (make-shape74 (assoc75 base-shape76 :name "debug-ray"77 :physical? false78 :shape (com.jme3.scene.shape.Line.79 (.getOrigin ray)80 (.add81 (.getOrigin ray)82 (.mult (.getDirection ray)83 (float (.getLimit ray))))))))86 (defn contact-color [contacts]87 (case contacts88 0 ColorRGBA/Gray89 1 ColorRGBA/Blue90 2 ColorRGBA/Green91 3 ColorRGBA/Yellow92 4 ColorRGBA/Orange93 5 ColorRGBA/Red94 6 ColorRGBA/Magenta95 7 ColorRGBA/Pink96 8 ColorRGBA/White))98 (defn normal-rays99 "returns rays"100 [limit #^Geometry geom]101 (vec102 (map103 (fn [tri]104 (doto105 (Ray. (get-ray-origin geom tri)106 (get-ray-direction geom tri))107 (.setLimit limit)))108 (triangles geom))))110 (defn update-ray-debug [node ray contacts]111 (let [origin (.getChild node 0)]112 (.setLocalTranslation origin (.getOrigin ray))113 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))115 (defn init-node116 [debug-node rays]117 (println-repl "Init touch debug node.")118 (.detachAllChildren debug-node)119 (dorun120 (for [ray rays]121 (do122 (.attachChild123 debug-node124 (doto (Node.)125 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))126 (.attachChild (ray-debug ray ColorRGBA/Gray))127 ))))))129 (defn manage-ray-debug-node [debug-node geom touch-data limit]130 (let [rays (normal-rays limit geom)]131 (if (not= (count (.getChildren debug-node)) (count touch-data))132 (init-node debug-node rays))133 (dorun134 (for [n (range (count touch-data))]135 (update-ray-debug136 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))138 (defn touch-percieve [limit geom node]139 (let [normals (normal-rays limit geom)]141 (doall142 (for [ray normals]143 (do144 (let [results (CollisionResults.)]145 (.collideWith node ray results)146 (let [touch-objects (set (filter #(not (= geom %))147 (map #(.getGeometry %) results)))]148 ;;(dorun (map #(println-repl (.getName %)) touch-objects))149 (count touch-objects))))))))151 (defn no-logging []152 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))154 (defn set-accuracy [world new-accuracy]155 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]156 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))158 (defn transparent-sphere []159 (doto160 (make-shape161 (merge base-shape162 {:position (Vector3f. 0 2 0)163 :name "the blob."164 :material "Common/MatDefs/Misc/Unshaded.j3md"165 :texture "Textures/purpleWisp.png"166 :physical? true167 :mass 70168 :color ColorRGBA/Blue169 :shape (Sphere. 10 10 1)}))170 (-> (.getMaterial)171 (.getAdditionalRenderState)172 (.setBlendMode RenderState$BlendMode/Alpha))173 (.setQueueBucket RenderQueue$Bucket/Transparent)))175 (defn transparent-box []176 (doto177 (make-shape178 (merge base-shape179 {:position (Vector3f. 0 2 0)180 :name "box"181 :material "Common/MatDefs/Misc/Unshaded.j3md"182 :texture "Textures/purpleWisp.png"183 :physical? true184 :mass 70185 :color ColorRGBA/Blue186 :shape (Box. 1 1 1)}))187 (-> (.getMaterial)188 (.getAdditionalRenderState)189 (.setBlendMode RenderState$BlendMode/Alpha))190 (.setQueueBucket RenderQueue$Bucket/Transparent)))192 (defn transparent-floor []193 (doto194 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)195 :material "Common/MatDefs/Misc/Unshaded.j3md"196 :texture "Textures/redWisp.png"197 :name "floor")198 (-> (.getMaterial)199 (.getAdditionalRenderState)200 (.setBlendMode RenderState$BlendMode/Alpha))201 (.setQueueBucket RenderQueue$Bucket/Transparent)))203 (defn test-skin []204 (let [b205 ;;(transparent-box)206 (transparent-sphere)207 ;;(sphere)208 f (transparent-floor)209 debug-node (Node.)210 node (doto (Node.) (.attachChild b) (.attachChild f))211 root-node (doto (Node.) (.attachChild node)212 (.attachChild debug-node))213 ]215 (world216 root-node217 {"key-return" (fire-cannon-ball node)}218 (fn [world]219 ;; (Capture/SimpleCaptureVideo220 ;; world221 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))222 ;; (no-logging)223 ;;(enable-debug world)224 ;; (set-accuracy world (/ 1 60))225 )227 (fn [& _]228 (let [sensitivity 0.2229 touch-data (touch-percieve sensitivity b node)]230 (manage-ray-debug-node debug-node b touch-data sensitivity)231 )232 (Thread/sleep 10)233 ))))235 #+end_src237 #+results: skin-main238 : #'body.skin/test-skin247 * COMMENT code generation248 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes249 <<skin-main>>250 #+end_src