rlm@37
|
1 #+title: Simulated Sense of Touch
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@37
|
4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.
|
rlm@37
|
5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure
|
rlm@4
|
6 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
7 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@0
|
8
|
rlm@229
|
9
|
rlm@229
|
10
|
rlm@37
|
11 * Touch
|
rlm@0
|
12
|
rlm@226
|
13 Touch is critical to navigation and spatial reasoning and as such I
|
rlm@226
|
14 need a simulated version of it to give to my AI creatures.
|
rlm@0
|
15
|
rlm@228
|
16 However, touch in my virtual can not exactly correspond to human touch
|
rlm@228
|
17 because my creatures are made out of completely rigid segments that
|
rlm@228
|
18 don't deform like human skin.
|
rlm@228
|
19
|
rlm@228
|
20 Human skin has a wide array of touch sensors, each of which speciliaze
|
rlm@228
|
21 in detecting different vibrational modes and pressures. These sensors
|
rlm@228
|
22 can integrate a vast expanse of skin (i.e. your entire palm), or a
|
rlm@228
|
23 tiny patch of skin at the tip of your finger. The hairs of the skin
|
rlm@228
|
24 help detect objects before they even come into contact with the skin
|
rlm@228
|
25 proper.
|
rlm@228
|
26
|
rlm@228
|
27 Instead of measuring deformation or vibration, I surround each rigid
|
rlm@228
|
28 part with a plenitude of hair-like objects which do not interact with
|
rlm@228
|
29 the physical world. Physical objects can pass through them with no
|
rlm@228
|
30 effect. The hairs are able to measure contact with other objects, and
|
rlm@228
|
31 constantly report how much of their extent is covered. So, even though
|
rlm@228
|
32 the creature's body parts do not deform, the hairs create a margin
|
rlm@228
|
33 around those body parts which achieves a sense of touch which is a
|
rlm@228
|
34 hybrid between a human's sense of deformation and sense from hairs.
|
rlm@228
|
35
|
rlm@228
|
36 Implementing touch in jMonkeyEngine follows a different techinal route
|
rlm@228
|
37 than vision and hearing. Those two senses piggybacked off
|
rlm@228
|
38 jMonkeyEngine's 3D audio and video rendering subsystems. To simulate
|
rlm@228
|
39 Touch, I use jMonkeyEngine's physics system to execute many small
|
rlm@229
|
40 collision detections, one for each "hair". The placement of the
|
rlm@229
|
41 "hairs" is determined by a UV-mapped image which shows where each hair
|
rlm@229
|
42 should be on the 3D surface of the body.
|
rlm@228
|
43
|
rlm@229
|
44
|
rlm@229
|
45 * Defining Touch Meta-Data in Blender
|
rlm@229
|
46
|
rlm@229
|
47 Each geometry can have a single UV map which describes the position
|
rlm@229
|
48 and length of the "hairs" which will constitute its sense of
|
rlm@229
|
49 touch. This image path is stored under the "touch" key. The image
|
rlm@229
|
50 itself is grayscale, with black meaning a hair length of 0 (no hair is
|
rlm@229
|
51 present) and white meaning a hair length of =scale=, which is a float
|
rlm@229
|
52 stored under the key "scale". If the pixel is gray then the resultant
|
rlm@238
|
53 hair length is linearly interpolated between 0 and =scale=. I call
|
rlm@238
|
54 these "hairs" /feelers/.
|
rlm@229
|
55
|
rlm@231
|
56 #+name: meta-data
|
rlm@0
|
57 #+begin_src clojure
|
rlm@229
|
58 (defn tactile-sensor-profile
|
rlm@229
|
59 "Return the touch-sensor distribution image in BufferedImage format,
|
rlm@229
|
60 or nil if it does not exist."
|
rlm@229
|
61 [#^Geometry obj]
|
rlm@229
|
62 (if-let [image-path (meta-data obj "touch")]
|
rlm@229
|
63 (load-image image-path)))
|
rlm@233
|
64
|
rlm@233
|
65 (defn tactile-scale
|
rlm@233
|
66 "Return the maximum length of a hair. All hairs are scalled between
|
rlm@233
|
67 0.0 and this length, depending on their color. Black is 0, and
|
rlm@233
|
68 white is maximum length, and everything in between is scalled
|
rlm@233
|
69 linearlly. Default scale is 0.01 jMonkeyEngine units."
|
rlm@233
|
70 [#^Geometry obj]
|
rlm@233
|
71 (if-let [scale (meta-data obj "scale")]
|
rlm@233
|
72 scale 0.1))
|
rlm@228
|
73 #+end_src
|
rlm@156
|
74
|
rlm@229
|
75 ** TODO add image showing example touch-uv map
|
rlm@229
|
76 ** TODO add metadata display for worm
|
rlm@229
|
77
|
rlm@234
|
78
|
rlm@233
|
79 * Skin Creation
|
rlm@243
|
80 * TODO get the actual lengths for each feeler
|
rlm@234
|
81
|
rlm@238
|
82
|
rlm@238
|
83 =(touch-kernel)= generates the functions which implement the sense of
|
rlm@238
|
84 touch for a creature. These functions must do 6 things to obtain touch
|
rlm@238
|
85 data.
|
rlm@238
|
86
|
rlm@238
|
87 - Get the tactile profile image and scale paramaters which describe
|
rlm@238
|
88 the layout of feelers along the object's surface.
|
rlm@239
|
89 =(tactile-sensor-profile)=, =(tactile-scale)=
|
rlm@239
|
90
|
rlm@238
|
91 - Get the lengths of each feeler by analyzing the color of the
|
rlm@238
|
92 pixels in the tactile profile image.
|
rlm@239
|
93 NOT IMPLEMENTED YET
|
rlm@239
|
94
|
rlm@238
|
95 - Find the triangles which make up the mesh in pixel-space and in
|
rlm@238
|
96 world-space.
|
rlm@239
|
97 =(triangles)= =(pixel-triangles)=
|
rlm@239
|
98
|
rlm@239
|
99 - Find the coordinates of each pixel in pixel space. These
|
rlm@239
|
100 coordinates are used to make the touch-topology.
|
rlm@240
|
101 =(feeler-pixel-coords)=
|
rlm@239
|
102
|
rlm@238
|
103 - Find the coordinates of each pixel in world-space. These
|
rlm@240
|
104 coordinates are the origins of the feelers. =(feeler-origins)=
|
rlm@239
|
105
|
rlm@238
|
106 - Calculate the normals of the triangles in world space, and add
|
rlm@238
|
107 them to each of the origins of the feelers. These are the
|
rlm@238
|
108 normalized coordinates of the tips of the feelers.
|
rlm@240
|
109 For both of these, =(feeler-tips)=
|
rlm@239
|
110
|
rlm@238
|
111 - Generate some sort of topology for the sensors.
|
rlm@239
|
112 =(touch-topology)=
|
rlm@239
|
113
|
rlm@238
|
114
|
rlm@233
|
115 #+name: kernel
|
rlm@233
|
116 #+begin_src clojure
|
rlm@233
|
117 (in-ns 'cortex.touch)
|
rlm@233
|
118
|
rlm@239
|
119 (declare touch-topology feelers set-ray)
|
rlm@234
|
120
|
rlm@243
|
121 (defn set-ray [#^Ray ray #^Matrix4f transform #^Vector3f origin
|
rlm@243
|
122 #^Vector3f tip length]
|
rlm@243
|
123 ;; Doing everything locally recduces garbage collection by enough to
|
rlm@243
|
124 ;; be worth it.
|
rlm@243
|
125 (.mult transform origin (.getOrigin ray))
|
rlm@243
|
126
|
rlm@243
|
127 (.mult transform tip (.getDirection ray))
|
rlm@243
|
128 (.subtractLocal (.getDirection ray) (.getOrigin ray))
|
rlm@243
|
129 (.setLimit ray length))
|
rlm@242
|
130
|
rlm@233
|
131 (defn touch-kernel
|
rlm@234
|
132 "Constructs a function which will return tactile sensory data from
|
rlm@234
|
133 'geo when called from inside a running simulation"
|
rlm@234
|
134 [#^Geometry geo]
|
rlm@243
|
135 (if-let
|
rlm@243
|
136 [profile (tactile-sensor-profile geo)]
|
rlm@243
|
137 (let [ray-reference-origins (feeler-origins geo profile)
|
rlm@243
|
138 ray-reference-tips (feeler-tips geo profile)
|
rlm@243
|
139 ray-lengths (repeat 9000 0.1)
|
rlm@243
|
140 current-rays (map (fn [_] (Ray.)) ray-reference-origins)
|
rlm@243
|
141 topology (touch-topology geo profile)]
|
rlm@233
|
142 (fn [node]
|
rlm@243
|
143 (let [transform (.getWorldMatrix geo)]
|
rlm@243
|
144 (dorun
|
rlm@243
|
145 (map (fn [ray ref-origin ref-tip length]
|
rlm@243
|
146 (set-ray ray transform ref-origin ref-tip length))
|
rlm@243
|
147 current-rays ray-reference-origins
|
rlm@243
|
148 ray-reference-tips ray-lengths))
|
rlm@233
|
149 (vector
|
rlm@243
|
150 topology
|
rlm@233
|
151 (vec
|
rlm@243
|
152 (for [ray current-rays]
|
rlm@233
|
153 (do
|
rlm@233
|
154 (let [results (CollisionResults.)]
|
rlm@233
|
155 (.collideWith node ray results)
|
rlm@233
|
156 (let [touch-objects
|
rlm@233
|
157 (filter #(not (= geo (.getGeometry %)))
|
rlm@233
|
158 results)]
|
rlm@233
|
159 [(if (empty? touch-objects)
|
rlm@243
|
160 (.getLimit ray)
|
rlm@243
|
161 (.getDistance (first touch-objects)))
|
rlm@243
|
162 (.getLimit ray)])))))))))))
|
rlm@233
|
163
|
rlm@233
|
164 (defn touch!
|
rlm@233
|
165 "Endow the creature with the sense of touch. Returns a sequence of
|
rlm@233
|
166 functions, one for each body part with a tactile-sensor-proile,
|
rlm@233
|
167 each of which when called returns sensory data for that body part."
|
rlm@233
|
168 [#^Node creature]
|
rlm@233
|
169 (filter
|
rlm@233
|
170 (comp not nil?)
|
rlm@233
|
171 (map touch-kernel
|
rlm@233
|
172 (filter #(isa? (class %) Geometry)
|
rlm@233
|
173 (node-seq creature)))))
|
rlm@233
|
174 #+end_src
|
rlm@233
|
175
|
rlm@243
|
176 #+results: kernel
|
rlm@243
|
177 : #'cortex.touch/touch!
|
rlm@243
|
178
|
rlm@238
|
179 * Sensor Related Functions
|
rlm@238
|
180
|
rlm@238
|
181 These functions analyze the touch-sensor-profile image convert the
|
rlm@238
|
182 location of each touch sensor from pixel coordinates to UV-coordinates
|
rlm@238
|
183 and XYZ-coordinates.
|
rlm@238
|
184
|
rlm@238
|
185 #+name: sensors
|
rlm@238
|
186 #+begin_src clojure
|
rlm@240
|
187 (in-ns 'cortex.touch)
|
rlm@240
|
188
|
rlm@240
|
189 (defn feeler-pixel-coords
|
rlm@239
|
190 "Returns the coordinates of the feelers in pixel space in lists, one
|
rlm@239
|
191 list for each triangle, ordered in the same way as (triangles) and
|
rlm@239
|
192 (pixel-triangles)."
|
rlm@239
|
193 [#^Geometry geo image]
|
rlm@240
|
194 (map
|
rlm@240
|
195 (fn [pixel-triangle]
|
rlm@240
|
196 (filter
|
rlm@240
|
197 (fn [coord]
|
rlm@240
|
198 (inside-triangle? (->triangle pixel-triangle)
|
rlm@240
|
199 (->vector3f coord)))
|
rlm@240
|
200 (white-coordinates image (convex-bounds pixel-triangle))))
|
rlm@240
|
201 (pixel-triangles geo image)))
|
rlm@239
|
202
|
rlm@242
|
203 (defn feeler-world-coords [#^Geometry geo image]
|
rlm@240
|
204 (let [transforms
|
rlm@240
|
205 (map #(triangles->affine-transform
|
rlm@240
|
206 (->triangle %1) (->triangle %2))
|
rlm@240
|
207 (pixel-triangles geo image)
|
rlm@240
|
208 (triangles geo))]
|
rlm@242
|
209 (map (fn [transform coords]
|
rlm@240
|
210 (map #(.mult transform (->vector3f %)) coords))
|
rlm@240
|
211 transforms (feeler-pixel-coords geo image))))
|
rlm@239
|
212
|
rlm@242
|
213 (defn feeler-origins [#^Geometry geo image]
|
rlm@242
|
214 (reduce concat (feeler-world-coords geo image)))
|
rlm@242
|
215
|
rlm@240
|
216 (defn feeler-tips [#^Geometry geo image]
|
rlm@242
|
217 (let [world-coords (feeler-world-coords geo image)
|
rlm@241
|
218 normals
|
rlm@241
|
219 (map
|
rlm@241
|
220 (fn [triangle]
|
rlm@241
|
221 (.calculateNormal triangle)
|
rlm@241
|
222 (.clone (.getNormal triangle)))
|
rlm@241
|
223 (map ->triangle (triangles geo)))]
|
rlm@242
|
224
|
rlm@242
|
225 (mapcat (fn [origins normal]
|
rlm@242
|
226 (map #(.add % normal) origins))
|
rlm@242
|
227 world-coords normals)))
|
rlm@242
|
228
|
rlm@241
|
229
|
rlm@241
|
230 (defn touch-topology [#^Geometry geo image]
|
rlm@243
|
231 (collapse (reduce concat (feeler-pixel-coords geo image))))
|
rlm@238
|
232 #+end_src
|
rlm@238
|
233
|
rlm@233
|
234 * Visualizing Touch
|
rlm@233
|
235 #+name: visualization
|
rlm@233
|
236 #+begin_src clojure
|
rlm@233
|
237 (in-ns 'cortex.touch)
|
rlm@233
|
238
|
rlm@233
|
239 (defn touch->gray
|
rlm@233
|
240 "Convert a pair of [distance, max-distance] into a grayscale pixel"
|
rlm@233
|
241 [distance max-distance]
|
rlm@233
|
242 (gray
|
rlm@233
|
243 (- 255
|
rlm@233
|
244 (rem
|
rlm@233
|
245 (int
|
rlm@233
|
246 (* 255 (/ distance max-distance)))
|
rlm@233
|
247 256))))
|
rlm@233
|
248
|
rlm@233
|
249 (defn view-touch
|
rlm@233
|
250 "Creates a function which accepts a list of touch sensor-data and
|
rlm@233
|
251 displays each element to the screen."
|
rlm@233
|
252 []
|
rlm@233
|
253 (view-sense
|
rlm@233
|
254 (fn
|
rlm@233
|
255 [[coords sensor-data]]
|
rlm@233
|
256 (let [image (points->image coords)]
|
rlm@233
|
257 (dorun
|
rlm@233
|
258 (for [i (range (count coords))]
|
rlm@233
|
259 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@233
|
260 (apply touch->gray (sensor-data i)))))
|
rlm@233
|
261 image))))
|
rlm@233
|
262 #+end_src
|
rlm@233
|
263
|
rlm@233
|
264
|
rlm@233
|
265
|
rlm@228
|
266 * Triangle Manipulation Functions
|
rlm@228
|
267
|
rlm@229
|
268 The rigid bodies which make up a creature have an underlying
|
rlm@229
|
269 =Geometry=, which is a =Mesh= plus a =Material= and other important
|
rlm@229
|
270 data involved with displaying the body.
|
rlm@229
|
271
|
rlm@229
|
272 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
|
rlm@229
|
273 verticies which have coordinates in XYZ space and UV space.
|
rlm@229
|
274
|
rlm@229
|
275 Here, =(triangles)= gets all the triangles which compose a mesh, and
|
rlm@229
|
276 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
|
rlm@229
|
277 of a triangle.
|
rlm@229
|
278
|
rlm@231
|
279 #+name: triangles-1
|
rlm@228
|
280 #+begin_src clojure
|
rlm@239
|
281 (in-ns 'cortex.touch)
|
rlm@239
|
282
|
rlm@239
|
283 (defn vector3f-seq [#^Vector3f v]
|
rlm@239
|
284 [(.getX v) (.getY v) (.getZ v)])
|
rlm@239
|
285
|
rlm@239
|
286 (defn triangle-seq [#^Triangle tri]
|
rlm@239
|
287 [(vector3f-seq (.get1 tri))
|
rlm@239
|
288 (vector3f-seq (.get2 tri))
|
rlm@239
|
289 (vector3f-seq (.get3 tri))])
|
rlm@239
|
290
|
rlm@240
|
291 (defn ->vector3f
|
rlm@240
|
292 ([coords] (Vector3f. (nth coords 0 0)
|
rlm@240
|
293 (nth coords 1 0)
|
rlm@240
|
294 (nth coords 2 0))))
|
rlm@239
|
295
|
rlm@239
|
296 (defn ->triangle [points]
|
rlm@239
|
297 (apply #(Triangle. %1 %2 %3) (map ->vector3f points)))
|
rlm@239
|
298
|
rlm@239
|
299 (defn triangle
|
rlm@239
|
300 "Get the triangle specified by triangle-index from the mesh within
|
rlm@239
|
301 bounds."
|
rlm@239
|
302 [#^Geometry geo triangle-index]
|
rlm@239
|
303 (triangle-seq
|
rlm@239
|
304 (let [scratch (Triangle.)]
|
rlm@239
|
305 (.getTriangle (.getMesh geo) triangle-index scratch) scratch)))
|
rlm@239
|
306
|
rlm@228
|
307 (defn triangles
|
rlm@228
|
308 "Return a sequence of all the Triangles which compose a given
|
rlm@228
|
309 Geometry."
|
rlm@239
|
310 [#^Geometry geo]
|
rlm@239
|
311 (map (partial triangle geo) (range (.getTriangleCount (.getMesh geo)))))
|
rlm@228
|
312
|
rlm@228
|
313 (defn triangle-vertex-indices
|
rlm@228
|
314 "Get the triangle vertex indices of a given triangle from a given
|
rlm@228
|
315 mesh."
|
rlm@228
|
316 [#^Mesh mesh triangle-index]
|
rlm@228
|
317 (let [indices (int-array 3)]
|
rlm@228
|
318 (.getTriangle mesh triangle-index indices)
|
rlm@228
|
319 (vec indices)))
|
rlm@228
|
320
|
rlm@228
|
321 (defn vertex-UV-coord
|
rlm@228
|
322 "Get the UV-coordinates of the vertex named by vertex-index"
|
rlm@228
|
323 [#^Mesh mesh vertex-index]
|
rlm@228
|
324 (let [UV-buffer
|
rlm@228
|
325 (.getData
|
rlm@228
|
326 (.getBuffer
|
rlm@228
|
327 mesh
|
rlm@228
|
328 VertexBuffer$Type/TexCoord))]
|
rlm@228
|
329 [(.get UV-buffer (* vertex-index 2))
|
rlm@228
|
330 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
|
rlm@228
|
331
|
rlm@239
|
332 (defn pixel-triangle [#^Geometry geo image index]
|
rlm@239
|
333 (let [mesh (.getMesh geo)
|
rlm@239
|
334 width (.getWidth image)
|
rlm@239
|
335 height (.getHeight image)]
|
rlm@239
|
336 (vec (map (fn [[u v]] (vector (* width u) (* height v)))
|
rlm@239
|
337 (map (partial vertex-UV-coord mesh)
|
rlm@239
|
338 (triangle-vertex-indices mesh index))))))
|
rlm@228
|
339
|
rlm@239
|
340 (defn pixel-triangles [#^Geometry geo image]
|
rlm@239
|
341 (let [height (.getHeight image)
|
rlm@239
|
342 width (.getWidth image)]
|
rlm@239
|
343 (map (partial pixel-triangle geo image)
|
rlm@239
|
344 (range (.getTriangleCount (.getMesh geo))))))
|
rlm@229
|
345
|
rlm@228
|
346 #+end_src
|
rlm@228
|
347
|
rlm@228
|
348 * Triangle Affine Transforms
|
rlm@228
|
349
|
rlm@229
|
350 The position of each hair is stored in a 2D image in UV
|
rlm@229
|
351 coordinates. To place the hair in 3D space we must convert from UV
|
rlm@229
|
352 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
|
rlm@229
|
353 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
|
rlm@229
|
354 for translating any coordinate within the UV triangle to the
|
rlm@229
|
355 cooresponding coordinate in the XYZ triangle.
|
rlm@229
|
356
|
rlm@231
|
357 #+name: triangles-3
|
rlm@228
|
358 #+begin_src clojure
|
rlm@243
|
359 (in-ns 'cortex.touch)
|
rlm@243
|
360
|
rlm@228
|
361 (defn triangle->matrix4f
|
rlm@228
|
362 "Converts the triangle into a 4x4 matrix: The first three columns
|
rlm@228
|
363 contain the vertices of the triangle; the last contains the unit
|
rlm@228
|
364 normal of the triangle. The bottom row is filled with 1s."
|
rlm@228
|
365 [#^Triangle t]
|
rlm@228
|
366 (let [mat (Matrix4f.)
|
rlm@228
|
367 [vert-1 vert-2 vert-3]
|
rlm@228
|
368 ((comp vec map) #(.get t %) (range 3))
|
rlm@228
|
369 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@228
|
370 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@228
|
371 (dorun
|
rlm@228
|
372 (for [row (range 4) col (range 3)]
|
rlm@228
|
373 (do
|
rlm@228
|
374 (.set mat col row (.get (vertices row)col))
|
rlm@228
|
375 (.set mat 3 row 1))))
|
rlm@228
|
376 mat))
|
rlm@228
|
377
|
rlm@240
|
378 (defn triangles->affine-transform
|
rlm@228
|
379 "Returns the affine transformation that converts each vertex in the
|
rlm@228
|
380 first triangle into the corresponding vertex in the second
|
rlm@228
|
381 triangle."
|
rlm@228
|
382 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@228
|
383 (.mult
|
rlm@228
|
384 (triangle->matrix4f tri-2)
|
rlm@228
|
385 (.invert (triangle->matrix4f tri-1))))
|
rlm@228
|
386 #+end_src
|
rlm@228
|
387
|
rlm@239
|
388
|
rlm@239
|
389 * Schrapnel Conversion Functions
|
rlm@239
|
390
|
rlm@239
|
391 It is convienent to treat a =Triangle= as a sequence of verticies, and
|
rlm@239
|
392 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
|
rlm@239
|
393 functions make this easy. If these classes implemented =Iterable= then
|
rlm@239
|
394 this code would not be necessary. Hopefully they will in the future.
|
rlm@239
|
395
|
rlm@239
|
396
|
rlm@229
|
397 * Triangle Boundaries
|
rlm@229
|
398
|
rlm@229
|
399 For efficiency's sake I will divide the UV-image into small squares
|
rlm@229
|
400 which inscribe each UV-triangle, then extract the points which lie
|
rlm@229
|
401 inside the triangle and map them to 3D-space using
|
rlm@229
|
402 =(triangle-transform)= above. To do this I need a function,
|
rlm@229
|
403 =(inside-triangle?)=, which determines whether a point is inside a
|
rlm@229
|
404 triangle in 2D UV-space.
|
rlm@228
|
405
|
rlm@231
|
406 #+name: triangles-4
|
rlm@228
|
407 #+begin_src clojure
|
rlm@229
|
408 (defn convex-bounds
|
rlm@229
|
409 "Returns the smallest square containing the given vertices, as a
|
rlm@229
|
410 vector of integers [left top width height]."
|
rlm@240
|
411 [verts]
|
rlm@240
|
412 (let [xs (map first verts)
|
rlm@240
|
413 ys (map second verts)
|
rlm@229
|
414 x0 (Math/floor (apply min xs))
|
rlm@229
|
415 y0 (Math/floor (apply min ys))
|
rlm@229
|
416 x1 (Math/ceil (apply max xs))
|
rlm@229
|
417 y1 (Math/ceil (apply max ys))]
|
rlm@229
|
418 [x0 y0 (- x1 x0) (- y1 y0)]))
|
rlm@229
|
419
|
rlm@229
|
420 (defn same-side?
|
rlm@229
|
421 "Given the points p1 and p2 and the reference point ref, is point p
|
rlm@229
|
422 on the same side of the line that goes through p1 and p2 as ref is?"
|
rlm@229
|
423 [p1 p2 ref p]
|
rlm@229
|
424 (<=
|
rlm@229
|
425 0
|
rlm@229
|
426 (.dot
|
rlm@229
|
427 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@229
|
428 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@229
|
429
|
rlm@229
|
430 (defn inside-triangle?
|
rlm@229
|
431 "Is the point inside the triangle?"
|
rlm@229
|
432 {:author "Dylan Holmes"}
|
rlm@229
|
433 [#^Triangle tri #^Vector3f p]
|
rlm@240
|
434 (let [[vert-1 vert-2 vert-3] [(.get1 tri) (.get2 tri) (.get3 tri)]]
|
rlm@229
|
435 (and
|
rlm@229
|
436 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@229
|
437 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@229
|
438 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@229
|
439 #+end_src
|
rlm@229
|
440
|
rlm@228
|
441 * Physics Collision Objects
|
rlm@230
|
442
|
rlm@234
|
443 The "hairs" are actually =Rays= which extend from a point on a
|
rlm@230
|
444 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
|
rlm@230
|
445
|
rlm@226
|
446 * Headers
|
rlm@231
|
447
|
rlm@231
|
448 #+name: touch-header
|
rlm@226
|
449 #+begin_src clojure
|
rlm@226
|
450 (ns cortex.touch
|
rlm@226
|
451 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
|
rlm@226
|
452 to be outfitted with touch sensors with density determined by a UV
|
rlm@226
|
453 image. In this way a Geometry can know what parts of itself are
|
rlm@226
|
454 touching nearby objects. Reads specially prepared blender files to
|
rlm@226
|
455 construct this sense automatically."
|
rlm@226
|
456 {:author "Robert McIntyre"}
|
rlm@226
|
457 (:use (cortex world util sense))
|
rlm@226
|
458 (:use clojure.contrib.def)
|
rlm@226
|
459 (:import (com.jme3.scene Geometry Node Mesh))
|
rlm@226
|
460 (:import com.jme3.collision.CollisionResults)
|
rlm@226
|
461 (:import com.jme3.scene.VertexBuffer$Type)
|
rlm@226
|
462 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
|
rlm@226
|
463 #+end_src
|
rlm@37
|
464
|
rlm@232
|
465 * Adding Touch to the Worm
|
rlm@232
|
466
|
rlm@232
|
467 #+name: test-touch
|
rlm@232
|
468 #+begin_src clojure
|
rlm@232
|
469 (ns cortex.test.touch
|
rlm@232
|
470 (:use (cortex world util sense body touch))
|
rlm@232
|
471 (:use cortex.test.body))
|
rlm@232
|
472
|
rlm@232
|
473 (cortex.import/mega-import-jme3)
|
rlm@232
|
474
|
rlm@232
|
475 (defn test-touch []
|
rlm@232
|
476 (let [the-worm (doto (worm) (body!))
|
rlm@232
|
477 touch (touch! the-worm)
|
rlm@232
|
478 touch-display (view-touch)]
|
rlm@232
|
479 (world (nodify [the-worm (floor)])
|
rlm@232
|
480 standard-debug-controls
|
rlm@232
|
481
|
rlm@232
|
482 (fn [world]
|
rlm@232
|
483 (light-up-everything world))
|
rlm@232
|
484
|
rlm@232
|
485 (fn [world tpf]
|
rlm@243
|
486
|
rlm@243
|
487
|
rlm@243
|
488 (dorun (map #(% (.getRootNode world)) touch))
|
rlm@243
|
489
|
rlm@243
|
490
|
rlm@243
|
491 ))))
|
rlm@232
|
492 #+end_src
|
rlm@228
|
493 * Source Listing
|
rlm@228
|
494 * Next
|
rlm@228
|
495
|
rlm@228
|
496
|
rlm@226
|
497 * COMMENT Code Generation
|
rlm@39
|
498 #+begin_src clojure :tangle ../src/cortex/touch.clj
|
rlm@231
|
499 <<touch-header>>
|
rlm@231
|
500 <<meta-data>>
|
rlm@231
|
501 <<triangles-1>>
|
rlm@231
|
502 <<triangles-3>>
|
rlm@231
|
503 <<triangles-4>>
|
rlm@231
|
504 <<sensors>>
|
rlm@231
|
505 <<kernel>>
|
rlm@231
|
506 <<visualization>>
|
rlm@0
|
507 #+end_src
|
rlm@0
|
508
|
rlm@232
|
509
|
rlm@68
|
510 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
|
rlm@232
|
511 <<test-touch>>
|
rlm@39
|
512 #+end_src
|
rlm@39
|
513
|
rlm@0
|
514
|
rlm@0
|
515
|
rlm@0
|
516
|
rlm@32
|
517
|
rlm@32
|
518
|
rlm@226
|
519
|