Mercurial > cortex
view org/skin.org @ 0:92f8d83b5d0b
initial import: I've made hearing and vision, and am working on touch.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 16 Oct 2011 05:12:19 -0700 |
parents | |
children | 4588025678b0 |
line wrap: on
line source
1 #+title: SKIN!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+MATHJAX: align:"left" mathml:t path:"../aurellem/src/MathJax/MathJax.js"5 #+STYLE: <link rel="stylesheet" type="text/css" href="../aurellem/src/css/aurellem.css"/>6 #+BABEL: :exports both :noweb yes :cache no :mkdirp yes7 #+INCLUDE: ../aurellem/src/templates/level-0.org8 #+description: Simulating touch in JMonkeyEngine13 * skin!15 #+srcname: skin-main16 #+begin_src clojure17 (ns body.skin)18 (use 'cortex.world)19 (use 'cortex.import)20 (use 'clojure.contrib.def)21 (cortex.import/mega-import-jme3)22 (rlm.rlm-commands/help)24 (import java.util.logging.Level)25 (import java.util.logging.Logger)29 ;; looks like we can use GhostControls for implementing touch.30 ;; for example:31 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))32 (def shifted-sphere33 (doto (CompoundCollisionShape.)34 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))36 (def gc (GhostControl. shifted-sphere))37 (def b (box 1 1 1 :mass 1))38 (.addControl b rc)39 (.addControl b gc)42 (defn looksies! []43 (view b))45 ;; overlapping objects can be gotten via =.getOverlappingCount= and46 ;; =.getOverlappingObjects=48 ;; looks like I might be able to place a small "touch-sphere" whther49 ;; on every triangle in the object's mesh, or at the verticies, using50 ;; .getTriangleCount() on the mesh gotten by .getMesh()52 ;; this way, I can create a mesh and just divide up it's faces using53 ;; blender, and this will create the touch sensor distribution.56 (defn make-touch-sphere [#^Geometry geom]57 (let [tri (Triangle.)58 mesh (.getMesh geom)59 controls! (transient [])]60 (dorun61 (for [n (range (.getTriangleCount mesh))]62 (do63 (.getTriangle mesh n tri)64 (.calculateCenter tri)65 (let [control66 (doto67 (GhostControl.68 (doto (CompoundCollisionShape.)69 (.addChildShape70 (SphereCollisionShape. (float 0.1))71 (.mult (.getCenter tri) (float 1)))72 (.setMargin -0.1)))73 (.setApplyPhysicsLocal true))]75 (.addControl geom control)76 (conj! controls! control)))))77 (persistent! controls!)))80 (defn make-touch [#^Geometry geom]81 (let [tri (Triangle.)82 mesh (.getMesh geom)83 controls! (transient [])]84 (dorun85 (for [n (range (.getTriangleCount mesh))]86 (do87 (.getTriangle mesh n tri)88 (.calculateCenter tri)89 (.calculateNormal tri)90 (println-repl tri)91 (println-repl (.get1 tri))92 (println-repl (.get2 tri))93 (println-repl (.get3 tri))94 (println-repl (.getCenter tri))95 (println-repl (.getNormal tri))96 (let [control97 (doto98 (GhostControl.100 (doto (CompoundCollisionShape.)101 (.addChildShape102 (SimplexCollisionShape. Vector3f/ZERO)103 (.mult (.getCenter tri) (float 1)))104 (.setMargin 0)105 ))106 (.setApplyPhysicsLocal true))]108 (.addControl geom control)109 (conj! controls! control)))))110 (persistent! controls!)))112 (defn make-fucked-touch [#^Geometry geom]113 (let [tri (Triangle.)114 mesh (.getMesh geom)115 controls! (transient [])]116 (dorun117 (for [n (range (.getTriangleCount mesh))]118 (do119 (.getTriangle mesh n tri)120 (.calculateCenter tri)121 (.calculateNormal tri)122 (println-repl tri)123 (println-repl (.get1 tri))124 (println-repl (.get2 tri))125 (println-repl (.get3 tri))126 (println-repl (.getCenter tri))127 (println-repl (.getNormal tri))128 (let [control1129 (doto130 (GhostControl.131 (doto (CompoundCollisionShape.)132 (.addChildShape133 (SimplexCollisionShape. Vector3f/ZERO)134 (.get1 tri))))135 (.setApplyPhysicsLocal true))]137 (.addControl geom control1)138 (conj! controls! control1)139 )141 ;; (let [control1142 ;; (doto143 ;; (GhostControl.144 ;; (doto (CompoundCollisionShape.)145 ;; (.addChildShape146 ;; (SimplexCollisionShape. Vector3f/ZERO)147 ;; (.get2 tri))))148 ;; (.setApplyPhysicsLocal true))]150 ;; (.addControl geom control1)151 ;; (conj! controls! control1)152 ;; )154 ;; (let [control1155 ;; (doto156 ;; (GhostControl.157 ;; (doto (CompoundCollisionShape.)158 ;; (.addChildShape159 ;; (SimplexCollisionShape. Vector3f/ZERO)160 ;; (.get3 tri))))161 ;; (.setApplyPhysicsLocal true))]163 ;; (.addControl geom control1)164 ;; (conj! controls! control1)166 )))167 (persistent! controls!)))171 (use 'hello.brick-wall)173 (defn touch-reception [controls]174 (let [control175 (first176 (filter177 #(< 2 (.getOverlappingCount %)) controls))]178 (if (not (nil? control))179 (println180 (seq181 (.getOverlappingObjects control))))))183 (defn touch-print [controls]184 (println185 (map #(count (.getNonGhostOverlappingObjects %)) controls)))187 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]188 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))190 (defn html-color [html-str]191 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))192 (map #(float (/ (Integer/parseInt % 16) 255))193 (map (partial apply str) (partition 2 html-str)))))196 (defn color-touch [controls]197 (no-exceptions198 (dorun199 (map200 (fn [control]201 (case (count (.getNonGhostOverlappingObjects control))202 0 (set-debug-color ColorRGBA/Gray control)203 1 (set-debug-color ColorRGBA/Blue control)204 2 (set-debug-color ColorRGBA/Green control)205 3 (set-debug-color ColorRGBA/Yellow control)206 4 (set-debug-color ColorRGBA/Orange control)207 5 (set-debug-color ColorRGBA/Red control)208 6 (set-debug-color ColorRGBA/Magenta control)209 7 (set-debug-color ColorRGBA/Pink control)210 8 (set-debug-color ColorRGBA/White control)))211 controls))))213 (defn enable-debug [world]214 (.enableDebug215 (.getPhysicsSpace216 (.getState217 (.getStateManager world)218 BulletAppState))219 (asset-manager)))221 (def with-debug222 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))223 (def no-debug224 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))228 (defn transparent-sphere []229 (doto230 (make-shape231 (merge base-shape232 {:position (Vector3f. 0 2 0)233 :name "the blob."234 :material "Common/MatDefs/Misc/Unshaded.j3md"235 :texture "Textures/purpleWisp.png"236 :physical? true237 :mass 70238 :color ColorRGBA/Blue239 :shape (Sphere. 10 10 1)}))240 (-> (.getMaterial)241 (.getAdditionalRenderState)242 (.setBlendMode RenderState$BlendMode/Alpha))243 (.setQueueBucket RenderQueue$Bucket/Transparent)))245 (defn transparent-box []246 (doto247 (make-shape248 (merge base-shape249 {:position (Vector3f. 0 2 0)250 :name "the blob."251 :material "Common/MatDefs/Misc/Unshaded.j3md"252 :texture "Textures/purpleWisp.png"253 :physical? true254 :mass 70255 :color ColorRGBA/Blue256 :shape (Box. 1 1 1)}))257 (-> (.getMaterial)258 (.getAdditionalRenderState)259 (.setBlendMode RenderState$BlendMode/Alpha))260 (.setQueueBucket RenderQueue$Bucket/Transparent)))264 (defn no-logging []265 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))267 (defn set-accuracy [world new-accuracy]268 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]269 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))271 (defn test-skin []272 (let [b273 ;;(transparent-box)274 (transparent-sphere)276 controls277 ;;(make-touch-sphere b)278 (make-touch b)279 ]281 (world282 (doto (Node.) (.attachChild b)283 (.attachChild284 (doto285 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)286 :material "Common/MatDefs/Misc/Unshaded.j3md"287 :texture "Textures/redWisp.png")288 (-> (.getMaterial)289 (.getAdditionalRenderState)290 (.setBlendMode RenderState$BlendMode/Alpha))291 (.setQueueBucket RenderQueue$Bucket/Transparent))))293 {"key-return" (fire-cannon-ball)294 "key-space" (fn [game value]295 (touch-print controls))}296 (fn [world]297 (Capture/SimpleCaptureVideo world (file-str "~/out-temp/blob.avi"))298 (no-logging)299 (enable-debug world)300 (set-accuracy world (/ 1 60))301 )303 (fn [& _]304 (touch-print controls)305 (color-touch controls)306 ))))308 #+end_src315 * COMMENT code generation316 #+begin_src clojure :tangle ../src/body/skin.clj317 <<skin-main>>318 #+end_src