annotate org/test-creature.org @ 115:247860e25536

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