annotate org/test-creature.org @ 129:bab47091534e

add some ideas
author Robert McIntyre <rlm@mit.edu>
date Mon, 30 Jan 2012 00:25:11 -0700
parents 4b38355ad6e3
children b26017d1fe9a
rev   line source
rlm@73 1 #+title: First attempt at a creature!
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@129 9
rlm@129 10
rlm@129 11 * Brainstorming different sensors and effectors.
rlm@129 12
rlm@129 13 Every sense that we have should have an effector that changes what
rlm@129 14 that sense (or others who have that sense) experiences.
rlm@129 15
rlm@129 16 ** Classic Senses
rlm@129 17 | Sense | Effector |
rlm@129 18 |------------------------------+---------------------------------|
rlm@129 19 | Vision | Variable Coloration |
rlm@129 20 | Hearing | Speech |
rlm@129 21 | Proprioception | Movement |
rlm@129 22 | Smell/Taste (Chemoreception) | Pheremones |
rlm@129 23 | Touch | Movement / Controllable Texture |
rlm@129 24 | Acceleration | Movement |
rlm@129 25 | Balance (sense gravity) | Movement |
rlm@129 26 | | |
rlm@129 27
rlm@129 28 - New Senses/Effectors
rlm@129 29 - Levitation
rlm@129 30 - Telekenesis
rlm@129 31
rlm@129 32 - Symbol Sense
rlm@129 33 Where objects in the world can be queried for description /
rlm@129 34 symbols.
rlm@129 35
rlm@129 36 - Symbol Marking
rlm@129 37 The ability to mark objects in the world with your own descriptions
rlm@129 38 and symbols.
rlm@129 39
rlm@129 40 - Vision
rlm@129 41 Distinguish the polarization of light
rlm@129 42 Color
rlm@129 43 Movement
rlm@129 44
rlm@129 45 * project ideas
rlm@129 46 - HACKER for writing muscle-control programs : Presented with
rlm@129 47 low-level muscle control/ sense API, generate higher level programs
rlm@129 48 for accomplishing various stated goals. Example goals might be
rlm@129 49 "extend all your fingers" or "move your hand into the area with
rlm@129 50 blue light" or "decrease the angle of this joint". It would be
rlm@129 51 like Sussman's HACKER, except it would operate with much more data
rlm@129 52 in a more realistic world. Start off with "calestanthics" to
rlm@129 53 develop subrouitines over the motor control API. This would be the
rlm@129 54 "spinal chord" of a more intelligent creature. The low level
rlm@129 55 programming code might be a turning machine that could develop
rlm@129 56 programs to iterate over a "tape" where each entry in the tape
rlm@129 57 could control recruitment of the fibers in a muscle.
rlm@129 58 - Make a virtual computer in the virtual world which with which the
rlm@129 59 creature interacts using its fingers to press keys on a virtual
rlm@129 60 keyboard. The creature can access the internet, watch videos, take
rlm@129 61 over the world, anything it wants.
rlm@129 62 - Make virtual insturments like pianos, drumbs, etc that it learns to
rlm@129 63 play.
rlm@129 64 - make a joint that figures out what type of joint it is (range of
rlm@129 65 motion)
rlm@129 66
rlm@129 67
rlm@129 68
rlm@129 69
rlm@129 70
rlm@129 71 * goals
rlm@125 72
rlm@125 73 ** have to get done before winston
rlm@126 74 - [ ] write an explination for why greyscale bitmaps for senses is
rlm@126 75 appropiate -- 1/2 day
rlm@126 76 - [ ] muscle control -- day
rlm@126 77 - [ ] proprioception sensor map in the style of the other senses -- day
rlm@125 78 - [ ] refactor integration code to distribute to each of the senses
rlm@126 79 -- day
rlm@126 80 - [ ] create video showing all the senses for Winston -- 2 days
rlm@126 81 - [ ] write summary of project for Winston \
rlm@126 82 - [ ] project proposals for Winston \
rlm@126 83 - [ ] additional senses to be implemented for Winston | -- 2 days
rlm@126 84 - [ ] send Winston package /
rlm@125 85
rlm@125 86 ** would be cool to get done before winston
rlm@126 87 - [X] enable greyscale bitmaps for touch -- 2 hours
rlm@126 88 - [X] use sawfish to auto-tile sense windows -- 6 hours
rlm@126 89 - [X] sawfish keybinding to automatically delete all sense windows
rlm@126 90 - [ ] directly change the UV-pixels to show sensor activation -- 2
rlm@126 91 days
rlm@126 92 - [ ] proof of concept C sense manipulation -- 2 days
rlm@126 93 - [ ] proof of concept GPU sense manipulation -- week
rlm@126 94 - [ ] fourier view of sound -- 2 or 3 days
rlm@127 95 - [ ] dancing music generator -- 1 day, depends on fourier
rlm@125 96
rlm@125 97 ** don't have to get done before winston
rlm@126 98 - [ ] write tests for integration -- 3 days
rlm@126 99 - [ ] usertime/gametime clock HUD display -- day
rlm@126 100 - [ ] find papers for each of the senses justifying my own
rlm@126 101 representation -- week
rlm@126 102 - [ ] show sensor maps in HUD display? -- 4 days
rlm@126 103 - [ ] show sensor maps in AWT display? -- 2 days
rlm@124 104
rlm@99 105
rlm@73 106 * Intro
rlm@73 107 So far, I've made the following senses --
rlm@73 108 - Vision
rlm@73 109 - Hearing
rlm@73 110 - Touch
rlm@73 111 - Proprioception
rlm@73 112
rlm@73 113 And one effector:
rlm@73 114 - Movement
rlm@73 115
rlm@73 116 However, the code so far has only enabled these senses, but has not
rlm@73 117 actually implemented them. For example, there is still a lot of work
rlm@73 118 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 119 simulation that can be moved around and see the world from different
rlm@73 120 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 121 for the visual input, and I need to determine how/wether to
rlm@73 122 disceritise the visual input.
rlm@73 123
rlm@73 124 I also want to be able to visualize both the sensors and the
rlm@104 125 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 126 first attempt at bringing everything together.
rlm@73 127
rlm@73 128 * The creature's body
rlm@73 129
rlm@73 130 Still going to do an eve-like body in blender, but due to problems
rlm@104 131 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 132 the connecting here in clojure code, using the names of the individual
rlm@73 133 components and trial and error. Later, I'll maybe make some sort of
rlm@73 134 creature-building modifications to blender that support whatever
rlm@73 135 discreitized senses I'm going to make.
rlm@73 136
rlm@73 137 #+name: body-1
rlm@73 138 #+begin_src clojure
rlm@73 139 (ns cortex.silly
rlm@73 140 "let's play!"
rlm@73 141 {:author "Robert McIntyre"})
rlm@73 142
rlm@73 143 ;; TODO remove this!
rlm@73 144 (require 'cortex.import)
rlm@73 145 (cortex.import/mega-import-jme3)
rlm@73 146 (use '(cortex world util body hearing touch vision))
rlm@73 147
rlm@73 148 (rlm.rlm-commands/help)
rlm@99 149 (import java.awt.image.BufferedImage)
rlm@99 150 (import javax.swing.JPanel)
rlm@99 151 (import javax.swing.SwingUtilities)
rlm@99 152 (import java.awt.Dimension)
rlm@99 153 (import javax.swing.JFrame)
rlm@99 154 (import java.awt.Dimension)
rlm@106 155 (import com.aurellem.capture.RatchetTimer)
rlm@99 156 (declare joint-create)
rlm@108 157 (use 'clojure.contrib.def)
rlm@73 158
rlm@100 159 (defn points->image
rlm@100 160 "Take a sparse collection of points and visuliaze it as a
rlm@100 161 BufferedImage."
rlm@102 162
rlm@102 163 ;; TODO maybe parallelize this since it's easy
rlm@102 164
rlm@100 165 [points]
rlm@106 166 (if (empty? points)
rlm@106 167 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
rlm@106 168 (let [xs (vec (map first points))
rlm@106 169 ys (vec (map second points))
rlm@106 170 x0 (apply min xs)
rlm@106 171 y0 (apply min ys)
rlm@106 172 width (- (apply max xs) x0)
rlm@106 173 height (- (apply max ys) y0)
rlm@106 174 image (BufferedImage. (inc width) (inc height)
rlm@119 175 BufferedImage/TYPE_INT_RGB)]
rlm@118 176 (dorun
rlm@118 177 (for [x (range (.getWidth image))
rlm@118 178 y (range (.getHeight image))]
rlm@119 179 (.setRGB image x y 0xFF0000)))
rlm@106 180 (dorun
rlm@106 181 (for [index (range (count points))]
rlm@106 182 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 183
rlm@106 184 image)))
rlm@100 185
rlm@101 186 (defn average [coll]
rlm@101 187 (/ (reduce + coll) (count coll)))
rlm@101 188
rlm@101 189 (defn collapse-1d
rlm@101 190 "One dimensional analogue of collapse"
rlm@101 191 [center line]
rlm@101 192 (let [length (count line)
rlm@101 193 num-above (count (filter (partial < center) line))
rlm@101 194 num-below (- length num-above)]
rlm@101 195 (range (- center num-below)
rlm@115 196 (+ center num-above))))
rlm@99 197
rlm@99 198 (defn collapse
rlm@99 199 "Take a set of pairs of integers and collapse them into a
rlm@99 200 contigous bitmap."
rlm@99 201 [points]
rlm@108 202 (if (empty? points) []
rlm@108 203 (let
rlm@108 204 [num-points (count points)
rlm@108 205 center (vector
rlm@108 206 (int (average (map first points)))
rlm@108 207 (int (average (map first points))))
rlm@108 208 flattened
rlm@108 209 (reduce
rlm@108 210 concat
rlm@108 211 (map
rlm@108 212 (fn [column]
rlm@108 213 (map vector
rlm@108 214 (map first column)
rlm@108 215 (collapse-1d (second center)
rlm@108 216 (map second column))))
rlm@108 217 (partition-by first (sort-by first points))))
rlm@108 218 squeezed
rlm@108 219 (reduce
rlm@108 220 concat
rlm@108 221 (map
rlm@108 222 (fn [row]
rlm@108 223 (map vector
rlm@108 224 (collapse-1d (first center)
rlm@108 225 (map first row))
rlm@108 226 (map second row)))
rlm@108 227 (partition-by second (sort-by second flattened))))
rlm@108 228 relocate
rlm@108 229 (let [min-x (apply min (map first squeezed))
rlm@108 230 min-y (apply min (map second squeezed))]
rlm@108 231 (map (fn [[x y]]
rlm@108 232 [(- x min-x)
rlm@108 233 (- y min-y)])
rlm@108 234 squeezed))]
rlm@115 235 relocate)))
rlm@83 236
rlm@83 237 (defn load-bullet []
rlm@84 238 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 239 (doto sim
rlm@102 240 (.enqueue
rlm@102 241 (fn []
rlm@102 242 (.stop sim)))
rlm@102 243 (.start))))
rlm@83 244
rlm@73 245 (defn load-blender-model
rlm@73 246 "Load a .blend file using an asset folder relative path."
rlm@73 247 [^String model]
rlm@73 248 (.loadModel
rlm@73 249 (doto (asset-manager)
rlm@73 250 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 251 model))
rlm@73 252
rlm@74 253 (defn meta-data [blender-node key]
rlm@74 254 (if-let [data (.getUserData blender-node "properties")]
rlm@74 255 (.findValue data key)
rlm@74 256 nil))
rlm@73 257
rlm@78 258 (defn blender-to-jme
rlm@78 259 "Convert from Blender coordinates to JME coordinates"
rlm@78 260 [#^Vector3f in]
rlm@78 261 (Vector3f. (.getX in)
rlm@78 262 (.getZ in)
rlm@78 263 (- (.getY in))))
rlm@74 264
rlm@79 265 (defn jme-to-blender
rlm@79 266 "Convert from JME coordinates to Blender coordinates"
rlm@79 267 [#^Vector3f in]
rlm@79 268 (Vector3f. (.getX in)
rlm@79 269 (- (.getZ in))
rlm@79 270 (.getY in)))
rlm@79 271
rlm@78 272 (defn joint-targets
rlm@78 273 "Return the two closest two objects to the joint object, ordered
rlm@78 274 from bottom to top according to the joint's rotation."
rlm@78 275 [#^Node parts #^Node joint]
rlm@78 276 (loop [radius (float 0.01)]
rlm@78 277 (let [results (CollisionResults.)]
rlm@78 278 (.collideWith
rlm@78 279 parts
rlm@78 280 (BoundingBox. (.getWorldTranslation joint)
rlm@78 281 radius radius radius)
rlm@78 282 results)
rlm@78 283 (let [targets
rlm@78 284 (distinct
rlm@78 285 (map #(.getGeometry %) results))]
rlm@78 286 (if (>= (count targets) 2)
rlm@78 287 (sort-by
rlm@79 288 #(let [v
rlm@79 289 (jme-to-blender
rlm@79 290 (.mult
rlm@79 291 (.inverse (.getWorldRotation joint))
rlm@79 292 (.subtract (.getWorldTranslation %)
rlm@79 293 (.getWorldTranslation joint))))]
rlm@79 294 (println-repl (.getName %) ":" v)
rlm@79 295 (.dot (Vector3f. 1 1 1)
rlm@79 296 v))
rlm@78 297 (take 2 targets))
rlm@78 298 (recur (float (* radius 2))))))))
rlm@74 299
rlm@87 300 (defn world-to-local
rlm@87 301 "Convert the world coordinates into coordinates relative to the
rlm@87 302 object (i.e. local coordinates), taking into account the rotation
rlm@87 303 of object."
rlm@87 304 [#^Spatial object world-coordinate]
rlm@87 305 (let [out (Vector3f.)]
rlm@88 306 (.worldToLocal object world-coordinate out) out))
rlm@87 307
rlm@96 308 (defn local-to-world
rlm@96 309 "Convert the local coordinates into coordinates into world relative
rlm@96 310 coordinates"
rlm@96 311 [#^Spatial object local-coordinate]
rlm@96 312 (let [world-coordinate (Vector3f.)]
rlm@96 313 (.localToWorld object local-coordinate world-coordinate)
rlm@96 314 world-coordinate))
rlm@96 315
rlm@87 316 (defmulti joint-dispatch
rlm@87 317 "Translate blender pseudo-joints into real JME joints."
rlm@88 318 (fn [constraints & _]
rlm@87 319 (:type constraints)))
rlm@87 320
rlm@87 321 (defmethod joint-dispatch :point
rlm@87 322 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 323 (println-repl "creating POINT2POINT joint")
rlm@87 324 (Point2PointJoint.
rlm@87 325 control-a
rlm@87 326 control-b
rlm@87 327 pivot-a
rlm@87 328 pivot-b))
rlm@87 329
rlm@87 330 (defmethod joint-dispatch :hinge
rlm@87 331 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 332 (println-repl "creating HINGE joint")
rlm@87 333 (let [axis
rlm@87 334 (if-let
rlm@87 335 [axis (:axis constraints)]
rlm@87 336 axis
rlm@87 337 Vector3f/UNIT_X)
rlm@87 338 [limit-1 limit-2] (:limit constraints)
rlm@87 339 hinge-axis
rlm@87 340 (.mult
rlm@87 341 rotation
rlm@87 342 (blender-to-jme axis))]
rlm@87 343 (doto
rlm@87 344 (HingeJoint.
rlm@87 345 control-a
rlm@87 346 control-b
rlm@87 347 pivot-a
rlm@87 348 pivot-b
rlm@87 349 hinge-axis
rlm@87 350 hinge-axis)
rlm@87 351 (.setLimit limit-1 limit-2))))
rlm@87 352
rlm@87 353 (defmethod joint-dispatch :cone
rlm@87 354 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 355 (let [limit-xz (:limit-xz constraints)
rlm@87 356 limit-xy (:limit-xy constraints)
rlm@87 357 twist (:twist constraints)]
rlm@87 358
rlm@87 359 (println-repl "creating CONE joint")
rlm@87 360 (println-repl rotation)
rlm@87 361 (println-repl
rlm@87 362 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 363 (println-repl
rlm@87 364 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 365 (println-repl
rlm@87 366 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 367 (doto
rlm@87 368 (ConeJoint.
rlm@87 369 control-a
rlm@87 370 control-b
rlm@87 371 pivot-a
rlm@87 372 pivot-b
rlm@87 373 rotation
rlm@87 374 rotation)
rlm@87 375 (.setLimit (float limit-xz)
rlm@87 376 (float limit-xy)
rlm@87 377 (float twist)))))
rlm@87 378
rlm@88 379 (defn connect
rlm@87 380 "here are some examples:
rlm@87 381 {:type :point}
rlm@87 382 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 383 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 384
rlm@89 385 {:type :cone :limit-xz 0]
rlm@89 386 :limit-xy 0]
rlm@89 387 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 388 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 389 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 390 control-b (.getControl obj-b RigidBodyControl)
rlm@87 391 joint-center (.getWorldTranslation joint)
rlm@87 392 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 393 pivot-a (world-to-local obj-a joint-center)
rlm@87 394 pivot-b (world-to-local obj-b joint-center)]
rlm@89 395
rlm@87 396 (if-let [constraints
rlm@87 397 (map-vals
rlm@87 398 eval
rlm@87 399 (read-string
rlm@87 400 (meta-data joint "joint")))]
rlm@89 401 ;; A side-effect of creating a joint registers
rlm@89 402 ;; it with both physics objects which in turn
rlm@89 403 ;; will register the joint with the physics system
rlm@89 404 ;; when the simulation is started.
rlm@87 405 (do
rlm@87 406 (println-repl "creating joint between"
rlm@87 407 (.getName obj-a) "and" (.getName obj-b))
rlm@87 408 (joint-dispatch constraints
rlm@87 409 control-a control-b
rlm@87 410 pivot-a pivot-b
rlm@87 411 joint-rotation))
rlm@87 412 (println-repl "could not find joint meta-data!"))))
rlm@87 413
rlm@78 414 (defn assemble-creature [#^Node pieces joints]
rlm@78 415 (dorun
rlm@78 416 (map
rlm@78 417 (fn [geom]
rlm@78 418 (let [physics-control
rlm@78 419 (RigidBodyControl.
rlm@78 420 (HullCollisionShape.
rlm@78 421 (.getMesh geom))
rlm@78 422 (if-let [mass (meta-data geom "mass")]
rlm@78 423 (do
rlm@78 424 (println-repl
rlm@78 425 "setting" (.getName geom) "mass to" (float mass))
rlm@78 426 (float mass))
rlm@78 427 (float 1)))]
rlm@78 428
rlm@78 429 (.addControl geom physics-control)))
rlm@78 430 (filter #(isa? (class %) Geometry )
rlm@78 431 (node-seq pieces))))
rlm@78 432 (dorun
rlm@78 433 (map
rlm@78 434 (fn [joint]
rlm@78 435 (let [[obj-a obj-b]
rlm@78 436 (joint-targets pieces joint)]
rlm@88 437 (connect obj-a obj-b joint)))
rlm@78 438 joints))
rlm@78 439 pieces)
rlm@74 440
rlm@116 441 (declare blender-creature)
rlm@74 442
rlm@78 443 (def hand "Models/creature1/one.blend")
rlm@74 444
rlm@78 445 (def worm "Models/creature1/try-again.blend")
rlm@78 446
rlm@90 447 (def touch "Models/creature1/touch.blend")
rlm@90 448
rlm@90 449 (defn worm-model [] (load-blender-model worm))
rlm@90 450
rlm@80 451 (defn x-ray [#^ColorRGBA color]
rlm@80 452 (doto (Material. (asset-manager)
rlm@80 453 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 454 (.setColor "Color" color)
rlm@80 455 (-> (.getAdditionalRenderState)
rlm@80 456 (.setDepthTest false))))
rlm@80 457
rlm@91 458 (defn colorful []
rlm@91 459 (.getChild (worm-model) "worm-21"))
rlm@90 460
rlm@90 461 (import jme3tools.converters.ImageToAwt)
rlm@90 462
rlm@90 463 (import ij.ImagePlus)
rlm@90 464
rlm@108 465 ;; Every Mesh has many triangles, each with its own index.
rlm@108 466 ;; Every vertex has its own index as well.
rlm@90 467
rlm@108 468 (defn tactile-sensor-image
rlm@110 469 "Return the touch-sensor distribution image in BufferedImage format,
rlm@110 470 or nil if it does not exist."
rlm@91 471 [#^Geometry obj]
rlm@110 472 (if-let [image-path (meta-data obj "touch")]
rlm@110 473 (ImageToAwt/convert
rlm@110 474 (.getImage
rlm@110 475 (.loadTexture
rlm@110 476 (asset-manager)
rlm@110 477 image-path))
rlm@110 478 false false 0)))
rlm@110 479
rlm@91 480 (import ij.process.ImageProcessor)
rlm@91 481 (import java.awt.image.BufferedImage)
rlm@91 482
rlm@92 483 (def white -1)
rlm@94 484
rlm@91 485 (defn filter-pixels
rlm@108 486 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 487 provided. Bounds -> [x0 y0 width height]"
rlm@92 488 {:author "Dylan Holmes"}
rlm@108 489 ([pred #^BufferedImage image]
rlm@108 490 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 491 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 492 ((fn accumulate [x y matches]
rlm@108 493 (cond
rlm@108 494 (>= y (+ height y0)) matches
rlm@108 495 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 496 (pred (.getRGB image x y))
rlm@108 497 (recur (inc x) y (conj matches [x y]))
rlm@108 498 :else (recur (inc x) y matches)))
rlm@108 499 x0 y0 [])))
rlm@91 500
rlm@91 501 (defn white-coordinates
rlm@108 502 "Coordinates of all the white pixels in a subset of the image."
rlm@112 503 ([#^BufferedImage image bounds]
rlm@112 504 (filter-pixels #(= % white) image bounds))
rlm@112 505 ([#^BufferedImage image]
rlm@112 506 (filter-pixels #(= % white) image)))
rlm@108 507
rlm@108 508 (defn triangle
rlm@112 509 "Get the triangle specified by triangle-index from the mesh within
rlm@112 510 bounds."
rlm@108 511 [#^Mesh mesh triangle-index]
rlm@108 512 (let [scratch (Triangle.)]
rlm@108 513 (.getTriangle mesh triangle-index scratch)
rlm@108 514 scratch))
rlm@108 515
rlm@108 516 (defn triangle-vertex-indices
rlm@108 517 "Get the triangle vertex indices of a given triangle from a given
rlm@108 518 mesh."
rlm@108 519 [#^Mesh mesh triangle-index]
rlm@108 520 (let [indices (int-array 3)]
rlm@108 521 (.getTriangle mesh triangle-index indices)
rlm@108 522 (vec indices)))
rlm@108 523
rlm@108 524 (defn vertex-UV-coord
rlm@108 525 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 526 [#^Mesh mesh vertex-index]
rlm@108 527 (let [UV-buffer
rlm@108 528 (.getData
rlm@108 529 (.getBuffer
rlm@108 530 mesh
rlm@108 531 VertexBuffer$Type/TexCoord))]
rlm@108 532 [(.get UV-buffer (* vertex-index 2))
rlm@108 533 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 534
rlm@108 535 (defn triangle-UV-coord
rlm@108 536 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 537 [#^Mesh mesh width height triangle-index]
rlm@108 538 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 539 (map (partial vertex-UV-coord mesh)
rlm@108 540 (triangle-vertex-indices mesh triangle-index))))
rlm@91 541
rlm@102 542 (defn same-side?
rlm@102 543 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 544 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 545 [p1 p2 ref p]
rlm@91 546 (<=
rlm@91 547 0
rlm@91 548 (.dot
rlm@91 549 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 550 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 551
rlm@108 552 (defn triangle-seq [#^Triangle tri]
rlm@108 553 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 554
rlm@108 555 (defn vector3f-seq [#^Vector3f v]
rlm@108 556 [(.getX v) (.getY v) (.getZ v)])
rlm@108 557
rlm@108 558 (defn inside-triangle?
rlm@108 559 "Is the point inside the triangle?"
rlm@108 560 {:author "Dylan Holmes"}
rlm@108 561 [#^Triangle tri #^Vector3f p]
rlm@108 562 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 563 (and
rlm@108 564 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 565 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 566 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 567
rlm@94 568 (defn triangle->matrix4f
rlm@108 569 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 570 contain the vertices of the triangle; the last contains the unit
rlm@108 571 normal of the triangle. The bottom row is filled with 1s."
rlm@94 572 [#^Triangle t]
rlm@94 573 (let [mat (Matrix4f.)
rlm@94 574 [vert-1 vert-2 vert-3]
rlm@94 575 ((comp vec map) #(.get t %) (range 3))
rlm@94 576 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 577 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 578 (dorun
rlm@94 579 (for [row (range 4) col (range 3)]
rlm@94 580 (do
rlm@94 581 (.set mat col row (.get (vertices row)col))
rlm@94 582 (.set mat 3 row 1))))
rlm@94 583 mat))
rlm@94 584
rlm@94 585 (defn triangle-transformation
rlm@94 586 "Returns the affine transformation that converts each vertex in the
rlm@94 587 first triangle into the corresponding vertex in the second
rlm@94 588 triangle."
rlm@94 589 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 590 (.mult
rlm@94 591 (triangle->matrix4f tri-2)
rlm@94 592 (.invert (triangle->matrix4f tri-1))))
rlm@94 593
rlm@108 594 (defn point->vector2f [[u v]]
rlm@108 595 (Vector2f. u v))
rlm@94 596
rlm@94 597 (defn vector2f->vector3f [v]
rlm@94 598 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 599
rlm@94 600 (defn map-triangle [f #^Triangle tri]
rlm@94 601 (Triangle.
rlm@94 602 (f 0 (.get1 tri))
rlm@94 603 (f 1 (.get2 tri))
rlm@94 604 (f 2 (.get3 tri))))
rlm@94 605
rlm@108 606 (defn points->triangle
rlm@108 607 "Convert a list of points into a triangle."
rlm@108 608 [points]
rlm@108 609 (apply #(Triangle. %1 %2 %3)
rlm@108 610 (map (fn [point]
rlm@108 611 (let [point (vec point)]
rlm@108 612 (Vector3f. (get point 0 0)
rlm@108 613 (get point 1 0)
rlm@108 614 (get point 2 0))))
rlm@108 615 (take 3 points))))
rlm@94 616
rlm@108 617 (defn convex-bounds
rlm@128 618 ;;dylan
rlm@128 619 "Returns the smallest square containing the given
rlm@128 620 vertices, as a vector of integers [left top width height]."
rlm@128 621 ;; "Dimensions of the smallest integer bounding square of the list of
rlm@128 622 ;; 2D verticies in the form: [x y width height]."
rlm@108 623 [uv-verts]
rlm@108 624 (let [xs (map first uv-verts)
rlm@108 625 ys (map second uv-verts)
rlm@108 626 x0 (Math/floor (apply min xs))
rlm@108 627 y0 (Math/floor (apply min ys))
rlm@108 628 x1 (Math/ceil (apply max xs))
rlm@108 629 y1 (Math/ceil (apply max ys))]
rlm@108 630 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 631
rlm@106 632 (defn sensors-in-triangle
rlm@128 633 ;;dylan
rlm@128 634 "Locate the touch sensors in the triangle, returning a map of their UV and geometry-relative coordinates."
rlm@128 635 ;;"Find the locations of the touch sensors within a triangle in both
rlm@128 636 ;; UV and gemoetry relative coordinates."
rlm@107 637 [image mesh tri-index]
rlm@107 638 (let [width (.getWidth image)
rlm@108 639 height (.getHeight image)
rlm@108 640 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 641 bounds (convex-bounds UV-vertex-coords)
rlm@108 642
rlm@108 643 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 644 UV-sensor-coords
rlm@108 645 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 646 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 647 (white-coordinates image bounds))
rlm@108 648 UV->geometry (triangle-transformation
rlm@108 649 cutout-triangle
rlm@108 650 (triangle mesh tri-index))
rlm@108 651 geometry-sensor-coords
rlm@108 652 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 653 UV-sensor-coords)]
rlm@108 654 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 655
rlm@108 656 (defn-memo locate-feelers
rlm@94 657 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 658 their positions in geometry-relative coordinates."
rlm@94 659 [#^Geometry geo]
rlm@108 660 (let [mesh (.getMesh geo)
rlm@108 661 num-triangles (.getTriangleCount mesh)]
rlm@108 662 (if-let [image (tactile-sensor-image geo)]
rlm@108 663 (map
rlm@108 664 (partial sensors-in-triangle image mesh)
rlm@108 665 (range num-triangles))
rlm@108 666 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 667
rlm@102 668 (use 'clojure.contrib.def)
rlm@102 669
rlm@102 670 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 671 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 672
rlm@108 673 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 674 (vec (map :geometry (locate-feelers geo))))
rlm@102 675
rlm@97 676 (defn enable-touch [#^Geometry geo]
rlm@108 677 (let [feeler-coords (feeler-coordinates geo)
rlm@96 678 tris (triangles geo)
rlm@109 679 limit 0.1
rlm@109 680 ;;results (CollisionResults.)
rlm@109 681 ]
rlm@111 682 (if (empty? (touch-topology geo))
rlm@111 683 nil
rlm@111 684 (fn [node]
rlm@111 685 (let [sensor-origins
rlm@111 686 (map
rlm@111 687 #(map (partial local-to-world geo) %)
rlm@111 688 feeler-coords)
rlm@111 689 triangle-normals
rlm@111 690 (map (partial get-ray-direction geo)
rlm@111 691 tris)
rlm@111 692 rays
rlm@111 693 (flatten
rlm@111 694 (map (fn [origins norm]
rlm@111 695 (map #(doto (Ray. % norm)
rlm@97 696 (.setLimit limit)) origins))
rlm@111 697 sensor-origins triangle-normals))]
rlm@111 698 (vector
rlm@111 699 (touch-topology geo)
rlm@111 700 (vec
rlm@111 701 (for [ray rays]
rlm@111 702 (do
rlm@111 703 (let [results (CollisionResults.)]
rlm@111 704 (.collideWith node ray results)
rlm@111 705 (let [touch-objects
rlm@126 706 (filter #(not (= geo (.getGeometry %)))
rlm@126 707 results)]
rlm@126 708 (- 255
rlm@126 709 (if (empty? touch-objects) 255
rlm@126 710 (rem
rlm@126 711 (int
rlm@126 712 (* 255 (/ (.getDistance
rlm@126 713 (first touch-objects)) limit)))
rlm@126 714 256))))))))))))))
rlm@126 715
rlm@111 716
rlm@111 717 (defn touch [#^Node pieces]
rlm@111 718 (filter (comp not nil?)
rlm@111 719 (map enable-touch
rlm@111 720 (filter #(isa? (class %) Geometry)
rlm@111 721 (node-seq pieces)))))
rlm@94 722
rlm@109 723
rlm@111 724 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
rlm@111 725 ;; http://en.wikipedia.org/wiki/Retina
rlm@109 726
rlm@111 727 (defn test-eye []
rlm@117 728 (.getChild
rlm@117 729 (.getChild (worm-model) "eyes")
rlm@117 730 "eye"))
rlm@111 731
rlm@111 732
rlm@111 733 (defn retina-sensor-image
rlm@111 734 "Return a map of pixel selection functions to BufferedImages
rlm@111 735 describing the distribution of light-sensitive components on this
rlm@111 736 geometry's surface. Each function creates an integer from the rgb
rlm@111 737 values found in the pixel. :red, :green, :blue, :gray are already
rlm@111 738 defined as extracting the red green blue and average components
rlm@111 739 respectively."
rlm@117 740 [#^Spatial eye]
rlm@111 741 (if-let [eye-map (meta-data eye "eye")]
rlm@111 742 (map-vals
rlm@111 743 #(ImageToAwt/convert
rlm@111 744 (.getImage (.loadTexture (asset-manager) %))
rlm@111 745 false false 0)
rlm@120 746 (eval (read-string eye-map)))))
rlm@111 747
rlm@117 748 (defn eye-dimensions
rlm@117 749 "returns the width and height specified in the metadata of the eye"
rlm@117 750 [#^Spatial eye]
rlm@117 751 (let [dimensions
rlm@117 752 (map #(vector (.getWidth %) (.getHeight %))
rlm@117 753 (vals (retina-sensor-image eye)))]
rlm@117 754 [(apply max (map first dimensions))
rlm@117 755 (apply max (map second dimensions))]))
rlm@117 756
rlm@116 757 (defn creature-eyes
rlm@128 758 ;;dylan
rlm@128 759 "Return the children of the creature's \"eyes\" node."
rlm@128 760 ;;"The eye nodes which are children of the \"eyes\" node in the
rlm@128 761 ;;creature."
rlm@116 762 [#^Node creature]
rlm@116 763 (if-let [eye-node (.getChild creature "eyes")]
rlm@116 764 (seq (.getChildren eye-node))
rlm@116 765 (do (println-repl "could not find eyes node") [])))
rlm@111 766
rlm@123 767 ;; Here's how vision will work.
rlm@112 768
rlm@123 769 ;; Make the continuation in scene-processor take FrameBuffer,
rlm@123 770 ;; byte-buffer, BufferedImage already sized to the correct
rlm@123 771 ;; dimensions. the continuation will decide wether to "mix" them
rlm@123 772 ;; into the BufferedImage, lazily ignore them, or mix them halfway
rlm@123 773 ;; and call c/graphics card routines.
rlm@112 774
rlm@123 775 ;; (vision creature) will take an optional :skip argument which will
rlm@123 776 ;; inform the continuations in scene processor to skip the given
rlm@123 777 ;; number of cycles; 0 means that no cycles will be skipped.
rlm@112 778
rlm@123 779 ;; (vision creature) will return [init-functions sensor-functions].
rlm@123 780 ;; The init-functions are each single-arg functions that take the
rlm@123 781 ;; world and register the cameras and must each be called before the
rlm@123 782 ;; corresponding sensor-functions. Each init-function returns the
rlm@123 783 ;; viewport for that eye which can be manipulated, saved, etc. Each
rlm@123 784 ;; sensor-function is a thunk and will return data in the same
rlm@123 785 ;; format as the tactile-sensor functions; the structure is
rlm@123 786 ;; [topology, sensor-data]. Internally, these sensor-functions
rlm@123 787 ;; maintain a reference to sensor-data which is periodically updated
rlm@123 788 ;; by the continuation function established by its init-function.
rlm@123 789 ;; They can be queried every cycle, but their information may not
rlm@123 790 ;; necessairly be different every cycle.
rlm@112 791
rlm@123 792 ;; Each eye in the creature in blender will work the same way as
rlm@123 793 ;; joints -- a zero dimensional object with no geometry whose local
rlm@123 794 ;; coordinate system determines the orientation of the resulting
rlm@123 795 ;; eye. All eyes will have a parent named "eyes" just as all joints
rlm@123 796 ;; have a parent named "joints". The resulting camera will be a
rlm@123 797 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
rlm@123 798 ;; the eye marker. The eye marker will contain the metadata for the
rlm@123 799 ;; eye, and will be moved by it's bound geometry. The dimensions of
rlm@123 800 ;; the eye's camera are equal to the dimensions of the eye's "UV"
rlm@123 801 ;; map.
rlm@116 802
rlm@123 803
rlm@123 804 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@123 805
rlm@123 806 ;; Ears work the same way as vision.
rlm@123 807
rlm@123 808 ;; (hearing creature) will return [init-functions
rlm@123 809 ;; sensor-functions]. The init functions each take the world and
rlm@123 810 ;; register a SoundProcessor that does foureier transforms on the
rlm@123 811 ;; incommong sound data, making it available to each sensor function.
rlm@123 812
rlm@123 813 (defn creature-ears
rlm@128 814 "Return the children of the creature's \"ears\" node."
rlm@128 815 ;;dylan
rlm@128 816 ;;"The ear nodes which are children of the \"ears\" node in the
rlm@128 817 ;;creature."
rlm@123 818 [#^Node creature]
rlm@123 819 (if-let [ear-node (.getChild creature "ears")]
rlm@123 820 (seq (.getChildren ear-node))
rlm@123 821 (do (println-repl "could not find ears node") [])))
rlm@123 822
rlm@123 823 (defn closest-node
rlm@128 824 "Return the object in creature which is closest to the given node."
rlm@128 825 ;;dylan"The closest object in creature to the given node."
rlm@116 826 [#^Node creature #^Node eye]
rlm@116 827 (loop [radius (float 0.01)]
rlm@116 828 (let [results (CollisionResults.)]
rlm@116 829 (.collideWith
rlm@116 830 creature
rlm@116 831 (BoundingBox. (.getWorldTranslation eye)
rlm@116 832 radius radius radius)
rlm@116 833 results)
rlm@116 834 (if-let [target (first results)]
rlm@116 835 (.getGeometry target)
rlm@116 836 (recur (float (* 2 radius)))))))
rlm@116 837
rlm@128 838 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
rlm@128 839 ;;anchor-qualia, augment-organ, with-organ
rlm@123 840 (defn bind-sense
rlm@123 841 "Bind the sense to the Spatial such that it will maintain its
rlm@117 842 current position relative to the Spatial no matter how the spatial
rlm@123 843 moves. 'sense can be either a Camera or Listener object."
rlm@123 844 [#^Spatial obj sense]
rlm@123 845 (let [sense-offset (.subtract (.getLocation sense)
rlm@123 846 (.getWorldTranslation obj))
rlm@123 847 initial-sense-rotation (Quaternion. (.getRotation sense))
rlm@117 848 base-anti-rotation (.inverse (.getWorldRotation obj))]
rlm@117 849 (.addControl
rlm@117 850 obj
rlm@117 851 (proxy [AbstractControl] []
rlm@117 852 (controlUpdate [tpf]
rlm@117 853 (let [total-rotation
rlm@117 854 (.mult base-anti-rotation (.getWorldRotation obj))]
rlm@123 855 (.setLocation sense
rlm@117 856 (.add
rlm@123 857 (.mult total-rotation sense-offset)
rlm@117 858 (.getWorldTranslation obj)))
rlm@123 859 (.setRotation sense
rlm@123 860 (.mult total-rotation initial-sense-rotation))))
rlm@117 861 (controlRender [_ _])))))
rlm@117 862
rlm@117 863
rlm@123 864 (defn update-listener-velocity
rlm@123 865 "Update the listener's velocity every update loop."
rlm@123 866 [#^Spatial obj #^Listener lis]
rlm@123 867 (let [old-position (atom (.getLocation lis))]
rlm@123 868 (.addControl
rlm@123 869 obj
rlm@123 870 (proxy [AbstractControl] []
rlm@123 871 (controlUpdate [tpf]
rlm@123 872 (let [new-position (.getLocation lis)]
rlm@123 873 (.setVelocity
rlm@123 874 lis
rlm@123 875 (.mult (.subtract new-position @old-position)
rlm@123 876 (float (/ tpf))))
rlm@123 877 (reset! old-position new-position)))
rlm@123 878 (controlRender [_ _])))))
rlm@123 879
rlm@123 880 (import com.aurellem.capture.audio.AudioSendRenderer)
rlm@123 881
rlm@123 882 (defn attach-ear
rlm@123 883 [#^Application world #^Node creature #^Spatial ear continuation]
rlm@123 884 (let [target (closest-node creature ear)
rlm@123 885 lis (Listener.)
rlm@123 886 audio-renderer (.getAudioRenderer world)
rlm@123 887 sp (sound-processor continuation)]
rlm@123 888 (.setLocation lis (.getWorldTranslation ear))
rlm@123 889 (.setRotation lis (.getWorldRotation ear))
rlm@123 890 (bind-sense target lis)
rlm@123 891 (update-listener-velocity target lis)
rlm@123 892 (.addListener audio-renderer lis)
rlm@123 893 (.registerSoundProcessor audio-renderer lis sp)))
rlm@123 894
rlm@123 895 (defn enable-hearing
rlm@123 896 [#^Node creature #^Spatial ear]
rlm@123 897 (let [hearing-data (atom [])]
rlm@123 898 [(fn [world]
rlm@123 899 (attach-ear world creature ear
rlm@123 900 (fn [data]
rlm@123 901 (reset! hearing-data (vec data)))))
rlm@123 902 [(fn []
rlm@123 903 (let [data @hearing-data
rlm@123 904 topology
rlm@123 905 (vec (map #(vector % 0) (range 0 (count data))))
rlm@123 906 scaled-data
rlm@123 907 (vec
rlm@123 908 (map
rlm@123 909 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)
rlm@123 910 data))]
rlm@123 911 [topology scaled-data]))
rlm@123 912 ]]))
rlm@123 913
rlm@123 914 (defn hearing
rlm@123 915 [#^Node creature]
rlm@123 916 (reduce
rlm@123 917 (fn [[init-a senses-a]
rlm@123 918 [init-b senses-b]]
rlm@123 919 [(conj init-a init-b)
rlm@123 920 (into senses-a senses-b)])
rlm@123 921 [[][]]
rlm@123 922 (for [ear (creature-ears creature)]
rlm@123 923 (enable-hearing creature ear))))
rlm@123 924
rlm@118 925 (defn attach-eye
rlm@118 926 "Attach a Camera to the appropiate area and return the Camera."
rlm@118 927 [#^Node creature #^Spatial eye]
rlm@123 928 (let [target (closest-node creature eye)
rlm@118 929 [cam-width cam-height] (eye-dimensions eye)
rlm@118 930 cam (Camera. cam-width cam-height)]
rlm@118 931 (.setLocation cam (.getWorldTranslation eye))
rlm@118 932 (.setRotation cam (.getWorldRotation eye))
rlm@119 933 (.setFrustumPerspective
rlm@119 934 cam 45 (/ (.getWidth cam) (.getHeight cam))
rlm@119 935 1 1000)
rlm@123 936 (bind-sense target cam)
rlm@118 937 cam))
rlm@118 938
rlm@118 939 (def presets
rlm@121 940 {:all 0xFFFFFF
rlm@119 941 :red 0xFF0000
rlm@119 942 :blue 0x0000FF
rlm@119 943 :green 0x00FF00})
rlm@119 944
rlm@118 945 (defn enable-vision
rlm@118 946 "return [init-function sensor-functions] for a particular eye"
rlm@118 947 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
rlm@118 948 (let [retinal-map (retina-sensor-image eye)
rlm@123 949 camera (attach-eye creature eye)
rlm@123 950 vision-image
rlm@123 951 (atom
rlm@123 952 (BufferedImage. (.getWidth camera)
rlm@123 953 (.getHeight camera)
rlm@123 954 BufferedImage/TYPE_BYTE_BINARY))]
rlm@123 955 [(fn [world]
rlm@123 956 (add-eye
rlm@123 957 world camera
rlm@123 958 (let [counter (atom 0)]
rlm@123 959 (fn [r fb bb bi]
rlm@123 960 (if (zero? (rem (swap! counter inc) (inc skip)))
rlm@123 961 (reset! vision-image (BufferedImage! r fb bb bi)))))))
rlm@123 962 (vec
rlm@123 963 (map
rlm@123 964 (fn [[key image]]
rlm@123 965 (let [whites (white-coordinates image)
rlm@123 966 topology (vec (collapse whites))
rlm@123 967 mask (presets key)]
rlm@123 968 (fn []
rlm@123 969 (vector
rlm@123 970 topology
rlm@123 971 (vec
rlm@123 972 (for [[x y] whites]
rlm@123 973 (bit-and
rlm@123 974 mask (.getRGB @vision-image x y))))))))
rlm@123 975 retinal-map))]))
rlm@118 976
rlm@116 977 (defn vision
rlm@121 978 [#^Node creature & {skip :skip :or {skip 0}}]
rlm@121 979 (reduce
rlm@121 980 (fn [[init-a senses-a]
rlm@121 981 [init-b senses-b]]
rlm@121 982 [(conj init-a init-b)
rlm@121 983 (into senses-a senses-b)])
rlm@121 984 [[][]]
rlm@121 985 (for [eye (creature-eyes creature)]
rlm@121 986 (enable-vision creature eye))))
rlm@128 987
rlm@128 988
rlm@128 989
rlm@128 990
rlm@128 991
rlm@128 992 ;; lower level --- nodes
rlm@128 993 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
rlm@128 994
rlm@128 995 ;; higher level -- organs
rlm@128 996 ;;
rlm@128 997
rlm@128 998 ;; higher level --- sense/effector
rlm@128 999 ;; these are the functions that provide world i/o, chinese-room style
rlm@128 1000
rlm@128 1001
rlm@116 1002
rlm@116 1003 (defn blender-creature
rlm@116 1004 "Return a creature with all joints in place."
rlm@116 1005 [blender-path]
rlm@116 1006 (let [model (load-blender-model blender-path)
rlm@116 1007 joints
rlm@116 1008 (if-let [joint-node (.getChild model "joints")]
rlm@116 1009 (seq (.getChildren joint-node))
rlm@116 1010 (do (println-repl "could not find joints node") []))]
rlm@116 1011 (assemble-creature model joints)))
rlm@116 1012
rlm@126 1013 (defn gray-scale [num]
rlm@126 1014 (+ num
rlm@126 1015 (bit-shift-left num 8)
rlm@126 1016 (bit-shift-left num 16)))
rlm@126 1017
rlm@103 1018 (defn debug-window
rlm@103 1019 "creates function that offers a debug view of sensor data"
rlm@103 1020 []
rlm@103 1021 (let [vi (view-image)]
rlm@103 1022 (fn
rlm@103 1023 [[coords sensor-data]]
rlm@103 1024 (let [image (points->image coords)]
rlm@103 1025 (dorun
rlm@103 1026 (for [i (range (count coords))]
rlm@103 1027 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@126 1028 (gray-scale (sensor-data i)))))
rlm@126 1029
rlm@126 1030
rlm@103 1031 (vi image)))))
rlm@103 1032
rlm@118 1033 (defn debug-vision-window
rlm@118 1034 "creates function that offers a debug view of sensor data"
rlm@118 1035 []
rlm@118 1036 (let [vi (view-image)]
rlm@118 1037 (fn
rlm@118 1038 [[coords sensor-data]]
rlm@118 1039 (let [image (points->image coords)]
rlm@118 1040 (dorun
rlm@118 1041 (for [i (range (count coords))]
rlm@118 1042 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@118 1043 (sensor-data i))))
rlm@118 1044 (vi image)))))
rlm@118 1045
rlm@123 1046 (defn debug-hearing-window
rlm@123 1047 "view audio data"
rlm@123 1048 [height]
rlm@123 1049 (let [vi (view-image)]
rlm@123 1050 (fn [[coords sensor-data]]
rlm@123 1051 (let [image (BufferedImage. (count coords) height
rlm@123 1052 BufferedImage/TYPE_INT_RGB)]
rlm@123 1053 (dorun
rlm@123 1054 (for [x (range (count coords))]
rlm@123 1055 (dorun
rlm@123 1056 (for [y (range height)]
rlm@123 1057 (let [raw-sensor (sensor-data x)]
rlm@126 1058 (.setRGB image x y (gray-scale raw-sensor)))))))
rlm@126 1059
rlm@123 1060 (vi image)))))
rlm@123 1061
rlm@123 1062
rlm@123 1063
rlm@106 1064 ;;(defn test-touch [world creature]
rlm@83 1065
rlm@78 1066
rlm@123 1067
rlm@123 1068
rlm@129 1069 ;; here's how motor-control/ proprioception will work:
rlm@129 1070
rlm@129 1071
rlm@129 1072
rlm@129 1073
rlm@129 1074
rlm@123 1075
rlm@123 1076
rlm@106 1077 (defn test-creature [thing]
rlm@106 1078 (let [x-axis
rlm@106 1079 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 1080 y-axis
rlm@106 1081 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 1082 z-axis
rlm@106 1083 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 1084 creature (blender-creature thing)
rlm@106 1085 touch-nerves (touch creature)
rlm@106 1086 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@121 1087 [init-vision-fns vision-data] (vision creature)
rlm@121 1088 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
rlm@118 1089 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@123 1090 [init-hearing-fns hearing-senses] (hearing creature)
rlm@123 1091 hearing-windows (map (fn [_] (debug-hearing-window 50))
rlm@123 1092 hearing-senses)
rlm@124 1093 bell (AudioNode. (asset-manager)
rlm@128 1094 "Sounds/pure.wav" false)
rlm@123 1095 ;; dream
rlm@123 1096
rlm@106 1097 ]
rlm@106 1098 (world
rlm@106 1099 (nodify [creature
rlm@106 1100 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 1101 :color ColorRGBA/Gray :mass 0)
rlm@106 1102 x-axis y-axis z-axis
rlm@118 1103 me
rlm@106 1104 ])
rlm@123 1105 (merge standard-debug-controls
rlm@123 1106 {"key-return"
rlm@123 1107 (fn [_ value]
rlm@123 1108 (if value
rlm@123 1109 (do
rlm@123 1110 (println-repl "play-sound")
rlm@124 1111 (.play bell))))})
rlm@106 1112 (fn [world]
rlm@106 1113 (light-up-everything world)
rlm@106 1114 (enable-debug world)
rlm@122 1115 (dorun (map #(% world) init-vision-fns))
rlm@123 1116 (dorun (map #(% world) init-hearing-fns))
rlm@118 1117
rlm@118 1118 (add-eye world
rlm@118 1119 (attach-eye creature (test-eye))
rlm@118 1120 (comp (view-image) BufferedImage!))
rlm@118 1121
rlm@118 1122 (add-eye world (.getCamera world) no-op)
rlm@118 1123
rlm@106 1124 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 1125 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@110 1126 ;;(.setTimer world (RatchetTimer. 60))
rlm@119 1127 (speed-up world)
rlm@106 1128 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 1129 )
rlm@106 1130 (fn [world tpf]
rlm@109 1131 ;;(dorun
rlm@109 1132 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@123 1133
rlm@123 1134
rlm@123 1135
rlm@106 1136 (dorun
rlm@109 1137 (map #(%1 (%2 (.getRootNode world)))
rlm@121 1138 touch-debug-windows touch-nerves))
rlm@123 1139
rlm@121 1140 (dorun
rlm@121 1141 (map #(%1 (%2))
rlm@121 1142 vision-debug vision-data))
rlm@123 1143 (dorun
rlm@123 1144 (map #(%1 (%2)) hearing-windows hearing-senses))
rlm@123 1145
rlm@123 1146
rlm@118 1147 ;;(println-repl (vision-data))
rlm@118 1148 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@118 1149
rlm@121 1150
rlm@106 1151 )
rlm@106 1152 ;;(let [timer (atom 0)]
rlm@106 1153 ;; (fn [_ _]
rlm@106 1154 ;; (swap! timer inc)
rlm@106 1155 ;; (if (= (rem @timer 60) 0)
rlm@106 1156 ;; (println-repl (float (/ @timer 60))))))
rlm@106 1157 )))
rlm@83 1158
rlm@109 1159
rlm@109 1160
rlm@109 1161
rlm@109 1162
rlm@109 1163
rlm@109 1164
rlm@109 1165
rlm@109 1166
rlm@109 1167 ;;; experiments in collisions
rlm@109 1168
rlm@109 1169
rlm@109 1170
rlm@109 1171 (defn collision-test []
rlm@110 1172 (let [b-radius 1
rlm@110 1173 b-position (Vector3f. 0 0 0)
rlm@109 1174 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 1175 :position b-position
rlm@110 1176 :mass 0)
rlm@110 1177 node (nodify [obj-b])
rlm@110 1178 bounds-b
rlm@110 1179 (doto (Picture.)
rlm@110 1180 (.setHeight 50)
rlm@110 1181 (.setWidth 50)
rlm@110 1182 (.setImage (asset-manager)
rlm@110 1183 "Models/creature1/hand.png"
rlm@110 1184 false
rlm@110 1185 ))
rlm@110 1186
rlm@110 1187 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
rlm@110 1188
rlm@110 1189 collisions
rlm@110 1190 (let [cr (CollisionResults.)]
rlm@110 1191 (.collideWith node bounds-b cr)
rlm@110 1192 (println (map #(.getContactPoint %) cr))
rlm@110 1193 cr)
rlm@110 1194
rlm@110 1195 ;;collision-points
rlm@110 1196 ;;(map #(sphere 0.1 :position (.getContactPoint %))
rlm@110 1197 ;; collisions)
rlm@110 1198
rlm@110 1199 ;;node (nodify (conj collision-points obj-b))
rlm@110 1200
rlm@109 1201 sim
rlm@109 1202 (world node
rlm@110 1203 {"key-space"
rlm@129 1204 vvvvvv (fn [_ value]
rlm@110 1205 (if value
rlm@110 1206 (let [cr (CollisionResults.)]
rlm@110 1207 (.collideWith node bounds-b cr)
rlm@110 1208 (println-repl (map #(.getContactPoint %) cr))
rlm@110 1209 cr)))}
rlm@109 1210 no-op
rlm@109 1211 no-op)
rlm@109 1212
rlm@109 1213 ]
rlm@110 1214 sim
rlm@109 1215
rlm@109 1216 ))
rlm@109 1217
rlm@116 1218
rlm@116 1219 ;; the camera will stay in its initial position/rotation with relation
rlm@116 1220 ;; to the spatial.
rlm@116 1221
rlm@116 1222
rlm@117 1223 (defn follow-test
rlm@117 1224 "show a camera that stays in the same relative position to a blue cube."
rlm@117 1225 []
rlm@116 1226 (let [camera-pos (Vector3f. 0 30 0)
rlm@116 1227 rock (box 1 1 1 :color ColorRGBA/Blue
rlm@116 1228 :position (Vector3f. 0 10 0)
rlm@116 1229 :mass 30
rlm@116 1230 )
rlm@118 1231 rot (.getWorldRotation rock)
rlm@116 1232
rlm@116 1233 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
rlm@116 1234 :position (Vector3f. 0 -3 0))]
rlm@116 1235
rlm@116 1236 (world
rlm@116 1237 (nodify [rock table])
rlm@116 1238 standard-debug-controls
rlm@116 1239 (fn [world]
rlm@116 1240 (let
rlm@116 1241 [cam (doto (.clone (.getCamera world))
rlm@116 1242 (.setLocation camera-pos)
rlm@116 1243 (.lookAt Vector3f/ZERO
rlm@116 1244 Vector3f/UNIT_X))]
rlm@123 1245 (bind-sense rock cam)
rlm@116 1246
rlm@116 1247 (.setTimer world (RatchetTimer. 60))
rlm@116 1248 (add-eye world cam (comp (view-image) BufferedImage!))
rlm@116 1249 (add-eye world (.getCamera world) no-op))
rlm@116 1250 )
rlm@118 1251 (fn [_ _] (println-repl rot)))))
rlm@116 1252
rlm@118 1253
rlm@123 1254
rlm@87 1255 #+end_src
rlm@83 1256
rlm@87 1257 #+results: body-1
rlm@109 1258 : #'cortex.silly/test-creature
rlm@78 1259
rlm@78 1260
rlm@78 1261 * COMMENT purgatory
rlm@78 1262 #+begin_src clojure
rlm@77 1263 (defn bullet-trans []
rlm@77 1264 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 1265 :position (Vector3f. -10 5 0))
rlm@77 1266 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 1267 :position (Vector3f. -10 -5 0)
rlm@77 1268 :mass 0)
rlm@77 1269 control-a (.getControl obj-a RigidBodyControl)
rlm@77 1270 control-b (.getControl obj-b RigidBodyControl)
rlm@77 1271 swivel
rlm@77 1272 (.toRotationMatrix
rlm@77 1273 (doto (Quaternion.)
rlm@77 1274 (.fromAngleAxis (/ Math/PI 2)
rlm@77 1275 Vector3f/UNIT_X)))]
rlm@77 1276 (doto
rlm@77 1277 (ConeJoint.
rlm@77 1278 control-a control-b
rlm@77 1279 (Vector3f. 0 5 0)
rlm@77 1280 (Vector3f. 0 -5 0)
rlm@77 1281 swivel swivel)
rlm@77 1282 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 1283 (/ Math/PI 4)
rlm@77 1284 (* Math/PI 0.8)))
rlm@77 1285 (world (nodify
rlm@77 1286 [obj-a obj-b])
rlm@77 1287 standard-debug-controls
rlm@77 1288 enable-debug
rlm@77 1289 no-op)))
rlm@74 1290
rlm@74 1291
rlm@77 1292 (defn bullet-trans* []
rlm@77 1293 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 1294 :position (Vector3f. 5 0 0)
rlm@77 1295 :mass 90)
rlm@77 1296 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 1297 :position (Vector3f. -5 0 0)
rlm@77 1298 :mass 0)
rlm@77 1299 control-a (.getControl obj-a RigidBodyControl)
rlm@77 1300 control-b (.getControl obj-b RigidBodyControl)
rlm@77 1301 move-up? (atom nil)
rlm@77 1302 move-down? (atom nil)
rlm@77 1303 move-left? (atom nil)
rlm@77 1304 move-right? (atom nil)
rlm@77 1305 roll-left? (atom nil)
rlm@77 1306 roll-right? (atom nil)
rlm@77 1307 force 100
rlm@77 1308 swivel
rlm@77 1309 (.toRotationMatrix
rlm@77 1310 (doto (Quaternion.)
rlm@77 1311 (.fromAngleAxis (/ Math/PI 2)
rlm@77 1312 Vector3f/UNIT_X)))
rlm@77 1313 x-move
rlm@77 1314 (doto (Matrix3f.)
rlm@77 1315 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 1316 (.normalize (Vector3f. 1 1 0))))
rlm@77 1317
rlm@77 1318 timer (atom 0)]
rlm@77 1319 (doto
rlm@77 1320 (ConeJoint.
rlm@77 1321 control-a control-b
rlm@77 1322 (Vector3f. -8 0 0)
rlm@77 1323 (Vector3f. 2 0 0)
rlm@77 1324 ;;swivel swivel
rlm@77 1325 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 1326 x-move Matrix3f/IDENTITY
rlm@77 1327 )
rlm@77 1328 (.setCollisionBetweenLinkedBodys false)
rlm@77 1329 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 1330 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 1331 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 1332 (world (nodify
rlm@77 1333 [obj-a obj-b])
rlm@77 1334 (merge standard-debug-controls
rlm@77 1335 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 1336 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 1337 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 1338 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 1339 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 1340 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 1341
rlm@77 1342 (fn [world]
rlm@77 1343 (enable-debug world)
rlm@77 1344 (set-gravity world Vector3f/ZERO)
rlm@77 1345 )
rlm@77 1346
rlm@77 1347 (fn [world _]
rlm@77 1348
rlm@77 1349 (if @move-up?
rlm@77 1350 (.applyForce control-a
rlm@77 1351 (Vector3f. force 0 0)
rlm@77 1352 (Vector3f. 0 0 0)))
rlm@77 1353 (if @move-down?
rlm@77 1354 (.applyForce control-a
rlm@77 1355 (Vector3f. (- force) 0 0)
rlm@77 1356 (Vector3f. 0 0 0)))
rlm@77 1357 (if @move-left?
rlm@77 1358 (.applyForce control-a
rlm@77 1359 (Vector3f. 0 force 0)
rlm@77 1360 (Vector3f. 0 0 0)))
rlm@77 1361 (if @move-right?
rlm@77 1362 (.applyForce control-a
rlm@77 1363 (Vector3f. 0 (- force) 0)
rlm@77 1364 (Vector3f. 0 0 0)))
rlm@77 1365
rlm@77 1366 (if @roll-left?
rlm@77 1367 (.applyForce control-a
rlm@77 1368 (Vector3f. 0 0 force)
rlm@77 1369 (Vector3f. 0 0 0)))
rlm@77 1370 (if @roll-right?
rlm@77 1371 (.applyForce control-a
rlm@77 1372 (Vector3f. 0 0 (- force))
rlm@77 1373 (Vector3f. 0 0 0)))
rlm@77 1374
rlm@77 1375 (if (zero? (rem (swap! timer inc) 100))
rlm@77 1376 (.attachChild
rlm@77 1377 (.getRootNode world)
rlm@77 1378 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 1379 :physical? false :position
rlm@77 1380 (.getWorldTranslation obj-a)))))
rlm@77 1381 )
rlm@77 1382 ))
rlm@77 1383
rlm@94 1384 (defn transform-trianglesdsd
rlm@94 1385 "Transform that converts each vertex in the first triangle
rlm@94 1386 into the corresponding vertex in the second triangle."
rlm@94 1387 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 1388 (let [in [(.get1 tri-1)
rlm@94 1389 (.get2 tri-1)
rlm@94 1390 (.get3 tri-1)]
rlm@94 1391 out [(.get1 tri-2)
rlm@94 1392 (.get2 tri-2)
rlm@94 1393 (.get3 tri-2)]]
rlm@94 1394 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 1395 in* [(.mult translate (in 0))
rlm@94 1396 (.mult translate (in 1))
rlm@94 1397 (.mult translate (in 2))]
rlm@94 1398 final-translation
rlm@94 1399 (doto (Matrix4f.)
rlm@94 1400 (.setTranslation (out 1)))
rlm@94 1401
rlm@94 1402 rotate-1
rlm@94 1403 (doto (Matrix3f.)
rlm@94 1404 (.fromStartEndVectors
rlm@94 1405 (.normalize
rlm@94 1406 (.subtract
rlm@94 1407 (in* 1) (in* 0)))
rlm@94 1408 (.normalize
rlm@94 1409 (.subtract
rlm@94 1410 (out 1) (out 0)))))
rlm@94 1411 in** [(.mult rotate-1 (in* 0))
rlm@94 1412 (.mult rotate-1 (in* 1))
rlm@94 1413 (.mult rotate-1 (in* 2))]
rlm@94 1414 scale-factor-1
rlm@94 1415 (.mult
rlm@94 1416 (.normalize
rlm@94 1417 (.subtract
rlm@94 1418 (out 1)
rlm@94 1419 (out 0)))
rlm@94 1420 (/ (.length
rlm@94 1421 (.subtract (out 1)
rlm@94 1422 (out 0)))
rlm@94 1423 (.length
rlm@94 1424 (.subtract (in** 1)
rlm@94 1425 (in** 0)))))
rlm@94 1426 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 1427 in*** [(.mult scale-1 (in** 0))
rlm@94 1428 (.mult scale-1 (in** 1))
rlm@94 1429 (.mult scale-1 (in** 2))]
rlm@94 1430
rlm@94 1431
rlm@94 1432
rlm@94 1433
rlm@94 1434
rlm@94 1435 ]
rlm@94 1436
rlm@94 1437 (dorun (map println in))
rlm@94 1438 (println)
rlm@94 1439 (dorun (map println in*))
rlm@94 1440 (println)
rlm@94 1441 (dorun (map println in**))
rlm@94 1442 (println)
rlm@94 1443 (dorun (map println in***))
rlm@94 1444 (println)
rlm@94 1445
rlm@99 1446 ))))
rlm@94 1447
rlm@94 1448
rlm@106 1449 (defn world-setup [joint]
rlm@106 1450 (let [joint-position (Vector3f. 0 0 0)
rlm@106 1451 joint-rotation
rlm@106 1452 (.toRotationMatrix
rlm@106 1453 (.mult
rlm@106 1454 (doto (Quaternion.)
rlm@106 1455 (.fromAngleAxis
rlm@106 1456 (* 1 (/ Math/PI 4))
rlm@106 1457 (Vector3f. -1 0 0)))
rlm@106 1458 (doto (Quaternion.)
rlm@106 1459 (.fromAngleAxis
rlm@106 1460 (* 1 (/ Math/PI 2))
rlm@106 1461 (Vector3f. 0 0 1)))))
rlm@106 1462 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 1463
rlm@106 1464 origin (doto
rlm@106 1465 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 1466 :position top-position))
rlm@106 1467 top (doto
rlm@106 1468 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 1469 :position top-position)
rlm@106 1470
rlm@106 1471 (.addControl
rlm@106 1472 (RigidBodyControl.
rlm@106 1473 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1474 bottom (doto
rlm@106 1475 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1476 :position (Vector3f. 0 0 0))
rlm@106 1477 (.addControl
rlm@106 1478 (RigidBodyControl.
rlm@106 1479 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1480 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1481 :color ColorRGBA/Gray :mass 0)
rlm@106 1482 a (.getControl top RigidBodyControl)
rlm@106 1483 b (.getControl bottom RigidBodyControl)]
rlm@106 1484
rlm@106 1485 (cond
rlm@106 1486 (= joint :cone)
rlm@106 1487
rlm@106 1488 (doto (ConeJoint.
rlm@106 1489 a b
rlm@106 1490 (world-to-local top joint-position)
rlm@106 1491 (world-to-local bottom joint-position)
rlm@106 1492 joint-rotation
rlm@106 1493 joint-rotation
rlm@106 1494 )
rlm@106 1495
rlm@106 1496
rlm@106 1497 (.setLimit (* (/ 10) Math/PI)
rlm@106 1498 (* (/ 4) Math/PI)
rlm@106 1499 0)))
rlm@106 1500 [origin top bottom table]))
rlm@106 1501
rlm@106 1502 (defn test-joint [joint]
rlm@106 1503 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1504 control (.getControl top RigidBodyControl)
rlm@106 1505 move-up? (atom false)
rlm@106 1506 move-down? (atom false)
rlm@106 1507 move-left? (atom false)
rlm@106 1508 move-right? (atom false)
rlm@106 1509 roll-left? (atom false)
rlm@106 1510 roll-right? (atom false)
rlm@106 1511 timer (atom 0)]
rlm@106 1512
rlm@106 1513 (world
rlm@106 1514 (nodify [top bottom floor origin])
rlm@106 1515 (merge standard-debug-controls
rlm@106 1516 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1517 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1518 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1519 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1520 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1521 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1522
rlm@106 1523 (fn [world]
rlm@106 1524 (light-up-everything world)
rlm@106 1525 (enable-debug world)
rlm@106 1526 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1527 )
rlm@106 1528
rlm@106 1529 (fn [world _]
rlm@106 1530 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1531 (do
rlm@106 1532 ;; (println-repl @timer)
rlm@106 1533 (.attachChild (.getRootNode world)
rlm@106 1534 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1535 :position (.getWorldTranslation top)
rlm@106 1536 :physical? false))
rlm@106 1537 (.attachChild (.getRootNode world)
rlm@106 1538 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1539 :position (.getWorldTranslation bottom)
rlm@106 1540 :physical? false))))
rlm@106 1541
rlm@106 1542 (if @move-up?
rlm@106 1543 (.applyTorque control
rlm@106 1544 (.mult (.getPhysicsRotation control)
rlm@106 1545 (Vector3f. 0 0 10))))
rlm@106 1546 (if @move-down?
rlm@106 1547 (.applyTorque control
rlm@106 1548 (.mult (.getPhysicsRotation control)
rlm@106 1549 (Vector3f. 0 0 -10))))
rlm@106 1550 (if @move-left?
rlm@106 1551 (.applyTorque control
rlm@106 1552 (.mult (.getPhysicsRotation control)
rlm@106 1553 (Vector3f. 0 10 0))))
rlm@106 1554 (if @move-right?
rlm@106 1555 (.applyTorque control
rlm@106 1556 (.mult (.getPhysicsRotation control)
rlm@106 1557 (Vector3f. 0 -10 0))))
rlm@106 1558 (if @roll-left?
rlm@106 1559 (.applyTorque control
rlm@106 1560 (.mult (.getPhysicsRotation control)
rlm@106 1561 (Vector3f. -1 0 0))))
rlm@106 1562 (if @roll-right?
rlm@106 1563 (.applyTorque control
rlm@106 1564 (.mult (.getPhysicsRotation control)
rlm@106 1565 (Vector3f. 1 0 0))))))))
rlm@106 1566
rlm@99 1567
rlm@99 1568
rlm@107 1569 (defprotocol Frame
rlm@107 1570 (frame [this]))
rlm@107 1571
rlm@107 1572 (extend-type BufferedImage
rlm@107 1573 Frame
rlm@107 1574 (frame [image]
rlm@107 1575 (merge
rlm@107 1576 (apply
rlm@107 1577 hash-map
rlm@107 1578 (interleave
rlm@107 1579 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1580 (vector x y)))
rlm@107 1581 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1582 (let [data (.getRGB image x y)]
rlm@107 1583 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1584 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1585 :b (bit-and 0x0000ff data)))))))
rlm@107 1586 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1587
rlm@107 1588
rlm@107 1589 (extend-type ImagePlus
rlm@107 1590 Frame
rlm@107 1591 (frame [image+]
rlm@107 1592 (frame (.getBufferedImage image+))))
rlm@107 1593
rlm@107 1594
rlm@99 1595 #+end_src
rlm@99 1596
rlm@99 1597
rlm@99 1598 * COMMENT generate source
rlm@99 1599 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1600 <<body-1>>
rlm@99 1601 #+end_src
rlm@99 1602
rlm@99 1603
rlm@94 1604
rlm@94 1605