view org/skin.org @ 44:f3e33fd5f97d

working through blender tutorial
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Nov 2011 01:38:22 -0700
parents 117eb477d0a7
children f080f1e49fba
line wrap: on
line source
1 #+title: Simulated Sense of Touch
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.
5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
10 * Touch
12 My creatures need to be able to feel their environments. The idea here
13 is to create thousands of small /touch receptors/ along the geometries
14 which make up the creature's body. The number of touch receptors in a
15 given area is determined by how complicated that area is, as
16 determined by the total number of triangles in that region. This way,
17 complicated regions like the hands/face, etc. get more touch receptors
18 than simpler areas of the body.
20 #+srcname: skin-main
21 #+begin_src clojure
22 (ns cortex.touch
23 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
24 to be outfitted with touch sensors with density proportional to the
25 density of triangles along the surface of the Geometry. Enables a
26 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 triangles
34 "Return a sequence of all the Triangles which compose a given
35 Geometry."
36 [#^Geometry geom]
37 (let
38 [mesh (.getMesh geom)
39 triangles (transient [])]
40 (dorun
41 (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-origin
50 "Return the origin which a Ray would have to have to be in the exact
51 center of a particular Triangle in the Geometry in World
52 Coordinates."
53 [geom tri]
54 (let [new (Vector3f.)]
55 (.calculateCenter tri)
56 (.localToWorld geom (.getCenter tri) new) new))
58 (defn get-ray-direction
59 "Return the direction which a Ray would have to have to be in the
60 exact center of a particular Triangle in the Geometry, pointing
61 normal to the Triangle, in coordinates relative to the center of the
62 Triangle."
63 [geom tri]
64 (let [n+c (Vector3f.)]
65 (.calculateNormal tri)
66 (.calculateCenter tri)
67 (.localToWorld
68 geom
69 (.add (.getCenter tri) (.getNormal tri)) n+c)
70 (.subtract n+c (get-ray-origin geom tri))))
72 (defn normal-rays
73 "For each Triangle which comprises the Geometry, returns a Ray which
74 is centered on that Triangle, points outward in a normal direction,
75 and extends for =limit= distance."
76 [limit #^Geometry geom]
77 (vec
78 (map
79 (fn [tri]
80 (doto
81 (Ray. (get-ray-origin geom tri)
82 (get-ray-direction geom tri))
83 (.setLimit limit)))
84 (triangles geom))))
86 (defn touch-percieve
87 "Augment a Geometry with the sense of touch. Returns a sequence of
88 non-negative integers, one for each triangle, with the value of the
89 integer describing how many objects a ray of length =limit=, normal
90 to the triangle and originating from its center, encountered. The
91 Geometry itself is not counted among the results."
92 [limit geom node]
93 (let [normals (normal-rays limit geom)]
94 (doall
95 (for [ray normals]
96 (do
97 (let [results (CollisionResults.)]
98 (.collideWith node ray results)
99 (let [touch-objects
100 (set (filter #(not (= geom %))
101 (map #(.getGeometry %) results)))]
102 (count touch-objects))))))))
103 #+end_src
106 * Example
108 #+srcname: touch-test
109 #+begin_src clojure
110 (ns test.touch
111 (:use (cortex world util touch)))
113 (cortex.import/mega-import-jme3)
115 (use 'hello.brick-wall)
117 (defn ray-origin-debug
118 [ray color]
119 (make-shape
120 (assoc base-shape
121 :shape (Sphere. 5 5 0.05)
122 :name "arrow"
123 :color color
124 :texture false
125 :physical? false
126 :position
127 (.getOrigin ray))))
129 (defn ray-debug [ray color]
130 (make-shape
131 (assoc
132 base-shape
133 :name "debug-ray"
134 :physical? false
135 :shape (com.jme3.scene.shape.Line.
136 (.getOrigin ray)
137 (.add
138 (.getOrigin ray)
139 (.mult (.getDirection ray)
140 (float (.getLimit ray))))))))
143 (defn contact-color [contacts]
144 (case contacts
145 0 ColorRGBA/Gray
146 1 ColorRGBA/Red
147 2 ColorRGBA/Green
148 3 ColorRGBA/Yellow
149 4 ColorRGBA/Orange
150 5 ColorRGBA/Red
151 6 ColorRGBA/Magenta
152 7 ColorRGBA/Pink
153 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-node
161 [debug-node rays]
162 (println-repl "Init touch debug node.")
163 (.detachAllChildren debug-node)
164 (dorun
165 (for [ray rays]
166 (do
167 (.attachChild
168 debug-node
169 (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 (dorun
179 (for [n (range (count touch-data))]
180 (update-ray-debug
181 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
183 (defn transparent-sphere []
184 (doto
185 (make-shape
186 (merge base-shape
187 {:position (Vector3f. 0 2 0)
188 :name "the blob."
189 :material "Common/MatDefs/Misc/Unshaded.j3md"
190 :texture "Textures/purpleWisp.png"
191 :physical? true
192 :mass 70
193 :color ColorRGBA/Blue
194 :shape (Sphere. 10 10 1)}))
195 (-> (.getMaterial)
196 (.getAdditionalRenderState)
197 (.setBlendMode RenderState$BlendMode/Alpha))
198 (.setQueueBucket RenderQueue$Bucket/Transparent)))
200 (defn transparent-box []
201 (doto
202 (make-shape
203 (merge base-shape
204 {:position (Vector3f. 0 2 0)
205 :name "box"
206 :material "Common/MatDefs/Misc/Unshaded.j3md"
207 :texture "Textures/purpleWisp.png"
208 :physical? true
209 :mass 70
210 :color ColorRGBA/Blue
211 :shape (Box. 1 1 1)}))
212 (-> (.getMaterial)
213 (.getAdditionalRenderState)
214 (.setBlendMode RenderState$BlendMode/Alpha))
215 (.setQueueBucket RenderQueue$Bucket/Transparent)))
217 (defn transparent-floor []
218 (doto
219 (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 [b
230 ;;(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 (world
241 root-node
242 {"key-return" (fire-cannon-ball node)}
243 (fn [world]
244 ;; (Capture/SimpleCaptureVideo
245 ;; world
246 ;; (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.2
254 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_src
267 * COMMENT code generation
268 #+begin_src clojure :tangle ../src/cortex/touch.clj
269 <<skin-main>>
270 #+end_src
272 #+begin_src clojure :tangle ../src/test/touch.clj
273 <<touch-test>>
274 #+end_src