rlm@0: #+title: SKIN! rlm@0: #+author: Robert McIntyre rlm@0: #+email: rlm@mit.edu rlm@0: #+description: Simulating touch in JMonkeyEngine rlm@4: #+SETUPFILE: ../../aurellem/org/setup.org rlm@4: #+INCLUDE: ../../aurellem/org/level-0.org rlm@6: #+babel: :mkdirp yes :noweb yes rlm@0: rlm@5: let's see what checkboxes look like: rlm@5: rlm@5: * test [1/2] rlm@5: - [ ] item 1 rlm@5: - [X] item 2 rlm@0: rlm@0: rlm@0: * skin! rlm@0: rlm@0: #+srcname: skin-main rlm@0: #+begin_src clojure rlm@0: (ns body.skin) rlm@0: (use 'cortex.world) rlm@0: (use 'cortex.import) rlm@0: (use 'clojure.contrib.def) rlm@0: (cortex.import/mega-import-jme3) rlm@0: (rlm.rlm-commands/help) rlm@0: rlm@0: (import java.util.logging.Level) rlm@0: (import java.util.logging.Logger) rlm@11: (use 'hello.brick-wall) rlm@0: rlm@6: (defn triangles [#^Geometry geom] rlm@6: (let rlm@6: [mesh (.getMesh geom) rlm@6: triangles (transient [])] rlm@6: (dorun rlm@6: (for [n (range (.getTriangleCount mesh))] rlm@6: (let [tri (Triangle.)] rlm@6: (.getTriangle mesh n tri) rlm@6: (.calculateNormal tri) rlm@6: (.calculateCenter tri) rlm@6: (conj! triangles tri)))) rlm@6: (persistent! triangles))) rlm@6: rlm@7: (defn get-ray-origin rlm@7: [geom tri] rlm@7: (let [new (Vector3f.)] rlm@7: (.calculateCenter tri) rlm@8: (.localToWorld geom (.getCenter tri) new) rlm@7: new)) rlm@6: rlm@7: (defn get-ray-direction rlm@7: [geom tri] rlm@9: (let [n+c (Vector3f.)] rlm@7: (.calculateNormal tri) rlm@9: (.calculateCenter tri) rlm@9: (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c) rlm@9: (.subtract n+c (get-ray-origin geom tri)) rlm@8: )) rlm@7: rlm@7: (defn ray-origin-debug rlm@9: [ray color] rlm@7: (make-shape rlm@20: (assoc base-shape rlm@20: :shape (Sphere. 5 5 0.05) rlm@20: :name "arrow" rlm@20: :color color rlm@20: :texture false rlm@20: :physical? false rlm@20: :position rlm@20: (.getOrigin ray)))) rlm@6: rlm@9: (defn ray-debug [ray color] rlm@6: (make-shape rlm@6: (assoc rlm@6: base-shape rlm@6: :name "debug-ray" rlm@6: :physical? false rlm@6: :shape (com.jme3.scene.shape.Line. rlm@6: (.getOrigin ray) rlm@6: (.add rlm@6: (.getOrigin ray) rlm@6: (.mult (.getDirection ray) rlm@6: (float (.getLimit ray)))))))) rlm@6: rlm@6: rlm@10: (defn contact-color [contacts] rlm@10: (case contacts rlm@10: 0 ColorRGBA/Gray rlm@10: 1 ColorRGBA/Blue rlm@10: 2 ColorRGBA/Green rlm@10: 3 ColorRGBA/Yellow rlm@10: 4 ColorRGBA/Orange rlm@10: 5 ColorRGBA/Red rlm@10: 6 ColorRGBA/Magenta rlm@10: 7 ColorRGBA/Pink rlm@10: 8 ColorRGBA/White)) rlm@6: rlm@6: (defn normal-rays rlm@6: "returns rays" rlm@6: [limit #^Geometry geom] rlm@6: (vec rlm@6: (map rlm@6: (fn [tri] rlm@6: (doto rlm@6: (Ray. (get-ray-origin geom tri) rlm@6: (get-ray-direction geom tri)) rlm@12: (.setLimit limit))) rlm@6: (triangles geom)))) rlm@6: rlm@14: (defn update-ray-debug [node ray contacts] rlm@14: (let [origin (.getChild node 0)] rlm@14: (.setLocalTranslation origin (.getOrigin ray)) rlm@14: (.setColor (.getMaterial origin) "Color" (contact-color contacts)))) rlm@14: rlm@13: (defn init-node rlm@13: [debug-node rays] rlm@13: (println-repl "Init touch debug node.") rlm@12: (.detachAllChildren debug-node) rlm@13: (dorun rlm@13: (for [ray rays] rlm@13: (do rlm@13: (.attachChild rlm@13: debug-node rlm@13: (doto (Node.) rlm@14: (.attachChild (ray-origin-debug ray ColorRGBA/Gray)) rlm@20: (.attachChild (ray-debug ray ColorRGBA/Gray)) rlm@14: )))))) rlm@14: rlm@13: (defn manage-ray-debug-node [debug-node geom touch-data limit] rlm@13: (let [rays (normal-rays limit geom)] rlm@13: (if (not= (count (.getChildren debug-node)) (count touch-data)) rlm@13: (init-node debug-node rays)) rlm@13: (dorun rlm@13: (for [n (range (count touch-data))] rlm@14: (update-ray-debug rlm@14: (.getChild debug-node n) (nth rays n) (nth touch-data n)))))) rlm@12: rlm@13: (defn touch-percieve [limit geom node] rlm@6: (let [normals (normal-rays limit geom)] rlm@12: rlm@6: (doall rlm@6: (for [ray normals] rlm@6: (do rlm@6: (let [results (CollisionResults.)] rlm@10: (.collideWith node ray results) rlm@19: (let [touch-objects (set (filter #(not (= geom %)) rlm@19: (map #(.getGeometry %) results)))] rlm@19: ;;(dorun (map #(println-repl (.getName %)) touch-objects)) rlm@19: (count touch-objects)))))))) rlm@6: rlm@11: (defn no-logging [] rlm@11: (.setLevel (Logger/getLogger "com.jme3") Level/OFF)) rlm@11: rlm@11: (defn set-accuracy [world new-accuracy] rlm@11: (let [physics-manager (.getState (.getStateManager world) BulletAppState)] rlm@11: (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy)))) rlm@11: rlm@0: (defn transparent-sphere [] rlm@0: (doto rlm@0: (make-shape rlm@0: (merge base-shape rlm@0: {:position (Vector3f. 0 2 0) rlm@0: :name "the blob." rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/purpleWisp.png" rlm@0: :physical? true rlm@0: :mass 70 rlm@0: :color ColorRGBA/Blue rlm@0: :shape (Sphere. 10 10 1)})) rlm@0: (-> (.getMaterial) rlm@0: (.getAdditionalRenderState) rlm@0: (.setBlendMode RenderState$BlendMode/Alpha)) rlm@0: (.setQueueBucket RenderQueue$Bucket/Transparent))) rlm@0: rlm@0: (defn transparent-box [] rlm@0: (doto rlm@0: (make-shape rlm@0: (merge base-shape rlm@0: {:position (Vector3f. 0 2 0) rlm@10: :name "box" rlm@0: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@0: :texture "Textures/purpleWisp.png" rlm@0: :physical? true rlm@0: :mass 70 rlm@0: :color ColorRGBA/Blue rlm@0: :shape (Box. 1 1 1)})) rlm@0: (-> (.getMaterial) rlm@0: (.getAdditionalRenderState) rlm@0: (.setBlendMode RenderState$BlendMode/Alpha)) rlm@0: (.setQueueBucket RenderQueue$Bucket/Transparent))) rlm@0: rlm@6: (defn transparent-floor [] rlm@6: (doto rlm@6: (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0) rlm@6: :material "Common/MatDefs/Misc/Unshaded.j3md" rlm@10: :texture "Textures/redWisp.png" rlm@10: :name "floor") rlm@6: (-> (.getMaterial) rlm@6: (.getAdditionalRenderState) rlm@6: (.setBlendMode RenderState$BlendMode/Alpha)) rlm@6: (.setQueueBucket RenderQueue$Bucket/Transparent))) rlm@6: rlm@0: (defn test-skin [] rlm@0: (let [b rlm@18: ;;(transparent-box) rlm@18: (transparent-sphere) rlm@10: ;;(sphere) rlm@6: f (transparent-floor) rlm@6: debug-node (Node.) rlm@12: node (doto (Node.) (.attachChild b) (.attachChild f)) rlm@12: root-node (doto (Node.) (.attachChild node) rlm@12: (.attachChild debug-node)) rlm@12: ] rlm@0: rlm@0: (world rlm@12: root-node rlm@15: {"key-return" (fire-cannon-ball node)} rlm@0: (fn [world] rlm@20: ;; (Capture/SimpleCaptureVideo rlm@20: ;; world rlm@20: ;; (file-str "/home/r/proj/cortex/tmp/blob.avi")) rlm@20: ;; (no-logging) rlm@20: ;;(enable-debug world) rlm@20: ;; (set-accuracy world (/ 1 60)) rlm@11: ) rlm@0: rlm@0: (fn [& _] rlm@19: (let [sensitivity 0.2 rlm@18: touch-data (touch-percieve sensitivity b node)] rlm@18: (manage-ray-debug-node debug-node b touch-data sensitivity) rlm@18: ) rlm@11: (Thread/sleep 10) rlm@0: )))) rlm@0: rlm@0: #+end_src rlm@0: rlm@3: #+results: skin-main rlm@3: : #'body.skin/test-skin rlm@3: rlm@0: rlm@0: rlm@0: rlm@10: rlm@10: rlm@6: rlm@0: rlm@0: * COMMENT code generation rlm@6: #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes rlm@0: <> rlm@0: #+end_src rlm@0: rlm@0: rlm@0: rlm@0: