Mercurial > cortex
view org/skin.org @ 19:5a371057078f
removed old cruft
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 12:09:29 -0700 |
parents | 221052dac374 |
children | 67d508a1e34d |
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)33 (defn triangles [#^Geometry geom]34 (let35 [mesh (.getMesh geom)36 triangles (transient [])]37 (dorun38 (for [n (range (.getTriangleCount mesh))]39 (let [tri (Triangle.)]40 (.getTriangle mesh n tri)41 (.calculateNormal tri)42 (.calculateCenter tri)43 (conj! triangles tri))))44 (persistent! triangles)))47 (defn get-ray-origin48 [geom tri]49 (let [new (Vector3f.)]50 (.calculateCenter tri)51 (.localToWorld geom (.getCenter tri) new)52 new))54 (defn get-ray-direction55 [geom tri]56 (let [n+c (Vector3f.)]57 (.calculateNormal tri)58 (.calculateCenter tri)59 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)60 (.subtract n+c (get-ray-origin geom tri))61 ))63 (defn ray-origin-debug64 [ray color]65 (make-shape66 (assoc base-shape67 :shape (Sphere. 5 5 0.05)68 :name "arrow"69 :color color70 :texture false71 :physical? false72 :position73 (.getOrigin ray))))75 (defn ray-debug [ray color]76 (make-shape77 (assoc78 base-shape79 :name "debug-ray"80 :physical? false81 :shape (com.jme3.scene.shape.Line.82 (.getOrigin ray)83 (.add84 (.getOrigin ray)85 (.mult (.getDirection ray)86 (float (.getLimit ray))))))))89 (defn contact-color [contacts]90 (case contacts91 0 ColorRGBA/Gray92 1 ColorRGBA/Blue93 2 ColorRGBA/Green94 3 ColorRGBA/Yellow95 4 ColorRGBA/Orange96 5 ColorRGBA/Red97 6 ColorRGBA/Magenta98 7 ColorRGBA/Pink99 8 ColorRGBA/White))101 (defn normal-rays102 "returns rays"103 [limit #^Geometry geom]104 (vec105 (map106 (fn [tri]107 (doto108 (Ray. (get-ray-origin geom tri)109 (get-ray-direction geom tri))110 (.setLimit limit)))111 (triangles geom))))113 (defn update-ray-debug [node ray contacts]114 (let [origin (.getChild node 0)]115 (.setLocalTranslation origin (.getOrigin ray))116 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))118 (defn init-node119 [debug-node rays]120 (println-repl "Init touch debug node.")121 (.detachAllChildren debug-node)122 (dorun123 (for [ray rays]124 (do125 (.attachChild126 debug-node127 (doto (Node.)128 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))129 ;;(.attachChild (ray-debug ray ColorRGBA/Gray))130 ))))))132 (defn manage-ray-debug-node [debug-node geom touch-data limit]133 (let [rays (normal-rays limit geom)]134 (if (not= (count (.getChildren debug-node)) (count touch-data))135 (init-node debug-node rays))136 (dorun137 (for [n (range (count touch-data))]138 (update-ray-debug139 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))141 (defn touch-percieve [limit geom node]142 (let [normals (normal-rays limit geom)]144 (doall145 (for [ray normals]146 (do147 (let [results (CollisionResults.)]148 (.collideWith node ray results)149 (let [touch-objects (set (filter #(not (= geom %))150 (map #(.getGeometry %) results)))]151 ;;(dorun (map #(println-repl (.getName %)) touch-objects))152 (count touch-objects))))))))154 (defn enable-debug [world]155 (.enableDebug156 (.getPhysicsSpace157 (.getState158 (.getStateManager world)159 BulletAppState))160 (asset-manager)))162 (defn no-logging []163 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))165 (defn set-accuracy [world new-accuracy]166 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]167 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))170 (defn transparent-sphere []171 (doto172 (make-shape173 (merge base-shape174 {:position (Vector3f. 0 2 0)175 :name "the blob."176 :material "Common/MatDefs/Misc/Unshaded.j3md"177 :texture "Textures/purpleWisp.png"178 :physical? true179 :mass 70180 :color ColorRGBA/Blue181 :shape (Sphere. 10 10 1)}))182 (-> (.getMaterial)183 (.getAdditionalRenderState)184 (.setBlendMode RenderState$BlendMode/Alpha))185 (.setQueueBucket RenderQueue$Bucket/Transparent)))187 (defn transparent-box []188 (doto189 (make-shape190 (merge base-shape191 {:position (Vector3f. 0 2 0)192 :name "box"193 :material "Common/MatDefs/Misc/Unshaded.j3md"194 :texture "Textures/purpleWisp.png"195 :physical? true196 :mass 70197 :color ColorRGBA/Blue198 :shape (Box. 1 1 1)}))199 (-> (.getMaterial)200 (.getAdditionalRenderState)201 (.setBlendMode RenderState$BlendMode/Alpha))202 (.setQueueBucket RenderQueue$Bucket/Transparent)))204 (defn transparent-floor []205 (doto206 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)207 :material "Common/MatDefs/Misc/Unshaded.j3md"208 :texture "Textures/redWisp.png"209 :name "floor")210 (-> (.getMaterial)211 (.getAdditionalRenderState)212 (.setBlendMode RenderState$BlendMode/Alpha))213 (.setQueueBucket RenderQueue$Bucket/Transparent)))215 (defn test-skin []216 (let [b217 ;;(transparent-box)218 (transparent-sphere)219 ;;(sphere)220 f (transparent-floor)221 debug-node (Node.)222 node (doto (Node.) (.attachChild b) (.attachChild f))223 root-node (doto (Node.) (.attachChild node)224 (.attachChild debug-node))225 ]227 (world228 root-node229 {"key-return" (fire-cannon-ball node)}230 ;;no-op231 (fn [world]232 ;; (Capture/SimpleCaptureVideo233 ;; world234 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))235 ;; (no-logging)236 ;;(enable-debug world)237 ;; (set-accuracy world (/ 1 60))238 )240 (fn [& _]241 (let [sensitivity 0.2242 touch-data (touch-percieve sensitivity b node)]243 (manage-ray-debug-node debug-node b touch-data sensitivity)244 )245 (Thread/sleep 10)246 ))))248 #+end_src250 #+results: skin-main251 : #'body.skin/test-skin260 * COMMENT code generation261 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes262 <<skin-main>>263 #+end_src