comparison org/touch.org @ 226:e5db1d2ff9a8

removed outdated test from touch.org
author Robert McIntyre <rlm@mit.edu>
date Sat, 11 Feb 2012 12:53:18 -0700
parents facc2ef3fe5c
children 2a7f57e7efdb
comparison
equal deleted inserted replaced
225:5cea47455b43 226:e5db1d2ff9a8
6 #+SETUPFILE: ../../aurellem/org/setup.org 6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org 7 #+INCLUDE: ../../aurellem/org/level-0.org
8 8
9 * Touch 9 * Touch
10 10
11 My creatures need to be able to feel their environments. The idea here 11 Touch is critical to navigation and spatial reasoning and as such I
12 is to create thousands of small /touch receptors/ along the geometries 12 need a simulated version of it to give to my AI creatures.
13 which make up the creature's body. The number of touch receptors in a
14 given area is determined by how complicated that area is, as
15 determined by the total number of triangles in that region. This way,
16 complicated regions like the hands/face, etc. get more touch receptors
17 than simpler areas of the body.
18 13
19 #+name: skin-main 14 #+name: skin-main
20 #+begin_src clojure 15 #+begin_src clojure
21 (ns cortex.touch 16 (in-ns 'cortex.touch)
22 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry 17
23 to be outfitted with touch sensors with density proportional to the
24 density of triangles along the surface of the Geometry. Enables a
25 Geometry to know what parts of itself are touching nearby objects."
26 {:author "Robert McIntyre"}
27 (:use (cortex world util sense))
28 (:use clojure.contrib.def)
29 (:import (com.jme3.scene Geometry Node Mesh))
30 (:import com.jme3.collision.CollisionResults)
31 (:import com.jme3.scene.VertexBuffer$Type)
32 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
33
34 (defn triangles 18 (defn triangles
35 "Return a sequence of all the Triangles which compose a given 19 "Return a sequence of all the Triangles which compose a given
36 Geometry." 20 Geometry."
37 [#^Geometry geom] 21 [#^Geometry geom]
38 (let 22 (let
311 (dorun 295 (dorun
312 (for [i (range (count coords))] 296 (for [i (range (count coords))]
313 (.setRGB image ((coords i) 0) ((coords i) 1) 297 (.setRGB image ((coords i) 0) ((coords i) 1)
314 (gray (sensor-data i))))) 298 (gray (sensor-data i)))))
315 image)))) 299 image))))
316
317
318 #+end_src 300 #+end_src
319 301
320 302 * Headers
321 * Example 303 #+begin_src clojure
322 304 (ns cortex.touch
323 #+name: touch-test 305 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
324 #+begin_src clojure 306 to be outfitted with touch sensors with density determined by a UV
325 (ns cortex.test.touch 307 image. In this way a Geometry can know what parts of itself are
326 (:use (cortex world util touch)) 308 touching nearby objects. Reads specially prepared blender files to
327 (:import 309 construct this sense automatically."
328 com.jme3.scene.shape.Sphere 310 {:author "Robert McIntyre"}
329 com.jme3.math.ColorRGBA 311 (:use (cortex world util sense))
330 com.jme3.math.Vector3f 312 (:use clojure.contrib.def)
331 com.jme3.material.RenderState$BlendMode 313 (:import (com.jme3.scene Geometry Node Mesh))
332 com.jme3.renderer.queue.RenderQueue$Bucket 314 (:import com.jme3.collision.CollisionResults)
333 com.jme3.scene.shape.Box 315 (:import com.jme3.scene.VertexBuffer$Type)
334 com.jme3.scene.Node)) 316 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
335 317 #+end_src
336 (defn ray-origin-debug 318
337 [ray color] 319 * COMMENT Code Generation
338 (make-shape
339 (assoc base-shape
340 :shape (Sphere. 5 5 0.05)
341 :name "arrow"
342 :color color
343 :texture false
344 :physical? false
345 :position
346 (.getOrigin ray))))
347
348 (defn ray-debug [ray color]
349 (make-shape
350 (assoc
351 base-shape
352 :name "debug-ray"
353 :physical? false
354 :shape (com.jme3.scene.shape.Line.
355 (.getOrigin ray)
356 (.add
357 (.getOrigin ray)
358 (.mult (.getDirection ray)
359 (float (.getLimit ray))))))))
360
361
362 (defn contact-color [contacts]
363 (case contacts
364 0 ColorRGBA/Gray
365 1 ColorRGBA/Red
366 2 ColorRGBA/Green
367 3 ColorRGBA/Yellow
368 4 ColorRGBA/Orange
369 5 ColorRGBA/Red
370 6 ColorRGBA/Magenta
371 7 ColorRGBA/Pink
372 8 ColorRGBA/White))
373
374 (defn update-ray-debug [node ray contacts]
375 (let [origin (.getChild node 0)]
376 (.setLocalTranslation origin (.getOrigin ray))
377 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
378
379 (defn init-node
380 [debug-node rays]
381 (.detachAllChildren debug-node)
382 (dorun
383 (for [ray rays]
384 (do
385 (.attachChild
386 debug-node
387 (doto (Node.)
388 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
389 (.attachChild (ray-debug ray ColorRGBA/Gray))
390 ))))))
391
392 (defn manage-ray-debug-node [debug-node geom touch-data limit]
393 (let [rays (normal-rays limit geom)]
394 (if (not= (count (.getChildren debug-node)) (count touch-data))
395 (init-node debug-node rays))
396 (dorun
397 (for [n (range (count touch-data))]
398 (update-ray-debug
399 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
400
401 (defn transparent-sphere []
402 (doto
403 (make-shape
404 (merge base-shape
405 {:position (Vector3f. 0 2 0)
406 :name "the blob."
407 :material "Common/MatDefs/Misc/Unshaded.j3md"
408 :texture "Textures/purpleWisp.png"
409 :physical? true
410 :mass 70
411 :color ColorRGBA/Blue
412 :shape (Sphere. 10 10 1)}))
413 (-> (.getMaterial)
414 (.getAdditionalRenderState)
415 (.setBlendMode RenderState$BlendMode/Alpha))
416 (.setQueueBucket RenderQueue$Bucket/Transparent)))
417
418 (defn transparent-box []
419 (doto
420 (make-shape
421 (merge base-shape
422 {:position (Vector3f. 0 2 0)
423 :name "box"
424 :material "Common/MatDefs/Misc/Unshaded.j3md"
425 :texture "Textures/purpleWisp.png"
426 :physical? true
427 :mass 70
428 :color ColorRGBA/Blue
429 :shape (Box. 1 1 1)}))
430 (-> (.getMaterial)
431 (.getAdditionalRenderState)
432 (.setBlendMode RenderState$BlendMode/Alpha))
433 (.setQueueBucket RenderQueue$Bucket/Transparent)))
434
435 (defn transparent-floor []
436 (doto
437 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
438 :material "Common/MatDefs/Misc/Unshaded.j3md"
439 :texture "Textures/redWisp.png"
440 :name "floor")
441 (-> (.getMaterial)
442 (.getAdditionalRenderState)
443 (.setBlendMode RenderState$BlendMode/Alpha))
444 (.setQueueBucket RenderQueue$Bucket/Transparent)))
445
446 (defn test-skin
447 "Testing touch:
448 you should see a ball which responds to the table
449 and whatever balls hit it."
450 []
451 (let [b
452 ;;(transparent-box)
453 (transparent-sphere)
454 ;;(sphere)
455 f (transparent-floor)
456 debug-node (Node.)
457 node (doto (Node.) (.attachChild b) (.attachChild f))
458 root-node (doto (Node.) (.attachChild node)
459 (.attachChild debug-node))
460 ]
461
462 (world
463 root-node
464 {"key-return" (fire-cannon-ball node)}
465 (fn [world]
466 ;; (Capture/SimpleCaptureVideo
467 ;; world
468 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
469 ;; (no-logging)
470 ;;(enable-debug world)
471 ;; (set-accuracy world (/ 1 60))
472 )
473
474 (fn [& _]
475 (let [sensitivity 0.2
476 touch-data (touch-percieve sensitivity b node)]
477 (manage-ray-debug-node debug-node b touch-data sensitivity))
478 ))))
479
480
481 #+end_src
482
483
484
485
486
487 * COMMENT code generation
488 #+begin_src clojure :tangle ../src/cortex/touch.clj 320 #+begin_src clojure :tangle ../src/cortex/touch.clj
489 <<skin-main>> 321 <<skin-main>>
490 #+end_src 322 #+end_src
491 323
492 #+begin_src clojure :tangle ../src/cortex/test/touch.clj 324 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
493 <<touch-test>>
494 #+end_src 325 #+end_src
495 326
496 327
497 328
498 329
499 330
500 331
332