# HG changeset patch # User Robert McIntyre # Date 1319354274 25200 # Node ID e3c6d1c1cb003626409f1bf7f9f0c0bc4634f28e # Parent 93ff2b4e7e6a416449a0d2d2f013c715e779dc20 fixing ray casting problem diff -r 93ff2b4e7e6a -r e3c6d1c1cb00 org/cortex.org --- a/org/cortex.org Sat Oct 22 01:20:34 2011 -0700 +++ b/org/cortex.org Sun Oct 23 00:17:54 2011 -0700 @@ -4,7 +4,7 @@ #+description: Simulating senses for AI research using JMonkeyEngine3 #+SETUPFILE: ../../aurellem/org/setup.org #+INCLUDE: ../../aurellem/org/level-0.org - +#+babel: :mkdirp yes :noweb yes * Background Artificial Intelligence has tried and failed for more than half a @@ -388,7 +388,7 @@ (.setFullscreen false) (.setTitle "Aurellem.") ;; disable 32 bit stuff for now - (.setAudioRenderer "Send") + ;;(.setAudioRenderer "Send") ) "These settings control how the game is displayed on the screen for debugging purposes. Use binding forms to change this if desired. @@ -597,20 +597,22 @@ (defn make-shape [#^shape-description d] - (let [mat (Material. (asset-manager) (:material d)) + (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager)) + mat (Material. asset-manager (:material d)) geom (Geometry. (:name d) (:shape d))] (if (:texture d) (let [key (TextureKey. (:texture d))] (.setGenerateMips key true) - (.setTexture mat "ColorMap" (.loadTexture (asset-manager) key)))) + (.setTexture mat "ColorMap" (.loadTexture asset-manager key)))) (if (:color d) (.setColor mat "Color" (:color d))) (.setMaterial geom mat) (if-let [rotation (:rotation d)] (.rotate geom rotation)) (.setLocalTranslation geom (:position d)) (if (:physical? d) - (let [impact-shape (doto (GImpactCollisionShape. (.getMesh geom)) (.setMargin 0)) + (let [impact-shape (doto (GImpactCollisionShape. + (.getMesh geom)) (.setMargin 0)) physics-control (RigidBodyControl. - impact-shape + ;;impact-shape ;; comment to disable (float (:mass d)))] (.createJmeMesh impact-shape) (.addControl geom physics-control) diff -r 93ff2b4e7e6a -r e3c6d1c1cb00 org/skin.org --- a/org/skin.org Sat Oct 22 01:20:34 2011 -0700 +++ b/org/skin.org Sun Oct 23 00:17:54 2011 -0700 @@ -4,7 +4,7 @@ #+description: Simulating touch in JMonkeyEngine #+SETUPFILE: ../../aurellem/org/setup.org #+INCLUDE: ../../aurellem/org/level-0.org - +#+babel: :mkdirp yes :noweb yes let's see what checkboxes look like: @@ -15,13 +15,6 @@ * skin! - - - - - - - #+srcname: skin-main #+begin_src clojure (ns body.skin) @@ -85,7 +78,133 @@ (.addControl geom control) (conj! controls! control))))) (persistent! controls!))) - + + +(defn triangles [#^Geometry geom] + (let + [mesh (.getMesh geom) + triangles (transient [])] + (dorun + (for [n (range (.getTriangleCount mesh))] + (let [tri (Triangle.)] + (.getTriangle mesh n tri) + (.calculateNormal tri) + (.calculateCenter tri) + (conj! triangles tri)))) + (persistent! triangles))) + + +(defn new-touch [#^Geometry geom] + (dorun (map + (comp no-op #(.getCenter %)) + (triangles geom)))) + + + + +(defn normal-arrows + "returns a node containing arrows which point + in the normal direction of each face of the + given Geometry" + [#^Geometry geom] + (let [node (Node.)] + (dorun + (map #(.attachChild node %) + (map + (fn [tri] + (make-shape + (assoc base-shape + :shape (Sphere. 5 5 0.05) + :name "arrow" + :color ColorRGBA/Orange + :texture false + :asset-manager (asset-manager) + :physical? false + ;;:rotation + ;;(.mult + ;; (doto (Quaternion.) + ;; (.lookAt (.getNormal tri) + ;; Vector3f/UNIT_Y)) + ;; (.getLocalRotation geom)) + :position + (.add + (.getCenter tri) + (.getLocalTranslation geom))))) + (triangles geom)))) + node)) + + +(defn get-ray-origin + [geom tri] + (.add + (.getCenter tri) + (.getLocalTranslation geom))) + +(defn get-ray-direction + [geom tri] + (.mult (.getLocalRotation geom) + (.getNormal tri))) + +(defn ray-debug [ray] + (make-shape + (assoc + base-shape + :name "debug-ray" + :physical? false + :shape (com.jme3.scene.shape.Line. + (.getOrigin ray) + (.add + (.getOrigin ray) + (.mult (.getDirection ray) + (float (.getLimit ray)))))))) + + + + +(defn normal-rays + "returns rays" + [limit #^Geometry geom] + (vec + (map + (fn [tri] + (doto + (Ray. (get-ray-origin geom tri) + (get-ray-direction geom tri)) + + (.setLimit limit))) + (triangles geom)))) + + +(defn collision-debug [node result] + + (println-repl "contact point: " (.getContactPoint result)) + + + ) + +(defn touch-percieve [limit geom node debug-node] + (let [normals (normal-rays limit geom)] + (.detachAllChildren debug-node) + (.attachChild debug-node (normal-arrows geom)) + + (println-repl "---------") + (doall + (for [ray normals] + (do + (let [results (CollisionResults.)] + (.attachChild debug-node (ray-debug ray)) + (.collideWith geom ray results) + + ;;(println-repl (.size results) "results for " ray) + ;;(doall (map (partial collision-debug node) results)) + (.size results) + )))))) + +(defn arrow-view [obj] + (view (doto (Node.) + (.attachChild (normal-arrows obj)) + (.attachChild obj)))) + (defn make-touch [#^Geometry geom] (let [tri (Triangle.) @@ -201,7 +320,17 @@ (.setBlendMode RenderState$BlendMode/Alpha)) (.setQueueBucket RenderQueue$Bucket/Transparent))) - + +(defn transparent-floor [] + (doto + (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0) + :material "Common/MatDefs/Misc/Unshaded.j3md" + :texture "Textures/redWisp.png") + (-> (.getMaterial) + (.getAdditionalRenderState) + (.setBlendMode RenderState$BlendMode/Alpha)) + (.setQueueBucket RenderQueue$Bucket/Transparent))) + (defn no-logging [] (.setLevel (Logger/getLogger "com.jme3") Level/OFF)) @@ -212,41 +341,39 @@ (defn test-skin [] (let [b - ;;(transparent-box) - (transparent-sphere) - - controls + ;;(transparent-sphere) + (transparent-box) + f (transparent-floor) + ;;controls ;;(make-touch-sphere b) - (make-touch b) + ;;(make-touch b) + debug-node (Node.) + node (doto (Node.) (.attachChild b) (.attachChild f) + (.attachChild debug-node)) + ] (world - (doto (Node.) (.attachChild b) - (.attachChild - (doto - (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0) - :material "Common/MatDefs/Misc/Unshaded.j3md" - :texture "Textures/redWisp.png") - (-> (.getMaterial) - (.getAdditionalRenderState) - (.setBlendMode RenderState$BlendMode/Alpha)) - (.setQueueBucket RenderQueue$Bucket/Transparent)))) + node {"key-return" (fire-cannon-ball) "key-space" (fn [game value] - (touch-print controls))} + (println-repl (touch-percieve 1 b node debug-node))) + } + ;;no-op (fn [world] - (Capture/SimpleCaptureVideo - world - (file-str "/home/r/proj/cortex/tmp/blob.avi")) - (no-logging) - (enable-debug world) - (set-accuracy world (/ 1 60)) + ;; (Capture/SimpleCaptureVideo + ;; world + ;; (file-str "/home/r/proj/cortex/tmp/blob.avi")) + ;; (no-logging) + (enable-debug world) + ;; (set-accuracy world (/ 1 60)) ) (fn [& _] + (Thread/sleep 10) ;;(touch-print controls) - (color-touch controls) + ;;(color-touch controls) )))) #+end_src @@ -257,10 +384,10 @@ - + * COMMENT code generation -#+begin_src clojure :tangle ../src/body/skin.clj +#+begin_src clojure :tangle ../src/body/skin.clj :noweb yes <> #+end_src