annotate org/body.org @ 63:7f2653ad3199

cleaning
author Robert McIntyre <rlm@mit.edu>
date Tue, 29 Nov 2011 02:46:46 -0700
parents 2b9d81017cb7
children ab1fee4280c3
rev   line source
rlm@0 1 #+title: The BODY!!!
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@4 4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.
rlm@4 5 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@4 6 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@4 7
rlm@63 8 * COMMENT Body
rlm@0 9
rlm@0 10 #+srcname: body-main
rlm@0 11 #+begin_src clojure
rlm@44 12 (ns cortex.body
rlm@44 13 (use (cortex world util import)))
rlm@44 14
rlm@0 15 (use 'clojure.contrib.def)
rlm@0 16 (cortex.import/mega-import-jme3)
rlm@0 17 (rlm.rlm-commands/help)
rlm@0 18
rlm@63 19 (defn load-blender-model
rlm@63 20 "Load a .blend file using an asset folder relative path."
rlm@49 21 [^String model]
rlm@49 22 (.loadModel
rlm@49 23 (doto (asset-manager)
rlm@49 24 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@49 25 model))
rlm@49 26
rlm@58 27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@58 28
rlm@60 29 ;;;;;;;;;;;; eve-style bodies ;;;;;;;;
rlm@58 30
rlm@63 31 (defn worm-segments
rlm@63 32 "Create multiple evenly spaced box segments. They're fabulous!"
rlm@63 33 [segment-length num-segments interstitial-space radius]
rlm@58 34 (letfn [(nth-segment
rlm@58 35 [n]
rlm@58 36 (box segment-length radius radius :mass 0.1
rlm@58 37 :position
rlm@58 38 (Vector3f.
rlm@58 39 (* 2 n (+ interstitial-space segment-length)) 0 0)
rlm@58 40 :name (str "worm-segment" n)
rlm@58 41 :color (ColorRGBA/randomColor)))]
rlm@58 42 (map nth-segment (range num-segments))))
rlm@58 43
rlm@58 44 (defn connect-at-midpoint
rlm@63 45 "Connect two physics objects with a Point2Point joint constraint at
rlm@63 46 the point equidistant from both objects' centers."
rlm@58 47 [segmentA segmentB]
rlm@58 48 (let [centerA (.getWorldTranslation segmentA)
rlm@58 49 centerB (.getWorldTranslation segmentB)
rlm@58 50 midpoint (.mult (.add centerA centerB) (float 0.5))
rlm@58 51 pivotA (.subtract midpoint centerA)
rlm@58 52 pivotB (.subtract midpoint centerB)
rlm@63 53
rlm@63 54 ;; A side-effect of creating a joint registers
rlm@63 55 ;; it with both physics objects which in turn
rlm@63 56 ;; will register the joint with the physics system
rlm@63 57 ;; when the simulation is started.
rlm@58 58 joint (Point2PointJoint.
rlm@58 59 (.getControl segmentA RigidBodyControl)
rlm@58 60 (.getControl segmentB RigidBodyControl)
rlm@58 61 pivotA
rlm@58 62 pivotB)]
rlm@58 63 segmentB))
rlm@58 64
rlm@58 65 (defn point-worm []
rlm@63 66 (let [segments (worm-segments 0.2 5 0.1 0.1)]
rlm@58 67 (dorun (map (partial apply connect-at-midpoint)
rlm@58 68 (partition 2 1 segments)))
rlm@63 69 (nodify "worm" segments)))
rlm@58 70
rlm@63 71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@63 72 ;;;;;;;;; Proprioception ;;;;;;;;;;;;;
rlm@63 73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@49 74
rlm@63 75 (declare
rlm@63 76 ;; generate an arbitray (but stable) orthogonal vector
rlm@63 77 ;; to a given vector.
rlm@63 78 some-orthogonal
rlm@56 79
rlm@63 80 ;; determine the amount of rotation a quaternion will
rlm@63 81 ;; cause about a given axis
rlm@63 82 project-quaternion
rlm@56 83
rlm@63 84 ;; proprioception for a single joint
rlm@63 85 joint-proprioception
rlm@63 86
rlm@63 87 ;; create a function that provides proprioceptive information
rlm@63 88 ;; about an entire body.
rlm@63 89 proprioception)
rlm@56 90
rlm@63 91 (defn some-orthogonal
rlm@63 92 "Generate an arbitray (but stable) orthogonal vector to a given
rlm@63 93 vector."
rlm@60 94 [vector]
rlm@60 95 (let [x (.getX vector)
rlm@60 96 y (.getY vector)
rlm@60 97 z (.getZ vector)]
rlm@60 98 (cond
rlm@60 99 (not= x (float 0)) (Vector3f. (- z) 0 x)
rlm@60 100 (not= y (float 0)) (Vector3f. 0 (- z) y)
rlm@60 101 (not= z (float 0)) (Vector3f. 0 (- z) y)
rlm@60 102 true Vector3f/ZERO)))
rlm@60 103
rlm@63 104 (defn project-quaternion
rlm@63 105 "From http://stackoverflow.com/questions/3684269/
rlm@63 106 component-of-a-quaternion-rotation-around-an-axis.
rlm@63 107
rlm@63 108 Determine the amount of rotation a quaternion will
rlm@63 109 cause about a given axis."
rlm@63 110 [#^Quaternion q #^Vector3f axis]
rlm@60 111 (let [basis-1 (orthogonal-vect axis)
rlm@60 112 basis-2 (.cross axis basis-1)
rlm@60 113 rotated (.mult q basis-1)
rlm@60 114 alpha (.dot basis-1 (.project rotated basis-1))
rlm@60 115 beta (.dot basis-2 (.project rotated basis-2))]
rlm@60 116 (Math/atan2 beta alpha)))
rlm@60 117
rlm@63 118 (defn joint-proprioception
rlm@63 119 "Relative position information for a two-part system connected by a
rlm@63 120 joint. Gives the pitch, yaw, and roll of the 'B' object relative to
rlm@63 121 the 'A' object, as determined by the joint."
rlm@63 122 [joint]
rlm@60 123 (let [object-a (.getUserObject (.getBodyA joint))
rlm@60 124 object-b (.getUserObject (.getBodyB joint))
rlm@60 125 arm-a
rlm@60 126 (.normalize
rlm@60 127 (.subtract
rlm@60 128 (.localToWorld object-a (.getPivotA joint) nil)
rlm@60 129 (.getWorldTranslation object-a)))
rlm@60 130 rotate-a
rlm@60 131 (doto (Matrix3f.)
rlm@60 132 (.fromStartEndVectors arm-a Vector3f/UNIT_X))
rlm@60 133 arm-b
rlm@60 134 (.mult
rlm@60 135 rotate-a
rlm@60 136 (.normalize
rlm@60 137 (.subtract
rlm@60 138 (.localToWorld object-b (.getPivotB joint) nil)
rlm@60 139 (.getWorldTranslation object-b))))
rlm@60 140 pitch
rlm@60 141 (.angleBetween
rlm@60 142 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
rlm@60 143 (Vector2f. 1 0))
rlm@60 144 yaw
rlm@60 145 (.angleBetween
rlm@60 146 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
rlm@60 147 (Vector2f. 1 0))
rlm@60 148
rlm@60 149 roll
rlm@61 150 (rot-about-axis
rlm@61 151 (.mult
rlm@61 152 (.getLocalRotation object-b)
rlm@61 153 (doto (Quaternion.)
rlm@61 154 (.fromRotationMatrix rotate-a)))
rlm@63 155 arm-b)]
rlm@60 156 [pitch yaw roll]))
rlm@60 157
rlm@63 158 (defn proprioception
rlm@63 159 "Create a function that provides proprioceptive information about an
rlm@63 160 entire body."
rlm@63 161 [body]
rlm@63 162 ;; extract the body's joints
rlm@63 163 (let [joints
rlm@63 164 (distinct
rlm@63 165 (reduce
rlm@63 166 concat
rlm@63 167 (map #(.getJoints %)
rlm@63 168 (keep
rlm@63 169 #(.getControl % RigidBodyControl)
rlm@63 170 (node-seq body)))))]
rlm@63 171 (fn []
rlm@63 172 (map joint-proprioception joints))))
rlm@60 173
rlm@63 174
rlm@63 175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@63 176 ;;;;;;;;; Mortor Control ;;;;;;;;;;;;;
rlm@63 177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@63 178
rlm@63 179
rlm@63 180 ;; surprisingly enough, terristerial creatures only move by using
rlm@63 181 ;; torque applied about their joints. There's not a single straight
rlm@63 182 ;; line of force in the human body at all! (A straight line of force
rlm@63 183 ;; would correspond to some sort of jet or rocket propulseion.)
rlm@63 184
rlm@63 185 (defn vector-motor-control
rlm@63 186 "Create a function that accepts a sequence of Vector3f objects that
rlm@63 187 describe the torque to be applied to each part of the body."
rlm@63 188 [body]
rlm@63 189 (let [nodes (node-seq body)
rlm@63 190 controls (keep #(.getControl % RigidBodyControl) nodes)]
rlm@63 191 (fn [torques]
rlm@63 192 (map #(.applyTorque %1 %2)
rlm@63 193 controls torques))))
rlm@63 194
rlm@63 195 ;; note -- might want to add a lower dimensional, discrete version of
rlm@63 196 ;; this if it proves usefull from a x-modal clustering perspective.
rlm@63 197
rlm@63 198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@63 199
rlm@63 200
rlm@63 201
rlm@63 202 (defn worm-pattern [time]
rlm@63 203 [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@63 204
rlm@63 205 0 0 0 0 0 0 0 0 0 0 0
rlm@63 206
rlm@63 207 (* 20 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))
rlm@63 208
rlm@63 209 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@63 210 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@63 211 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@63 212
rlm@63 213 ])
rlm@63 214
rlm@63 215 (defn worm-pattern [time]
rlm@63 216 (let [angle (* Math/PI (/ 4 20))
rlm@63 217 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
rlm@63 218 [Vector3f/ZERO
rlm@63 219 (.mult
rlm@63 220 direction
rlm@63 221 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
rlm@63 222 Vector3f/ZERO
rlm@63 223 Vector3f/ZERO
rlm@63 224 Vector3f/ZERO]))
rlm@60 225
rlm@60 226 (defn test-worm-control
rlm@60 227 []
rlm@60 228 (let [worm (point-worm)
rlm@60 229 time (atom 0)
rlm@63 230 worm-motor-map (vector-motor-control worm)]
rlm@60 231 (world
rlm@60 232 (nodify [worm
rlm@60 233 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 234 :color ColorRGBA/Gray)])
rlm@60 235 standard-debug-controls
rlm@60 236 (fn [world]
rlm@60 237 (enable-debug world)
rlm@60 238 (light-up-everything world)
rlm@63 239 (comment
rlm@63 240 (com.aurellem.capture.Capture/captureVideo
rlm@63 241 world
rlm@63 242 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
rlm@63 243 )
rlm@60 244
rlm@60 245 (fn [_ _]
rlm@60 246 (swap! time inc)
rlm@63 247 ;;(Thread/sleep 200)
rlm@60 248 (dorun (worm-motor-map
rlm@60 249 (worm-pattern @time)))))))
rlm@60 250
rlm@60 251
rlm@60 252
rlm@60 253
rlm@60 254 (defn test-prop
rlm@60 255 "see how torque works."
rlm@60 256 []
rlm@60 257 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
rlm@60 258 :mass 0 :color ColorRGBA/Green)
rlm@60 259 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
rlm@61 260 :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
rlm@60 261 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
rlm@60 262 :mass 0 :color ColorRGBA/Gray)
rlm@60 263
rlm@60 264 move-up? (atom false)
rlm@60 265 move-down? (atom false)
rlm@60 266 move-left? (atom false)
rlm@60 267 move-right? (atom false)
rlm@60 268 roll-left? (atom false)
rlm@60 269 roll-right? (atom false)
rlm@60 270 control (.getControl finger RigidBodyControl)
rlm@60 271 joint
rlm@60 272 (doto
rlm@60 273 (Point2PointJoint.
rlm@60 274 (.getControl hand RigidBodyControl)
rlm@60 275 control
rlm@60 276 (Vector3f. 1.2 0 0)
rlm@60 277 (Vector3f. -1.2 0 0 ))
rlm@60 278 (.setCollisionBetweenLinkedBodys false))
rlm@60 279 time (atom 0)
rlm@60 280 ]
rlm@60 281 (world
rlm@60 282 (nodify [hand finger floor])
rlm@60 283 (merge standard-debug-controls
rlm@60 284 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@60 285 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@60 286 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@60 287 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@60 288 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@60 289 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@60 290 (fn [world]
rlm@61 291 (set-gravity world (Vector3f. 0 0 0))
rlm@60 292 (.setMoveSpeed (.getFlyByCamera world) 50)
rlm@60 293 (.setRotationSpeed (.getFlyByCamera world) 50)
rlm@60 294 (light-up-everything world)
rlm@60 295 (.setTimer world (NanoTimer.))
rlm@60 296 )
rlm@60 297 (fn [_ _]
rlm@60 298 (if @move-up?
rlm@60 299 (.applyTorque control
rlm@60 300 (.mult (.getPhysicsRotation control)
rlm@61 301 (Vector3f. 0 0 10))))
rlm@60 302 (if @move-down?
rlm@60 303 (.applyTorque control
rlm@60 304 (.mult (.getPhysicsRotation control)
rlm@61 305 (Vector3f. 0 0 -10))))
rlm@60 306 (if @move-left?
rlm@60 307 (.applyTorque control
rlm@60 308 (.mult (.getPhysicsRotation control)
rlm@61 309 (Vector3f. 0 10 0))))
rlm@60 310 (if @move-right?
rlm@60 311 (.applyTorque control
rlm@60 312 (.mult (.getPhysicsRotation control)
rlm@61 313 (Vector3f. 0 -10 0))))
rlm@60 314 (if @roll-left?
rlm@60 315 (.applyTorque control
rlm@60 316 (.mult (.getPhysicsRotation control)
rlm@61 317 (Vector3f. -1 0 0))))
rlm@60 318 (if @roll-right?
rlm@60 319 (.applyTorque control
rlm@60 320 (.mult (.getPhysicsRotation control)
rlm@61 321 (Vector3f. 1 0 0))))
rlm@60 322
rlm@60 323 (if (= 0 (rem (swap! time inc) 2000))
rlm@61 324 (do
rlm@61 325
rlm@61 326 (apply
rlm@61 327 (comp
rlm@61 328 println-repl
rlm@61 329 #(format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n" %1 %2 %3))
rlm@61 330 (relative-positions joint))))))))
rlm@56 331
rlm@0 332 #+end_src
rlm@0 333
rlm@60 334
rlm@63 335 * COMMENT code-limbo
rlm@61 336 #+begin_src clojure
rlm@61 337 ;;(.loadModel
rlm@61 338 ;; (doto (asset-manager)
rlm@61 339 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 340 ;; "Models/person/person.blend")
rlm@61 341
rlm@61 342 (defn view-model [^String model]
rlm@61 343 (view
rlm@61 344 (.loadModel
rlm@61 345 (doto (asset-manager)
rlm@61 346 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 347 model)))
rlm@61 348
rlm@61 349 (defn load-blender-scene [^String model]
rlm@61 350 (.loadModel
rlm@61 351 (doto (asset-manager)
rlm@61 352 (.registerLoader BlenderLoader (into-array String ["blend"])))
rlm@61 353 model))
rlm@61 354
rlm@61 355 (defn worm
rlm@61 356 []
rlm@61 357 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
rlm@61 358
rlm@61 359 (defn oto
rlm@61 360 []
rlm@61 361 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
rlm@61 362
rlm@61 363 (defn sinbad
rlm@61 364 []
rlm@61 365 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
rlm@61 366
rlm@61 367 (defn worm-blender
rlm@61 368 []
rlm@61 369 (first (seq (.getChildren (load-blender-model
rlm@61 370 "Models/anim2/simple-worm.blend")))))
rlm@61 371
rlm@61 372 (defn body
rlm@61 373 "given a node with a SkeletonControl, will produce a body sutiable
rlm@61 374 for AI control with movement and proprioception."
rlm@61 375 [node]
rlm@61 376 (let [skeleton-control (.getControl node SkeletonControl)
rlm@61 377 krc (KinematicRagdollControl.)]
rlm@61 378 (comment
rlm@61 379 (dorun
rlm@61 380 (map #(.addBoneName krc %)
rlm@61 381 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
rlm@61 382 ;;"mid2" "mid3" "tail" "head"]
rlm@61 383 )))
rlm@61 384 (.addControl node krc)
rlm@61 385 (.setRagdollMode krc)
rlm@61 386 )
rlm@61 387 node
rlm@61 388 )
rlm@61 389 (defn show-skeleton [node]
rlm@61 390 (let [sd
rlm@61 391
rlm@61 392 (doto
rlm@61 393 (SkeletonDebugger. "aurellem-skel-debug"
rlm@61 394 (skel node))
rlm@61 395 (.setMaterial (green-x-ray)))]
rlm@61 396 (.attachChild node sd)
rlm@61 397 node))
rlm@61 398
rlm@61 399
rlm@61 400
rlm@61 401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 402
rlm@61 403 ;; this could be a good way to give objects special properties like
rlm@61 404 ;; being eyes and the like
rlm@61 405
rlm@61 406 (.getUserData
rlm@61 407 (.getChild
rlm@61 408 (load-blender-model "Models/property/test.blend") 0)
rlm@61 409 "properties")
rlm@61 410
rlm@61 411 ;; the properties are saved along with the blender file.
rlm@61 412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 413
rlm@61 414
rlm@61 415
rlm@61 416
rlm@61 417 (defn init-debug-skel-node
rlm@61 418 [f debug-node skeleton]
rlm@61 419 (let [bones
rlm@61 420 (map #(.getBone skeleton %)
rlm@61 421 (range (.getBoneCount skeleton)))]
rlm@61 422 (dorun (map #(.setUserControl % true) bones))
rlm@61 423 (dorun (map (fn [b]
rlm@61 424 (println (.getName b)
rlm@61 425 " -- " (f b)))
rlm@61 426 bones))
rlm@61 427 (dorun
rlm@61 428 (map #(.attachChild
rlm@61 429 debug-node
rlm@61 430 (doto
rlm@61 431 (sphere 0.1
rlm@61 432 :position (f %)
rlm@61 433 :physical? false)
rlm@61 434 (.setMaterial (green-x-ray))))
rlm@61 435 bones)))
rlm@61 436 debug-node)
rlm@61 437
rlm@61 438 (import jme3test.bullet.PhysicsTestHelper)
rlm@61 439
rlm@61 440
rlm@61 441 (defn test-zzz [the-worm world value]
rlm@61 442 (if (not value)
rlm@61 443 (let [skeleton (skel the-worm)]
rlm@61 444 (println-repl "enabling bones")
rlm@61 445 (dorun
rlm@61 446 (map
rlm@61 447 #(.setUserControl (.getBone skeleton %) true)
rlm@61 448 (range (.getBoneCount skeleton))))
rlm@61 449
rlm@61 450
rlm@61 451 (let [b (.getBone skeleton 2)]
rlm@61 452 (println-repl "moving " (.getName b))
rlm@61 453 (println-repl (.getLocalPosition b))
rlm@61 454 (.setUserTransforms b
rlm@61 455 Vector3f/UNIT_X
rlm@61 456 Quaternion/IDENTITY
rlm@61 457 ;;(doto (Quaternion.)
rlm@61 458 ;; (.fromAngles (/ Math/PI 2)
rlm@61 459 ;; 0
rlm@61 460 ;; 0
rlm@61 461
rlm@61 462 (Vector3f. 1 1 1))
rlm@61 463 )
rlm@61 464
rlm@61 465 (println-repl "hi! <3"))))
rlm@61 466
rlm@61 467
rlm@61 468 (defn test-ragdoll []
rlm@61 469
rlm@61 470 (let [the-worm
rlm@61 471
rlm@61 472 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
rlm@61 473 (doto (show-skeleton (worm-blender))
rlm@61 474 (.setLocalTranslation (Vector3f. 0 10 0))
rlm@61 475 ;;(worm)
rlm@61 476 ;;(oto)
rlm@61 477 ;;(sinbad)
rlm@61 478 )
rlm@61 479 ]
rlm@61 480
rlm@61 481
rlm@61 482 (.start
rlm@61 483 (world
rlm@61 484 (doto (Node.)
rlm@61 485 (.attachChild the-worm))
rlm@61 486 {"key-return" (fire-cannon-ball)
rlm@61 487 "key-space" (partial test-zzz the-worm)
rlm@61 488 }
rlm@61 489 (fn [world]
rlm@61 490 (light-up-everything world)
rlm@61 491 (PhysicsTestHelper/createPhysicsTestWorld
rlm@61 492 (.getRootNode world)
rlm@61 493 (asset-manager)
rlm@61 494 (.getPhysicsSpace
rlm@61 495 (.getState (.getStateManager world) BulletAppState)))
rlm@61 496 (set-gravity world Vector3f/ZERO)
rlm@61 497 ;;(.setTimer world (NanoTimer.))
rlm@61 498 ;;(org.lwjgl.input.Mouse/setGrabbed false)
rlm@61 499 )
rlm@61 500 no-op
rlm@61 501 )
rlm@61 502
rlm@61 503
rlm@61 504 )))
rlm@61 505
rlm@61 506
rlm@61 507 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 508 ;;; here is the ragdoll stuff
rlm@61 509
rlm@61 510 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
rlm@61 511 (def mesh worm-mesh)
rlm@61 512
rlm@61 513 (.getFloatBuffer mesh VertexBuffer$Type/Position)
rlm@61 514 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 515 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 516
rlm@61 517
rlm@61 518 (defn position [index]
rlm@61 519 (.get
rlm@61 520 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
rlm@61 521 index))
rlm@61 522
rlm@61 523 (defn bones [index]
rlm@61 524 (.get
rlm@61 525 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 526 index))
rlm@61 527
rlm@61 528 (defn bone-weights [index]
rlm@61 529 (.get
rlm@61 530 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 531 index))
rlm@61 532
rlm@61 533
rlm@61 534
rlm@61 535 (defn vertex-bones [vertex]
rlm@61 536 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 537
rlm@61 538 (defn vertex-weights [vertex]
rlm@61 539 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 540
rlm@61 541 (defn vertex-position [index]
rlm@61 542 (let [offset (* index 3)]
rlm@61 543 (Vector3f. (position offset)
rlm@61 544 (position (inc offset))
rlm@61 545 (position (inc(inc offset))))))
rlm@61 546
rlm@61 547 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
rlm@61 548
rlm@61 549 (defn bone-control-color [index]
rlm@61 550 (get {[1 0 0 0] ColorRGBA/Red
rlm@61 551 [1 2 0 0] ColorRGBA/Magenta
rlm@61 552 [2 0 0 0] ColorRGBA/Blue}
rlm@61 553 (vertex-bones index)
rlm@61 554 ColorRGBA/White))
rlm@61 555
rlm@61 556 (defn influence-color [index bone-num]
rlm@61 557 (get
rlm@61 558 {(float 0) ColorRGBA/Blue
rlm@61 559 (float 0.5) ColorRGBA/Green
rlm@61 560 (float 1) ColorRGBA/Red}
rlm@61 561 ;; find the weight of the desired bone
rlm@61 562 ((zipmap (vertex-bones index)(vertex-weights index))
rlm@61 563 bone-num)
rlm@61 564 ColorRGBA/Blue))
rlm@61 565
rlm@61 566 (def worm-vertices (set (map vertex-info (range 60))))
rlm@61 567
rlm@61 568
rlm@61 569 (defn test-info []
rlm@61 570 (let [points (Node.)]
rlm@61 571 (dorun
rlm@61 572 (map #(.attachChild points %)
rlm@61 573 (map #(sphere 0.01
rlm@61 574 :position (vertex-position %)
rlm@61 575 :color (influence-color % 1)
rlm@61 576 :physical? false)
rlm@61 577 (range 60))))
rlm@61 578 (view points)))
rlm@61 579
rlm@61 580
rlm@61 581 (defrecord JointControl [joint physics-space]
rlm@61 582 PhysicsControl
rlm@61 583 (setPhysicsSpace [this space]
rlm@61 584 (dosync
rlm@61 585 (ref-set (:physics-space this) space))
rlm@61 586 (.addJoint space (:joint this)))
rlm@61 587 (update [this tpf])
rlm@61 588 (setSpatial [this spatial])
rlm@61 589 (render [this rm vp])
rlm@61 590 (getPhysicsSpace [this] (deref (:physics-space this)))
rlm@61 591 (isEnabled [this] true)
rlm@61 592 (setEnabled [this state]))
rlm@61 593
rlm@61 594 (defn add-joint
rlm@61 595 "Add a joint to a particular object. When the object is added to the
rlm@61 596 PhysicsSpace of a simulation, the joint will also be added"
rlm@61 597 [object joint]
rlm@61 598 (let [control (JointControl. joint (ref nil))]
rlm@61 599 (.addControl object control))
rlm@61 600 object)
rlm@61 601
rlm@61 602
rlm@61 603 (defn hinge-world
rlm@61 604 []
rlm@61 605 (let [sphere1 (sphere)
rlm@61 606 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
rlm@61 607 joint (Point2PointJoint.
rlm@61 608 (.getControl sphere1 RigidBodyControl)
rlm@61 609 (.getControl sphere2 RigidBodyControl)
rlm@61 610 Vector3f/ZERO (Vector3f. 3 3 3))]
rlm@61 611 (add-joint sphere1 joint)
rlm@61 612 (doto (Node. "hinge-world")
rlm@61 613 (.attachChild sphere1)
rlm@61 614 (.attachChild sphere2))))
rlm@61 615
rlm@61 616
rlm@61 617 (defn test-joint []
rlm@61 618 (view (hinge-world)))
rlm@61 619
rlm@61 620 ;; (defn copier-gen []
rlm@61 621 ;; (let [count (atom 0)]
rlm@61 622 ;; (fn [in]
rlm@61 623 ;; (swap! count inc)
rlm@61 624 ;; (clojure.contrib.duck-streams/copy
rlm@61 625 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
rlm@61 626 ;; ;;/home/r/tmp/mao-test/clojure-images
rlm@61 627 ;; (format "%08d.png" @count)))))))
rlm@61 628 ;; (defn decrease-framerate []
rlm@61 629 ;; (map
rlm@61 630 ;; (copier-gen)
rlm@61 631 ;; (sort
rlm@61 632 ;; (map first
rlm@61 633 ;; (partition
rlm@61 634 ;; 4
rlm@61 635 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
rlm@61 636 ;; (file-seq
rlm@61 637 ;; (file-str
rlm@61 638 ;; "/home/r/media/anime/mao-temp/images"))))))))
rlm@61 639
rlm@61 640
rlm@61 641
rlm@61 642 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 643
rlm@61 644 (defn proprioception
rlm@61 645 "Create a proprioception map that reports the rotations of the
rlm@61 646 various limbs of the creature's body"
rlm@61 647 [creature]
rlm@61 648 [#^Node creature]
rlm@61 649 (let [
rlm@61 650 nodes (node-seq creature)
rlm@61 651 joints
rlm@61 652 (map
rlm@61 653 :joint
rlm@61 654 (filter
rlm@61 655 #(isa? (class %) JointControl)
rlm@61 656 (reduce
rlm@61 657 concat
rlm@61 658 (map (fn [node]
rlm@61 659 (map (fn [num] (.getControl node num))
rlm@61 660 (range (.getNumControls node))))
rlm@61 661 nodes))))]
rlm@61 662 (fn []
rlm@61 663 (reduce concat (map relative-positions (list (first joints)))))))
rlm@61 664
rlm@61 665
rlm@63 666 (defn skel [node]
rlm@63 667 (doto
rlm@63 668 (.getSkeleton
rlm@63 669 (.getControl node SkeletonControl))
rlm@63 670 ;; this is necessary to force the skeleton to have accurate world
rlm@63 671 ;; transforms before it is rendered to the screen.
rlm@63 672 (.resetAndUpdate)))
rlm@63 673
rlm@63 674 (defn green-x-ray []
rlm@63 675 (doto (Material. (asset-manager)
rlm@63 676 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@63 677 (.setColor "Color" ColorRGBA/Green)
rlm@63 678 (-> (.getAdditionalRenderState)
rlm@63 679 (.setDepthTest false))))
rlm@63 680
rlm@63 681 (defn test-worm []
rlm@63 682 (.start
rlm@63 683 (world
rlm@63 684 (doto (Node.)
rlm@63 685 ;;(.attachChild (point-worm))
rlm@63 686 (.attachChild (load-blender-model
rlm@63 687 "Models/anim2/joint-worm.blend"))
rlm@63 688
rlm@63 689 (.attachChild (box 10 1 10
rlm@63 690 :position (Vector3f. 0 -2 0) :mass 0
rlm@63 691 :color (ColorRGBA/Gray))))
rlm@63 692 {
rlm@63 693 "key-space" (fire-cannon-ball)
rlm@63 694 }
rlm@63 695 (fn [world]
rlm@63 696 (enable-debug world)
rlm@63 697 (light-up-everything world)
rlm@63 698 ;;(.setTimer world (NanoTimer.))
rlm@63 699 )
rlm@63 700 no-op)))
rlm@63 701
rlm@63 702
rlm@63 703
rlm@63 704 ;; defunct movement stuff
rlm@63 705 (defn torque-controls [control]
rlm@63 706 (let [torques
rlm@63 707 (concat
rlm@63 708 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
rlm@63 709 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
rlm@63 710 [Vector3f/UNIT_X])]
rlm@63 711 (map (fn [torque-axis]
rlm@63 712 (fn [torque]
rlm@63 713 (.applyTorque
rlm@63 714 control
rlm@63 715 (.mult (.mult (.getPhysicsRotation control)
rlm@63 716 torque-axis)
rlm@63 717 (float
rlm@63 718 (* (.getMass control) torque))))))
rlm@63 719 torques)))
rlm@63 720
rlm@63 721 (defn motor-map
rlm@63 722 "Take a creature and generate a function that will enable fine
rlm@63 723 grained control over all the creature's limbs."
rlm@63 724 [#^Node creature]
rlm@63 725 (let [controls (keep #(.getControl % RigidBodyControl)
rlm@63 726 (node-seq creature))
rlm@63 727 limb-controls (reduce concat (map torque-controls controls))
rlm@63 728 body-control (partial map #(%1 %2) limb-controls)]
rlm@63 729 body-control))
rlm@63 730
rlm@63 731 (defn test-motor-map
rlm@63 732 "see how torque works."
rlm@63 733 []
rlm@63 734 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
rlm@63 735 :mass 1 :color ColorRGBA/Green)
rlm@63 736 motor-map (motor-map finger)]
rlm@63 737 (world
rlm@63 738 (nodify [finger
rlm@63 739 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@63 740 :color ColorRGBA/Gray)])
rlm@63 741 standard-debug-controls
rlm@63 742 (fn [world]
rlm@63 743 (set-gravity world Vector3f/ZERO)
rlm@63 744 (light-up-everything world)
rlm@63 745 (.setTimer world (NanoTimer.)))
rlm@63 746 (fn [_ _]
rlm@63 747 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
rlm@63 748
rlm@63 749
rlm@61 750 #+end_src
rlm@0 751
rlm@0 752
rlm@0 753
rlm@0 754
rlm@0 755
rlm@0 756
rlm@0 757
rlm@0 758 * COMMENT generate Source.
rlm@44 759 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@0 760 <<body-main>>
rlm@0 761 #+end_src
rlm@0 762
rlm@60 763