annotate org/touch.org @ 235:be78d7bd6920

fixed major bug in collapse
author Robert McIntyre <rlm@mit.edu>
date Sun, 12 Feb 2012 09:56:38 -0700
parents 712bd7e5b148
children 3fa49ff1649a
rev   line source
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@229 53 hair length is linearly interpolated between 0 and =scale=.
rlm@229 54
rlm@231 55 #+name: meta-data
rlm@0 56 #+begin_src clojure
rlm@229 57 (defn tactile-sensor-profile
rlm@229 58 "Return the touch-sensor distribution image in BufferedImage format,
rlm@229 59 or nil if it does not exist."
rlm@229 60 [#^Geometry obj]
rlm@229 61 (if-let [image-path (meta-data obj "touch")]
rlm@229 62 (load-image image-path)))
rlm@233 63
rlm@233 64 (defn tactile-scale
rlm@233 65 "Return the maximum length of a hair. All hairs are scalled between
rlm@233 66 0.0 and this length, depending on their color. Black is 0, and
rlm@233 67 white is maximum length, and everything in between is scalled
rlm@233 68 linearlly. Default scale is 0.01 jMonkeyEngine units."
rlm@233 69 [#^Geometry obj]
rlm@233 70 (if-let [scale (meta-data obj "scale")]
rlm@233 71 scale 0.1))
rlm@228 72 #+end_src
rlm@156 73
rlm@229 74 ** TODO add image showing example touch-uv map
rlm@229 75 ** TODO add metadata display for worm
rlm@229 76
rlm@234 77
rlm@233 78 * Skin Creation
rlm@234 79 * TODO get the actual lengths for each hair
rlm@234 80
rlm@234 81 #+begin_src clojure
rlm@234 82 pixel-triangles
rlm@234 83 xyz-triangles
rlm@234 84 conversions (map triangles->affine-transform pixel-triangles
rlm@234 85 xyz-triangles)
rlm@234 86
rlm@234 87 #+end_src
rlm@234 88
rlm@233 89 #+name: kernel
rlm@233 90 #+begin_src clojure
rlm@233 91 (in-ns 'cortex.touch)
rlm@233 92
rlm@234 93 (declare touch-topology touch-hairs set-ray)
rlm@234 94
rlm@233 95 (defn touch-kernel
rlm@234 96 "Constructs a function which will return tactile sensory data from
rlm@234 97 'geo when called from inside a running simulation"
rlm@234 98 [#^Geometry geo]
rlm@234 99 (let [[ray-reference-origins
rlm@234 100 ray-reference-tips
rlm@234 101 ray-lengths] (touch-hairs geo)
rlm@234 102 current-rays (map (fn [] (Ray.)) ray-reference-origins)
rlm@234 103 topology (touch-topology geo)]
rlm@234 104 (if (empty? ray-reference-origins) nil
rlm@234 105 (fn [node]
rlm@234 106 (let [transform (.getWorldMatrix geo)]
rlm@234 107 (dorun
rlm@234 108 (map (fn [ray ref-origin ref-tip length]
rlm@234 109 (set-ray ray transform ref-origin ref-tip length))
rlm@234 110 current-rays ray-reference-origins
rlm@234 111 ray-reference-tips ray-lengths))
rlm@234 112 (vector
rlm@234 113 topology
rlm@234 114 (vec
rlm@234 115 (for [ray current-rays]
rlm@234 116 (do
rlm@234 117 (let [results (CollisionResults.)]
rlm@234 118 (.collideWith node ray results)
rlm@234 119 (let [touch-objects
rlm@234 120 (filter #(not (= geo (.getGeometry %)))
rlm@234 121 results)]
rlm@234 122 [(if (empty? touch-objects)
rlm@234 123 (.getLimit ray)
rlm@234 124 (.getDistance (first touch-objects)))
rlm@234 125 (.getLimit ray)])))))))))))
rlm@234 126
rlm@234 127 (defn touch-kernel*
rlm@233 128 "Returns a function which returns tactile sensory data when called
rlm@233 129 inside a running simulation."
rlm@233 130 [#^Geometry geo]
rlm@233 131 (let [feeler-coords (feeler-coordinates geo)
rlm@233 132 tris (triangles geo)
rlm@233 133 limit (tactile-scale geo)]
rlm@233 134 (if (empty? (touch-topology geo))
rlm@233 135 nil
rlm@233 136 (fn [node]
rlm@233 137 (let [sensor-origins
rlm@233 138 (map
rlm@233 139 #(map (partial local-to-world geo) %)
rlm@233 140 feeler-coords)
rlm@233 141 triangle-normals
rlm@233 142 (map (partial get-ray-direction geo)
rlm@233 143 tris)
rlm@233 144 rays
rlm@233 145 (flatten
rlm@233 146 (map (fn [origins norm]
rlm@233 147 (map #(doto (Ray. % norm)
rlm@233 148 (.setLimit limit)) origins))
rlm@233 149 sensor-origins triangle-normals))]
rlm@233 150 (vector
rlm@233 151 (touch-topology geo)
rlm@233 152 (vec
rlm@233 153 (for [ray rays]
rlm@233 154 (do
rlm@233 155 (let [results (CollisionResults.)]
rlm@233 156 (.collideWith node ray results)
rlm@233 157 (let [touch-objects
rlm@233 158 (filter #(not (= geo (.getGeometry %)))
rlm@233 159 results)]
rlm@233 160 [(if (empty? touch-objects)
rlm@233 161 limit (.getDistance (first touch-objects)))
rlm@233 162 limit])))))))))))
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@233 176 * Visualizing Touch
rlm@233 177 #+name: visualization
rlm@233 178 #+begin_src clojure
rlm@233 179 (in-ns 'cortex.touch)
rlm@233 180
rlm@233 181 (defn touch->gray
rlm@233 182 "Convert a pair of [distance, max-distance] into a grayscale pixel"
rlm@233 183 [distance max-distance]
rlm@233 184 (gray
rlm@233 185 (- 255
rlm@233 186 (rem
rlm@233 187 (int
rlm@233 188 (* 255 (/ distance max-distance)))
rlm@233 189 256))))
rlm@233 190
rlm@233 191 (defn view-touch
rlm@233 192 "Creates a function which accepts a list of touch sensor-data and
rlm@233 193 displays each element to the screen."
rlm@233 194 []
rlm@233 195 (view-sense
rlm@233 196 (fn
rlm@233 197 [[coords sensor-data]]
rlm@233 198 (let [image (points->image coords)]
rlm@233 199 (dorun
rlm@233 200 (for [i (range (count coords))]
rlm@233 201 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@233 202 (apply touch->gray (sensor-data i)))))
rlm@233 203 image))))
rlm@233 204 #+end_src
rlm@233 205
rlm@233 206
rlm@233 207
rlm@228 208 * Triangle Manipulation Functions
rlm@228 209
rlm@229 210 The rigid bodies which make up a creature have an underlying
rlm@229 211 =Geometry=, which is a =Mesh= plus a =Material= and other important
rlm@229 212 data involved with displaying the body.
rlm@229 213
rlm@229 214 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
rlm@229 215 verticies which have coordinates in XYZ space and UV space.
rlm@229 216
rlm@229 217 Here, =(triangles)= gets all the triangles which compose a mesh, and
rlm@229 218 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
rlm@229 219 of a triangle.
rlm@229 220
rlm@231 221 #+name: triangles-1
rlm@228 222 #+begin_src clojure
rlm@228 223 (defn triangles
rlm@228 224 "Return a sequence of all the Triangles which compose a given
rlm@228 225 Geometry."
rlm@228 226 [#^Geometry geom]
rlm@228 227 (let
rlm@228 228 [mesh (.getMesh geom)
rlm@228 229 triangles (transient [])]
rlm@228 230 (dorun
rlm@228 231 (for [n (range (.getTriangleCount mesh))]
rlm@228 232 (let [tri (Triangle.)]
rlm@228 233 (.getTriangle mesh n tri)
rlm@228 234 ;; (.calculateNormal tri)
rlm@228 235 ;; (.calculateCenter tri)
rlm@228 236 (conj! triangles tri))))
rlm@228 237 (persistent! triangles)))
rlm@228 238
rlm@228 239 (defn mesh-triangle
rlm@228 240 "Get the triangle specified by triangle-index from the mesh within
rlm@228 241 bounds."
rlm@228 242 [#^Mesh mesh triangle-index]
rlm@228 243 (let [scratch (Triangle.)]
rlm@228 244 (.getTriangle mesh triangle-index scratch)
rlm@228 245 scratch))
rlm@228 246
rlm@228 247 (defn triangle-vertex-indices
rlm@228 248 "Get the triangle vertex indices of a given triangle from a given
rlm@228 249 mesh."
rlm@228 250 [#^Mesh mesh triangle-index]
rlm@228 251 (let [indices (int-array 3)]
rlm@228 252 (.getTriangle mesh triangle-index indices)
rlm@228 253 (vec indices)))
rlm@228 254
rlm@228 255 (defn vertex-UV-coord
rlm@228 256 "Get the UV-coordinates of the vertex named by vertex-index"
rlm@228 257 [#^Mesh mesh vertex-index]
rlm@228 258 (let [UV-buffer
rlm@228 259 (.getData
rlm@228 260 (.getBuffer
rlm@228 261 mesh
rlm@228 262 VertexBuffer$Type/TexCoord))]
rlm@228 263 [(.get UV-buffer (* vertex-index 2))
rlm@228 264 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@228 265
rlm@228 266 (defn triangle-UV-coord
rlm@228 267 "Get the UV-cooridnates of the triangle's verticies."
rlm@228 268 [#^Mesh mesh width height triangle-index]
rlm@228 269 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@228 270 (map (partial vertex-UV-coord mesh)
rlm@228 271 (triangle-vertex-indices mesh triangle-index))))
rlm@228 272 #+end_src
rlm@228 273
rlm@228 274 * Schrapnel Conversion Functions
rlm@229 275
rlm@229 276 It is convienent to treat a =Triangle= as a sequence of verticies, and
rlm@229 277 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
rlm@229 278 functions make this easy. If these classes implemented =Iterable= then
rlm@229 279 this code would not be necessary. Hopefully they will in the future.
rlm@229 280
rlm@231 281 #+name: triangles-2
rlm@228 282 #+begin_src clojure
rlm@228 283 (defn triangle-seq [#^Triangle tri]
rlm@228 284 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@228 285
rlm@228 286 (defn vector3f-seq [#^Vector3f v]
rlm@228 287 [(.getX v) (.getY v) (.getZ v)])
rlm@228 288
rlm@228 289 (defn point->vector2f [[u v]]
rlm@228 290 (Vector2f. u v))
rlm@228 291
rlm@228 292 (defn vector2f->vector3f [v]
rlm@228 293 (Vector3f. (.getX v) (.getY v) 0))
rlm@228 294
rlm@228 295 (defn map-triangle [f #^Triangle tri]
rlm@228 296 (Triangle.
rlm@228 297 (f 0 (.get1 tri))
rlm@228 298 (f 1 (.get2 tri))
rlm@228 299 (f 2 (.get3 tri))))
rlm@228 300
rlm@228 301 (defn points->triangle
rlm@228 302 "Convert a list of points into a triangle."
rlm@228 303 [points]
rlm@228 304 (apply #(Triangle. %1 %2 %3)
rlm@228 305 (map (fn [point]
rlm@228 306 (let [point (vec point)]
rlm@228 307 (Vector3f. (get point 0 0)
rlm@228 308 (get point 1 0)
rlm@228 309 (get point 2 0))))
rlm@228 310 (take 3 points))))
rlm@228 311 #+end_src
rlm@228 312
rlm@228 313 * Triangle Affine Transforms
rlm@228 314
rlm@229 315 The position of each hair is stored in a 2D image in UV
rlm@229 316 coordinates. To place the hair in 3D space we must convert from UV
rlm@229 317 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
rlm@229 318 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
rlm@229 319 for translating any coordinate within the UV triangle to the
rlm@229 320 cooresponding coordinate in the XYZ triangle.
rlm@229 321
rlm@231 322 #+name: triangles-3
rlm@228 323 #+begin_src clojure
rlm@228 324 (defn triangle->matrix4f
rlm@228 325 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@228 326 contain the vertices of the triangle; the last contains the unit
rlm@228 327 normal of the triangle. The bottom row is filled with 1s."
rlm@228 328 [#^Triangle t]
rlm@228 329 (let [mat (Matrix4f.)
rlm@228 330 [vert-1 vert-2 vert-3]
rlm@228 331 ((comp vec map) #(.get t %) (range 3))
rlm@228 332 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@228 333 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@228 334 (dorun
rlm@228 335 (for [row (range 4) col (range 3)]
rlm@228 336 (do
rlm@228 337 (.set mat col row (.get (vertices row)col))
rlm@228 338 (.set mat 3 row 1))))
rlm@228 339 mat))
rlm@228 340
rlm@228 341 (defn triangle-transformation
rlm@228 342 "Returns the affine transformation that converts each vertex in the
rlm@228 343 first triangle into the corresponding vertex in the second
rlm@228 344 triangle."
rlm@228 345 [#^Triangle tri-1 #^Triangle tri-2]
rlm@228 346 (.mult
rlm@228 347 (triangle->matrix4f tri-2)
rlm@228 348 (.invert (triangle->matrix4f tri-1))))
rlm@228 349 #+end_src
rlm@228 350
rlm@229 351 * Triangle Boundaries
rlm@229 352
rlm@229 353 For efficiency's sake I will divide the UV-image into small squares
rlm@229 354 which inscribe each UV-triangle, then extract the points which lie
rlm@229 355 inside the triangle and map them to 3D-space using
rlm@229 356 =(triangle-transform)= above. To do this I need a function,
rlm@229 357 =(inside-triangle?)=, which determines whether a point is inside a
rlm@229 358 triangle in 2D UV-space.
rlm@228 359
rlm@231 360 #+name: triangles-4
rlm@228 361 #+begin_src clojure
rlm@229 362 (defn convex-bounds
rlm@229 363 "Returns the smallest square containing the given vertices, as a
rlm@229 364 vector of integers [left top width height]."
rlm@229 365 [uv-verts]
rlm@229 366 (let [xs (map first uv-verts)
rlm@229 367 ys (map second uv-verts)
rlm@229 368 x0 (Math/floor (apply min xs))
rlm@229 369 y0 (Math/floor (apply min ys))
rlm@229 370 x1 (Math/ceil (apply max xs))
rlm@229 371 y1 (Math/ceil (apply max ys))]
rlm@229 372 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@229 373
rlm@229 374 (defn same-side?
rlm@229 375 "Given the points p1 and p2 and the reference point ref, is point p
rlm@229 376 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@229 377 [p1 p2 ref p]
rlm@229 378 (<=
rlm@229 379 0
rlm@229 380 (.dot
rlm@229 381 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@229 382 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@229 383
rlm@229 384 (defn inside-triangle?
rlm@229 385 "Is the point inside the triangle?"
rlm@229 386 {:author "Dylan Holmes"}
rlm@229 387 [#^Triangle tri #^Vector3f p]
rlm@229 388 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@229 389 (and
rlm@229 390 (same-side? vert-1 vert-2 vert-3 p)
rlm@229 391 (same-side? vert-2 vert-3 vert-1 p)
rlm@229 392 (same-side? vert-3 vert-1 vert-2 p))))
rlm@229 393 #+end_src
rlm@229 394
rlm@229 395
rlm@229 396 * Sensor Related Functions
rlm@229 397
rlm@229 398 These functions analyze the touch-sensor-profile image convert the
rlm@229 399 location of each touch sensor from pixel coordinates to UV-coordinates
rlm@229 400 and XYZ-coordinates.
rlm@229 401
rlm@231 402 #+name: sensors
rlm@229 403 #+begin_src clojure
rlm@229 404 (defn sensors-in-triangle
rlm@229 405 "Locate the touch sensors in the triangle, returning a map of their
rlm@229 406 UV and geometry-relative coordinates."
rlm@229 407 [image mesh tri-index]
rlm@229 408 (let [width (.getWidth image)
rlm@229 409 height (.getHeight image)
rlm@229 410 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@229 411 bounds (convex-bounds UV-vertex-coords)
rlm@229 412
rlm@229 413 cutout-triangle (points->triangle UV-vertex-coords)
rlm@229 414 UV-sensor-coords
rlm@229 415 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@229 416 (fn [[u v]] (Vector3f. u v 0)))
rlm@229 417 (white-coordinates image bounds))
rlm@229 418 UV->geometry (triangle-transformation
rlm@229 419 cutout-triangle
rlm@229 420 (mesh-triangle mesh tri-index))
rlm@229 421 geometry-sensor-coords
rlm@229 422 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@229 423 UV-sensor-coords)]
rlm@229 424 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@229 425
rlm@229 426 (defn-memo locate-feelers
rlm@229 427 "Search the geometry's tactile UV profile for touch sensors,
rlm@229 428 returning their positions in geometry-relative coordinates."
rlm@229 429 [#^Geometry geo]
rlm@229 430 (let [mesh (.getMesh geo)
rlm@229 431 num-triangles (.getTriangleCount mesh)]
rlm@229 432 (if-let [image (tactile-sensor-profile geo)]
rlm@229 433 (map
rlm@229 434 (partial sensors-in-triangle image mesh)
rlm@229 435 (range num-triangles))
rlm@229 436 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@229 437
rlm@229 438 (defn-memo touch-topology
rlm@229 439 "Return a sequence of vectors of the form [x y] describing the
rlm@229 440 \"topology\" of the tactile sensors. Points that are close together
rlm@229 441 in the touch-topology are generally close together in the simulation."
rlm@229 442 [#^Gemoetry geo]
rlm@229 443 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@229 444
rlm@229 445 (defn-memo feeler-coordinates
rlm@229 446 "The location of the touch sensors in world-space coordinates."
rlm@229 447 [#^Geometry geo]
rlm@229 448 (vec (map :geometry (locate-feelers geo))))
rlm@228 449 #+end_src
rlm@228 450
rlm@228 451 * Physics Collision Objects
rlm@230 452
rlm@234 453 The "hairs" are actually =Rays= which extend from a point on a
rlm@230 454 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
rlm@230 455
rlm@231 456 #+name: rays
rlm@228 457 #+begin_src clojure
rlm@228 458 (defn get-ray-origin
rlm@228 459 "Return the origin which a Ray would have to have to be in the exact
rlm@228 460 center of a particular Triangle in the Geometry in World
rlm@228 461 Coordinates."
rlm@228 462 [geom tri]
rlm@228 463 (let [new (Vector3f.)]
rlm@228 464 (.calculateCenter tri)
rlm@228 465 (.localToWorld geom (.getCenter tri) new) new))
rlm@228 466
rlm@228 467 (defn get-ray-direction
rlm@228 468 "Return the direction which a Ray would have to have to be to point
rlm@228 469 normal to the Triangle, in coordinates relative to the center of the
rlm@228 470 Triangle."
rlm@228 471 [geom tri]
rlm@228 472 (let [n+c (Vector3f.)]
rlm@228 473 (.calculateNormal tri)
rlm@228 474 (.calculateCenter tri)
rlm@228 475 (.localToWorld
rlm@228 476 geom
rlm@228 477 (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@228 478 (.subtract n+c (get-ray-origin geom tri))))
rlm@228 479 #+end_src
rlm@226 480 * Headers
rlm@231 481
rlm@231 482 #+name: touch-header
rlm@226 483 #+begin_src clojure
rlm@226 484 (ns cortex.touch
rlm@226 485 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
rlm@226 486 to be outfitted with touch sensors with density determined by a UV
rlm@226 487 image. In this way a Geometry can know what parts of itself are
rlm@226 488 touching nearby objects. Reads specially prepared blender files to
rlm@226 489 construct this sense automatically."
rlm@226 490 {:author "Robert McIntyre"}
rlm@226 491 (:use (cortex world util sense))
rlm@226 492 (:use clojure.contrib.def)
rlm@226 493 (:import (com.jme3.scene Geometry Node Mesh))
rlm@226 494 (:import com.jme3.collision.CollisionResults)
rlm@226 495 (:import com.jme3.scene.VertexBuffer$Type)
rlm@226 496 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
rlm@226 497 #+end_src
rlm@37 498
rlm@232 499 * Adding Touch to the Worm
rlm@232 500
rlm@232 501 #+name: test-touch
rlm@232 502 #+begin_src clojure
rlm@232 503 (ns cortex.test.touch
rlm@232 504 (:use (cortex world util sense body touch))
rlm@232 505 (:use cortex.test.body))
rlm@232 506
rlm@232 507 (cortex.import/mega-import-jme3)
rlm@232 508
rlm@232 509 (defn test-touch []
rlm@232 510 (let [the-worm (doto (worm) (body!))
rlm@232 511 touch (touch! the-worm)
rlm@232 512 touch-display (view-touch)]
rlm@232 513 (world (nodify [the-worm (floor)])
rlm@232 514 standard-debug-controls
rlm@232 515
rlm@232 516 (fn [world]
rlm@232 517 (light-up-everything world))
rlm@232 518
rlm@232 519 (fn [world tpf]
rlm@232 520 (touch-display (map #(% (.getRootNode world)) touch))))))
rlm@232 521 #+end_src
rlm@228 522 * Source Listing
rlm@228 523 * Next
rlm@228 524
rlm@228 525
rlm@226 526 * COMMENT Code Generation
rlm@39 527 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@231 528 <<touch-header>>
rlm@231 529 <<meta-data>>
rlm@231 530 <<triangles-1>>
rlm@231 531 <<triangles-2>>
rlm@231 532 <<triangles-3>>
rlm@231 533 <<triangles-4>>
rlm@231 534 <<sensors>>
rlm@231 535 <<rays>>
rlm@231 536 <<kernel>>
rlm@231 537 <<visualization>>
rlm@0 538 #+end_src
rlm@0 539
rlm@232 540
rlm@68 541 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
rlm@232 542 <<test-touch>>
rlm@39 543 #+end_src
rlm@39 544
rlm@0 545
rlm@0 546
rlm@0 547
rlm@32 548
rlm@32 549
rlm@226 550