Mercurial > cortex
view org/skin.org @ 23:cab2da252494
split off the rest of cortex.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 23:54:26 -0700 |
parents | 67d508a1e34d |
children | 6372c108c5c6 |
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 enable-debug [world]152 (.enableDebug153 (.getPhysicsSpace154 (.getState155 (.getStateManager world)156 BulletAppState))157 (asset-manager)))159 (defn no-logging []160 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))162 (defn set-accuracy [world new-accuracy]163 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]164 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))166 (defn transparent-sphere []167 (doto168 (make-shape169 (merge base-shape170 {:position (Vector3f. 0 2 0)171 :name "the blob."172 :material "Common/MatDefs/Misc/Unshaded.j3md"173 :texture "Textures/purpleWisp.png"174 :physical? true175 :mass 70176 :color ColorRGBA/Blue177 :shape (Sphere. 10 10 1)}))178 (-> (.getMaterial)179 (.getAdditionalRenderState)180 (.setBlendMode RenderState$BlendMode/Alpha))181 (.setQueueBucket RenderQueue$Bucket/Transparent)))183 (defn transparent-box []184 (doto185 (make-shape186 (merge base-shape187 {:position (Vector3f. 0 2 0)188 :name "box"189 :material "Common/MatDefs/Misc/Unshaded.j3md"190 :texture "Textures/purpleWisp.png"191 :physical? true192 :mass 70193 :color ColorRGBA/Blue194 :shape (Box. 1 1 1)}))195 (-> (.getMaterial)196 (.getAdditionalRenderState)197 (.setBlendMode RenderState$BlendMode/Alpha))198 (.setQueueBucket RenderQueue$Bucket/Transparent)))200 (defn transparent-floor []201 (doto202 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)203 :material "Common/MatDefs/Misc/Unshaded.j3md"204 :texture "Textures/redWisp.png"205 :name "floor")206 (-> (.getMaterial)207 (.getAdditionalRenderState)208 (.setBlendMode RenderState$BlendMode/Alpha))209 (.setQueueBucket RenderQueue$Bucket/Transparent)))211 (defn test-skin []212 (let [b213 ;;(transparent-box)214 (transparent-sphere)215 ;;(sphere)216 f (transparent-floor)217 debug-node (Node.)218 node (doto (Node.) (.attachChild b) (.attachChild f))219 root-node (doto (Node.) (.attachChild node)220 (.attachChild debug-node))221 ]223 (world224 root-node225 {"key-return" (fire-cannon-ball node)}226 (fn [world]227 ;; (Capture/SimpleCaptureVideo228 ;; world229 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))230 ;; (no-logging)231 ;;(enable-debug world)232 ;; (set-accuracy world (/ 1 60))233 )235 (fn [& _]236 (let [sensitivity 0.2237 touch-data (touch-percieve sensitivity b node)]238 (manage-ray-debug-node debug-node b touch-data sensitivity)239 )240 (Thread/sleep 10)241 ))))243 #+end_src245 #+results: skin-main246 : #'body.skin/test-skin255 * COMMENT code generation256 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes257 <<skin-main>>258 #+end_src