annotate org/body.org @ 66:1381a6ebd08b

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