changeset 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
files org/cortex.org org/skin.org
diffstat 2 files changed, 170 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/cortex.org	Sat Oct 22 01:20:34 2011 -0700
     1.2 +++ b/org/cortex.org	Sun Oct 23 00:17:54 2011 -0700
     1.3 @@ -4,7 +4,7 @@
     1.4  #+description: Simulating senses for AI research using JMonkeyEngine3
     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  * Background
    1.11  Artificial Intelligence has tried and failed for more than half a
    1.12 @@ -388,7 +388,7 @@
    1.13      (.setFullscreen false)
    1.14      (.setTitle "Aurellem.")
    1.15      ;; disable 32 bit stuff for now
    1.16 -    (.setAudioRenderer "Send")
    1.17 +    ;;(.setAudioRenderer "Send")
    1.18      )
    1.19    "These settings control how the game is displayed on the screen for
    1.20     debugging purposes.  Use binding forms to change this if desired.
    1.21 @@ -597,20 +597,22 @@
    1.22  
    1.23  (defn make-shape
    1.24    [#^shape-description d]
    1.25 -  (let [mat (Material. (asset-manager) (:material d))
    1.26 +  (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
    1.27 +        mat (Material. asset-manager (:material d))
    1.28  	geom (Geometry. (:name d) (:shape d))]
    1.29      (if (:texture d)
    1.30        (let [key (TextureKey. (:texture d))]
    1.31  	(.setGenerateMips key true)
    1.32 -	(.setTexture mat "ColorMap" (.loadTexture (asset-manager) key))))
    1.33 +	(.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
    1.34      (if (:color d) (.setColor mat "Color" (:color d)))
    1.35      (.setMaterial geom mat)
    1.36      (if-let [rotation (:rotation d)] (.rotate geom rotation))
    1.37      (.setLocalTranslation geom (:position d))
    1.38      (if (:physical? d)
    1.39 -      (let [impact-shape (doto (GImpactCollisionShape. (.getMesh geom)) (.setMargin 0))
    1.40 +      (let [impact-shape (doto (GImpactCollisionShape. 
    1.41 +                                (.getMesh geom)) (.setMargin 0))
    1.42  	    physics-control (RigidBodyControl.
    1.43 -			     impact-shape 
    1.44 +			     ;;impact-shape ;; comment to disable 
    1.45  			     (float (:mass d)))]
    1.46  	(.createJmeMesh impact-shape)
    1.47  	(.addControl geom physics-control)
     2.1 --- a/org/skin.org	Sat Oct 22 01:20:34 2011 -0700
     2.2 +++ b/org/skin.org	Sun Oct 23 00:17:54 2011 -0700
     2.3 @@ -4,7 +4,7 @@
     2.4  #+description: Simulating touch in JMonkeyEngine
     2.5  #+SETUPFILE: ../../aurellem/org/setup.org
     2.6  #+INCLUDE: ../../aurellem/org/level-0.org
     2.7 -
     2.8 +#+babel: :mkdirp yes :noweb yes
     2.9  
    2.10  let's see what checkboxes look like:
    2.11   
    2.12 @@ -15,13 +15,6 @@
    2.13  
    2.14  * skin!
    2.15  
    2.16 -
    2.17 -  
    2.18 -
    2.19 -
    2.20 -
    2.21 -
    2.22 -
    2.23  #+srcname: skin-main
    2.24  #+begin_src clojure
    2.25  (ns body.skin)
    2.26 @@ -85,7 +78,133 @@
    2.27  	   (.addControl geom control)
    2.28  	   (conj! controls! control)))))
    2.29      (persistent! controls!)))
    2.30 -	 
    2.31 +
    2.32 +
    2.33 +(defn triangles [#^Geometry geom]
    2.34 +  (let
    2.35 +      [mesh (.getMesh geom)
    2.36 +       triangles (transient [])]
    2.37 +    (dorun
    2.38 +     (for [n (range (.getTriangleCount mesh))]
    2.39 +       (let [tri (Triangle.)]
    2.40 +         (.getTriangle mesh n tri)
    2.41 +         (.calculateNormal tri)
    2.42 +         (.calculateCenter tri)
    2.43 +         (conj! triangles tri))))
    2.44 +    (persistent! triangles)))
    2.45 +   
    2.46 +
    2.47 +(defn new-touch [#^Geometry geom]
    2.48 +  (dorun (map
    2.49 +          (comp no-op #(.getCenter %))
    2.50 +              (triangles geom))))
    2.51 +
    2.52 +
    2.53 +
    2.54 +        
    2.55 +(defn normal-arrows
    2.56 +  "returns a node containing arrows which point
    2.57 +   in the normal direction of each face of the
    2.58 +   given Geometry"
    2.59 +  [#^Geometry geom]
    2.60 +  (let [node (Node.)]
    2.61 +    (dorun
    2.62 +     (map #(.attachChild node %)
    2.63 +          (map 
    2.64 +           (fn [tri]
    2.65 +             (make-shape
    2.66 +              (assoc base-shape
    2.67 +                :shape (Sphere. 5 5 0.05)
    2.68 +                :name "arrow"
    2.69 +                :color ColorRGBA/Orange
    2.70 +                :texture false
    2.71 +                :asset-manager (asset-manager)
    2.72 +                :physical? false
    2.73 +                ;;:rotation
    2.74 +                ;;(.mult
    2.75 +                ;; (doto (Quaternion.)
    2.76 +                ;;   (.lookAt (.getNormal tri)
    2.77 +                ;;            Vector3f/UNIT_Y))
    2.78 +                ;; (.getLocalRotation geom))
    2.79 +                :position
    2.80 +                (.add
    2.81 +                 (.getCenter tri)
    2.82 +                 (.getLocalTranslation geom)))))
    2.83 +           (triangles geom))))
    2.84 +    node))
    2.85 +
    2.86 +
    2.87 +(defn get-ray-origin
    2.88 +  [geom tri]
    2.89 +  (.add
    2.90 +   (.getCenter tri)
    2.91 +   (.getLocalTranslation geom)))
    2.92 +
    2.93 +(defn get-ray-direction
    2.94 +  [geom tri]
    2.95 +  (.mult (.getLocalRotation geom)
    2.96 +         (.getNormal tri)))
    2.97 +
    2.98 +(defn ray-debug [ray]
    2.99 +  (make-shape
   2.100 +   (assoc
   2.101 +       base-shape
   2.102 +     :name "debug-ray"
   2.103 +     :physical? false
   2.104 +     :shape (com.jme3.scene.shape.Line.
   2.105 +             (.getOrigin ray)
   2.106 +             (.add
   2.107 +              (.getOrigin ray)
   2.108 +              (.mult (.getDirection ray)
   2.109 +                     (float (.getLimit ray))))))))
   2.110 +             
   2.111 +                               
   2.112 +
   2.113 +
   2.114 +(defn normal-rays
   2.115 +  "returns rays"
   2.116 +  [limit #^Geometry geom]
   2.117 +  (vec
   2.118 +   (map 
   2.119 +    (fn [tri]
   2.120 +      (doto 
   2.121 +          (Ray. (get-ray-origin geom tri)
   2.122 +                (get-ray-direction geom tri))
   2.123 +                
   2.124 +      (.setLimit limit)))
   2.125 +    (triangles geom))))
   2.126 +
   2.127 +
   2.128 +(defn collision-debug [node result]
   2.129 +
   2.130 +  (println-repl "contact point: " (.getContactPoint result))
   2.131 +
   2.132 +
   2.133 +  )
   2.134 +
   2.135 +(defn touch-percieve [limit geom node debug-node]
   2.136 +  (let [normals  (normal-rays limit geom)]
   2.137 +    (.detachAllChildren debug-node)
   2.138 +    (.attachChild debug-node (normal-arrows geom))
   2.139 +    
   2.140 +    (println-repl "---------")
   2.141 +    (doall
   2.142 +     (for [ray normals]
   2.143 +       (do
   2.144 +         (let [results (CollisionResults.)]
   2.145 +           (.attachChild debug-node (ray-debug ray))
   2.146 +           (.collideWith geom ray results)
   2.147 +           
   2.148 +            ;;(println-repl (.size results) "results for " ray)
   2.149 +           ;;(doall (map (partial collision-debug node) results))
   2.150 +           (.size results)
   2.151 +           ))))))
   2.152 +
   2.153 +(defn arrow-view [obj]
   2.154 +  (view (doto (Node.)
   2.155 +          (.attachChild (normal-arrows obj))
   2.156 +          (.attachChild obj))))
   2.157 +
   2.158  
   2.159  (defn make-touch [#^Geometry geom]
   2.160    (let [tri (Triangle.)
   2.161 @@ -201,7 +320,17 @@
   2.162  	(.setBlendMode RenderState$BlendMode/Alpha))
   2.163      (.setQueueBucket RenderQueue$Bucket/Transparent)))
   2.164  
   2.165 -  
   2.166 +
   2.167 +(defn transparent-floor []
   2.168 +  (doto
   2.169 +      (box 5 0.2 5  :mass 0 :position (Vector3f. 0 -2 0)
   2.170 +           :material "Common/MatDefs/Misc/Unshaded.j3md"
   2.171 +           :texture "Textures/redWisp.png")
   2.172 +    (-> (.getMaterial)
   2.173 +        (.getAdditionalRenderState)
   2.174 +        (.setBlendMode RenderState$BlendMode/Alpha))
   2.175 +    (.setQueueBucket RenderQueue$Bucket/Transparent)))
   2.176 +
   2.177  
   2.178  (defn no-logging []
   2.179    (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
   2.180 @@ -212,41 +341,39 @@
   2.181  
   2.182  (defn test-skin []
   2.183    (let [b
   2.184 -	;;(transparent-box)
   2.185 -	(transparent-sphere)
   2.186 -	
   2.187 -	controls
   2.188 +	;;(transparent-sphere)
   2.189 +	(transparent-box)
   2.190 +	f (transparent-floor)
   2.191 +	;;controls
   2.192  	;;(make-touch-sphere b)
   2.193 -	(make-touch b)
   2.194 +	;;(make-touch b)
   2.195 +        debug-node (Node.)
   2.196 +        node      (doto (Node.) (.attachChild b) (.attachChild f)
   2.197 +                        (.attachChild debug-node))
   2.198 +        
   2.199  	]
   2.200      
   2.201      (world
   2.202 -     (doto (Node.) (.attachChild b)
   2.203 -	   (.attachChild
   2.204 -	    (doto
   2.205 -		(box 5 0.2 5  :mass 0 :position (Vector3f. 0 -2 0)
   2.206 -		     :material "Common/MatDefs/Misc/Unshaded.j3md"
   2.207 -		     :texture "Textures/redWisp.png")
   2.208 -	      (-> (.getMaterial)
   2.209 -		  (.getAdditionalRenderState)
   2.210 -		  (.setBlendMode RenderState$BlendMode/Alpha))
   2.211 -	      (.setQueueBucket RenderQueue$Bucket/Transparent))))
   2.212       
   2.213 +     node
   2.214       {"key-return" (fire-cannon-ball)
   2.215        "key-space"  (fn [game value]
   2.216 -     		     (touch-print controls))}
   2.217 +      		    (println-repl (touch-percieve 1 b node debug-node)))
   2.218 +      }
   2.219 +     ;;no-op
   2.220       (fn [world]
   2.221 -       (Capture/SimpleCaptureVideo
   2.222 -        world 
   2.223 -        (file-str "/home/r/proj/cortex/tmp/blob.avi"))
   2.224 -       (no-logging)
   2.225 -       (enable-debug world)
   2.226 -       (set-accuracy world (/ 1 60))
   2.227 +     ;;  (Capture/SimpleCaptureVideo
   2.228 +     ;;   world 
   2.229 +     ;;   (file-str "/home/r/proj/cortex/tmp/blob.avi"))
   2.230 +     ;;  (no-logging)
   2.231 +     (enable-debug world)
   2.232 +     ;;  (set-accuracy world (/ 1 60))
   2.233         )
   2.234  
   2.235       (fn [& _]
   2.236 +       (Thread/sleep 10)
   2.237         ;;(touch-print controls)
   2.238 -       (color-touch controls)
   2.239 +       ;;(color-touch controls)
   2.240         ))))
   2.241  
   2.242  #+end_src
   2.243 @@ -257,10 +384,10 @@
   2.244  
   2.245  
   2.246  
   2.247 -
   2.248 +  
   2.249  
   2.250  * COMMENT code generation
   2.251 -#+begin_src clojure :tangle ../src/body/skin.clj
   2.252 +#+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
   2.253  <<skin-main>>
   2.254  #+end_src
   2.255