annotate org/test-creature.org @ 109:c05d8d222166

ready to start touch UV maps with GIMP
author Robert McIntyre <rlm@mit.edu>
date Mon, 16 Jan 2012 03:19:15 -0700
parents 92b857b6145d
children f89f0b9ed2fe
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@99 9 * objectives
rlm@103 10 - [X] get an overall bitmap-like image for touch
rlm@103 11 - [X] write code to visuliaze this bitmap
rlm@99 12 - [ ] directly change the UV-pixels to show touch sensor activation
rlm@99 13 - [ ] write an explination for why b&w bitmaps for senses is appropiate
rlm@99 14 - [ ] clean up touch code and write visulazation test
rlm@99 15 - [ ] do the same for eyes
rlm@99 16
rlm@73 17 * Intro
rlm@73 18 So far, I've made the following senses --
rlm@73 19 - Vision
rlm@73 20 - Hearing
rlm@73 21 - Touch
rlm@73 22 - Proprioception
rlm@73 23
rlm@73 24 And one effector:
rlm@73 25 - Movement
rlm@73 26
rlm@73 27 However, the code so far has only enabled these senses, but has not
rlm@73 28 actually implemented them. For example, there is still a lot of work
rlm@73 29 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 30 simulation that can be moved around and see the world from different
rlm@73 31 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 32 for the visual input, and I need to determine how/wether to
rlm@73 33 disceritise the visual input.
rlm@73 34
rlm@73 35 I also want to be able to visualize both the sensors and the
rlm@104 36 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 37 first attempt at bringing everything together.
rlm@73 38
rlm@73 39 * The creature's body
rlm@73 40
rlm@73 41 Still going to do an eve-like body in blender, but due to problems
rlm@104 42 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 43 the connecting here in clojure code, using the names of the individual
rlm@73 44 components and trial and error. Later, I'll maybe make some sort of
rlm@73 45 creature-building modifications to blender that support whatever
rlm@73 46 discreitized senses I'm going to make.
rlm@73 47
rlm@73 48 #+name: body-1
rlm@73 49 #+begin_src clojure
rlm@73 50 (ns cortex.silly
rlm@73 51 "let's play!"
rlm@73 52 {:author "Robert McIntyre"})
rlm@73 53
rlm@73 54 ;; TODO remove this!
rlm@73 55 (require 'cortex.import)
rlm@73 56 (cortex.import/mega-import-jme3)
rlm@73 57 (use '(cortex world util body hearing touch vision))
rlm@73 58
rlm@73 59 (rlm.rlm-commands/help)
rlm@99 60 (import java.awt.image.BufferedImage)
rlm@99 61 (import javax.swing.JPanel)
rlm@99 62 (import javax.swing.SwingUtilities)
rlm@99 63 (import java.awt.Dimension)
rlm@99 64 (import javax.swing.JFrame)
rlm@99 65 (import java.awt.Dimension)
rlm@106 66 (import com.aurellem.capture.RatchetTimer)
rlm@99 67 (declare joint-create)
rlm@108 68 (use 'clojure.contrib.def)
rlm@73 69
rlm@99 70 (defn view-image
rlm@99 71 "Initailizes a JPanel on which you may draw a BufferedImage.
rlm@99 72 Returns a function that accepts a BufferedImage and draws it to the
rlm@99 73 JPanel."
rlm@99 74 []
rlm@99 75 (let [image
rlm@99 76 (atom
rlm@99 77 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
rlm@99 78 panel
rlm@99 79 (proxy [JPanel] []
rlm@99 80 (paint
rlm@99 81 [graphics]
rlm@99 82 (proxy-super paintComponent graphics)
rlm@99 83 (.drawImage graphics @image 0 0 nil)))
rlm@99 84 frame (JFrame. "Display Image")]
rlm@99 85 (SwingUtilities/invokeLater
rlm@99 86 (fn []
rlm@99 87 (doto frame
rlm@99 88 (-> (.getContentPane) (.add panel))
rlm@99 89 (.pack)
rlm@99 90 (.setLocationRelativeTo nil)
rlm@99 91 (.setResizable true)
rlm@99 92 (.setVisible true))))
rlm@99 93 (fn [#^BufferedImage i]
rlm@99 94 (reset! image i)
rlm@99 95 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
rlm@99 96 (.repaint panel 0 0 (.getWidth i) (.getHeight i)))))
rlm@99 97
rlm@100 98 (defn points->image
rlm@100 99 "Take a sparse collection of points and visuliaze it as a
rlm@100 100 BufferedImage."
rlm@102 101
rlm@102 102 ;; TODO maybe parallelize this since it's easy
rlm@102 103
rlm@100 104 [points]
rlm@106 105 (if (empty? points)
rlm@106 106 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
rlm@106 107 (let [xs (vec (map first points))
rlm@106 108 ys (vec (map second points))
rlm@106 109 x0 (apply min xs)
rlm@106 110 y0 (apply min ys)
rlm@106 111 width (- (apply max xs) x0)
rlm@106 112 height (- (apply max ys) y0)
rlm@106 113 image (BufferedImage. (inc width) (inc height)
rlm@106 114 BufferedImage/TYPE_BYTE_BINARY)]
rlm@106 115 (dorun
rlm@106 116 (for [index (range (count points))]
rlm@106 117 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 118
rlm@106 119 image)))
rlm@100 120
rlm@101 121 (defn test-data
rlm@101 122 []
rlm@101 123 (vec
rlm@102 124 (for [a (range 0 1000 2)
rlm@102 125 b (range 0 1000 2)]
rlm@101 126 (vector a b))
rlm@101 127 ))
rlm@99 128
rlm@101 129 (defn average [coll]
rlm@101 130 (/ (reduce + coll) (count coll)))
rlm@101 131
rlm@101 132 (defn collapse-1d
rlm@101 133 "One dimensional analogue of collapse"
rlm@101 134 [center line]
rlm@101 135 (let [length (count line)
rlm@101 136 num-above (count (filter (partial < center) line))
rlm@101 137 num-below (- length num-above)]
rlm@101 138 (range (- center num-below)
rlm@101 139 (+ center num-above))
rlm@101 140 ))
rlm@99 141
rlm@99 142 (defn collapse
rlm@99 143 "Take a set of pairs of integers and collapse them into a
rlm@99 144 contigous bitmap."
rlm@99 145 [points]
rlm@108 146 (if (empty? points) []
rlm@108 147 (let
rlm@108 148 [num-points (count points)
rlm@108 149 center (vector
rlm@108 150 (int (average (map first points)))
rlm@108 151 (int (average (map first points))))
rlm@108 152 flattened
rlm@108 153 (reduce
rlm@108 154 concat
rlm@108 155 (map
rlm@108 156 (fn [column]
rlm@108 157 (map vector
rlm@108 158 (map first column)
rlm@108 159 (collapse-1d (second center)
rlm@108 160 (map second column))))
rlm@108 161 (partition-by first (sort-by first points))))
rlm@108 162 squeezed
rlm@108 163 (reduce
rlm@108 164 concat
rlm@108 165 (map
rlm@108 166 (fn [row]
rlm@108 167 (map vector
rlm@108 168 (collapse-1d (first center)
rlm@108 169 (map first row))
rlm@108 170 (map second row)))
rlm@108 171 (partition-by second (sort-by second flattened))))
rlm@108 172 relocate
rlm@108 173 (let [min-x (apply min (map first squeezed))
rlm@108 174 min-y (apply min (map second squeezed))]
rlm@108 175 (map (fn [[x y]]
rlm@108 176 [(- x min-x)
rlm@108 177 (- y min-y)])
rlm@108 178 squeezed))]
rlm@108 179 relocate
rlm@108 180 )))
rlm@83 181
rlm@83 182 (defn load-bullet []
rlm@84 183 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 184 (doto sim
rlm@102 185 (.enqueue
rlm@102 186 (fn []
rlm@102 187 (.stop sim)))
rlm@102 188 (.start))))
rlm@83 189
rlm@73 190 (defn load-blender-model
rlm@73 191 "Load a .blend file using an asset folder relative path."
rlm@73 192 [^String model]
rlm@73 193 (.loadModel
rlm@73 194 (doto (asset-manager)
rlm@73 195 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 196 model))
rlm@73 197
rlm@74 198 (defn meta-data [blender-node key]
rlm@74 199 (if-let [data (.getUserData blender-node "properties")]
rlm@74 200 (.findValue data key)
rlm@74 201 nil))
rlm@73 202
rlm@78 203 (defn blender-to-jme
rlm@78 204 "Convert from Blender coordinates to JME coordinates"
rlm@78 205 [#^Vector3f in]
rlm@78 206 (Vector3f. (.getX in)
rlm@78 207 (.getZ in)
rlm@78 208 (- (.getY in))))
rlm@74 209
rlm@79 210 (defn jme-to-blender
rlm@79 211 "Convert from JME coordinates to Blender coordinates"
rlm@79 212 [#^Vector3f in]
rlm@79 213 (Vector3f. (.getX in)
rlm@79 214 (- (.getZ in))
rlm@79 215 (.getY in)))
rlm@79 216
rlm@78 217 (defn joint-targets
rlm@78 218 "Return the two closest two objects to the joint object, ordered
rlm@78 219 from bottom to top according to the joint's rotation."
rlm@78 220 [#^Node parts #^Node joint]
rlm@78 221 ;;(println (meta-data joint "joint"))
rlm@78 222 (.getWorldRotation joint)
rlm@78 223 (loop [radius (float 0.01)]
rlm@78 224 (let [results (CollisionResults.)]
rlm@78 225 (.collideWith
rlm@78 226 parts
rlm@78 227 (BoundingBox. (.getWorldTranslation joint)
rlm@78 228 radius radius radius)
rlm@78 229 results)
rlm@78 230 (let [targets
rlm@78 231 (distinct
rlm@78 232 (map #(.getGeometry %) results))]
rlm@78 233 (if (>= (count targets) 2)
rlm@78 234 (sort-by
rlm@79 235 #(let [v
rlm@79 236 (jme-to-blender
rlm@79 237 (.mult
rlm@79 238 (.inverse (.getWorldRotation joint))
rlm@79 239 (.subtract (.getWorldTranslation %)
rlm@79 240 (.getWorldTranslation joint))))]
rlm@79 241 (println-repl (.getName %) ":" v)
rlm@79 242 (.dot (Vector3f. 1 1 1)
rlm@79 243 v))
rlm@78 244 (take 2 targets))
rlm@78 245 (recur (float (* radius 2))))))))
rlm@74 246
rlm@87 247 (defn world-to-local
rlm@87 248 "Convert the world coordinates into coordinates relative to the
rlm@87 249 object (i.e. local coordinates), taking into account the rotation
rlm@87 250 of object."
rlm@87 251 [#^Spatial object world-coordinate]
rlm@87 252 (let [out (Vector3f.)]
rlm@88 253 (.worldToLocal object world-coordinate out) out))
rlm@87 254
rlm@96 255 (defn local-to-world
rlm@96 256 "Convert the local coordinates into coordinates into world relative
rlm@96 257 coordinates"
rlm@96 258 [#^Spatial object local-coordinate]
rlm@96 259 (let [world-coordinate (Vector3f.)]
rlm@96 260 (.localToWorld object local-coordinate world-coordinate)
rlm@96 261 world-coordinate))
rlm@96 262
rlm@96 263
rlm@87 264 (defmulti joint-dispatch
rlm@87 265 "Translate blender pseudo-joints into real JME joints."
rlm@88 266 (fn [constraints & _]
rlm@87 267 (:type constraints)))
rlm@87 268
rlm@87 269 (defmethod joint-dispatch :point
rlm@87 270 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 271 (println-repl "creating POINT2POINT joint")
rlm@87 272 (Point2PointJoint.
rlm@87 273 control-a
rlm@87 274 control-b
rlm@87 275 pivot-a
rlm@87 276 pivot-b))
rlm@87 277
rlm@87 278 (defmethod joint-dispatch :hinge
rlm@87 279 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 280 (println-repl "creating HINGE joint")
rlm@87 281 (let [axis
rlm@87 282 (if-let
rlm@87 283 [axis (:axis constraints)]
rlm@87 284 axis
rlm@87 285 Vector3f/UNIT_X)
rlm@87 286 [limit-1 limit-2] (:limit constraints)
rlm@87 287 hinge-axis
rlm@87 288 (.mult
rlm@87 289 rotation
rlm@87 290 (blender-to-jme axis))]
rlm@87 291 (doto
rlm@87 292 (HingeJoint.
rlm@87 293 control-a
rlm@87 294 control-b
rlm@87 295 pivot-a
rlm@87 296 pivot-b
rlm@87 297 hinge-axis
rlm@87 298 hinge-axis)
rlm@87 299 (.setLimit limit-1 limit-2))))
rlm@87 300
rlm@87 301 (defmethod joint-dispatch :cone
rlm@87 302 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 303 (let [limit-xz (:limit-xz constraints)
rlm@87 304 limit-xy (:limit-xy constraints)
rlm@87 305 twist (:twist constraints)]
rlm@87 306
rlm@87 307 (println-repl "creating CONE joint")
rlm@87 308 (println-repl rotation)
rlm@87 309 (println-repl
rlm@87 310 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 311 (println-repl
rlm@87 312 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 313 (println-repl
rlm@87 314 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 315 (doto
rlm@87 316 (ConeJoint.
rlm@87 317 control-a
rlm@87 318 control-b
rlm@87 319 pivot-a
rlm@87 320 pivot-b
rlm@87 321 rotation
rlm@87 322 rotation)
rlm@87 323 (.setLimit (float limit-xz)
rlm@87 324 (float limit-xy)
rlm@87 325 (float twist)))))
rlm@87 326
rlm@88 327 (defn connect
rlm@87 328 "here are some examples:
rlm@87 329 {:type :point}
rlm@87 330 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 331 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 332
rlm@89 333 {:type :cone :limit-xz 0]
rlm@89 334 :limit-xy 0]
rlm@89 335 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 336 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 337 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 338 control-b (.getControl obj-b RigidBodyControl)
rlm@87 339 joint-center (.getWorldTranslation joint)
rlm@87 340 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 341 pivot-a (world-to-local obj-a joint-center)
rlm@87 342 pivot-b (world-to-local obj-b joint-center)]
rlm@89 343
rlm@87 344 (if-let [constraints
rlm@87 345 (map-vals
rlm@87 346 eval
rlm@87 347 (read-string
rlm@87 348 (meta-data joint "joint")))]
rlm@89 349 ;; A side-effect of creating a joint registers
rlm@89 350 ;; it with both physics objects which in turn
rlm@89 351 ;; will register the joint with the physics system
rlm@89 352 ;; when the simulation is started.
rlm@87 353 (do
rlm@87 354 (println-repl "creating joint between"
rlm@87 355 (.getName obj-a) "and" (.getName obj-b))
rlm@87 356 (joint-dispatch constraints
rlm@87 357 control-a control-b
rlm@87 358 pivot-a pivot-b
rlm@87 359 joint-rotation))
rlm@87 360 (println-repl "could not find joint meta-data!"))))
rlm@87 361
rlm@78 362 (defn assemble-creature [#^Node pieces joints]
rlm@78 363 (dorun
rlm@78 364 (map
rlm@78 365 (fn [geom]
rlm@78 366 (let [physics-control
rlm@78 367 (RigidBodyControl.
rlm@78 368 (HullCollisionShape.
rlm@78 369 (.getMesh geom))
rlm@78 370 (if-let [mass (meta-data geom "mass")]
rlm@78 371 (do
rlm@78 372 (println-repl
rlm@78 373 "setting" (.getName geom) "mass to" (float mass))
rlm@78 374 (float mass))
rlm@78 375 (float 1)))]
rlm@78 376
rlm@78 377 (.addControl geom physics-control)))
rlm@78 378 (filter #(isa? (class %) Geometry )
rlm@78 379 (node-seq pieces))))
rlm@78 380 (dorun
rlm@78 381 (map
rlm@78 382 (fn [joint]
rlm@78 383 (let [[obj-a obj-b]
rlm@78 384 (joint-targets pieces joint)]
rlm@88 385 (connect obj-a obj-b joint)))
rlm@78 386 joints))
rlm@78 387 pieces)
rlm@74 388
rlm@78 389 (defn blender-creature [blender-path]
rlm@78 390 (let [model (load-blender-model blender-path)
rlm@78 391 joints
rlm@78 392 (if-let [joint-node (.getChild model "joints")]
rlm@78 393 (seq (.getChildren joint-node))
rlm@78 394 (do (println-repl "could not find joints node")
rlm@78 395 []))]
rlm@78 396 (assemble-creature model joints)))
rlm@74 397
rlm@78 398 (def hand "Models/creature1/one.blend")
rlm@74 399
rlm@78 400 (def worm "Models/creature1/try-again.blend")
rlm@78 401
rlm@90 402 (def touch "Models/creature1/touch.blend")
rlm@90 403
rlm@90 404 (defn worm-model [] (load-blender-model worm))
rlm@90 405
rlm@80 406 (defn x-ray [#^ColorRGBA color]
rlm@80 407 (doto (Material. (asset-manager)
rlm@80 408 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 409 (.setColor "Color" color)
rlm@80 410 (-> (.getAdditionalRenderState)
rlm@80 411 (.setDepthTest false))))
rlm@80 412
rlm@106 413
rlm@90 414
rlm@91 415 (defn colorful []
rlm@91 416 (.getChild (worm-model) "worm-21"))
rlm@90 417
rlm@90 418 (import jme3tools.converters.ImageToAwt)
rlm@90 419
rlm@90 420 (import ij.ImagePlus)
rlm@90 421
rlm@108 422 ;; Every Mesh has many triangles, each with its own index.
rlm@108 423 ;; Every vertex has its own index as well.
rlm@90 424
rlm@108 425 (defn tactile-sensor-image
rlm@97 426 "Return the touch-sensor distribution image in ImagePlus format, or
rlm@97 427 nil if it does not exist."
rlm@91 428 [#^Geometry obj]
rlm@97 429 (let [mat (.getMaterial obj)]
rlm@97 430 (if-let [texture-param
rlm@97 431 (.getTextureParam
rlm@97 432 mat
rlm@97 433 MaterialHelper/TEXTURE_TYPE_DIFFUSE)]
rlm@97 434 (let
rlm@97 435 [texture
rlm@97 436 (.getTextureValue texture-param)
rlm@97 437 im (.getImage texture)]
rlm@108 438 (ImageToAwt/convert im false false 0)))))
rlm@91 439
rlm@91 440 (import ij.process.ImageProcessor)
rlm@91 441 (import java.awt.image.BufferedImage)
rlm@91 442
rlm@92 443 (def white -1)
rlm@94 444
rlm@91 445 (defn filter-pixels
rlm@108 446 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 447 provided. Bounds -> [x0 y0 width height]"
rlm@92 448 {:author "Dylan Holmes"}
rlm@108 449 ([pred #^BufferedImage image]
rlm@108 450 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 451 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 452 ((fn accumulate [x y matches]
rlm@108 453 (cond
rlm@108 454 (>= y (+ height y0)) matches
rlm@108 455 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 456 (pred (.getRGB image x y))
rlm@108 457 (recur (inc x) y (conj matches [x y]))
rlm@108 458 :else (recur (inc x) y matches)))
rlm@108 459 x0 y0 [])))
rlm@91 460
rlm@91 461 (defn white-coordinates
rlm@108 462 "Coordinates of all the white pixels in a subset of the image."
rlm@108 463 [#^BufferedImage image bounds]
rlm@108 464 (filter-pixels #(= % white) image bounds))
rlm@108 465
rlm@108 466 (defn triangle
rlm@108 467 "Get the triangle specified by triangle-index from the mesh"
rlm@108 468 [#^Mesh mesh triangle-index]
rlm@108 469 (let [scratch (Triangle.)]
rlm@108 470 (.getTriangle mesh triangle-index scratch)
rlm@108 471 scratch))
rlm@108 472
rlm@108 473 (defn triangle-vertex-indices
rlm@108 474 "Get the triangle vertex indices of a given triangle from a given
rlm@108 475 mesh."
rlm@108 476 [#^Mesh mesh triangle-index]
rlm@108 477 (let [indices (int-array 3)]
rlm@108 478 (.getTriangle mesh triangle-index indices)
rlm@108 479 (vec indices)))
rlm@108 480
rlm@108 481 (defn vertex-UV-coord
rlm@108 482 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 483 [#^Mesh mesh vertex-index]
rlm@108 484 (let [UV-buffer
rlm@108 485 (.getData
rlm@108 486 (.getBuffer
rlm@108 487 mesh
rlm@108 488 VertexBuffer$Type/TexCoord))]
rlm@108 489 [(.get UV-buffer (* vertex-index 2))
rlm@108 490 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 491
rlm@108 492 (defn triangle-UV-coord
rlm@108 493 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 494 [#^Mesh mesh width height triangle-index]
rlm@108 495 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 496 (map (partial vertex-UV-coord mesh)
rlm@108 497 (triangle-vertex-indices mesh triangle-index))))
rlm@91 498
rlm@102 499 (defn same-side?
rlm@102 500 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 501 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 502 [p1 p2 ref p]
rlm@91 503 (<=
rlm@91 504 0
rlm@91 505 (.dot
rlm@91 506 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 507 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 508
rlm@108 509 (defn triangle-seq [#^Triangle tri]
rlm@108 510 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 511
rlm@108 512 (defn vector3f-seq [#^Vector3f v]
rlm@108 513 [(.getX v) (.getY v) (.getZ v)])
rlm@108 514
rlm@108 515 (defn inside-triangle?
rlm@108 516 "Is the point inside the triangle?"
rlm@108 517 {:author "Dylan Holmes"}
rlm@108 518 [#^Triangle tri #^Vector3f p]
rlm@108 519 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 520 (and
rlm@108 521 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 522 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 523 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 524
rlm@94 525 (defn triangle->matrix4f
rlm@108 526 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 527 contain the vertices of the triangle; the last contains the unit
rlm@108 528 normal of the triangle. The bottom row is filled with 1s."
rlm@94 529 [#^Triangle t]
rlm@94 530 (let [mat (Matrix4f.)
rlm@94 531 [vert-1 vert-2 vert-3]
rlm@94 532 ((comp vec map) #(.get t %) (range 3))
rlm@94 533 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 534 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 535 (dorun
rlm@94 536 (for [row (range 4) col (range 3)]
rlm@94 537 (do
rlm@94 538 (.set mat col row (.get (vertices row)col))
rlm@94 539 (.set mat 3 row 1))))
rlm@94 540 mat))
rlm@94 541
rlm@94 542 (defn triangle-transformation
rlm@94 543 "Returns the affine transformation that converts each vertex in the
rlm@94 544 first triangle into the corresponding vertex in the second
rlm@94 545 triangle."
rlm@94 546 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 547 (.mult
rlm@94 548 (triangle->matrix4f tri-2)
rlm@94 549 (.invert (triangle->matrix4f tri-1))))
rlm@94 550
rlm@108 551 (defn point->vector2f [[u v]]
rlm@108 552 (Vector2f. u v))
rlm@94 553
rlm@94 554 (defn vector2f->vector3f [v]
rlm@94 555 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 556
rlm@94 557 (defn map-triangle [f #^Triangle tri]
rlm@94 558 (Triangle.
rlm@94 559 (f 0 (.get1 tri))
rlm@94 560 (f 1 (.get2 tri))
rlm@94 561 (f 2 (.get3 tri))))
rlm@94 562
rlm@108 563 (defn points->triangle
rlm@108 564 "Convert a list of points into a triangle."
rlm@108 565 [points]
rlm@108 566 (apply #(Triangle. %1 %2 %3)
rlm@108 567 (map (fn [point]
rlm@108 568 (let [point (vec point)]
rlm@108 569 (Vector3f. (get point 0 0)
rlm@108 570 (get point 1 0)
rlm@108 571 (get point 2 0))))
rlm@108 572 (take 3 points))))
rlm@94 573
rlm@108 574 (defn convex-bounds
rlm@108 575 "Dimensions of the smallest integer bounding square of the list of
rlm@108 576 2D verticies in the form: [x y width height]."
rlm@108 577 [uv-verts]
rlm@108 578 (let [xs (map first uv-verts)
rlm@108 579 ys (map second uv-verts)
rlm@108 580 x0 (Math/floor (apply min xs))
rlm@108 581 y0 (Math/floor (apply min ys))
rlm@108 582 x1 (Math/ceil (apply max xs))
rlm@108 583 y1 (Math/ceil (apply max ys))]
rlm@108 584 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 585
rlm@106 586 (defn sensors-in-triangle
rlm@107 587 "Find the locations of the touch sensors within a triangle in both
rlm@107 588 UV and gemoetry relative coordinates."
rlm@107 589 [image mesh tri-index]
rlm@107 590 (let [width (.getWidth image)
rlm@108 591 height (.getHeight image)
rlm@108 592 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 593 bounds (convex-bounds UV-vertex-coords)
rlm@108 594
rlm@108 595 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 596 UV-sensor-coords
rlm@108 597 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 598 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 599 (white-coordinates image bounds))
rlm@108 600 UV->geometry (triangle-transformation
rlm@108 601 cutout-triangle
rlm@108 602 (triangle mesh tri-index))
rlm@108 603 geometry-sensor-coords
rlm@108 604 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 605 UV-sensor-coords)]
rlm@108 606 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 607
rlm@108 608 (defn-memo locate-feelers
rlm@94 609 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 610 their positions in geometry-relative coordinates."
rlm@94 611 [#^Geometry geo]
rlm@108 612 (let [mesh (.getMesh geo)
rlm@108 613 num-triangles (.getTriangleCount mesh)]
rlm@108 614 (if-let [image (tactile-sensor-image geo)]
rlm@108 615 (map
rlm@108 616 (partial sensors-in-triangle image mesh)
rlm@108 617 (range num-triangles))
rlm@108 618 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 619
rlm@102 620 (use 'clojure.contrib.def)
rlm@102 621
rlm@102 622 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 623 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 624
rlm@108 625 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 626 (vec (map :geometry (locate-feelers geo))))
rlm@102 627
rlm@97 628 (defn enable-touch [#^Geometry geo]
rlm@108 629 (let [feeler-coords (feeler-coordinates geo)
rlm@96 630 tris (triangles geo)
rlm@109 631 limit 0.1
rlm@109 632 ;;results (CollisionResults.)
rlm@109 633 ]
rlm@97 634 (fn [node]
rlm@97 635 (let [sensor-origins
rlm@97 636 (map
rlm@97 637 #(map (partial local-to-world geo) %)
rlm@97 638 feeler-coords)
rlm@97 639 triangle-normals
rlm@97 640 (map (partial get-ray-direction geo)
rlm@97 641 tris)
rlm@97 642 rays
rlm@97 643 (flatten
rlm@97 644 (map (fn [origins norm]
rlm@97 645 (map #(doto (Ray. % norm)
rlm@97 646 (.setLimit limit)) origins))
rlm@97 647 sensor-origins triangle-normals))]
rlm@102 648 (vector
rlm@102 649 (touch-topology geo)
rlm@102 650 (vec
rlm@102 651 (for [ray rays]
rlm@102 652 (do
rlm@102 653 (let [results (CollisionResults.)]
rlm@102 654 (.collideWith node ray results)
rlm@102 655 (let [touch-objects
rlm@102 656 (set
rlm@102 657 (filter #(not (= geo %))
rlm@102 658 (map #(.getGeometry %) results)))]
rlm@102 659 (if (> (count touch-objects) 0)
rlm@102 660 1 0)))))))))))
rlm@94 661
rlm@109 662
rlm@109 663
rlm@97 664 (defn touch [#^Node pieces]
rlm@102 665 (map enable-touch
rlm@102 666 (filter #(isa? (class %) Geometry)
rlm@102 667 (node-seq pieces))))
rlm@102 668
rlm@103 669 (defn debug-window
rlm@103 670 "creates function that offers a debug view of sensor data"
rlm@103 671 []
rlm@103 672 (let [vi (view-image)]
rlm@103 673 (fn
rlm@103 674 [[coords sensor-data]]
rlm@103 675 (let [image (points->image coords)]
rlm@103 676 (dorun
rlm@103 677 (for [i (range (count coords))]
rlm@103 678 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@103 679 ({0 -16777216
rlm@103 680 1 -1} (sensor-data i)))))
rlm@103 681 (vi image)))))
rlm@103 682
rlm@83 683
rlm@106 684 ;;(defn test-touch [world creature]
rlm@83 685
rlm@78 686
rlm@106 687 (defn test-creature [thing]
rlm@106 688 (let [x-axis
rlm@106 689 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 690 y-axis
rlm@106 691 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 692 z-axis
rlm@106 693 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 694 creature (blender-creature thing)
rlm@106 695 touch-nerves (touch creature)
rlm@106 696 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@106 697 ]
rlm@106 698 (world
rlm@106 699 (nodify [creature
rlm@106 700 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 701 :color ColorRGBA/Gray :mass 0)
rlm@106 702 x-axis y-axis z-axis
rlm@106 703 ])
rlm@106 704 standard-debug-controls
rlm@106 705 (fn [world]
rlm@106 706 (light-up-everything world)
rlm@106 707 (enable-debug world)
rlm@106 708 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 709 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@106 710 (.setTimer world (RatchetTimer. 60))
rlm@109 711 (speed-up world)
rlm@106 712 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 713 )
rlm@106 714 (fn [world tpf]
rlm@109 715 ;;(dorun
rlm@109 716 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@109 717
rlm@106 718 (dorun
rlm@109 719 (map #(%1 (%2 (.getRootNode world)))
rlm@109 720 touch-debug-windows touch-nerves))
rlm@109 721
rlm@109 722
rlm@106 723 )
rlm@106 724 ;;(let [timer (atom 0)]
rlm@106 725 ;; (fn [_ _]
rlm@106 726 ;; (swap! timer inc)
rlm@106 727 ;; (if (= (rem @timer 60) 0)
rlm@106 728 ;; (println-repl (float (/ @timer 60))))))
rlm@106 729 )))
rlm@83 730
rlm@109 731
rlm@109 732
rlm@109 733
rlm@109 734
rlm@109 735
rlm@109 736
rlm@109 737
rlm@109 738
rlm@109 739 ;;; experiments in collisions
rlm@109 740
rlm@109 741
rlm@109 742
rlm@109 743 (defn collision-test []
rlm@109 744 (let [a-radius 1
rlm@109 745 a-position (Vector3f. -0.25 3 0)
rlm@109 746 b-radius 1
rlm@109 747 b-position (Vector3f. 0.25 3 0)
rlm@109 748
rlm@109 749
rlm@109 750 obj-a (box 1 1 1 :color ColorRGBA/Red
rlm@109 751 :position a-position
rlm@109 752 :physical? false)
rlm@109 753 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 754 :position b-position
rlm@109 755 :physical? false)
rlm@109 756 node (nodify [obj-a obj-b])
rlm@109 757 bounds-a (BoundingBox. a-position 1 1 1)
rlm@109 758 bounds-b (BoundingBox. b-position 1 1 1)
rlm@109 759 sim
rlm@109 760 (world node
rlm@109 761 {}
rlm@109 762 no-op
rlm@109 763 no-op)
rlm@109 764
rlm@109 765 ]
rlm@109 766 (.setBound (.getMesh obj-a) bounds-a)
rlm@109 767 (.updateBound (.getMesh obj-a))
rlm@109 768
rlm@109 769
rlm@109 770 (.setBound (.getMesh obj-b) bounds-b)
rlm@109 771 (.updateBound (.getMesh obj-b))
rlm@109 772
rlm@109 773
rlm@109 774
rlm@109 775 (let [cr (CollisionResults.)]
rlm@109 776 (.collideWith node bounds-a cr)
rlm@109 777 [cr sim])
rlm@109 778
rlm@109 779 ))
rlm@109 780
rlm@109 781
rlm@109 782
rlm@109 783
rlm@87 784 #+end_src
rlm@83 785
rlm@87 786 #+results: body-1
rlm@109 787 : #'cortex.silly/test-creature
rlm@78 788
rlm@78 789
rlm@78 790 * COMMENT purgatory
rlm@78 791 #+begin_src clojure
rlm@77 792 (defn bullet-trans []
rlm@77 793 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 794 :position (Vector3f. -10 5 0))
rlm@77 795 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 796 :position (Vector3f. -10 -5 0)
rlm@77 797 :mass 0)
rlm@77 798 control-a (.getControl obj-a RigidBodyControl)
rlm@77 799 control-b (.getControl obj-b RigidBodyControl)
rlm@77 800 swivel
rlm@77 801 (.toRotationMatrix
rlm@77 802 (doto (Quaternion.)
rlm@77 803 (.fromAngleAxis (/ Math/PI 2)
rlm@77 804 Vector3f/UNIT_X)))]
rlm@77 805 (doto
rlm@77 806 (ConeJoint.
rlm@77 807 control-a control-b
rlm@77 808 (Vector3f. 0 5 0)
rlm@77 809 (Vector3f. 0 -5 0)
rlm@77 810 swivel swivel)
rlm@77 811 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 812 (/ Math/PI 4)
rlm@77 813 (* Math/PI 0.8)))
rlm@77 814 (world (nodify
rlm@77 815 [obj-a obj-b])
rlm@77 816 standard-debug-controls
rlm@77 817 enable-debug
rlm@77 818 no-op)))
rlm@74 819
rlm@74 820
rlm@77 821 (defn bullet-trans* []
rlm@77 822 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 823 :position (Vector3f. 5 0 0)
rlm@77 824 :mass 90)
rlm@77 825 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 826 :position (Vector3f. -5 0 0)
rlm@77 827 :mass 0)
rlm@77 828 control-a (.getControl obj-a RigidBodyControl)
rlm@77 829 control-b (.getControl obj-b RigidBodyControl)
rlm@77 830 move-up? (atom nil)
rlm@77 831 move-down? (atom nil)
rlm@77 832 move-left? (atom nil)
rlm@77 833 move-right? (atom nil)
rlm@77 834 roll-left? (atom nil)
rlm@77 835 roll-right? (atom nil)
rlm@77 836 force 100
rlm@77 837 swivel
rlm@77 838 (.toRotationMatrix
rlm@77 839 (doto (Quaternion.)
rlm@77 840 (.fromAngleAxis (/ Math/PI 2)
rlm@77 841 Vector3f/UNIT_X)))
rlm@77 842 x-move
rlm@77 843 (doto (Matrix3f.)
rlm@77 844 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 845 (.normalize (Vector3f. 1 1 0))))
rlm@77 846
rlm@77 847 timer (atom 0)]
rlm@77 848 (doto
rlm@77 849 (ConeJoint.
rlm@77 850 control-a control-b
rlm@77 851 (Vector3f. -8 0 0)
rlm@77 852 (Vector3f. 2 0 0)
rlm@77 853 ;;swivel swivel
rlm@77 854 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 855 x-move Matrix3f/IDENTITY
rlm@77 856 )
rlm@77 857 (.setCollisionBetweenLinkedBodys false)
rlm@77 858 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 859 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 860 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 861 (world (nodify
rlm@77 862 [obj-a obj-b])
rlm@77 863 (merge standard-debug-controls
rlm@77 864 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 865 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 866 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 867 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 868 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 869 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 870
rlm@77 871 (fn [world]
rlm@77 872 (enable-debug world)
rlm@77 873 (set-gravity world Vector3f/ZERO)
rlm@77 874 )
rlm@77 875
rlm@77 876 (fn [world _]
rlm@77 877
rlm@77 878 (if @move-up?
rlm@77 879 (.applyForce control-a
rlm@77 880 (Vector3f. force 0 0)
rlm@77 881 (Vector3f. 0 0 0)))
rlm@77 882 (if @move-down?
rlm@77 883 (.applyForce control-a
rlm@77 884 (Vector3f. (- force) 0 0)
rlm@77 885 (Vector3f. 0 0 0)))
rlm@77 886 (if @move-left?
rlm@77 887 (.applyForce control-a
rlm@77 888 (Vector3f. 0 force 0)
rlm@77 889 (Vector3f. 0 0 0)))
rlm@77 890 (if @move-right?
rlm@77 891 (.applyForce control-a
rlm@77 892 (Vector3f. 0 (- force) 0)
rlm@77 893 (Vector3f. 0 0 0)))
rlm@77 894
rlm@77 895 (if @roll-left?
rlm@77 896 (.applyForce control-a
rlm@77 897 (Vector3f. 0 0 force)
rlm@77 898 (Vector3f. 0 0 0)))
rlm@77 899 (if @roll-right?
rlm@77 900 (.applyForce control-a
rlm@77 901 (Vector3f. 0 0 (- force))
rlm@77 902 (Vector3f. 0 0 0)))
rlm@77 903
rlm@77 904 (if (zero? (rem (swap! timer inc) 100))
rlm@77 905 (.attachChild
rlm@77 906 (.getRootNode world)
rlm@77 907 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 908 :physical? false :position
rlm@77 909 (.getWorldTranslation obj-a)))))
rlm@77 910 )
rlm@77 911 ))
rlm@77 912
rlm@94 913 (defn transform-trianglesdsd
rlm@94 914 "Transform that converts each vertex in the first triangle
rlm@94 915 into the corresponding vertex in the second triangle."
rlm@94 916 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 917 (let [in [(.get1 tri-1)
rlm@94 918 (.get2 tri-1)
rlm@94 919 (.get3 tri-1)]
rlm@94 920 out [(.get1 tri-2)
rlm@94 921 (.get2 tri-2)
rlm@94 922 (.get3 tri-2)]]
rlm@94 923 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 924 in* [(.mult translate (in 0))
rlm@94 925 (.mult translate (in 1))
rlm@94 926 (.mult translate (in 2))]
rlm@94 927 final-translation
rlm@94 928 (doto (Matrix4f.)
rlm@94 929 (.setTranslation (out 1)))
rlm@94 930
rlm@94 931 rotate-1
rlm@94 932 (doto (Matrix3f.)
rlm@94 933 (.fromStartEndVectors
rlm@94 934 (.normalize
rlm@94 935 (.subtract
rlm@94 936 (in* 1) (in* 0)))
rlm@94 937 (.normalize
rlm@94 938 (.subtract
rlm@94 939 (out 1) (out 0)))))
rlm@94 940 in** [(.mult rotate-1 (in* 0))
rlm@94 941 (.mult rotate-1 (in* 1))
rlm@94 942 (.mult rotate-1 (in* 2))]
rlm@94 943 scale-factor-1
rlm@94 944 (.mult
rlm@94 945 (.normalize
rlm@94 946 (.subtract
rlm@94 947 (out 1)
rlm@94 948 (out 0)))
rlm@94 949 (/ (.length
rlm@94 950 (.subtract (out 1)
rlm@94 951 (out 0)))
rlm@94 952 (.length
rlm@94 953 (.subtract (in** 1)
rlm@94 954 (in** 0)))))
rlm@94 955 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 956 in*** [(.mult scale-1 (in** 0))
rlm@94 957 (.mult scale-1 (in** 1))
rlm@94 958 (.mult scale-1 (in** 2))]
rlm@94 959
rlm@94 960
rlm@94 961
rlm@94 962
rlm@94 963
rlm@94 964 ]
rlm@94 965
rlm@94 966 (dorun (map println in))
rlm@94 967 (println)
rlm@94 968 (dorun (map println in*))
rlm@94 969 (println)
rlm@94 970 (dorun (map println in**))
rlm@94 971 (println)
rlm@94 972 (dorun (map println in***))
rlm@94 973 (println)
rlm@94 974
rlm@99 975 ))))
rlm@94 976
rlm@94 977
rlm@106 978 (defn world-setup [joint]
rlm@106 979 (let [joint-position (Vector3f. 0 0 0)
rlm@106 980 joint-rotation
rlm@106 981 (.toRotationMatrix
rlm@106 982 (.mult
rlm@106 983 (doto (Quaternion.)
rlm@106 984 (.fromAngleAxis
rlm@106 985 (* 1 (/ Math/PI 4))
rlm@106 986 (Vector3f. -1 0 0)))
rlm@106 987 (doto (Quaternion.)
rlm@106 988 (.fromAngleAxis
rlm@106 989 (* 1 (/ Math/PI 2))
rlm@106 990 (Vector3f. 0 0 1)))))
rlm@106 991 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 992
rlm@106 993 origin (doto
rlm@106 994 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 995 :position top-position))
rlm@106 996 top (doto
rlm@106 997 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 998 :position top-position)
rlm@106 999
rlm@106 1000 (.addControl
rlm@106 1001 (RigidBodyControl.
rlm@106 1002 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1003 bottom (doto
rlm@106 1004 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1005 :position (Vector3f. 0 0 0))
rlm@106 1006 (.addControl
rlm@106 1007 (RigidBodyControl.
rlm@106 1008 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1009 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1010 :color ColorRGBA/Gray :mass 0)
rlm@106 1011 a (.getControl top RigidBodyControl)
rlm@106 1012 b (.getControl bottom RigidBodyControl)]
rlm@106 1013
rlm@106 1014 (cond
rlm@106 1015 (= joint :cone)
rlm@106 1016
rlm@106 1017 (doto (ConeJoint.
rlm@106 1018 a b
rlm@106 1019 (world-to-local top joint-position)
rlm@106 1020 (world-to-local bottom joint-position)
rlm@106 1021 joint-rotation
rlm@106 1022 joint-rotation
rlm@106 1023 )
rlm@106 1024
rlm@106 1025
rlm@106 1026 (.setLimit (* (/ 10) Math/PI)
rlm@106 1027 (* (/ 4) Math/PI)
rlm@106 1028 0)))
rlm@106 1029 [origin top bottom table]))
rlm@106 1030
rlm@106 1031 (defn test-joint [joint]
rlm@106 1032 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1033 control (.getControl top RigidBodyControl)
rlm@106 1034 move-up? (atom false)
rlm@106 1035 move-down? (atom false)
rlm@106 1036 move-left? (atom false)
rlm@106 1037 move-right? (atom false)
rlm@106 1038 roll-left? (atom false)
rlm@106 1039 roll-right? (atom false)
rlm@106 1040 timer (atom 0)]
rlm@106 1041
rlm@106 1042 (world
rlm@106 1043 (nodify [top bottom floor origin])
rlm@106 1044 (merge standard-debug-controls
rlm@106 1045 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1046 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1047 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1048 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1049 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1050 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1051
rlm@106 1052 (fn [world]
rlm@106 1053 (light-up-everything world)
rlm@106 1054 (enable-debug world)
rlm@106 1055 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1056 )
rlm@106 1057
rlm@106 1058 (fn [world _]
rlm@106 1059 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1060 (do
rlm@106 1061 ;; (println-repl @timer)
rlm@106 1062 (.attachChild (.getRootNode world)
rlm@106 1063 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1064 :position (.getWorldTranslation top)
rlm@106 1065 :physical? false))
rlm@106 1066 (.attachChild (.getRootNode world)
rlm@106 1067 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1068 :position (.getWorldTranslation bottom)
rlm@106 1069 :physical? false))))
rlm@106 1070
rlm@106 1071 (if @move-up?
rlm@106 1072 (.applyTorque control
rlm@106 1073 (.mult (.getPhysicsRotation control)
rlm@106 1074 (Vector3f. 0 0 10))))
rlm@106 1075 (if @move-down?
rlm@106 1076 (.applyTorque control
rlm@106 1077 (.mult (.getPhysicsRotation control)
rlm@106 1078 (Vector3f. 0 0 -10))))
rlm@106 1079 (if @move-left?
rlm@106 1080 (.applyTorque control
rlm@106 1081 (.mult (.getPhysicsRotation control)
rlm@106 1082 (Vector3f. 0 10 0))))
rlm@106 1083 (if @move-right?
rlm@106 1084 (.applyTorque control
rlm@106 1085 (.mult (.getPhysicsRotation control)
rlm@106 1086 (Vector3f. 0 -10 0))))
rlm@106 1087 (if @roll-left?
rlm@106 1088 (.applyTorque control
rlm@106 1089 (.mult (.getPhysicsRotation control)
rlm@106 1090 (Vector3f. -1 0 0))))
rlm@106 1091 (if @roll-right?
rlm@106 1092 (.applyTorque control
rlm@106 1093 (.mult (.getPhysicsRotation control)
rlm@106 1094 (Vector3f. 1 0 0))))))))
rlm@106 1095
rlm@99 1096
rlm@99 1097
rlm@107 1098 (defprotocol Frame
rlm@107 1099 (frame [this]))
rlm@107 1100
rlm@107 1101 (extend-type BufferedImage
rlm@107 1102 Frame
rlm@107 1103 (frame [image]
rlm@107 1104 (merge
rlm@107 1105 (apply
rlm@107 1106 hash-map
rlm@107 1107 (interleave
rlm@107 1108 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1109 (vector x y)))
rlm@107 1110 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1111 (let [data (.getRGB image x y)]
rlm@107 1112 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1113 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1114 :b (bit-and 0x0000ff data)))))))
rlm@107 1115 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1116
rlm@107 1117
rlm@107 1118 (extend-type ImagePlus
rlm@107 1119 Frame
rlm@107 1120 (frame [image+]
rlm@107 1121 (frame (.getBufferedImage image+))))
rlm@107 1122
rlm@107 1123
rlm@99 1124 #+end_src
rlm@99 1125
rlm@99 1126
rlm@99 1127 * COMMENT generate source
rlm@99 1128 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1129 <<body-1>>
rlm@99 1130 #+end_src
rlm@99 1131
rlm@99 1132
rlm@94 1133
rlm@94 1134