Mercurial > cortex
view org/skin.org @ 13:0eb2eac53361
progress!!
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 11:22:06 -0700 |
parents | 22c6878a1bc0 |
children | 3aa1ee6c6308 |
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))))114 (defn collision-debug [node result]115 (println-repl "contact point: " (.getContactPoint result))116 )118 (defn update-ray-debug [node ray]119 (.setLocalTranslation (.getChild node 1) (.getOrigin ray)))122 (defn init-node123 [debug-node rays]124 (println-repl "Init touch debug node.")125 (.detachAllChildren debug-node)126 (dorun127 (for [ray rays]128 (do129 (.attachChild130 debug-node131 (doto (Node.)132 (.attachChild (ray-debug ray ColorRGBA/Gray))133 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))))))))136 (defn manage-ray-debug-node [debug-node geom touch-data limit]137 (let [rays (normal-rays limit geom)]138 (if (not= (count (.getChildren debug-node)) (count touch-data))139 (init-node debug-node rays))140 (dorun141 (for [n (range (count touch-data))]142 (update-ray-debug (.getChild debug-node n) (nth rays n))))))145 (defn touch-percieve [limit geom node]146 (let [normals (normal-rays limit geom)]148 (doall149 (for [ray normals]150 (do151 (let [results (CollisionResults.)]152 (.collideWith node ray results)153 (let [answer (count (filter #(not (= geom (.getGeometry %))) results))154 ;;color (contact-color answer)155 ]156 ;;(dorun (map #(println-repl (.getName (.getGeometry %))) results))159 ;;(println-repl (.size results) "results for " ray)160 ;;(doall (map (partial collision-debug node) results))161 answer162 )))))))164 (defn enable-debug [world]165 (.enableDebug166 (.getPhysicsSpace167 (.getState168 (.getStateManager world)169 BulletAppState))170 (asset-manager)))172 (defn no-logging []173 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))175 (defn set-accuracy [world new-accuracy]176 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]177 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))180 (defn transparent-sphere []181 (doto182 (make-shape183 (merge base-shape184 {:position (Vector3f. 0 2 0)185 :name "the blob."186 :material "Common/MatDefs/Misc/Unshaded.j3md"187 :texture "Textures/purpleWisp.png"188 :physical? true189 :mass 70190 :color ColorRGBA/Blue191 :shape (Sphere. 10 10 1)}))192 (-> (.getMaterial)193 (.getAdditionalRenderState)194 (.setBlendMode RenderState$BlendMode/Alpha))195 (.setQueueBucket RenderQueue$Bucket/Transparent)))197 (defn transparent-box []198 (doto199 (make-shape200 (merge base-shape201 {:position (Vector3f. 0 2 0)202 :name "box"203 :material "Common/MatDefs/Misc/Unshaded.j3md"204 :texture "Textures/purpleWisp.png"205 :physical? true206 :mass 70207 :color ColorRGBA/Blue208 :shape (Box. 1 1 1)}))209 (-> (.getMaterial)210 (.getAdditionalRenderState)211 (.setBlendMode RenderState$BlendMode/Alpha))212 (.setQueueBucket RenderQueue$Bucket/Transparent)))214 (defn transparent-floor []215 (doto216 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)217 :material "Common/MatDefs/Misc/Unshaded.j3md"218 :texture "Textures/redWisp.png"219 :name "floor")220 (-> (.getMaterial)221 (.getAdditionalRenderState)222 (.setBlendMode RenderState$BlendMode/Alpha))223 (.setQueueBucket RenderQueue$Bucket/Transparent)))225 (defn test-skin []226 (let [b227 (transparent-box)228 ;;(transparent-sphere)229 ;;(sphere)230 f (transparent-floor)231 ;;controls232 ;;(make-touch-sphere b)233 ;;(make-touch b)234 debug-node (Node.)235 node (doto (Node.) (.attachChild b) (.attachChild f))236 root-node (doto (Node.) (.attachChild node)237 (.attachChild debug-node))238 ]240 (world241 root-node242 {"key-return" (fire-cannon-ball)}243 ;;no-op244 (fn [world]245 ;; (Capture/SimpleCaptureVideo246 ;; world247 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))248 ;; (no-logging)249 (enable-debug world)250 ;; (set-accuracy world (/ 1 60))251 )253 (fn [& _]254 (let [touch-data (touch-percieve 0.2 b node)]255 (println-repl touch-data)256 (manage-ray-debug-node debug-node b touch-data 0.2))257 (Thread/sleep 10)258 ;;(touch-print controls)259 ;;(color-touch controls)260 ))))262 #+end_src264 #+results: skin-main265 : #'body.skin/test-skin274 * COMMENT code generation275 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes276 <<skin-main>>277 #+end_src