diff org/skin.org @ 6:e3c6d1c1cb00

fixing ray casting problem
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 00:17:54 -0700
parents 93ff2b4e7e6a
children 9ccfbcbed90a
line wrap: on
line diff
     1.1 --- a/org/skin.org	Sat Oct 22 01:20:34 2011 -0700
     1.2 +++ b/org/skin.org	Sun Oct 23 00:17:54 2011 -0700
     1.3 @@ -4,7 +4,7 @@
     1.4  #+description: Simulating touch in JMonkeyEngine
     1.5  #+SETUPFILE: ../../aurellem/org/setup.org
     1.6  #+INCLUDE: ../../aurellem/org/level-0.org
     1.7 -
     1.8 +#+babel: :mkdirp yes :noweb yes
     1.9  
    1.10  let's see what checkboxes look like:
    1.11   
    1.12 @@ -15,13 +15,6 @@
    1.13  
    1.14  * skin!
    1.15  
    1.16 -
    1.17 -  
    1.18 -
    1.19 -
    1.20 -
    1.21 -
    1.22 -
    1.23  #+srcname: skin-main
    1.24  #+begin_src clojure
    1.25  (ns body.skin)
    1.26 @@ -85,7 +78,133 @@
    1.27  	   (.addControl geom control)
    1.28  	   (conj! controls! control)))))
    1.29      (persistent! controls!)))
    1.30 -	 
    1.31 +
    1.32 +
    1.33 +(defn triangles [#^Geometry geom]
    1.34 +  (let
    1.35 +      [mesh (.getMesh geom)
    1.36 +       triangles (transient [])]
    1.37 +    (dorun
    1.38 +     (for [n (range (.getTriangleCount mesh))]
    1.39 +       (let [tri (Triangle.)]
    1.40 +         (.getTriangle mesh n tri)
    1.41 +         (.calculateNormal tri)
    1.42 +         (.calculateCenter tri)
    1.43 +         (conj! triangles tri))))
    1.44 +    (persistent! triangles)))
    1.45 +   
    1.46 +
    1.47 +(defn new-touch [#^Geometry geom]
    1.48 +  (dorun (map
    1.49 +          (comp no-op #(.getCenter %))
    1.50 +              (triangles geom))))
    1.51 +
    1.52 +
    1.53 +
    1.54 +        
    1.55 +(defn normal-arrows
    1.56 +  "returns a node containing arrows which point
    1.57 +   in the normal direction of each face of the
    1.58 +   given Geometry"
    1.59 +  [#^Geometry geom]
    1.60 +  (let [node (Node.)]
    1.61 +    (dorun
    1.62 +     (map #(.attachChild node %)
    1.63 +          (map 
    1.64 +           (fn [tri]
    1.65 +             (make-shape
    1.66 +              (assoc base-shape
    1.67 +                :shape (Sphere. 5 5 0.05)
    1.68 +                :name "arrow"
    1.69 +                :color ColorRGBA/Orange
    1.70 +                :texture false
    1.71 +                :asset-manager (asset-manager)
    1.72 +                :physical? false
    1.73 +                ;;:rotation
    1.74 +                ;;(.mult
    1.75 +                ;; (doto (Quaternion.)
    1.76 +                ;;   (.lookAt (.getNormal tri)
    1.77 +                ;;            Vector3f/UNIT_Y))
    1.78 +                ;; (.getLocalRotation geom))
    1.79 +                :position
    1.80 +                (.add
    1.81 +                 (.getCenter tri)
    1.82 +                 (.getLocalTranslation geom)))))
    1.83 +           (triangles geom))))
    1.84 +    node))
    1.85 +
    1.86 +
    1.87 +(defn get-ray-origin
    1.88 +  [geom tri]
    1.89 +  (.add
    1.90 +   (.getCenter tri)
    1.91 +   (.getLocalTranslation geom)))
    1.92 +
    1.93 +(defn get-ray-direction
    1.94 +  [geom tri]
    1.95 +  (.mult (.getLocalRotation geom)
    1.96 +         (.getNormal tri)))
    1.97 +
    1.98 +(defn ray-debug [ray]
    1.99 +  (make-shape
   1.100 +   (assoc
   1.101 +       base-shape
   1.102 +     :name "debug-ray"
   1.103 +     :physical? false
   1.104 +     :shape (com.jme3.scene.shape.Line.
   1.105 +             (.getOrigin ray)
   1.106 +             (.add
   1.107 +              (.getOrigin ray)
   1.108 +              (.mult (.getDirection ray)
   1.109 +                     (float (.getLimit ray))))))))
   1.110 +             
   1.111 +                               
   1.112 +
   1.113 +
   1.114 +(defn normal-rays
   1.115 +  "returns rays"
   1.116 +  [limit #^Geometry geom]
   1.117 +  (vec
   1.118 +   (map 
   1.119 +    (fn [tri]
   1.120 +      (doto 
   1.121 +          (Ray. (get-ray-origin geom tri)
   1.122 +                (get-ray-direction geom tri))
   1.123 +                
   1.124 +      (.setLimit limit)))
   1.125 +    (triangles geom))))
   1.126 +
   1.127 +
   1.128 +(defn collision-debug [node result]
   1.129 +
   1.130 +  (println-repl "contact point: " (.getContactPoint result))
   1.131 +
   1.132 +
   1.133 +  )
   1.134 +
   1.135 +(defn touch-percieve [limit geom node debug-node]
   1.136 +  (let [normals  (normal-rays limit geom)]
   1.137 +    (.detachAllChildren debug-node)
   1.138 +    (.attachChild debug-node (normal-arrows geom))
   1.139 +    
   1.140 +    (println-repl "---------")
   1.141 +    (doall
   1.142 +     (for [ray normals]
   1.143 +       (do
   1.144 +         (let [results (CollisionResults.)]
   1.145 +           (.attachChild debug-node (ray-debug ray))
   1.146 +           (.collideWith geom ray results)
   1.147 +           
   1.148 +            ;;(println-repl (.size results) "results for " ray)
   1.149 +           ;;(doall (map (partial collision-debug node) results))
   1.150 +           (.size results)
   1.151 +           ))))))
   1.152 +
   1.153 +(defn arrow-view [obj]
   1.154 +  (view (doto (Node.)
   1.155 +          (.attachChild (normal-arrows obj))
   1.156 +          (.attachChild obj))))
   1.157 +
   1.158  
   1.159  (defn make-touch [#^Geometry geom]
   1.160    (let [tri (Triangle.)
   1.161 @@ -201,7 +320,17 @@
   1.162  	(.setBlendMode RenderState$BlendMode/Alpha))
   1.163      (.setQueueBucket RenderQueue$Bucket/Transparent)))
   1.164  
   1.165 -  
   1.166 +
   1.167 +(defn transparent-floor []
   1.168 +  (doto
   1.169 +      (box 5 0.2 5  :mass 0 :position (Vector3f. 0 -2 0)
   1.170 +           :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.171 +           :texture "Textures/redWisp.png")
   1.172 +    (-> (.getMaterial)
   1.173 +        (.getAdditionalRenderState)
   1.174 +        (.setBlendMode RenderState$BlendMode/Alpha))
   1.175 +    (.setQueueBucket RenderQueue$Bucket/Transparent)))
   1.176 +
   1.177  
   1.178  (defn no-logging []
   1.179    (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
   1.180 @@ -212,41 +341,39 @@
   1.181  
   1.182  (defn test-skin []
   1.183    (let [b
   1.184 -	;;(transparent-box)
   1.185 -	(transparent-sphere)
   1.186 -	
   1.187 -	controls
   1.188 +	;;(transparent-sphere)
   1.189 +	(transparent-box)
   1.190 +	f (transparent-floor)
   1.191 +	;;controls
   1.192  	;;(make-touch-sphere b)
   1.193 -	(make-touch b)
   1.194 +	;;(make-touch b)
   1.195 +        debug-node (Node.)
   1.196 +        node      (doto (Node.) (.attachChild b) (.attachChild f)
   1.197 +                        (.attachChild debug-node))
   1.198 +        
   1.199  	]
   1.200      
   1.201      (world
   1.202 -     (doto (Node.) (.attachChild b)
   1.203 -	   (.attachChild
   1.204 -	    (doto
   1.205 -		(box 5 0.2 5  :mass 0 :position (Vector3f. 0 -2 0)
   1.206 -		     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.207 -		     :texture "Textures/redWisp.png")
   1.208 -	      (-> (.getMaterial)
   1.209 -		  (.getAdditionalRenderState)
   1.210 -		  (.setBlendMode RenderState$BlendMode/Alpha))
   1.211 -	      (.setQueueBucket RenderQueue$Bucket/Transparent))))
   1.212       
   1.213 +     node
   1.214       {"key-return" (fire-cannon-ball)
   1.215        "key-space"  (fn [game value]
   1.216 -     		     (touch-print controls))}
   1.217 +      		    (println-repl (touch-percieve 1 b node debug-node)))
   1.218 +      }
   1.219 +     ;;no-op
   1.220       (fn [world]
   1.221 -       (Capture/SimpleCaptureVideo
   1.222 -        world 
   1.223 -        (file-str "/home/r/proj/cortex/tmp/blob.avi"))
   1.224 -       (no-logging)
   1.225 -       (enable-debug world)
   1.226 -       (set-accuracy world (/ 1 60))
   1.227 +     ;;  (Capture/SimpleCaptureVideo
   1.228 +     ;;   world 
   1.229 +     ;;   (file-str "/home/r/proj/cortex/tmp/blob.avi"))
   1.230 +     ;;  (no-logging)
   1.231 +     (enable-debug world)
   1.232 +     ;;  (set-accuracy world (/ 1 60))
   1.233         )
   1.234  
   1.235       (fn [& _]
   1.236 +       (Thread/sleep 10)
   1.237         ;;(touch-print controls)
   1.238 -       (color-touch controls)
   1.239 +       ;;(color-touch controls)
   1.240         ))))
   1.241  
   1.242  #+end_src
   1.243 @@ -257,10 +384,10 @@
   1.244  
   1.245  
   1.246  
   1.247 -
   1.248 +  
   1.249  
   1.250  * COMMENT code generation
   1.251 -#+begin_src clojure :tangle ../src/body/skin.clj
   1.252 +#+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
   1.253  <<skin-main>>
   1.254  #+end_src
   1.255