Mercurial > cortex
view org/skin.org @ 4:50c92af2018e
fixed headers, make compat symlink
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 20 Oct 2011 15:10:38 -0700 |
parents | 6c93f2018655 |
children | 93ff2b4e7e6a |
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.org11 * skin!13 #+srcname: skin-main14 #+begin_src clojure15 (ns body.skin)16 (use 'cortex.world)17 (use 'cortex.import)18 (use 'clojure.contrib.def)19 (cortex.import/mega-import-jme3)20 (rlm.rlm-commands/help)22 (import java.util.logging.Level)23 (import java.util.logging.Logger)27 ;; looks like we can use GhostControls for implementing touch.28 ;; for example:29 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))30 (def shifted-sphere31 (doto (CompoundCollisionShape.)32 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))34 (def gc (GhostControl. shifted-sphere))35 (def b (box 1 1 1 :mass 1))36 (.addControl b rc)37 (.addControl b gc)40 (defn looksies! []41 (view b))43 ;; overlapping objects can be gotten via =.getOverlappingCount= and44 ;; =.getOverlappingObjects=46 ;; looks like I might be able to place a small "touch-sphere" whther47 ;; on every triangle in the object's mesh, or at the verticies, using48 ;; .getTriangleCount() on the mesh gotten by .getMesh()50 ;; this way, I can create a mesh and just divide up it's faces using51 ;; blender, and this will create the touch sensor distribution.54 (defn make-touch-sphere [#^Geometry geom]55 (let [tri (Triangle.)56 mesh (.getMesh geom)57 controls! (transient [])]58 (dorun59 (for [n (range (.getTriangleCount mesh))]60 (do61 (.getTriangle mesh n tri)62 (.calculateCenter tri)63 (let [control64 (doto65 (GhostControl.66 (doto (CompoundCollisionShape.)67 (.addChildShape68 (SphereCollisionShape. (float 0.1))69 (.mult (.getCenter tri) (float 1)))70 (.setMargin -0.1)))71 (.setApplyPhysicsLocal true))]73 (.addControl geom control)74 (conj! controls! control)))))75 (persistent! controls!)))78 (defn make-touch [#^Geometry geom]79 (let [tri (Triangle.)80 mesh (.getMesh geom)81 controls! (transient [])]82 (dorun83 (for [n (range (.getTriangleCount mesh))]84 (do85 (.getTriangle mesh n tri)86 (.calculateCenter tri)87 (.calculateNormal tri)88 (println-repl tri)89 (println-repl (.get1 tri))90 (println-repl (.get2 tri))91 (println-repl (.get3 tri))92 (println-repl (.getCenter tri))93 (println-repl (.getNormal tri))94 (let [control95 (doto96 (GhostControl.98 (doto (CompoundCollisionShape.)99 (.addChildShape100 (SimplexCollisionShape. Vector3f/ZERO)101 (.mult (.getCenter tri) (float 1)))102 (.setMargin 0)103 ))104 (.setApplyPhysicsLocal true))]106 (.addControl geom control)107 (conj! controls! control)))))108 (persistent! controls!)))110 (defn make-fucked-touch [#^Geometry geom]111 (let [tri (Triangle.)112 mesh (.getMesh geom)113 controls! (transient [])]114 (dorun115 (for [n (range (.getTriangleCount mesh))]116 (do117 (.getTriangle mesh n tri)118 (.calculateCenter tri)119 (.calculateNormal tri)120 (println-repl tri)121 (println-repl (.get1 tri))122 (println-repl (.get2 tri))123 (println-repl (.get3 tri))124 (println-repl (.getCenter tri))125 (println-repl (.getNormal tri))126 (let [control1127 (doto128 (GhostControl.129 (doto (CompoundCollisionShape.)130 (.addChildShape131 (SimplexCollisionShape. Vector3f/ZERO)132 (.get1 tri))))133 (.setApplyPhysicsLocal true))]135 (.addControl geom control1)136 (conj! controls! control1)137 )139 ;; (let [control1140 ;; (doto141 ;; (GhostControl.142 ;; (doto (CompoundCollisionShape.)143 ;; (.addChildShape144 ;; (SimplexCollisionShape. Vector3f/ZERO)145 ;; (.get2 tri))))146 ;; (.setApplyPhysicsLocal true))]148 ;; (.addControl geom control1)149 ;; (conj! controls! control1)150 ;; )152 ;; (let [control1153 ;; (doto154 ;; (GhostControl.155 ;; (doto (CompoundCollisionShape.)156 ;; (.addChildShape157 ;; (SimplexCollisionShape. Vector3f/ZERO)158 ;; (.get3 tri))))159 ;; (.setApplyPhysicsLocal true))]161 ;; (.addControl geom control1)162 ;; (conj! controls! control1)164 )))165 (persistent! controls!)))169 (use 'hello.brick-wall)171 (defn touch-reception [controls]172 (let [control173 (first174 (filter175 #(< 2 (.getOverlappingCount %)) controls))]176 (if (not (nil? control))177 (println178 (seq179 (.getOverlappingObjects control))))))181 (defn touch-print [controls]182 (println183 (map #(count (.getNonGhostOverlappingObjects %)) controls)))185 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]186 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))188 (defn html-color [html-str]189 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))190 (map #(float (/ (Integer/parseInt % 16) 255))191 (map (partial apply str) (partition 2 html-str)))))194 (defn color-touch [controls]195 (no-exceptions196 (dorun197 (map198 (fn [control]199 (case (count (.getNonGhostOverlappingObjects control))200 0 (set-debug-color ColorRGBA/Gray control)201 1 (set-debug-color ColorRGBA/Blue control)202 2 (set-debug-color ColorRGBA/Green control)203 3 (set-debug-color ColorRGBA/Yellow control)204 4 (set-debug-color ColorRGBA/Orange control)205 5 (set-debug-color ColorRGBA/Red control)206 6 (set-debug-color ColorRGBA/Magenta control)207 7 (set-debug-color ColorRGBA/Pink control)208 8 (set-debug-color ColorRGBA/White control)))209 controls))))211 (defn enable-debug [world]212 (.enableDebug213 (.getPhysicsSpace214 (.getState215 (.getStateManager world)216 BulletAppState))217 (asset-manager)))219 (def with-debug220 '(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))221 (def no-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))226 (defn transparent-sphere []227 (doto228 (make-shape229 (merge base-shape230 {:position (Vector3f. 0 2 0)231 :name "the blob."232 :material "Common/MatDefs/Misc/Unshaded.j3md"233 :texture "Textures/purpleWisp.png"234 :physical? true235 :mass 70236 :color ColorRGBA/Blue237 :shape (Sphere. 10 10 1)}))238 (-> (.getMaterial)239 (.getAdditionalRenderState)240 (.setBlendMode RenderState$BlendMode/Alpha))241 (.setQueueBucket RenderQueue$Bucket/Transparent)))243 (defn transparent-box []244 (doto245 (make-shape246 (merge base-shape247 {:position (Vector3f. 0 2 0)248 :name "the blob."249 :material "Common/MatDefs/Misc/Unshaded.j3md"250 :texture "Textures/purpleWisp.png"251 :physical? true252 :mass 70253 :color ColorRGBA/Blue254 :shape (Box. 1 1 1)}))255 (-> (.getMaterial)256 (.getAdditionalRenderState)257 (.setBlendMode RenderState$BlendMode/Alpha))258 (.setQueueBucket RenderQueue$Bucket/Transparent)))262 (defn no-logging []263 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))265 (defn set-accuracy [world new-accuracy]266 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]267 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))269 (defn test-skin []270 (let [b271 ;;(transparent-box)272 (transparent-sphere)274 controls275 ;;(make-touch-sphere b)276 (make-touch b)277 ]279 (world280 (doto (Node.) (.attachChild b)281 (.attachChild282 (doto283 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)284 :material "Common/MatDefs/Misc/Unshaded.j3md"285 :texture "Textures/redWisp.png")286 (-> (.getMaterial)287 (.getAdditionalRenderState)288 (.setBlendMode RenderState$BlendMode/Alpha))289 (.setQueueBucket RenderQueue$Bucket/Transparent))))291 {"key-return" (fire-cannon-ball)292 "key-space" (fn [game value]293 (touch-print controls))}294 (fn [world]295 (Capture/SimpleCaptureVideo296 world297 (file-str "/home/r/proj/cortex/tmp/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_src310 #+results: skin-main311 : #'body.skin/test-skin318 * COMMENT code generation319 #+begin_src clojure :tangle ../src/body/skin.clj320 <<skin-main>>321 #+end_src