annotate org/test-creature.org @ 158:811127d79d24

refactored muscles
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:33:37 -0700
parents 84c67be00abe
children 75b6c2ebbf8e
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@123 262 ;; Ears work the same way as vision.
rlm@123 263
rlm@123 264 ;; (hearing creature) will return [init-functions
rlm@123 265 ;; sensor-functions]. The init functions each take the world and
rlm@123 266 ;; register a SoundProcessor that does foureier transforms on the
rlm@123 267 ;; incommong sound data, making it available to each sensor function.
rlm@123 268
rlm@123 269 (defn creature-ears
rlm@128 270 "Return the children of the creature's \"ears\" node."
rlm@128 271 ;;dylan
rlm@128 272 ;;"The ear nodes which are children of the \"ears\" node in the
rlm@128 273 ;;creature."
rlm@123 274 [#^Node creature]
rlm@123 275 (if-let [ear-node (.getChild creature "ears")]
rlm@123 276 (seq (.getChildren ear-node))
rlm@123 277 (do (println-repl "could not find ears node") [])))
rlm@123 278
rlm@116 279
rlm@128 280 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
rlm@128 281 ;;anchor-qualia, augment-organ, with-organ
rlm@117 282
rlm@117 283
rlm@123 284 (defn update-listener-velocity
rlm@123 285 "Update the listener's velocity every update loop."
rlm@123 286 [#^Spatial obj #^Listener lis]
rlm@123 287 (let [old-position (atom (.getLocation lis))]
rlm@123 288 (.addControl
rlm@123 289 obj
rlm@123 290 (proxy [AbstractControl] []
rlm@123 291 (controlUpdate [tpf]
rlm@123 292 (let [new-position (.getLocation lis)]
rlm@123 293 (.setVelocity
rlm@123 294 lis
rlm@123 295 (.mult (.subtract new-position @old-position)
rlm@123 296 (float (/ tpf))))
rlm@123 297 (reset! old-position new-position)))
rlm@123 298 (controlRender [_ _])))))
rlm@123 299
rlm@123 300 (import com.aurellem.capture.audio.AudioSendRenderer)
rlm@123 301
rlm@123 302 (defn attach-ear
rlm@123 303 [#^Application world #^Node creature #^Spatial ear continuation]
rlm@123 304 (let [target (closest-node creature ear)
rlm@123 305 lis (Listener.)
rlm@123 306 audio-renderer (.getAudioRenderer world)
rlm@123 307 sp (sound-processor continuation)]
rlm@123 308 (.setLocation lis (.getWorldTranslation ear))
rlm@123 309 (.setRotation lis (.getWorldRotation ear))
rlm@123 310 (bind-sense target lis)
rlm@123 311 (update-listener-velocity target lis)
rlm@123 312 (.addListener audio-renderer lis)
rlm@123 313 (.registerSoundProcessor audio-renderer lis sp)))
rlm@123 314
rlm@123 315 (defn enable-hearing
rlm@123 316 [#^Node creature #^Spatial ear]
rlm@123 317 (let [hearing-data (atom [])]
rlm@123 318 [(fn [world]
rlm@123 319 (attach-ear world creature ear
rlm@123 320 (fn [data]
rlm@123 321 (reset! hearing-data (vec data)))))
rlm@123 322 [(fn []
rlm@123 323 (let [data @hearing-data
rlm@123 324 topology
rlm@123 325 (vec (map #(vector % 0) (range 0 (count data))))
rlm@123 326 scaled-data
rlm@123 327 (vec
rlm@123 328 (map
rlm@123 329 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)
rlm@123 330 data))]
rlm@123 331 [topology scaled-data]))
rlm@123 332 ]]))
rlm@123 333
rlm@123 334 (defn hearing
rlm@123 335 [#^Node creature]
rlm@123 336 (reduce
rlm@123 337 (fn [[init-a senses-a]
rlm@123 338 [init-b senses-b]]
rlm@123 339 [(conj init-a init-b)
rlm@123 340 (into senses-a senses-b)])
rlm@123 341 [[][]]
rlm@123 342 (for [ear (creature-ears creature)]
rlm@123 343 (enable-hearing creature ear))))
rlm@123 344
rlm@128 345
rlm@128 346
rlm@128 347
rlm@128 348
rlm@128 349
rlm@128 350 ;; lower level --- nodes
rlm@128 351 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
rlm@128 352
rlm@128 353 ;; higher level -- organs
rlm@128 354 ;;
rlm@128 355
rlm@128 356 ;; higher level --- sense/effector
rlm@128 357 ;; these are the functions that provide world i/o, chinese-room style
rlm@128 358
rlm@128 359
rlm@134 360
rlm@116 361
rlm@116 362 (defn blender-creature
rlm@116 363 "Return a creature with all joints in place."
rlm@116 364 [blender-path]
rlm@116 365 (let [model (load-blender-model blender-path)
rlm@134 366 joints (creature-joints model)]
rlm@134 367 (assemble-creature model joints)))
rlm@116 368
rlm@126 369 (defn gray-scale [num]
rlm@126 370 (+ num
rlm@126 371 (bit-shift-left num 8)
rlm@126 372 (bit-shift-left num 16)))
rlm@126 373
rlm@130 374 (defn debug-touch-window
rlm@103 375 "creates function that offers a debug view of sensor data"
rlm@103 376 []
rlm@103 377 (let [vi (view-image)]
rlm@103 378 (fn
rlm@103 379 [[coords sensor-data]]
rlm@103 380 (let [image (points->image coords)]
rlm@103 381 (dorun
rlm@103 382 (for [i (range (count coords))]
rlm@103 383 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@126 384 (gray-scale (sensor-data i)))))
rlm@126 385
rlm@126 386
rlm@103 387 (vi image)))))
rlm@103 388
rlm@118 389 (defn debug-vision-window
rlm@118 390 "creates function that offers a debug view of sensor data"
rlm@118 391 []
rlm@118 392 (let [vi (view-image)]
rlm@118 393 (fn
rlm@118 394 [[coords sensor-data]]
rlm@118 395 (let [image (points->image coords)]
rlm@118 396 (dorun
rlm@118 397 (for [i (range (count coords))]
rlm@118 398 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@118 399 (sensor-data i))))
rlm@118 400 (vi image)))))
rlm@118 401
rlm@123 402 (defn debug-hearing-window
rlm@123 403 "view audio data"
rlm@123 404 [height]
rlm@123 405 (let [vi (view-image)]
rlm@123 406 (fn [[coords sensor-data]]
rlm@123 407 (let [image (BufferedImage. (count coords) height
rlm@123 408 BufferedImage/TYPE_INT_RGB)]
rlm@123 409 (dorun
rlm@123 410 (for [x (range (count coords))]
rlm@123 411 (dorun
rlm@123 412 (for [y (range height)]
rlm@123 413 (let [raw-sensor (sensor-data x)]
rlm@126 414 (.setRGB image x y (gray-scale raw-sensor)))))))
rlm@126 415
rlm@123 416 (vi image)))))
rlm@123 417
rlm@123 418
rlm@123 419
rlm@106 420 ;;(defn test-touch [world creature]
rlm@83 421
rlm@78 422
rlm@123 423
rlm@123 424
rlm@130 425
rlm@106 426 (defn test-creature [thing]
rlm@106 427 (let [x-axis
rlm@106 428 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 429 y-axis
rlm@106 430 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 431 z-axis
rlm@106 432 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 433 creature (blender-creature thing)
rlm@106 434 touch-nerves (touch creature)
rlm@130 435 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
rlm@121 436 [init-vision-fns vision-data] (vision creature)
rlm@121 437 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
rlm@118 438 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@123 439 [init-hearing-fns hearing-senses] (hearing creature)
rlm@123 440 hearing-windows (map (fn [_] (debug-hearing-window 50))
rlm@123 441 hearing-senses)
rlm@124 442 bell (AudioNode. (asset-manager)
rlm@128 443 "Sounds/pure.wav" false)
rlm@130 444 prop (proprioception creature)
rlm@135 445 prop-debug (proprioception-debug-window)
rlm@148 446
rlm@148 447 muscle-fns (enable-muscles creature)
rlm@123 448 ;; dream
rlm@123 449
rlm@106 450 ]
rlm@143 451
rlm@143 452
rlm@143 453 (apply
rlm@143 454 world
rlm@143 455 (with-movement
rlm@143 456 (.getChild creature "worm-21")
rlm@143 457 ["key-r" "key-t"
rlm@143 458 "key-f" "key-g"
rlm@143 459 "key-v" "key-b"]
rlm@143 460 [10 10 10 10 1 1]
rlm@143 461 [(nodify [creature
rlm@143 462 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@143 463 :color ColorRGBA/Gray :mass 0)
rlm@143 464 x-axis y-axis z-axis
rlm@143 465 me
rlm@143 466 ])
rlm@143 467 (merge standard-debug-controls
rlm@143 468 {"key-return"
rlm@143 469 (fn [_ value]
rlm@143 470 (if value
rlm@143 471 (do
rlm@143 472 (println-repl "play-sound")
rlm@148 473 (.play bell))))
rlm@148 474 "key-h"
rlm@148 475 (fn [_ value]
rlm@148 476 (if value
rlm@148 477 (do
rlm@148 478 (println-repl "muscle activating!")
rlm@148 479 ((first muscle-fns) 199))))
rlm@148 480
rlm@148 481 })
rlm@143 482 (fn [world]
rlm@143 483 (light-up-everything world)
rlm@143 484 (enable-debug world)
rlm@143 485 (dorun (map #(% world) init-vision-fns))
rlm@143 486 (dorun (map #(% world) init-hearing-fns))
rlm@143 487
rlm@143 488 (add-eye world
rlm@143 489 (attach-eye creature (test-eye))
rlm@143 490 (comp (view-image) BufferedImage!))
rlm@143 491
rlm@143 492 (add-eye world (.getCamera world) no-op)
rlm@145 493 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@143 494 ;;(com.aurellem.capture.Capture/captureVideo
rlm@143 495 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@143 496 ;;(.setTimer world (RatchetTimer. 60))
rlm@143 497 (speed-up world)
rlm@148 498 (set-gravity world (Vector3f. 0 0 0))
rlm@143 499 )
rlm@143 500 (fn [world tpf]
rlm@143 501 ;;(dorun
rlm@143 502 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@143 503
rlm@143 504 (prop-debug (prop))
rlm@143 505
rlm@143 506 (dorun
rlm@143 507 (map #(%1 (%2 (.getRootNode world)))
rlm@143 508 touch-debug-windows touch-nerves))
rlm@143 509
rlm@143 510 (dorun
rlm@143 511 (map #(%1 (%2))
rlm@143 512 vision-debug vision-data))
rlm@143 513 (dorun
rlm@143 514 (map #(%1 (%2)) hearing-windows hearing-senses))
rlm@143 515
rlm@143 516
rlm@143 517 ;;(println-repl (vision-data))
rlm@143 518 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@143 519
rlm@143 520
rlm@143 521 )]
rlm@106 522 ;;(let [timer (atom 0)]
rlm@106 523 ;; (fn [_ _]
rlm@106 524 ;; (swap! timer inc)
rlm@106 525 ;; (if (= (rem @timer 60) 0)
rlm@106 526 ;; (println-repl (float (/ @timer 60))))))
rlm@143 527 ))))
rlm@83 528
rlm@109 529
rlm@116 530
rlm@116 531 ;; the camera will stay in its initial position/rotation with relation
rlm@116 532 ;; to the spatial.
rlm@116 533
rlm@116 534
rlm@117 535 (defn follow-test
rlm@117 536 "show a camera that stays in the same relative position to a blue cube."
rlm@117 537 []
rlm@116 538 (let [camera-pos (Vector3f. 0 30 0)
rlm@116 539 rock (box 1 1 1 :color ColorRGBA/Blue
rlm@116 540 :position (Vector3f. 0 10 0)
rlm@116 541 :mass 30
rlm@116 542 )
rlm@118 543 rot (.getWorldRotation rock)
rlm@116 544
rlm@116 545 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
rlm@116 546 :position (Vector3f. 0 -3 0))]
rlm@116 547
rlm@116 548 (world
rlm@116 549 (nodify [rock table])
rlm@116 550 standard-debug-controls
rlm@116 551 (fn [world]
rlm@116 552 (let
rlm@116 553 [cam (doto (.clone (.getCamera world))
rlm@116 554 (.setLocation camera-pos)
rlm@116 555 (.lookAt Vector3f/ZERO
rlm@116 556 Vector3f/UNIT_X))]
rlm@123 557 (bind-sense rock cam)
rlm@116 558
rlm@116 559 (.setTimer world (RatchetTimer. 60))
rlm@116 560 (add-eye world cam (comp (view-image) BufferedImage!))
rlm@116 561 (add-eye world (.getCamera world) no-op))
rlm@116 562 )
rlm@118 563 (fn [_ _] (println-repl rot)))))
rlm@116 564
rlm@118 565
rlm@123 566
rlm@87 567 #+end_src
rlm@83 568
rlm@87 569 #+results: body-1
rlm@133 570 : #'cortex.silly/follow-test
rlm@78 571
rlm@78 572
rlm@78 573 * COMMENT purgatory
rlm@78 574 #+begin_src clojure
rlm@74 575
rlm@77 576 (defn bullet-trans* []
rlm@77 577 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 578 :position (Vector3f. 5 0 0)
rlm@77 579 :mass 90)
rlm@77 580 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 581 :position (Vector3f. -5 0 0)
rlm@77 582 :mass 0)
rlm@77 583 control-a (.getControl obj-a RigidBodyControl)
rlm@77 584 control-b (.getControl obj-b RigidBodyControl)
rlm@77 585 move-up? (atom nil)
rlm@77 586 move-down? (atom nil)
rlm@77 587 move-left? (atom nil)
rlm@77 588 move-right? (atom nil)
rlm@77 589 roll-left? (atom nil)
rlm@77 590 roll-right? (atom nil)
rlm@77 591 force 100
rlm@77 592 swivel
rlm@77 593 (.toRotationMatrix
rlm@77 594 (doto (Quaternion.)
rlm@77 595 (.fromAngleAxis (/ Math/PI 2)
rlm@77 596 Vector3f/UNIT_X)))
rlm@77 597 x-move
rlm@77 598 (doto (Matrix3f.)
rlm@77 599 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 600 (.normalize (Vector3f. 1 1 0))))
rlm@77 601
rlm@77 602 timer (atom 0)]
rlm@77 603 (doto
rlm@77 604 (ConeJoint.
rlm@77 605 control-a control-b
rlm@77 606 (Vector3f. -8 0 0)
rlm@77 607 (Vector3f. 2 0 0)
rlm@77 608 ;;swivel swivel
rlm@77 609 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 610 x-move Matrix3f/IDENTITY
rlm@77 611 )
rlm@77 612 (.setCollisionBetweenLinkedBodys false)
rlm@77 613 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 614 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 615 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 616 (world (nodify
rlm@77 617 [obj-a obj-b])
rlm@77 618 (merge standard-debug-controls
rlm@77 619 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 620 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 621 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 622 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 623 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 624 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 625
rlm@77 626 (fn [world]
rlm@77 627 (enable-debug world)
rlm@77 628 (set-gravity world Vector3f/ZERO)
rlm@77 629 )
rlm@77 630
rlm@77 631 (fn [world _]
rlm@77 632
rlm@77 633 (if @move-up?
rlm@77 634 (.applyForce control-a
rlm@77 635 (Vector3f. force 0 0)
rlm@77 636 (Vector3f. 0 0 0)))
rlm@77 637 (if @move-down?
rlm@77 638 (.applyForce control-a
rlm@77 639 (Vector3f. (- force) 0 0)
rlm@77 640 (Vector3f. 0 0 0)))
rlm@77 641 (if @move-left?
rlm@77 642 (.applyForce control-a
rlm@77 643 (Vector3f. 0 force 0)
rlm@77 644 (Vector3f. 0 0 0)))
rlm@77 645 (if @move-right?
rlm@77 646 (.applyForce control-a
rlm@77 647 (Vector3f. 0 (- force) 0)
rlm@77 648 (Vector3f. 0 0 0)))
rlm@77 649
rlm@77 650 (if @roll-left?
rlm@77 651 (.applyForce control-a
rlm@77 652 (Vector3f. 0 0 force)
rlm@77 653 (Vector3f. 0 0 0)))
rlm@77 654 (if @roll-right?
rlm@77 655 (.applyForce control-a
rlm@77 656 (Vector3f. 0 0 (- force))
rlm@77 657 (Vector3f. 0 0 0)))
rlm@77 658
rlm@77 659 (if (zero? (rem (swap! timer inc) 100))
rlm@77 660 (.attachChild
rlm@77 661 (.getRootNode world)
rlm@77 662 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 663 :physical? false :position
rlm@77 664 (.getWorldTranslation obj-a)))))
rlm@77 665 )
rlm@77 666 ))
rlm@77 667
rlm@106 668 (defn test-joint [joint]
rlm@106 669 (let [[origin top bottom floor] (world-setup joint)
rlm@106 670 control (.getControl top RigidBodyControl)
rlm@106 671 move-up? (atom false)
rlm@106 672 move-down? (atom false)
rlm@106 673 move-left? (atom false)
rlm@106 674 move-right? (atom false)
rlm@106 675 roll-left? (atom false)
rlm@106 676 roll-right? (atom false)
rlm@106 677 timer (atom 0)]
rlm@106 678
rlm@106 679 (world
rlm@106 680 (nodify [top bottom floor origin])
rlm@106 681 (merge standard-debug-controls
rlm@106 682 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 683 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 684 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 685 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 686 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 687 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 688
rlm@106 689 (fn [world]
rlm@106 690 (light-up-everything world)
rlm@106 691 (enable-debug world)
rlm@106 692 (set-gravity world (Vector3f. 0 0 0))
rlm@106 693 )
rlm@106 694
rlm@106 695 (fn [world _]
rlm@106 696 (if (zero? (rem (swap! timer inc) 100))
rlm@106 697 (do
rlm@106 698 ;; (println-repl @timer)
rlm@106 699 (.attachChild (.getRootNode world)
rlm@106 700 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 701 :position (.getWorldTranslation top)
rlm@106 702 :physical? false))
rlm@106 703 (.attachChild (.getRootNode world)
rlm@106 704 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 705 :position (.getWorldTranslation bottom)
rlm@106 706 :physical? false))))
rlm@106 707
rlm@106 708 (if @move-up?
rlm@106 709 (.applyTorque control
rlm@106 710 (.mult (.getPhysicsRotation control)
rlm@106 711 (Vector3f. 0 0 10))))
rlm@106 712 (if @move-down?
rlm@106 713 (.applyTorque control
rlm@106 714 (.mult (.getPhysicsRotation control)
rlm@106 715 (Vector3f. 0 0 -10))))
rlm@106 716 (if @move-left?
rlm@106 717 (.applyTorque control
rlm@106 718 (.mult (.getPhysicsRotation control)
rlm@106 719 (Vector3f. 0 10 0))))
rlm@106 720 (if @move-right?
rlm@106 721 (.applyTorque control
rlm@106 722 (.mult (.getPhysicsRotation control)
rlm@106 723 (Vector3f. 0 -10 0))))
rlm@106 724 (if @roll-left?
rlm@106 725 (.applyTorque control
rlm@106 726 (.mult (.getPhysicsRotation control)
rlm@106 727 (Vector3f. -1 0 0))))
rlm@106 728 (if @roll-right?
rlm@106 729 (.applyTorque control
rlm@106 730 (.mult (.getPhysicsRotation control)
rlm@106 731 (Vector3f. 1 0 0))))))))
rlm@99 732 #+end_src
rlm@99 733
rlm@99 734
rlm@99 735 * COMMENT generate source
rlm@99 736 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 737 <<body-1>>
rlm@99 738 #+end_src
rlm@99 739
rlm@99 740
rlm@94 741
rlm@94 742