diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/skin.org	Sun Oct 16 05:12:19 2011 -0700
     1.3 @@ -0,0 +1,322 @@
     1.4 +#+title: SKIN!
     1.5 +#+author: Robert McIntyre
     1.6 +#+email: rlm@mit.edu
     1.7 +#+MATHJAX: align:"left" mathml:t path:"../aurellem/src/MathJax/MathJax.js"
     1.8 +#+STYLE: <link rel="stylesheet" type="text/css" href="../aurellem/src/css/aurellem.css"/>
     1.9 +#+BABEL: :exports both :noweb yes :cache no :mkdirp yes
    1.10 +#+INCLUDE: ../aurellem/src/templates/level-0.org
    1.11 +#+description: Simulating touch in JMonkeyEngine
    1.12 +
    1.13 +
    1.14 +
    1.15 +
    1.16 +* skin!
    1.17 +
    1.18 +#+srcname: skin-main
    1.19 +#+begin_src clojure
    1.20 +(ns body.skin)
    1.21 +(use 'cortex.world)
    1.22 +(use 'cortex.import)
    1.23 +(use 'clojure.contrib.def)
    1.24 +(cortex.import/mega-import-jme3)
    1.25 +(rlm.rlm-commands/help)
    1.26 +
    1.27 +(import java.util.logging.Level)
    1.28 +(import java.util.logging.Logger)
    1.29 +
    1.30 +
    1.31 +
    1.32 +;; looks like we can use GhostControls for implementing touch.
    1.33 +;; for example:
    1.34 +(def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
    1.35 +(def shifted-sphere
    1.36 +     (doto (CompoundCollisionShape.)
    1.37 +       (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
    1.38 +
    1.39 +(def gc (GhostControl. shifted-sphere))
    1.40 +(def b (box 1 1 1 :mass 1))
    1.41 +(.addControl b rc)
    1.42 +(.addControl b gc)
    1.43 +
    1.44 +
    1.45 +(defn looksies! []
    1.46 +  (view b))
    1.47 +
    1.48 +;; overlapping objects can be gotten via =.getOverlappingCount= and
    1.49 +;; =.getOverlappingObjects=
    1.50 +
    1.51 +;; looks like I might be able to place a small "touch-sphere" whther
    1.52 +;; on every triangle in the object's  mesh, or at the verticies, using
    1.53 +;; .getTriangleCount() on the mesh gotten by .getMesh()
    1.54 +
    1.55 +;; this way, I can create a mesh and just divide up it's faces using
    1.56 +;; blender, and this will create the touch sensor distribution.
    1.57 +
    1.58 +
    1.59 +(defn make-touch-sphere [#^Geometry geom]
    1.60 +  (let [tri (Triangle.)
    1.61 +	mesh (.getMesh geom)
    1.62 +	controls! (transient [])]
    1.63 +    (dorun
    1.64 +     (for [n (range (.getTriangleCount mesh))]
    1.65 +       (do
    1.66 +	 (.getTriangle mesh n tri)
    1.67 +	 (.calculateCenter tri)
    1.68 +	 (let [control 
    1.69 +	       (doto
    1.70 +		   (GhostControl.
    1.71 +		    (doto (CompoundCollisionShape.)
    1.72 +		      (.addChildShape
    1.73 +		       (SphereCollisionShape. (float 0.1))
    1.74 +		       (.mult (.getCenter tri) (float 1)))
    1.75 +		      (.setMargin -0.1)))
    1.76 +		 (.setApplyPhysicsLocal true))]
    1.77 +		  
    1.78 +	   (.addControl geom control)
    1.79 +	   (conj! controls! control)))))
    1.80 +    (persistent! controls!)))
    1.81 +	 
    1.82 +
    1.83 +(defn make-touch [#^Geometry geom]
    1.84 +  (let [tri (Triangle.)
    1.85 +	mesh (.getMesh geom)
    1.86 +	controls! (transient [])]
    1.87 +    (dorun
    1.88 +     (for [n (range (.getTriangleCount mesh))]
    1.89 +       (do
    1.90 +	 (.getTriangle mesh n tri)
    1.91 +	 (.calculateCenter tri)
    1.92 +	 (.calculateNormal tri)
    1.93 +	 (println-repl tri)
    1.94 +	 (println-repl (.get1 tri))
    1.95 +	 (println-repl (.get2 tri))
    1.96 +	 (println-repl (.get3 tri))
    1.97 +	 (println-repl (.getCenter tri))
    1.98 +	 (println-repl (.getNormal tri))
    1.99 +	 (let [control
   1.100 +	       (doto 
   1.101 +		   (GhostControl.
   1.102 +		    
   1.103 +		     (doto (CompoundCollisionShape.)
   1.104 +		       (.addChildShape
   1.105 +		        (SimplexCollisionShape. Vector3f/ZERO)
   1.106 +			(.mult (.getCenter tri) (float 1)))		    
   1.107 +		      (.setMargin 0)
   1.108 +		      ))
   1.109 +		 (.setApplyPhysicsLocal true))]
   1.110 +
   1.111 +	   (.addControl geom control)
   1.112 +	   (conj! controls! control)))))
   1.113 +    (persistent! controls!)))
   1.114 +
   1.115 +(defn make-fucked-touch [#^Geometry geom]
   1.116 +  (let [tri (Triangle.)
   1.117 +	mesh (.getMesh geom)
   1.118 +	controls! (transient [])]
   1.119 +    (dorun
   1.120 +     (for [n (range (.getTriangleCount mesh))]
   1.121 +       (do
   1.122 +	 (.getTriangle mesh n tri)
   1.123 +	 (.calculateCenter tri)
   1.124 +	 (.calculateNormal tri)
   1.125 +	 (println-repl tri)
   1.126 +	 (println-repl (.get1 tri))
   1.127 +	 (println-repl (.get2 tri))
   1.128 +	 (println-repl (.get3 tri))
   1.129 +	 (println-repl (.getCenter tri))
   1.130 +	 (println-repl (.getNormal tri))
   1.131 +	 (let [control1
   1.132 +	       (doto 
   1.133 +		   (GhostControl.
   1.134 +		    (doto (CompoundCollisionShape.)
   1.135 +		       (.addChildShape
   1.136 +		        (SimplexCollisionShape. Vector3f/ZERO)
   1.137 +		       (.get1 tri))))
   1.138 +		 (.setApplyPhysicsLocal true))]
   1.139 +
   1.140 +	   (.addControl geom control1)
   1.141 +	   (conj! controls! control1)
   1.142 +	   )
   1.143 +	 
   1.144 +	 ;; (let [control1
   1.145 +	 ;;       (doto 
   1.146 +	 ;; 	   (GhostControl.
   1.147 +	 ;; 	    (doto (CompoundCollisionShape.)
   1.148 +	 ;; 	      (.addChildShape
   1.149 +	 ;; 	       (SimplexCollisionShape. Vector3f/ZERO)
   1.150 +	 ;; 	      (.get2 tri))))
   1.151 +	 ;; 	 (.setApplyPhysicsLocal true))]
   1.152 +	   
   1.153 +	 ;;   (.addControl geom control1)
   1.154 +	 ;;   (conj! controls! control1)
   1.155 +	 ;;   )
   1.156 +	 
   1.157 +	 ;; (let [control1
   1.158 +	 ;;       (doto 
   1.159 +	 ;; 	   (GhostControl.
   1.160 +	 ;; 	    (doto (CompoundCollisionShape.)
   1.161 +	 ;; 	       (.addChildShape
   1.162 +	 ;; 	        (SimplexCollisionShape. Vector3f/ZERO)
   1.163 +	 ;; 	       (.get3 tri))))
   1.164 +	 ;; 	 (.setApplyPhysicsLocal true))]
   1.165 +	   
   1.166 +	 ;;   (.addControl geom control1)
   1.167 +	 ;;   (conj! controls! control1)
   1.168 +	 
   1.169 +	   )))
   1.170 +	 (persistent! controls!)))
   1.171 +
   1.172 +
   1.173 +
   1.174 +(use 'hello.brick-wall)
   1.175 +
   1.176 +(defn touch-reception [controls]
   1.177 +    (let [control
   1.178 +	  (first 
   1.179 +	   (filter 
   1.180 +	    #(< 2 (.getOverlappingCount %)) controls))]
   1.181 +      (if (not (nil? control))
   1.182 +	(println
   1.183 +	 (seq
   1.184 +	  (.getOverlappingObjects control))))))
   1.185 +
   1.186 +(defn touch-print [controls]
   1.187 +  (println
   1.188 +   (map #(count (.getNonGhostOverlappingObjects %)) controls)))
   1.189 +
   1.190 +(defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
   1.191 +  (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
   1.192 +
   1.193 +(defn html-color [html-str]
   1.194 +  ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
   1.195 +   (map #(float (/ (Integer/parseInt % 16) 255))
   1.196 +	(map (partial apply str) (partition 2 html-str)))))
   1.197 +  
   1.198 +
   1.199 +(defn color-touch [controls]
   1.200 +  (no-exceptions
   1.201 +   (dorun
   1.202 +    (map
   1.203 +     (fn [control]
   1.204 +       (case (count (.getNonGhostOverlappingObjects control))
   1.205 +	     0 (set-debug-color ColorRGBA/Gray control)
   1.206 +	     1 (set-debug-color ColorRGBA/Blue control)
   1.207 +	     2 (set-debug-color ColorRGBA/Green control)
   1.208 +	     3 (set-debug-color ColorRGBA/Yellow control)
   1.209 +	     4 (set-debug-color ColorRGBA/Orange control)
   1.210 +	     5 (set-debug-color ColorRGBA/Red control)
   1.211 +	     6 (set-debug-color ColorRGBA/Magenta control)
   1.212 +	     7 (set-debug-color ColorRGBA/Pink control)
   1.213 +	     8 (set-debug-color ColorRGBA/White control)))
   1.214 +     controls))))
   1.215 +
   1.216 +(defn enable-debug [world]
   1.217 +  (.enableDebug 
   1.218 +   (.getPhysicsSpace
   1.219 +    (.getState
   1.220 +     (.getStateManager world)
   1.221 +     BulletAppState))
   1.222 +   (asset-manager)))
   1.223 +
   1.224 +(def with-debug
   1.225 +     '(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))
   1.226 +(def no-debug
   1.227 +     '(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))
   1.228 +
   1.229 +
   1.230 +
   1.231 +(defn transparent-sphere []
   1.232 +  (doto
   1.233 +      (make-shape
   1.234 +       (merge base-shape
   1.235 +	      {:position (Vector3f. 0 2 0)
   1.236 +	       :name "the blob."
   1.237 +	       :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.238 +	       :texture "Textures/purpleWisp.png"
   1.239 +	       :physical? true
   1.240 +	       :mass 70
   1.241 +	       :color ColorRGBA/Blue
   1.242 +	       :shape (Sphere. 10 10 1)}))
   1.243 +    (-> (.getMaterial)
   1.244 +	(.getAdditionalRenderState)
   1.245 +	(.setBlendMode RenderState$BlendMode/Alpha))
   1.246 +    (.setQueueBucket RenderQueue$Bucket/Transparent)))
   1.247 +
   1.248 +(defn transparent-box []
   1.249 +    (doto
   1.250 +      (make-shape
   1.251 +       (merge base-shape
   1.252 +	      {:position (Vector3f. 0 2 0)
   1.253 +	       :name "the blob."
   1.254 +	       :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.255 +	       :texture "Textures/purpleWisp.png"
   1.256 +	       :physical? true
   1.257 +	       :mass 70
   1.258 +	       :color ColorRGBA/Blue
   1.259 +	       :shape (Box. 1 1 1)}))
   1.260 +    (-> (.getMaterial)
   1.261 +	(.getAdditionalRenderState)
   1.262 +	(.setBlendMode RenderState$BlendMode/Alpha))
   1.263 +    (.setQueueBucket RenderQueue$Bucket/Transparent)))
   1.264 +
   1.265 +  
   1.266 +
   1.267 +(defn no-logging []
   1.268 +  (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
   1.269 +
   1.270 +(defn set-accuracy [world new-accuracy]
   1.271 +  (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
   1.272 +    (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
   1.273 +
   1.274 +(defn test-skin []
   1.275 +  (let [b
   1.276 +	;;(transparent-box)
   1.277 +	(transparent-sphere)
   1.278 +	
   1.279 +	controls
   1.280 +	;;(make-touch-sphere b)
   1.281 +	(make-touch b)
   1.282 +	]
   1.283 +    
   1.284 +    (world
   1.285 +     (doto (Node.) (.attachChild b)
   1.286 +	   (.attachChild
   1.287 +	    (doto
   1.288 +		(box 5 0.2 5  :mass 0 :position (Vector3f. 0 -2 0)
   1.289 +		     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.290 +		     :texture "Textures/redWisp.png")
   1.291 +	      (-> (.getMaterial)
   1.292 +		  (.getAdditionalRenderState)
   1.293 +		  (.setBlendMode RenderState$BlendMode/Alpha))
   1.294 +	      (.setQueueBucket RenderQueue$Bucket/Transparent))))
   1.295 +     
   1.296 +     {"key-return" (fire-cannon-ball)
   1.297 +      "key-space"  (fn [game value]
   1.298 +     		     (touch-print controls))}
   1.299 +     (fn [world]
   1.300 +       (Capture/SimpleCaptureVideo world (file-str "~/out-temp/blob.avi"))
   1.301 +       (no-logging)
   1.302 +       (enable-debug world)
   1.303 +       (set-accuracy world (/ 1 60))
   1.304 +       )
   1.305 +
   1.306 +     (fn [& _]
   1.307 +       (touch-print controls)
   1.308 +       (color-touch controls)
   1.309 +       ))))
   1.310 +
   1.311 +#+end_src
   1.312 +
   1.313 +
   1.314 +
   1.315 +
   1.316 +
   1.317 +
   1.318 +* COMMENT code generation
   1.319 +#+begin_src clojure :tangle ../src/body/skin.clj
   1.320 +<<skin-main>>
   1.321 +#+end_src
   1.322 +
   1.323 +
   1.324 +
   1.325 +