Mercurial > cortex
view org/skin.org @ 5:93ff2b4e7e6a
trying to resolve sensitivity bugs with tough implementation
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 22 Oct 2011 01:20:34 -0700 |
parents | 50c92af2018e |
children | e3c6d1c1cb00 |
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.org9 let's see what checkboxes look like:11 * test [1/2]12 - [ ] item 113 - [X] item 216 * skin!25 #+srcname: skin-main26 #+begin_src clojure27 (ns body.skin)28 (use 'cortex.world)29 (use 'cortex.import)30 (use 'clojure.contrib.def)31 (cortex.import/mega-import-jme3)32 (rlm.rlm-commands/help)34 (import java.util.logging.Level)35 (import java.util.logging.Logger)39 ;; looks like we can use GhostControls for implementing touch.40 ;; for example:41 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))42 (def shifted-sphere43 (doto (CompoundCollisionShape.)44 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))46 (def gc (GhostControl. shifted-sphere))47 (def b (box 1 1 1 :mass 1))48 (.addControl b rc)49 (.addControl b gc)52 (defn looksies! []53 (view b))55 ;; overlapping objects can be gotten via =.getOverlappingCount= and56 ;; =.getOverlappingObjects=58 ;; looks like I might be able to place a small "touch-sphere" whther59 ;; on every triangle in the object's mesh, or at the verticies, using60 ;; .getTriangleCount() on the mesh gotten by .getMesh()62 ;; this way, I can create a mesh and just divide up it's faces using63 ;; blender, and this will create the touch sensor distribution.66 (defn make-touch-sphere [#^Geometry geom]67 (let [tri (Triangle.)68 mesh (.getMesh geom)69 controls! (transient [])]70 (dorun71 (for [n (range (.getTriangleCount mesh))]72 (do73 (.getTriangle mesh n tri)74 (.calculateCenter tri)75 (let [control76 (doto77 (GhostControl.78 (doto (CompoundCollisionShape.)79 (.addChildShape80 (SphereCollisionShape. (float 0.1))81 (.mult (.getCenter tri) (float 1)))82 (.setMargin -0.1)))83 (.setApplyPhysicsLocal true))]85 (.addControl geom control)86 (conj! controls! control)))))87 (persistent! controls!)))90 (defn make-touch [#^Geometry geom]91 (let [tri (Triangle.)92 mesh (.getMesh geom)93 controls! (transient [])]94 (dorun95 (for [n (range (.getTriangleCount mesh))]96 (do97 (.getTriangle mesh n tri)98 (.calculateCenter tri)99 (.calculateNormal tri)100 ;; (println-repl tri)101 ;; (println-repl (.get1 tri))102 ;; (println-repl (.get2 tri))103 ;; (println-repl (.get3 tri))104 ;; (println-repl (.getCenter tri))105 ;; (println-repl (.getNormal tri))106 (let [control107 (doto108 (GhostControl.110 (doto (CompoundCollisionShape.)111 (.addChildShape112 (SimplexCollisionShape. Vector3f/ZERO)113 (.mult (.getCenter tri) (float 1)))114 (.setMargin 0)115 ))116 (.setApplyPhysicsLocal true))]117 (.addControl geom control)118 (conj! controls! control)))))119 (persistent! controls!)))121 (use 'hello.brick-wall)123 (defn touch-reception [controls]124 (let [control125 (first126 (filter127 #(< 2 (.getOverlappingCount %)) controls))]128 (if (not (nil? control))129 (println130 (seq131 (.getOverlappingObjects control))))))133 (defn touch-print [controls]134 (println135 (map #(count (.getNonGhostOverlappingObjects %)) controls)))137 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]138 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))140 (defn html-color [html-str]141 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))142 (map #(float (/ (Integer/parseInt % 16) 255))143 (map (partial apply str) (partition 2 html-str)))))145 (defn color-touch [controls]146 (no-exceptions147 (dorun148 (map149 (fn [control]150 (case (count (.getNonGhostOverlappingObjects control))151 0 (set-debug-color ColorRGBA/Gray control)152 1 (set-debug-color ColorRGBA/Blue control)153 2 (set-debug-color ColorRGBA/Green control)154 3 (set-debug-color ColorRGBA/Yellow control)155 4 (set-debug-color ColorRGBA/Orange control)156 5 (set-debug-color ColorRGBA/Red control)157 6 (set-debug-color ColorRGBA/Magenta control)158 7 (set-debug-color ColorRGBA/Pink control)159 8 (set-debug-color ColorRGBA/White control)))160 controls))))162 (defn enable-debug [world]163 (.enableDebug164 (.getPhysicsSpace165 (.getState166 (.getStateManager world)167 BulletAppState))168 (asset-manager)))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 "the blob."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)))206 (defn no-logging []207 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))209 (defn set-accuracy [world new-accuracy]210 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]211 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))213 (defn test-skin []214 (let [b215 ;;(transparent-box)216 (transparent-sphere)218 controls219 ;;(make-touch-sphere b)220 (make-touch b)221 ]223 (world224 (doto (Node.) (.attachChild b)225 (.attachChild226 (doto227 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)228 :material "Common/MatDefs/Misc/Unshaded.j3md"229 :texture "Textures/redWisp.png")230 (-> (.getMaterial)231 (.getAdditionalRenderState)232 (.setBlendMode RenderState$BlendMode/Alpha))233 (.setQueueBucket RenderQueue$Bucket/Transparent))))235 {"key-return" (fire-cannon-ball)236 "key-space" (fn [game value]237 (touch-print controls))}238 (fn [world]239 (Capture/SimpleCaptureVideo240 world241 (file-str "/home/r/proj/cortex/tmp/blob.avi"))242 (no-logging)243 (enable-debug world)244 (set-accuracy world (/ 1 60))245 )247 (fn [& _]248 ;;(touch-print controls)249 (color-touch controls)250 ))))252 #+end_src254 #+results: skin-main255 : #'body.skin/test-skin262 * COMMENT code generation263 #+begin_src clojure :tangle ../src/body/skin.clj264 <<skin-main>>265 #+end_src