annotate org/touch.org @ 238:3fa49ff1649a

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