annotate org/body.org @ 64:ab1fee4280c3

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