annotate org/body.org @ 86:35f485a0c93f

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