Mercurial > cortex
view org/skin.org @ 46:286a00500ee5
more work in blender
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 10 Nov 2011 23:24:02 -0700 |
parents | f080f1e49fba |
children | 00d0e1639d4b |
line wrap: on
line source
1 #+title: Simulated Sense of Touch2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org10 * Touch12 My creatures need to be able to feel their environments. The idea here13 is to create thousands of small /touch receptors/ along the geometries14 which make up the creature's body. The number of touch receptors in a15 given area is determined by how complicated that area is, as16 determined by the total number of triangles in that region. This way,17 complicated regions like the hands/face, etc. get more touch receptors18 than simpler areas of the body.20 #+srcname: skin-main21 #+begin_src clojure22 (ns cortex.touch23 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry24 to be outfitted with touch sensors with density proportional to the25 density of triangles along the surface of the Geometry. Enables a26 Geometry to know what parts of itself are touching nearby objects."27 {:author "Robert McIntyre"}28 (:use (cortex world util))29 (:import com.jme3.scene.Geometry)30 (:import com.jme3.collision.CollisionResults)31 (:import (com.jme3.math Triangle Vector3f Ray)))33 (defn triangles34 "Return a sequence of all the Triangles which compose a given35 Geometry."36 [#^Geometry geom]37 (let38 [mesh (.getMesh geom)39 triangles (transient [])]40 (dorun41 (for [n (range (.getTriangleCount mesh))]42 (let [tri (Triangle.)]43 (.getTriangle mesh n tri)44 ;; (.calculateNormal tri)45 ;; (.calculateCenter tri)46 (conj! triangles tri))))47 (persistent! triangles)))49 (defn get-ray-origin50 "Return the origin which a Ray would have to have to be in the exact51 center of a particular Triangle in the Geometry in World52 Coordinates."53 [geom tri]54 (let [new (Vector3f.)]55 (.calculateCenter tri)56 (.localToWorld geom (.getCenter tri) new) new))58 (defn get-ray-direction59 "Return the direction which a Ray would have to have to be in the60 exact center of a particular Triangle in the Geometry, pointing61 normal to the Triangle, in coordinates relative to the center of the62 Triangle."63 [geom tri]64 (let [n+c (Vector3f.)]65 (.calculateNormal tri)66 (.calculateCenter tri)67 (.localToWorld68 geom69 (.add (.getCenter tri) (.getNormal tri)) n+c)70 (.subtract n+c (get-ray-origin geom tri))))72 (defn normal-rays73 "For each Triangle which comprises the Geometry, returns a Ray which74 is centered on that Triangle, points outward in a normal direction,75 and extends for =limit= distance."76 [limit #^Geometry geom]77 (vec78 (map79 (fn [tri]80 (doto81 (Ray. (get-ray-origin geom tri)82 (get-ray-direction geom tri))83 (.setLimit limit)))84 (triangles geom))))86 (defn touch-percieve87 "Augment a Geometry with the sense of touch. Returns a sequence of88 non-negative integers, one for each triangle, with the value of the89 integer describing how many objects a ray of length =limit=, normal90 to the triangle and originating from its center, encountered. The91 Geometry itself is not counted among the results."92 [limit geom node]93 (let [normals (normal-rays limit geom)]94 (doall95 (for [ray normals]96 (do97 (let [results (CollisionResults.)]98 (.collideWith node ray results)99 (let [touch-objects100 (set (filter #(not (= geom %))101 (map #(.getGeometry %) results)))]102 (count touch-objects))))))))103 #+end_src106 * Example108 #+srcname: touch-test109 #+begin_src clojure110 (ns test.touch111 (:use (cortex import world util touch)))113 (cortex.import/mega-import-jme3)115 (use 'hello.brick-wall)117 (defn ray-origin-debug118 [ray color]119 (make-shape120 (assoc base-shape121 :shape (Sphere. 5 5 0.05)122 :name "arrow"123 :color color124 :texture false125 :physical? false126 :position127 (.getOrigin ray))))129 (defn ray-debug [ray color]130 (make-shape131 (assoc132 base-shape133 :name "debug-ray"134 :physical? false135 :shape (com.jme3.scene.shape.Line.136 (.getOrigin ray)137 (.add138 (.getOrigin ray)139 (.mult (.getDirection ray)140 (float (.getLimit ray))))))))143 (defn contact-color [contacts]144 (case contacts145 0 ColorRGBA/Gray146 1 ColorRGBA/Red147 2 ColorRGBA/Green148 3 ColorRGBA/Yellow149 4 ColorRGBA/Orange150 5 ColorRGBA/Red151 6 ColorRGBA/Magenta152 7 ColorRGBA/Pink153 8 ColorRGBA/White))155 (defn update-ray-debug [node ray contacts]156 (let [origin (.getChild node 0)]157 (.setLocalTranslation origin (.getOrigin ray))158 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))160 (defn init-node161 [debug-node rays]162 (println-repl "Init touch debug node.")163 (.detachAllChildren debug-node)164 (dorun165 (for [ray rays]166 (do167 (.attachChild168 debug-node169 (doto (Node.)170 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))171 (.attachChild (ray-debug ray ColorRGBA/Gray))172 ))))))174 (defn manage-ray-debug-node [debug-node geom touch-data limit]175 (let [rays (normal-rays limit geom)]176 (if (not= (count (.getChildren debug-node)) (count touch-data))177 (init-node debug-node rays))178 (dorun179 (for [n (range (count touch-data))]180 (update-ray-debug181 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))183 (defn transparent-sphere []184 (doto185 (make-shape186 (merge base-shape187 {:position (Vector3f. 0 2 0)188 :name "the blob."189 :material "Common/MatDefs/Misc/Unshaded.j3md"190 :texture "Textures/purpleWisp.png"191 :physical? true192 :mass 70193 :color ColorRGBA/Blue194 :shape (Sphere. 10 10 1)}))195 (-> (.getMaterial)196 (.getAdditionalRenderState)197 (.setBlendMode RenderState$BlendMode/Alpha))198 (.setQueueBucket RenderQueue$Bucket/Transparent)))200 (defn transparent-box []201 (doto202 (make-shape203 (merge base-shape204 {:position (Vector3f. 0 2 0)205 :name "box"206 :material "Common/MatDefs/Misc/Unshaded.j3md"207 :texture "Textures/purpleWisp.png"208 :physical? true209 :mass 70210 :color ColorRGBA/Blue211 :shape (Box. 1 1 1)}))212 (-> (.getMaterial)213 (.getAdditionalRenderState)214 (.setBlendMode RenderState$BlendMode/Alpha))215 (.setQueueBucket RenderQueue$Bucket/Transparent)))217 (defn transparent-floor []218 (doto219 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)220 :material "Common/MatDefs/Misc/Unshaded.j3md"221 :texture "Textures/redWisp.png"222 :name "floor")223 (-> (.getMaterial)224 (.getAdditionalRenderState)225 (.setBlendMode RenderState$BlendMode/Alpha))226 (.setQueueBucket RenderQueue$Bucket/Transparent)))228 (defn test-skin []229 (let [b230 ;;(transparent-box)231 (transparent-sphere)232 ;;(sphere)233 f (transparent-floor)234 debug-node (Node.)235 node (doto (Node.) (.attachChild b) (.attachChild f))236 root-node (doto (Node.) (.attachChild node)237 (.attachChild debug-node))238 ]240 (world241 root-node242 {"key-return" (fire-cannon-ball node)}243 (fn [world]244 ;; (Capture/SimpleCaptureVideo245 ;; world246 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))247 ;; (no-logging)248 ;;(enable-debug world)249 ;; (set-accuracy world (/ 1 60))250 )252 (fn [& _]253 (let [sensitivity 0.2254 touch-data (touch-percieve sensitivity b node)]255 (manage-ray-debug-node debug-node b touch-data sensitivity)256 )257 (Thread/sleep 10)258 ))))261 #+end_src267 * COMMENT code generation268 #+begin_src clojure :tangle ../src/cortex/touch.clj269 <<skin-main>>270 #+end_src272 #+begin_src clojure :tangle ../src/test/touch.clj273 <<touch-test>>274 #+end_src