annotate org/test-creature.org @ 101:65332841b7d9

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