annotate org/test-creature.org @ 111:61d9c0e8d188

don't create useless touch-maps, add some eye stuf
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Jan 2012 08:09:15 -0700
parents f89f0b9ed2fe
children 128fa71ee188
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@91 413 (defn colorful []
rlm@91 414 (.getChild (worm-model) "worm-21"))
rlm@90 415
rlm@90 416 (import jme3tools.converters.ImageToAwt)
rlm@90 417
rlm@90 418 (import ij.ImagePlus)
rlm@90 419
rlm@108 420 ;; Every Mesh has many triangles, each with its own index.
rlm@108 421 ;; Every vertex has its own index as well.
rlm@90 422
rlm@108 423 (defn tactile-sensor-image
rlm@110 424 "Return the touch-sensor distribution image in BufferedImage format,
rlm@110 425 or nil if it does not exist."
rlm@91 426 [#^Geometry obj]
rlm@110 427 (if-let [image-path (meta-data obj "touch")]
rlm@110 428 (ImageToAwt/convert
rlm@110 429 (.getImage
rlm@110 430 (.loadTexture
rlm@110 431 (asset-manager)
rlm@110 432 image-path))
rlm@110 433 false false 0)))
rlm@110 434
rlm@91 435 (import ij.process.ImageProcessor)
rlm@91 436 (import java.awt.image.BufferedImage)
rlm@91 437
rlm@92 438 (def white -1)
rlm@94 439
rlm@91 440 (defn filter-pixels
rlm@108 441 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 442 provided. Bounds -> [x0 y0 width height]"
rlm@92 443 {:author "Dylan Holmes"}
rlm@108 444 ([pred #^BufferedImage image]
rlm@108 445 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 446 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 447 ((fn accumulate [x y matches]
rlm@108 448 (cond
rlm@108 449 (>= y (+ height y0)) matches
rlm@108 450 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 451 (pred (.getRGB image x y))
rlm@108 452 (recur (inc x) y (conj matches [x y]))
rlm@108 453 :else (recur (inc x) y matches)))
rlm@108 454 x0 y0 [])))
rlm@91 455
rlm@91 456 (defn white-coordinates
rlm@108 457 "Coordinates of all the white pixels in a subset of the image."
rlm@108 458 [#^BufferedImage image bounds]
rlm@108 459 (filter-pixels #(= % white) image bounds))
rlm@108 460
rlm@108 461 (defn triangle
rlm@108 462 "Get the triangle specified by triangle-index from the mesh"
rlm@108 463 [#^Mesh mesh triangle-index]
rlm@108 464 (let [scratch (Triangle.)]
rlm@108 465 (.getTriangle mesh triangle-index scratch)
rlm@108 466 scratch))
rlm@108 467
rlm@108 468 (defn triangle-vertex-indices
rlm@108 469 "Get the triangle vertex indices of a given triangle from a given
rlm@108 470 mesh."
rlm@108 471 [#^Mesh mesh triangle-index]
rlm@108 472 (let [indices (int-array 3)]
rlm@108 473 (.getTriangle mesh triangle-index indices)
rlm@108 474 (vec indices)))
rlm@108 475
rlm@108 476 (defn vertex-UV-coord
rlm@108 477 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 478 [#^Mesh mesh vertex-index]
rlm@108 479 (let [UV-buffer
rlm@108 480 (.getData
rlm@108 481 (.getBuffer
rlm@108 482 mesh
rlm@108 483 VertexBuffer$Type/TexCoord))]
rlm@108 484 [(.get UV-buffer (* vertex-index 2))
rlm@108 485 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 486
rlm@108 487 (defn triangle-UV-coord
rlm@108 488 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 489 [#^Mesh mesh width height triangle-index]
rlm@108 490 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 491 (map (partial vertex-UV-coord mesh)
rlm@108 492 (triangle-vertex-indices mesh triangle-index))))
rlm@91 493
rlm@102 494 (defn same-side?
rlm@102 495 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 496 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 497 [p1 p2 ref p]
rlm@91 498 (<=
rlm@91 499 0
rlm@91 500 (.dot
rlm@91 501 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 502 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 503
rlm@108 504 (defn triangle-seq [#^Triangle tri]
rlm@108 505 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 506
rlm@108 507 (defn vector3f-seq [#^Vector3f v]
rlm@108 508 [(.getX v) (.getY v) (.getZ v)])
rlm@108 509
rlm@108 510 (defn inside-triangle?
rlm@108 511 "Is the point inside the triangle?"
rlm@108 512 {:author "Dylan Holmes"}
rlm@108 513 [#^Triangle tri #^Vector3f p]
rlm@108 514 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 515 (and
rlm@108 516 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 517 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 518 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 519
rlm@94 520 (defn triangle->matrix4f
rlm@108 521 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 522 contain the vertices of the triangle; the last contains the unit
rlm@108 523 normal of the triangle. The bottom row is filled with 1s."
rlm@94 524 [#^Triangle t]
rlm@94 525 (let [mat (Matrix4f.)
rlm@94 526 [vert-1 vert-2 vert-3]
rlm@94 527 ((comp vec map) #(.get t %) (range 3))
rlm@94 528 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 529 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 530 (dorun
rlm@94 531 (for [row (range 4) col (range 3)]
rlm@94 532 (do
rlm@94 533 (.set mat col row (.get (vertices row)col))
rlm@94 534 (.set mat 3 row 1))))
rlm@94 535 mat))
rlm@94 536
rlm@94 537 (defn triangle-transformation
rlm@94 538 "Returns the affine transformation that converts each vertex in the
rlm@94 539 first triangle into the corresponding vertex in the second
rlm@94 540 triangle."
rlm@94 541 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 542 (.mult
rlm@94 543 (triangle->matrix4f tri-2)
rlm@94 544 (.invert (triangle->matrix4f tri-1))))
rlm@94 545
rlm@108 546 (defn point->vector2f [[u v]]
rlm@108 547 (Vector2f. u v))
rlm@94 548
rlm@94 549 (defn vector2f->vector3f [v]
rlm@94 550 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 551
rlm@94 552 (defn map-triangle [f #^Triangle tri]
rlm@94 553 (Triangle.
rlm@94 554 (f 0 (.get1 tri))
rlm@94 555 (f 1 (.get2 tri))
rlm@94 556 (f 2 (.get3 tri))))
rlm@94 557
rlm@108 558 (defn points->triangle
rlm@108 559 "Convert a list of points into a triangle."
rlm@108 560 [points]
rlm@108 561 (apply #(Triangle. %1 %2 %3)
rlm@108 562 (map (fn [point]
rlm@108 563 (let [point (vec point)]
rlm@108 564 (Vector3f. (get point 0 0)
rlm@108 565 (get point 1 0)
rlm@108 566 (get point 2 0))))
rlm@108 567 (take 3 points))))
rlm@94 568
rlm@108 569 (defn convex-bounds
rlm@108 570 "Dimensions of the smallest integer bounding square of the list of
rlm@108 571 2D verticies in the form: [x y width height]."
rlm@108 572 [uv-verts]
rlm@108 573 (let [xs (map first uv-verts)
rlm@108 574 ys (map second uv-verts)
rlm@108 575 x0 (Math/floor (apply min xs))
rlm@108 576 y0 (Math/floor (apply min ys))
rlm@108 577 x1 (Math/ceil (apply max xs))
rlm@108 578 y1 (Math/ceil (apply max ys))]
rlm@108 579 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 580
rlm@106 581 (defn sensors-in-triangle
rlm@107 582 "Find the locations of the touch sensors within a triangle in both
rlm@107 583 UV and gemoetry relative coordinates."
rlm@107 584 [image mesh tri-index]
rlm@107 585 (let [width (.getWidth image)
rlm@108 586 height (.getHeight image)
rlm@108 587 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 588 bounds (convex-bounds UV-vertex-coords)
rlm@108 589
rlm@108 590 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 591 UV-sensor-coords
rlm@108 592 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 593 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 594 (white-coordinates image bounds))
rlm@108 595 UV->geometry (triangle-transformation
rlm@108 596 cutout-triangle
rlm@108 597 (triangle mesh tri-index))
rlm@108 598 geometry-sensor-coords
rlm@108 599 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 600 UV-sensor-coords)]
rlm@108 601 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 602
rlm@108 603 (defn-memo locate-feelers
rlm@94 604 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 605 their positions in geometry-relative coordinates."
rlm@94 606 [#^Geometry geo]
rlm@108 607 (let [mesh (.getMesh geo)
rlm@108 608 num-triangles (.getTriangleCount mesh)]
rlm@108 609 (if-let [image (tactile-sensor-image geo)]
rlm@108 610 (map
rlm@108 611 (partial sensors-in-triangle image mesh)
rlm@108 612 (range num-triangles))
rlm@108 613 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 614
rlm@102 615 (use 'clojure.contrib.def)
rlm@102 616
rlm@102 617 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 618 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 619
rlm@108 620 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 621 (vec (map :geometry (locate-feelers geo))))
rlm@102 622
rlm@97 623 (defn enable-touch [#^Geometry geo]
rlm@108 624 (let [feeler-coords (feeler-coordinates geo)
rlm@96 625 tris (triangles geo)
rlm@109 626 limit 0.1
rlm@109 627 ;;results (CollisionResults.)
rlm@109 628 ]
rlm@111 629 (if (empty? (touch-topology geo))
rlm@111 630 nil
rlm@111 631 (fn [node]
rlm@111 632 (let [sensor-origins
rlm@111 633 (map
rlm@111 634 #(map (partial local-to-world geo) %)
rlm@111 635 feeler-coords)
rlm@111 636 triangle-normals
rlm@111 637 (map (partial get-ray-direction geo)
rlm@111 638 tris)
rlm@111 639 rays
rlm@111 640 (flatten
rlm@111 641 (map (fn [origins norm]
rlm@111 642 (map #(doto (Ray. % norm)
rlm@97 643 (.setLimit limit)) origins))
rlm@111 644 sensor-origins triangle-normals))]
rlm@111 645 (vector
rlm@111 646 (touch-topology geo)
rlm@111 647 (vec
rlm@111 648 (for [ray rays]
rlm@111 649 (do
rlm@111 650 (let [results (CollisionResults.)]
rlm@111 651 (.collideWith node ray results)
rlm@111 652 (let [touch-objects
rlm@111 653 (set
rlm@111 654 (filter #(not (= geo %))
rlm@111 655 (map #(.getGeometry %) results)))]
rlm@111 656 (if (> (count touch-objects) 0)
rlm@111 657 1 0))))))))))))
rlm@111 658
rlm@111 659 (defn touch [#^Node pieces]
rlm@111 660 (filter (comp not nil?)
rlm@111 661 (map enable-touch
rlm@111 662 (filter #(isa? (class %) Geometry)
rlm@111 663 (node-seq pieces)))))
rlm@94 664
rlm@109 665
rlm@111 666 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
rlm@111 667 ;; http://en.wikipedia.org/wiki/Retina
rlm@109 668
rlm@111 669 (defn test-eye []
rlm@111 670 (.getChild (worm-model) "worm-11"))
rlm@111 671
rlm@111 672
rlm@111 673 (defn retina-sensor-image
rlm@111 674 "Return a map of pixel selection functions to BufferedImages
rlm@111 675 describing the distribution of light-sensitive components on this
rlm@111 676 geometry's surface. Each function creates an integer from the rgb
rlm@111 677 values found in the pixel. :red, :green, :blue, :gray are already
rlm@111 678 defined as extracting the red green blue and average components
rlm@111 679 respectively."
rlm@111 680 [#^Geometry eye]
rlm@111 681 (if-let [eye-map (meta-data eye "eye")]
rlm@111 682 (map-vals
rlm@111 683 #(ImageToAwt/convert
rlm@111 684 (.getImage (.loadTexture (asset-manager) %))
rlm@111 685 false false 0)
rlm@111 686 (read-string
rlm@111 687 eye-map))))
rlm@111 688
rlm@111 689
rlm@111 690
rlm@111 691
rlm@102 692
rlm@103 693 (defn debug-window
rlm@103 694 "creates function that offers a debug view of sensor data"
rlm@103 695 []
rlm@103 696 (let [vi (view-image)]
rlm@103 697 (fn
rlm@103 698 [[coords sensor-data]]
rlm@103 699 (let [image (points->image coords)]
rlm@103 700 (dorun
rlm@103 701 (for [i (range (count coords))]
rlm@103 702 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@103 703 ({0 -16777216
rlm@103 704 1 -1} (sensor-data i)))))
rlm@103 705 (vi image)))))
rlm@103 706
rlm@83 707
rlm@106 708 ;;(defn test-touch [world creature]
rlm@83 709
rlm@78 710
rlm@106 711 (defn test-creature [thing]
rlm@106 712 (let [x-axis
rlm@106 713 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 714 y-axis
rlm@106 715 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 716 z-axis
rlm@106 717 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 718 creature (blender-creature thing)
rlm@106 719 touch-nerves (touch creature)
rlm@106 720 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@106 721 ]
rlm@106 722 (world
rlm@106 723 (nodify [creature
rlm@106 724 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 725 :color ColorRGBA/Gray :mass 0)
rlm@106 726 x-axis y-axis z-axis
rlm@106 727 ])
rlm@106 728 standard-debug-controls
rlm@106 729 (fn [world]
rlm@106 730 (light-up-everything world)
rlm@106 731 (enable-debug world)
rlm@106 732 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 733 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@110 734 ;;(.setTimer world (RatchetTimer. 60))
rlm@110 735 ;;(speed-up world)
rlm@106 736 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 737 )
rlm@106 738 (fn [world tpf]
rlm@109 739 ;;(dorun
rlm@109 740 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@110 741
rlm@106 742 (dorun
rlm@109 743 (map #(%1 (%2 (.getRootNode world)))
rlm@110 744 touch-debug-windows touch-nerves)
rlm@110 745 )
rlm@109 746
rlm@106 747 )
rlm@106 748 ;;(let [timer (atom 0)]
rlm@106 749 ;; (fn [_ _]
rlm@106 750 ;; (swap! timer inc)
rlm@106 751 ;; (if (= (rem @timer 60) 0)
rlm@106 752 ;; (println-repl (float (/ @timer 60))))))
rlm@106 753 )))
rlm@83 754
rlm@109 755
rlm@109 756
rlm@109 757
rlm@109 758
rlm@109 759
rlm@109 760
rlm@109 761
rlm@109 762
rlm@109 763 ;;; experiments in collisions
rlm@109 764
rlm@109 765
rlm@109 766
rlm@109 767 (defn collision-test []
rlm@110 768 (let [b-radius 1
rlm@110 769 b-position (Vector3f. 0 0 0)
rlm@109 770 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 771 :position b-position
rlm@110 772 :mass 0)
rlm@110 773 node (nodify [obj-b])
rlm@110 774 bounds-b
rlm@110 775 (doto (Picture.)
rlm@110 776 (.setHeight 50)
rlm@110 777 (.setWidth 50)
rlm@110 778 (.setImage (asset-manager)
rlm@110 779 "Models/creature1/hand.png"
rlm@110 780 false
rlm@110 781 ))
rlm@110 782
rlm@110 783 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
rlm@110 784
rlm@110 785 collisions
rlm@110 786 (let [cr (CollisionResults.)]
rlm@110 787 (.collideWith node bounds-b cr)
rlm@110 788 (println (map #(.getContactPoint %) cr))
rlm@110 789 cr)
rlm@110 790
rlm@110 791 ;;collision-points
rlm@110 792 ;;(map #(sphere 0.1 :position (.getContactPoint %))
rlm@110 793 ;; collisions)
rlm@110 794
rlm@110 795 ;;node (nodify (conj collision-points obj-b))
rlm@110 796
rlm@109 797 sim
rlm@109 798 (world node
rlm@110 799 {"key-space"
rlm@110 800 (fn [_ value]
rlm@110 801 (if value
rlm@110 802 (let [cr (CollisionResults.)]
rlm@110 803 (.collideWith node bounds-b cr)
rlm@110 804 (println-repl (map #(.getContactPoint %) cr))
rlm@110 805 cr)))}
rlm@109 806 no-op
rlm@109 807 no-op)
rlm@109 808
rlm@109 809 ]
rlm@110 810 sim
rlm@109 811
rlm@109 812 ))
rlm@109 813
rlm@109 814
rlm@109 815
rlm@109 816
rlm@87 817 #+end_src
rlm@83 818
rlm@87 819 #+results: body-1
rlm@109 820 : #'cortex.silly/test-creature
rlm@78 821
rlm@78 822
rlm@78 823 * COMMENT purgatory
rlm@78 824 #+begin_src clojure
rlm@77 825 (defn bullet-trans []
rlm@77 826 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 827 :position (Vector3f. -10 5 0))
rlm@77 828 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 829 :position (Vector3f. -10 -5 0)
rlm@77 830 :mass 0)
rlm@77 831 control-a (.getControl obj-a RigidBodyControl)
rlm@77 832 control-b (.getControl obj-b RigidBodyControl)
rlm@77 833 swivel
rlm@77 834 (.toRotationMatrix
rlm@77 835 (doto (Quaternion.)
rlm@77 836 (.fromAngleAxis (/ Math/PI 2)
rlm@77 837 Vector3f/UNIT_X)))]
rlm@77 838 (doto
rlm@77 839 (ConeJoint.
rlm@77 840 control-a control-b
rlm@77 841 (Vector3f. 0 5 0)
rlm@77 842 (Vector3f. 0 -5 0)
rlm@77 843 swivel swivel)
rlm@77 844 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 845 (/ Math/PI 4)
rlm@77 846 (* Math/PI 0.8)))
rlm@77 847 (world (nodify
rlm@77 848 [obj-a obj-b])
rlm@77 849 standard-debug-controls
rlm@77 850 enable-debug
rlm@77 851 no-op)))
rlm@74 852
rlm@74 853
rlm@77 854 (defn bullet-trans* []
rlm@77 855 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 856 :position (Vector3f. 5 0 0)
rlm@77 857 :mass 90)
rlm@77 858 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 859 :position (Vector3f. -5 0 0)
rlm@77 860 :mass 0)
rlm@77 861 control-a (.getControl obj-a RigidBodyControl)
rlm@77 862 control-b (.getControl obj-b RigidBodyControl)
rlm@77 863 move-up? (atom nil)
rlm@77 864 move-down? (atom nil)
rlm@77 865 move-left? (atom nil)
rlm@77 866 move-right? (atom nil)
rlm@77 867 roll-left? (atom nil)
rlm@77 868 roll-right? (atom nil)
rlm@77 869 force 100
rlm@77 870 swivel
rlm@77 871 (.toRotationMatrix
rlm@77 872 (doto (Quaternion.)
rlm@77 873 (.fromAngleAxis (/ Math/PI 2)
rlm@77 874 Vector3f/UNIT_X)))
rlm@77 875 x-move
rlm@77 876 (doto (Matrix3f.)
rlm@77 877 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 878 (.normalize (Vector3f. 1 1 0))))
rlm@77 879
rlm@77 880 timer (atom 0)]
rlm@77 881 (doto
rlm@77 882 (ConeJoint.
rlm@77 883 control-a control-b
rlm@77 884 (Vector3f. -8 0 0)
rlm@77 885 (Vector3f. 2 0 0)
rlm@77 886 ;;swivel swivel
rlm@77 887 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 888 x-move Matrix3f/IDENTITY
rlm@77 889 )
rlm@77 890 (.setCollisionBetweenLinkedBodys false)
rlm@77 891 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 892 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 893 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 894 (world (nodify
rlm@77 895 [obj-a obj-b])
rlm@77 896 (merge standard-debug-controls
rlm@77 897 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 898 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 899 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 900 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 901 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 902 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 903
rlm@77 904 (fn [world]
rlm@77 905 (enable-debug world)
rlm@77 906 (set-gravity world Vector3f/ZERO)
rlm@77 907 )
rlm@77 908
rlm@77 909 (fn [world _]
rlm@77 910
rlm@77 911 (if @move-up?
rlm@77 912 (.applyForce control-a
rlm@77 913 (Vector3f. force 0 0)
rlm@77 914 (Vector3f. 0 0 0)))
rlm@77 915 (if @move-down?
rlm@77 916 (.applyForce control-a
rlm@77 917 (Vector3f. (- force) 0 0)
rlm@77 918 (Vector3f. 0 0 0)))
rlm@77 919 (if @move-left?
rlm@77 920 (.applyForce control-a
rlm@77 921 (Vector3f. 0 force 0)
rlm@77 922 (Vector3f. 0 0 0)))
rlm@77 923 (if @move-right?
rlm@77 924 (.applyForce control-a
rlm@77 925 (Vector3f. 0 (- force) 0)
rlm@77 926 (Vector3f. 0 0 0)))
rlm@77 927
rlm@77 928 (if @roll-left?
rlm@77 929 (.applyForce control-a
rlm@77 930 (Vector3f. 0 0 force)
rlm@77 931 (Vector3f. 0 0 0)))
rlm@77 932 (if @roll-right?
rlm@77 933 (.applyForce control-a
rlm@77 934 (Vector3f. 0 0 (- force))
rlm@77 935 (Vector3f. 0 0 0)))
rlm@77 936
rlm@77 937 (if (zero? (rem (swap! timer inc) 100))
rlm@77 938 (.attachChild
rlm@77 939 (.getRootNode world)
rlm@77 940 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 941 :physical? false :position
rlm@77 942 (.getWorldTranslation obj-a)))))
rlm@77 943 )
rlm@77 944 ))
rlm@77 945
rlm@94 946 (defn transform-trianglesdsd
rlm@94 947 "Transform that converts each vertex in the first triangle
rlm@94 948 into the corresponding vertex in the second triangle."
rlm@94 949 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 950 (let [in [(.get1 tri-1)
rlm@94 951 (.get2 tri-1)
rlm@94 952 (.get3 tri-1)]
rlm@94 953 out [(.get1 tri-2)
rlm@94 954 (.get2 tri-2)
rlm@94 955 (.get3 tri-2)]]
rlm@94 956 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 957 in* [(.mult translate (in 0))
rlm@94 958 (.mult translate (in 1))
rlm@94 959 (.mult translate (in 2))]
rlm@94 960 final-translation
rlm@94 961 (doto (Matrix4f.)
rlm@94 962 (.setTranslation (out 1)))
rlm@94 963
rlm@94 964 rotate-1
rlm@94 965 (doto (Matrix3f.)
rlm@94 966 (.fromStartEndVectors
rlm@94 967 (.normalize
rlm@94 968 (.subtract
rlm@94 969 (in* 1) (in* 0)))
rlm@94 970 (.normalize
rlm@94 971 (.subtract
rlm@94 972 (out 1) (out 0)))))
rlm@94 973 in** [(.mult rotate-1 (in* 0))
rlm@94 974 (.mult rotate-1 (in* 1))
rlm@94 975 (.mult rotate-1 (in* 2))]
rlm@94 976 scale-factor-1
rlm@94 977 (.mult
rlm@94 978 (.normalize
rlm@94 979 (.subtract
rlm@94 980 (out 1)
rlm@94 981 (out 0)))
rlm@94 982 (/ (.length
rlm@94 983 (.subtract (out 1)
rlm@94 984 (out 0)))
rlm@94 985 (.length
rlm@94 986 (.subtract (in** 1)
rlm@94 987 (in** 0)))))
rlm@94 988 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 989 in*** [(.mult scale-1 (in** 0))
rlm@94 990 (.mult scale-1 (in** 1))
rlm@94 991 (.mult scale-1 (in** 2))]
rlm@94 992
rlm@94 993
rlm@94 994
rlm@94 995
rlm@94 996
rlm@94 997 ]
rlm@94 998
rlm@94 999 (dorun (map println in))
rlm@94 1000 (println)
rlm@94 1001 (dorun (map println in*))
rlm@94 1002 (println)
rlm@94 1003 (dorun (map println in**))
rlm@94 1004 (println)
rlm@94 1005 (dorun (map println in***))
rlm@94 1006 (println)
rlm@94 1007
rlm@99 1008 ))))
rlm@94 1009
rlm@94 1010
rlm@106 1011 (defn world-setup [joint]
rlm@106 1012 (let [joint-position (Vector3f. 0 0 0)
rlm@106 1013 joint-rotation
rlm@106 1014 (.toRotationMatrix
rlm@106 1015 (.mult
rlm@106 1016 (doto (Quaternion.)
rlm@106 1017 (.fromAngleAxis
rlm@106 1018 (* 1 (/ Math/PI 4))
rlm@106 1019 (Vector3f. -1 0 0)))
rlm@106 1020 (doto (Quaternion.)
rlm@106 1021 (.fromAngleAxis
rlm@106 1022 (* 1 (/ Math/PI 2))
rlm@106 1023 (Vector3f. 0 0 1)))))
rlm@106 1024 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 1025
rlm@106 1026 origin (doto
rlm@106 1027 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 1028 :position top-position))
rlm@106 1029 top (doto
rlm@106 1030 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 1031 :position top-position)
rlm@106 1032
rlm@106 1033 (.addControl
rlm@106 1034 (RigidBodyControl.
rlm@106 1035 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1036 bottom (doto
rlm@106 1037 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1038 :position (Vector3f. 0 0 0))
rlm@106 1039 (.addControl
rlm@106 1040 (RigidBodyControl.
rlm@106 1041 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1042 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1043 :color ColorRGBA/Gray :mass 0)
rlm@106 1044 a (.getControl top RigidBodyControl)
rlm@106 1045 b (.getControl bottom RigidBodyControl)]
rlm@106 1046
rlm@106 1047 (cond
rlm@106 1048 (= joint :cone)
rlm@106 1049
rlm@106 1050 (doto (ConeJoint.
rlm@106 1051 a b
rlm@106 1052 (world-to-local top joint-position)
rlm@106 1053 (world-to-local bottom joint-position)
rlm@106 1054 joint-rotation
rlm@106 1055 joint-rotation
rlm@106 1056 )
rlm@106 1057
rlm@106 1058
rlm@106 1059 (.setLimit (* (/ 10) Math/PI)
rlm@106 1060 (* (/ 4) Math/PI)
rlm@106 1061 0)))
rlm@106 1062 [origin top bottom table]))
rlm@106 1063
rlm@106 1064 (defn test-joint [joint]
rlm@106 1065 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1066 control (.getControl top RigidBodyControl)
rlm@106 1067 move-up? (atom false)
rlm@106 1068 move-down? (atom false)
rlm@106 1069 move-left? (atom false)
rlm@106 1070 move-right? (atom false)
rlm@106 1071 roll-left? (atom false)
rlm@106 1072 roll-right? (atom false)
rlm@106 1073 timer (atom 0)]
rlm@106 1074
rlm@106 1075 (world
rlm@106 1076 (nodify [top bottom floor origin])
rlm@106 1077 (merge standard-debug-controls
rlm@106 1078 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1079 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1080 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1081 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1082 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1083 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1084
rlm@106 1085 (fn [world]
rlm@106 1086 (light-up-everything world)
rlm@106 1087 (enable-debug world)
rlm@106 1088 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1089 )
rlm@106 1090
rlm@106 1091 (fn [world _]
rlm@106 1092 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1093 (do
rlm@106 1094 ;; (println-repl @timer)
rlm@106 1095 (.attachChild (.getRootNode world)
rlm@106 1096 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1097 :position (.getWorldTranslation top)
rlm@106 1098 :physical? false))
rlm@106 1099 (.attachChild (.getRootNode world)
rlm@106 1100 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1101 :position (.getWorldTranslation bottom)
rlm@106 1102 :physical? false))))
rlm@106 1103
rlm@106 1104 (if @move-up?
rlm@106 1105 (.applyTorque control
rlm@106 1106 (.mult (.getPhysicsRotation control)
rlm@106 1107 (Vector3f. 0 0 10))))
rlm@106 1108 (if @move-down?
rlm@106 1109 (.applyTorque control
rlm@106 1110 (.mult (.getPhysicsRotation control)
rlm@106 1111 (Vector3f. 0 0 -10))))
rlm@106 1112 (if @move-left?
rlm@106 1113 (.applyTorque control
rlm@106 1114 (.mult (.getPhysicsRotation control)
rlm@106 1115 (Vector3f. 0 10 0))))
rlm@106 1116 (if @move-right?
rlm@106 1117 (.applyTorque control
rlm@106 1118 (.mult (.getPhysicsRotation control)
rlm@106 1119 (Vector3f. 0 -10 0))))
rlm@106 1120 (if @roll-left?
rlm@106 1121 (.applyTorque control
rlm@106 1122 (.mult (.getPhysicsRotation control)
rlm@106 1123 (Vector3f. -1 0 0))))
rlm@106 1124 (if @roll-right?
rlm@106 1125 (.applyTorque control
rlm@106 1126 (.mult (.getPhysicsRotation control)
rlm@106 1127 (Vector3f. 1 0 0))))))))
rlm@106 1128
rlm@99 1129
rlm@99 1130
rlm@107 1131 (defprotocol Frame
rlm@107 1132 (frame [this]))
rlm@107 1133
rlm@107 1134 (extend-type BufferedImage
rlm@107 1135 Frame
rlm@107 1136 (frame [image]
rlm@107 1137 (merge
rlm@107 1138 (apply
rlm@107 1139 hash-map
rlm@107 1140 (interleave
rlm@107 1141 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1142 (vector x y)))
rlm@107 1143 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1144 (let [data (.getRGB image x y)]
rlm@107 1145 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1146 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1147 :b (bit-and 0x0000ff data)))))))
rlm@107 1148 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1149
rlm@107 1150
rlm@107 1151 (extend-type ImagePlus
rlm@107 1152 Frame
rlm@107 1153 (frame [image+]
rlm@107 1154 (frame (.getBufferedImage image+))))
rlm@107 1155
rlm@107 1156
rlm@99 1157 #+end_src
rlm@99 1158
rlm@99 1159
rlm@99 1160 * COMMENT generate source
rlm@99 1161 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1162 <<body-1>>
rlm@99 1163 #+end_src
rlm@99 1164
rlm@99 1165
rlm@94 1166
rlm@94 1167