annotate org/test-creature.org @ 159:75b6c2ebbf8e

refactored audio code
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:41:16 -0700
parents 811127d79d24
children 33278bf028e7
rev   line source
rlm@73 1 #+title: First attempt at a creature!
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@129 9
rlm@129 10
rlm@99 11
rlm@73 12 * Intro
rlm@73 13 So far, I've made the following senses --
rlm@73 14 - Vision
rlm@73 15 - Hearing
rlm@73 16 - Touch
rlm@73 17 - Proprioception
rlm@73 18
rlm@73 19 And one effector:
rlm@73 20 - Movement
rlm@73 21
rlm@73 22 However, the code so far has only enabled these senses, but has not
rlm@73 23 actually implemented them. For example, there is still a lot of work
rlm@73 24 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 25 simulation that can be moved around and see the world from different
rlm@73 26 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 27 for the visual input, and I need to determine how/wether to
rlm@73 28 disceritise the visual input.
rlm@73 29
rlm@73 30 I also want to be able to visualize both the sensors and the
rlm@104 31 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 32 first attempt at bringing everything together.
rlm@73 33
rlm@73 34 * The creature's body
rlm@73 35
rlm@73 36 Still going to do an eve-like body in blender, but due to problems
rlm@104 37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 38 the connecting here in clojure code, using the names of the individual
rlm@73 39 components and trial and error. Later, I'll maybe make some sort of
rlm@73 40 creature-building modifications to blender that support whatever
rlm@158 41 discritized senses I'm going to make.
rlm@73 42
rlm@73 43 #+name: body-1
rlm@73 44 #+begin_src clojure
rlm@73 45 (ns cortex.silly
rlm@73 46 "let's play!"
rlm@73 47 {:author "Robert McIntyre"})
rlm@73 48
rlm@73 49 ;; TODO remove this!
rlm@73 50 (require 'cortex.import)
rlm@73 51 (cortex.import/mega-import-jme3)
rlm@158 52 (use '(cortex world util body hearing touch vision sense
rlm@158 53 proprioception movement))
rlm@73 54
rlm@73 55 (rlm.rlm-commands/help)
rlm@99 56 (import java.awt.image.BufferedImage)
rlm@99 57 (import javax.swing.JPanel)
rlm@99 58 (import javax.swing.SwingUtilities)
rlm@99 59 (import java.awt.Dimension)
rlm@99 60 (import javax.swing.JFrame)
rlm@99 61 (import java.awt.Dimension)
rlm@106 62 (import com.aurellem.capture.RatchetTimer)
rlm@99 63 (declare joint-create)
rlm@108 64 (use 'clojure.contrib.def)
rlm@73 65
rlm@73 66 (defn load-blender-model
rlm@73 67 "Load a .blend file using an asset folder relative path."
rlm@73 68 [^String model]
rlm@73 69 (.loadModel
rlm@73 70 (doto (asset-manager)
rlm@73 71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 72 model))
rlm@73 73
rlm@78 74 (defn blender-to-jme
rlm@78 75 "Convert from Blender coordinates to JME coordinates"
rlm@78 76 [#^Vector3f in]
rlm@78 77 (Vector3f. (.getX in)
rlm@78 78 (.getZ in)
rlm@78 79 (- (.getY in))))
rlm@74 80
rlm@96 81
rlm@87 82 (defmulti joint-dispatch
rlm@87 83 "Translate blender pseudo-joints into real JME joints."
rlm@88 84 (fn [constraints & _]
rlm@87 85 (:type constraints)))
rlm@87 86
rlm@87 87 (defmethod joint-dispatch :point
rlm@87 88 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 89 (println-repl "creating POINT2POINT joint")
rlm@130 90 ;; bullet's point2point joints are BROKEN, so we must use the
rlm@130 91 ;; generic 6DOF joint instead of an actual Point2Point joint!
rlm@130 92
rlm@130 93 ;; should be able to do this:
rlm@130 94 (comment
rlm@130 95 (Point2PointJoint.
rlm@130 96 control-a
rlm@130 97 control-b
rlm@130 98 pivot-a
rlm@130 99 pivot-b))
rlm@130 100
rlm@130 101 ;; but instead we must do this:
rlm@130 102 (println-repl "substuting 6DOF joint for POINT2POINT joint!")
rlm@130 103 (doto
rlm@130 104 (SixDofJoint.
rlm@130 105 control-a
rlm@130 106 control-b
rlm@130 107 pivot-a
rlm@130 108 pivot-b
rlm@130 109 false)
rlm@130 110 (.setLinearLowerLimit Vector3f/ZERO)
rlm@130 111 (.setLinearUpperLimit Vector3f/ZERO)
rlm@130 112 ;;(.setAngularLowerLimit (Vector3f. 1 1 1))
rlm@130 113 ;;(.setAngularUpperLimit (Vector3f. 0 0 0))
rlm@130 114
rlm@130 115 ))
rlm@130 116
rlm@87 117
rlm@87 118 (defmethod joint-dispatch :hinge
rlm@87 119 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 120 (println-repl "creating HINGE joint")
rlm@87 121 (let [axis
rlm@87 122 (if-let
rlm@87 123 [axis (:axis constraints)]
rlm@87 124 axis
rlm@87 125 Vector3f/UNIT_X)
rlm@87 126 [limit-1 limit-2] (:limit constraints)
rlm@87 127 hinge-axis
rlm@87 128 (.mult
rlm@87 129 rotation
rlm@87 130 (blender-to-jme axis))]
rlm@87 131 (doto
rlm@87 132 (HingeJoint.
rlm@87 133 control-a
rlm@87 134 control-b
rlm@87 135 pivot-a
rlm@87 136 pivot-b
rlm@87 137 hinge-axis
rlm@87 138 hinge-axis)
rlm@87 139 (.setLimit limit-1 limit-2))))
rlm@87 140
rlm@87 141 (defmethod joint-dispatch :cone
rlm@87 142 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 143 (let [limit-xz (:limit-xz constraints)
rlm@87 144 limit-xy (:limit-xy constraints)
rlm@87 145 twist (:twist constraints)]
rlm@87 146
rlm@87 147 (println-repl "creating CONE joint")
rlm@87 148 (println-repl rotation)
rlm@87 149 (println-repl
rlm@87 150 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 151 (println-repl
rlm@87 152 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 153 (println-repl
rlm@87 154 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 155 (doto
rlm@87 156 (ConeJoint.
rlm@87 157 control-a
rlm@87 158 control-b
rlm@87 159 pivot-a
rlm@87 160 pivot-b
rlm@87 161 rotation
rlm@87 162 rotation)
rlm@87 163 (.setLimit (float limit-xz)
rlm@87 164 (float limit-xy)
rlm@87 165 (float twist)))))
rlm@87 166
rlm@88 167 (defn connect
rlm@87 168 "here are some examples:
rlm@87 169 {:type :point}
rlm@87 170 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 171 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 172
rlm@89 173 {:type :cone :limit-xz 0]
rlm@89 174 :limit-xy 0]
rlm@89 175 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 176 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 177 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 178 control-b (.getControl obj-b RigidBodyControl)
rlm@87 179 joint-center (.getWorldTranslation joint)
rlm@87 180 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 181 pivot-a (world-to-local obj-a joint-center)
rlm@87 182 pivot-b (world-to-local obj-b joint-center)]
rlm@89 183
rlm@87 184 (if-let [constraints
rlm@87 185 (map-vals
rlm@87 186 eval
rlm@87 187 (read-string
rlm@87 188 (meta-data joint "joint")))]
rlm@89 189 ;; A side-effect of creating a joint registers
rlm@89 190 ;; it with both physics objects which in turn
rlm@89 191 ;; will register the joint with the physics system
rlm@89 192 ;; when the simulation is started.
rlm@87 193 (do
rlm@87 194 (println-repl "creating joint between"
rlm@87 195 (.getName obj-a) "and" (.getName obj-b))
rlm@87 196 (joint-dispatch constraints
rlm@87 197 control-a control-b
rlm@87 198 pivot-a pivot-b
rlm@87 199 joint-rotation))
rlm@87 200 (println-repl "could not find joint meta-data!"))))
rlm@87 201
rlm@130 202
rlm@130 203
rlm@130 204
rlm@78 205 (defn assemble-creature [#^Node pieces joints]
rlm@78 206 (dorun
rlm@78 207 (map
rlm@78 208 (fn [geom]
rlm@78 209 (let [physics-control
rlm@78 210 (RigidBodyControl.
rlm@78 211 (HullCollisionShape.
rlm@78 212 (.getMesh geom))
rlm@78 213 (if-let [mass (meta-data geom "mass")]
rlm@78 214 (do
rlm@78 215 (println-repl
rlm@78 216 "setting" (.getName geom) "mass to" (float mass))
rlm@78 217 (float mass))
rlm@78 218 (float 1)))]
rlm@78 219
rlm@78 220 (.addControl geom physics-control)))
rlm@78 221 (filter #(isa? (class %) Geometry )
rlm@78 222 (node-seq pieces))))
rlm@78 223 (dorun
rlm@78 224 (map
rlm@78 225 (fn [joint]
rlm@133 226 (let [[obj-a obj-b] (joint-targets pieces joint)]
rlm@88 227 (connect obj-a obj-b joint)))
rlm@78 228 joints))
rlm@78 229 pieces)
rlm@74 230
rlm@116 231 (declare blender-creature)
rlm@74 232
rlm@78 233 (def hand "Models/creature1/one.blend")
rlm@74 234
rlm@78 235 (def worm "Models/creature1/try-again.blend")
rlm@78 236
rlm@90 237 (defn worm-model [] (load-blender-model worm))
rlm@90 238
rlm@80 239 (defn x-ray [#^ColorRGBA color]
rlm@80 240 (doto (Material. (asset-manager)
rlm@80 241 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 242 (.setColor "Color" color)
rlm@80 243 (-> (.getAdditionalRenderState)
rlm@80 244 (.setDepthTest false))))
rlm@80 245
rlm@91 246 (defn colorful []
rlm@91 247 (.getChild (worm-model) "worm-21"))
rlm@90 248
rlm@90 249 (import jme3tools.converters.ImageToAwt)
rlm@90 250
rlm@90 251 (import ij.ImagePlus)
rlm@90 252
rlm@94 253
rlm@109 254
rlm@111 255 (defn test-eye []
rlm@117 256 (.getChild
rlm@117 257 (.getChild (worm-model) "eyes")
rlm@117 258 "eye"))
rlm@111 259
rlm@111 260
rlm@123 261
rlm@128 262 ;; lower level --- nodes
rlm@128 263 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
rlm@128 264
rlm@128 265 ;; higher level -- organs
rlm@128 266 ;;
rlm@128 267
rlm@128 268 ;; higher level --- sense/effector
rlm@128 269 ;; these are the functions that provide world i/o, chinese-room style
rlm@128 270
rlm@128 271
rlm@134 272
rlm@116 273
rlm@116 274 (defn blender-creature
rlm@116 275 "Return a creature with all joints in place."
rlm@116 276 [blender-path]
rlm@116 277 (let [model (load-blender-model blender-path)
rlm@134 278 joints (creature-joints model)]
rlm@134 279 (assemble-creature model joints)))
rlm@116 280
rlm@126 281 (defn gray-scale [num]
rlm@126 282 (+ num
rlm@126 283 (bit-shift-left num 8)
rlm@126 284 (bit-shift-left num 16)))
rlm@126 285
rlm@130 286 (defn debug-touch-window
rlm@103 287 "creates function that offers a debug view of sensor data"
rlm@103 288 []
rlm@103 289 (let [vi (view-image)]
rlm@103 290 (fn
rlm@103 291 [[coords sensor-data]]
rlm@103 292 (let [image (points->image coords)]
rlm@103 293 (dorun
rlm@103 294 (for [i (range (count coords))]
rlm@103 295 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@126 296 (gray-scale (sensor-data i)))))
rlm@126 297
rlm@126 298
rlm@103 299 (vi image)))))
rlm@103 300
rlm@118 301 (defn debug-vision-window
rlm@118 302 "creates function that offers a debug view of sensor data"
rlm@118 303 []
rlm@118 304 (let [vi (view-image)]
rlm@118 305 (fn
rlm@118 306 [[coords sensor-data]]
rlm@118 307 (let [image (points->image coords)]
rlm@118 308 (dorun
rlm@118 309 (for [i (range (count coords))]
rlm@118 310 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@118 311 (sensor-data i))))
rlm@118 312 (vi image)))))
rlm@118 313
rlm@123 314 (defn debug-hearing-window
rlm@123 315 "view audio data"
rlm@123 316 [height]
rlm@123 317 (let [vi (view-image)]
rlm@123 318 (fn [[coords sensor-data]]
rlm@123 319 (let [image (BufferedImage. (count coords) height
rlm@123 320 BufferedImage/TYPE_INT_RGB)]
rlm@123 321 (dorun
rlm@123 322 (for [x (range (count coords))]
rlm@123 323 (dorun
rlm@123 324 (for [y (range height)]
rlm@123 325 (let [raw-sensor (sensor-data x)]
rlm@126 326 (.setRGB image x y (gray-scale raw-sensor)))))))
rlm@126 327
rlm@123 328 (vi image)))))
rlm@123 329
rlm@106 330 (defn test-creature [thing]
rlm@106 331 (let [x-axis
rlm@106 332 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 333 y-axis
rlm@106 334 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 335 z-axis
rlm@106 336 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 337 creature (blender-creature thing)
rlm@106 338 touch-nerves (touch creature)
rlm@130 339 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
rlm@121 340 [init-vision-fns vision-data] (vision creature)
rlm@121 341 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
rlm@118 342 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@123 343 [init-hearing-fns hearing-senses] (hearing creature)
rlm@123 344 hearing-windows (map (fn [_] (debug-hearing-window 50))
rlm@123 345 hearing-senses)
rlm@124 346 bell (AudioNode. (asset-manager)
rlm@128 347 "Sounds/pure.wav" false)
rlm@130 348 prop (proprioception creature)
rlm@135 349 prop-debug (proprioception-debug-window)
rlm@148 350
rlm@148 351 muscle-fns (enable-muscles creature)
rlm@123 352 ;; dream
rlm@123 353
rlm@106 354 ]
rlm@143 355
rlm@143 356
rlm@143 357 (apply
rlm@143 358 world
rlm@143 359 (with-movement
rlm@143 360 (.getChild creature "worm-21")
rlm@143 361 ["key-r" "key-t"
rlm@143 362 "key-f" "key-g"
rlm@143 363 "key-v" "key-b"]
rlm@143 364 [10 10 10 10 1 1]
rlm@143 365 [(nodify [creature
rlm@143 366 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@143 367 :color ColorRGBA/Gray :mass 0)
rlm@143 368 x-axis y-axis z-axis
rlm@143 369 me
rlm@143 370 ])
rlm@143 371 (merge standard-debug-controls
rlm@143 372 {"key-return"
rlm@143 373 (fn [_ value]
rlm@143 374 (if value
rlm@143 375 (do
rlm@143 376 (println-repl "play-sound")
rlm@148 377 (.play bell))))
rlm@148 378 "key-h"
rlm@148 379 (fn [_ value]
rlm@148 380 (if value
rlm@148 381 (do
rlm@148 382 (println-repl "muscle activating!")
rlm@148 383 ((first muscle-fns) 199))))
rlm@148 384
rlm@148 385 })
rlm@143 386 (fn [world]
rlm@143 387 (light-up-everything world)
rlm@143 388 (enable-debug world)
rlm@143 389 (dorun (map #(% world) init-vision-fns))
rlm@143 390 (dorun (map #(% world) init-hearing-fns))
rlm@143 391
rlm@143 392 (add-eye world
rlm@143 393 (attach-eye creature (test-eye))
rlm@143 394 (comp (view-image) BufferedImage!))
rlm@143 395
rlm@143 396 (add-eye world (.getCamera world) no-op)
rlm@145 397 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@143 398 ;;(com.aurellem.capture.Capture/captureVideo
rlm@143 399 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@143 400 ;;(.setTimer world (RatchetTimer. 60))
rlm@143 401 (speed-up world)
rlm@148 402 (set-gravity world (Vector3f. 0 0 0))
rlm@143 403 )
rlm@143 404 (fn [world tpf]
rlm@143 405 ;;(dorun
rlm@143 406 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@143 407
rlm@143 408 (prop-debug (prop))
rlm@143 409
rlm@143 410 (dorun
rlm@143 411 (map #(%1 (%2 (.getRootNode world)))
rlm@143 412 touch-debug-windows touch-nerves))
rlm@143 413
rlm@143 414 (dorun
rlm@143 415 (map #(%1 (%2))
rlm@143 416 vision-debug vision-data))
rlm@143 417 (dorun
rlm@143 418 (map #(%1 (%2)) hearing-windows hearing-senses))
rlm@143 419
rlm@143 420
rlm@143 421 ;;(println-repl (vision-data))
rlm@143 422 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@143 423
rlm@143 424
rlm@143 425 )]
rlm@106 426 ;;(let [timer (atom 0)]
rlm@106 427 ;; (fn [_ _]
rlm@106 428 ;; (swap! timer inc)
rlm@106 429 ;; (if (= (rem @timer 60) 0)
rlm@106 430 ;; (println-repl (float (/ @timer 60))))))
rlm@143 431 ))))
rlm@83 432
rlm@109 433
rlm@116 434
rlm@116 435 ;; the camera will stay in its initial position/rotation with relation
rlm@116 436 ;; to the spatial.
rlm@116 437
rlm@116 438
rlm@117 439 (defn follow-test
rlm@117 440 "show a camera that stays in the same relative position to a blue cube."
rlm@117 441 []
rlm@116 442 (let [camera-pos (Vector3f. 0 30 0)
rlm@116 443 rock (box 1 1 1 :color ColorRGBA/Blue
rlm@116 444 :position (Vector3f. 0 10 0)
rlm@116 445 :mass 30
rlm@116 446 )
rlm@118 447 rot (.getWorldRotation rock)
rlm@116 448
rlm@116 449 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
rlm@116 450 :position (Vector3f. 0 -3 0))]
rlm@116 451
rlm@116 452 (world
rlm@116 453 (nodify [rock table])
rlm@116 454 standard-debug-controls
rlm@116 455 (fn [world]
rlm@116 456 (let
rlm@116 457 [cam (doto (.clone (.getCamera world))
rlm@116 458 (.setLocation camera-pos)
rlm@116 459 (.lookAt Vector3f/ZERO
rlm@116 460 Vector3f/UNIT_X))]
rlm@123 461 (bind-sense rock cam)
rlm@116 462
rlm@116 463 (.setTimer world (RatchetTimer. 60))
rlm@116 464 (add-eye world cam (comp (view-image) BufferedImage!))
rlm@116 465 (add-eye world (.getCamera world) no-op))
rlm@116 466 )
rlm@118 467 (fn [_ _] (println-repl rot)))))
rlm@116 468
rlm@118 469
rlm@123 470
rlm@87 471 #+end_src
rlm@83 472
rlm@87 473 #+results: body-1
rlm@133 474 : #'cortex.silly/follow-test
rlm@78 475
rlm@78 476
rlm@78 477 * COMMENT purgatory
rlm@78 478 #+begin_src clojure
rlm@74 479
rlm@77 480 (defn bullet-trans* []
rlm@77 481 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 482 :position (Vector3f. 5 0 0)
rlm@77 483 :mass 90)
rlm@77 484 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 485 :position (Vector3f. -5 0 0)
rlm@77 486 :mass 0)
rlm@77 487 control-a (.getControl obj-a RigidBodyControl)
rlm@77 488 control-b (.getControl obj-b RigidBodyControl)
rlm@77 489 move-up? (atom nil)
rlm@77 490 move-down? (atom nil)
rlm@77 491 move-left? (atom nil)
rlm@77 492 move-right? (atom nil)
rlm@77 493 roll-left? (atom nil)
rlm@77 494 roll-right? (atom nil)
rlm@77 495 force 100
rlm@77 496 swivel
rlm@77 497 (.toRotationMatrix
rlm@77 498 (doto (Quaternion.)
rlm@77 499 (.fromAngleAxis (/ Math/PI 2)
rlm@77 500 Vector3f/UNIT_X)))
rlm@77 501 x-move
rlm@77 502 (doto (Matrix3f.)
rlm@77 503 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 504 (.normalize (Vector3f. 1 1 0))))
rlm@77 505
rlm@77 506 timer (atom 0)]
rlm@77 507 (doto
rlm@77 508 (ConeJoint.
rlm@77 509 control-a control-b
rlm@77 510 (Vector3f. -8 0 0)
rlm@77 511 (Vector3f. 2 0 0)
rlm@77 512 ;;swivel swivel
rlm@77 513 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 514 x-move Matrix3f/IDENTITY
rlm@77 515 )
rlm@77 516 (.setCollisionBetweenLinkedBodys false)
rlm@77 517 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 518 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 519 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 520 (world (nodify
rlm@77 521 [obj-a obj-b])
rlm@77 522 (merge standard-debug-controls
rlm@77 523 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 524 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 525 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 526 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 527 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 528 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 529
rlm@77 530 (fn [world]
rlm@77 531 (enable-debug world)
rlm@77 532 (set-gravity world Vector3f/ZERO)
rlm@77 533 )
rlm@77 534
rlm@77 535 (fn [world _]
rlm@77 536
rlm@77 537 (if @move-up?
rlm@77 538 (.applyForce control-a
rlm@77 539 (Vector3f. force 0 0)
rlm@77 540 (Vector3f. 0 0 0)))
rlm@77 541 (if @move-down?
rlm@77 542 (.applyForce control-a
rlm@77 543 (Vector3f. (- force) 0 0)
rlm@77 544 (Vector3f. 0 0 0)))
rlm@77 545 (if @move-left?
rlm@77 546 (.applyForce control-a
rlm@77 547 (Vector3f. 0 force 0)
rlm@77 548 (Vector3f. 0 0 0)))
rlm@77 549 (if @move-right?
rlm@77 550 (.applyForce control-a
rlm@77 551 (Vector3f. 0 (- force) 0)
rlm@77 552 (Vector3f. 0 0 0)))
rlm@77 553
rlm@77 554 (if @roll-left?
rlm@77 555 (.applyForce control-a
rlm@77 556 (Vector3f. 0 0 force)
rlm@77 557 (Vector3f. 0 0 0)))
rlm@77 558 (if @roll-right?
rlm@77 559 (.applyForce control-a
rlm@77 560 (Vector3f. 0 0 (- force))
rlm@77 561 (Vector3f. 0 0 0)))
rlm@77 562
rlm@77 563 (if (zero? (rem (swap! timer inc) 100))
rlm@77 564 (.attachChild
rlm@77 565 (.getRootNode world)
rlm@77 566 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 567 :physical? false :position
rlm@77 568 (.getWorldTranslation obj-a)))))
rlm@77 569 )
rlm@77 570 ))
rlm@77 571
rlm@106 572 (defn test-joint [joint]
rlm@106 573 (let [[origin top bottom floor] (world-setup joint)
rlm@106 574 control (.getControl top RigidBodyControl)
rlm@106 575 move-up? (atom false)
rlm@106 576 move-down? (atom false)
rlm@106 577 move-left? (atom false)
rlm@106 578 move-right? (atom false)
rlm@106 579 roll-left? (atom false)
rlm@106 580 roll-right? (atom false)
rlm@106 581 timer (atom 0)]
rlm@106 582
rlm@106 583 (world
rlm@106 584 (nodify [top bottom floor origin])
rlm@106 585 (merge standard-debug-controls
rlm@106 586 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 587 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 588 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 589 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 590 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 591 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 592
rlm@106 593 (fn [world]
rlm@106 594 (light-up-everything world)
rlm@106 595 (enable-debug world)
rlm@106 596 (set-gravity world (Vector3f. 0 0 0))
rlm@106 597 )
rlm@106 598
rlm@106 599 (fn [world _]
rlm@106 600 (if (zero? (rem (swap! timer inc) 100))
rlm@106 601 (do
rlm@106 602 ;; (println-repl @timer)
rlm@106 603 (.attachChild (.getRootNode world)
rlm@106 604 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 605 :position (.getWorldTranslation top)
rlm@106 606 :physical? false))
rlm@106 607 (.attachChild (.getRootNode world)
rlm@106 608 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 609 :position (.getWorldTranslation bottom)
rlm@106 610 :physical? false))))
rlm@106 611
rlm@106 612 (if @move-up?
rlm@106 613 (.applyTorque control
rlm@106 614 (.mult (.getPhysicsRotation control)
rlm@106 615 (Vector3f. 0 0 10))))
rlm@106 616 (if @move-down?
rlm@106 617 (.applyTorque control
rlm@106 618 (.mult (.getPhysicsRotation control)
rlm@106 619 (Vector3f. 0 0 -10))))
rlm@106 620 (if @move-left?
rlm@106 621 (.applyTorque control
rlm@106 622 (.mult (.getPhysicsRotation control)
rlm@106 623 (Vector3f. 0 10 0))))
rlm@106 624 (if @move-right?
rlm@106 625 (.applyTorque control
rlm@106 626 (.mult (.getPhysicsRotation control)
rlm@106 627 (Vector3f. 0 -10 0))))
rlm@106 628 (if @roll-left?
rlm@106 629 (.applyTorque control
rlm@106 630 (.mult (.getPhysicsRotation control)
rlm@106 631 (Vector3f. -1 0 0))))
rlm@106 632 (if @roll-right?
rlm@106 633 (.applyTorque control
rlm@106 634 (.mult (.getPhysicsRotation control)
rlm@106 635 (Vector3f. 1 0 0))))))))
rlm@99 636 #+end_src
rlm@99 637
rlm@99 638
rlm@99 639 * COMMENT generate source
rlm@99 640 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 641 <<body-1>>
rlm@99 642 #+end_src
rlm@99 643
rlm@99 644
rlm@94 645
rlm@94 646