annotate org/body.org @ 130:b26017d1fe9a

added workaround for problem with point2point joints in native bullet; added basic muscle description image.
author Robert McIntyre <rlm@mit.edu>
date Mon, 30 Jan 2012 05:47:51 -0700
parents fb810a2c50c2
children e98850b83c2c
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@130 85 ;;(println-repl (.getName object-a) (.getName object-b))
rlm@60 86 [pitch yaw roll]))
rlm@60 87
rlm@63 88 (defn proprioception
rlm@63 89 "Create a function that provides proprioceptive information about an
rlm@63 90 entire body."
rlm@63 91 [body]
rlm@63 92 ;; extract the body's joints
rlm@63 93 (let [joints
rlm@63 94 (distinct
rlm@63 95 (reduce
rlm@63 96 concat
rlm@63 97 (map #(.getJoints %)
rlm@63 98 (keep
rlm@63 99 #(.getControl % RigidBodyControl)
rlm@63 100 (node-seq body)))))]
rlm@63 101 (fn []
rlm@63 102 (map joint-proprioception joints))))
rlm@60 103
rlm@64 104 #+end_src
rlm@63 105
rlm@65 106 * Motor Control
rlm@66 107 #+name: 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@69 133 #+name: test-body
rlm@64 134 #+begin_src clojure
rlm@69 135 (ns cortex.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@72 179 "Create a worm-like 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@69 202 "Testing motor-control:
rlm@69 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@130 229
rlm@130 230 (require 'cortex.silly)
rlm@130 231 (defn join-at-point [obj-a obj-b world-pivot]
rlm@130 232 (cortex.silly/joint-dispatch
rlm@130 233 {:type :point}
rlm@130 234 (.getControl obj-a RigidBodyControl)
rlm@130 235 (.getControl obj-b RigidBodyControl)
rlm@130 236 (cortex.silly/world-to-local obj-a world-pivot)
rlm@130 237 (cortex.silly/world-to-local obj-b world-pivot)
rlm@130 238 nil
rlm@130 239 ))
rlm@130 240
rlm@130 241
rlm@130 242
rlm@130 243 (defn blab-* []
rlm@130 244 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
rlm@130 245 :mass 0 :color ColorRGBA/Green)
rlm@130 246 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
rlm@130 247 :mass 1 :color ColorRGBA/Red)
rlm@130 248 connection-point (Vector3f. 1.2 0 0)
rlm@130 249 root (nodify [hand finger])]
rlm@130 250
rlm@130 251 (join-at-point hand finger (Vector3f. 1.2 0 0))
rlm@130 252
rlm@130 253 (.setCollisionGroup
rlm@130 254 (.getControl hand RigidBodyControl)
rlm@130 255 PhysicsCollisionObject/COLLISION_GROUP_NONE)
rlm@130 256 (world
rlm@130 257 root
rlm@130 258 standard-debug-controls
rlm@130 259 (fn [world]
rlm@130 260 (enable-debug world)
rlm@130 261 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
rlm@130 262 (set-gravity world Vector3f/ZERO)
rlm@130 263 )
rlm@130 264 no-op)))
rlm@130 265
rlm@130 266 (defn proprioception-debug-window
rlm@130 267 []
rlm@130 268 (let [vi (view-image)]
rlm@130 269 (fn [prop-data]
rlm@130 270 (dorun
rlm@130 271 (map
rlm@130 272 (comp
rlm@130 273 println-repl
rlm@130 274 (fn [[p y r]]
rlm@130 275 (format
rlm@130 276 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
rlm@130 277 p y r)))
rlm@130 278 prop-data)))))
rlm@130 279
rlm@130 280
rlm@130 281
rlm@130 282
rlm@130 283
rlm@64 284 (defn test-proprioception
rlm@69 285 "Testing proprioception:
rlm@69 286 You should see two foating bars, and a printout of pitch, yaw, and
rlm@64 287 roll. Pressing key-r/key-t should move the blue bar up and down and
rlm@64 288 change only the value of pitch. key-f/key-g moves it side to side
rlm@64 289 and changes yaw. key-v/key-b will spin the blue segment clockwise
rlm@64 290 and counterclockwise, and only affect roll."
rlm@60 291 []
rlm@60 292 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
rlm@60 293 :mass 0 :color ColorRGBA/Green)
rlm@60 294 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
rlm@130 295 :mass 1 :color ColorRGBA/Red)
rlm@60 296 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
rlm@60 297 :mass 0 :color ColorRGBA/Gray)
rlm@60 298
rlm@60 299 move-up? (atom false)
rlm@60 300 move-down? (atom false)
rlm@60 301 move-left? (atom false)
rlm@60 302 move-right? (atom false)
rlm@60 303 roll-left? (atom false)
rlm@60 304 roll-right? (atom false)
rlm@60 305 control (.getControl finger RigidBodyControl)
rlm@130 306 time (atom 0)
rlm@130 307 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
rlm@130 308 creature (nodify [hand finger])
rlm@130 309 prop (proprioception creature)
rlm@130 310
rlm@130 311 prop-view (proprioception-debug-window)
rlm@130 312
rlm@130 313
rlm@130 314 ]
rlm@130 315
rlm@130 316
rlm@130 317
rlm@130 318 (.setCollisionGroup
rlm@130 319 (.getControl hand RigidBodyControl)
rlm@130 320 PhysicsCollisionObject/COLLISION_GROUP_NONE)
rlm@130 321
rlm@130 322
rlm@60 323 (world
rlm@60 324 (nodify [hand finger floor])
rlm@60 325 (merge standard-debug-controls
rlm@60 326 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@60 327 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@60 328 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@60 329 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@60 330 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@60 331 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@60 332 (fn [world]
rlm@130 333 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
rlm@61 334 (set-gravity world (Vector3f. 0 0 0))
rlm@69 335 (light-up-everything world))
rlm@60 336 (fn [_ _]
rlm@60 337 (if @move-up?
rlm@60 338 (.applyTorque control
rlm@60 339 (.mult (.getPhysicsRotation control)
rlm@61 340 (Vector3f. 0 0 10))))
rlm@60 341 (if @move-down?
rlm@60 342 (.applyTorque control
rlm@60 343 (.mult (.getPhysicsRotation control)
rlm@61 344 (Vector3f. 0 0 -10))))
rlm@60 345 (if @move-left?
rlm@60 346 (.applyTorque control
rlm@60 347 (.mult (.getPhysicsRotation control)
rlm@61 348 (Vector3f. 0 10 0))))
rlm@60 349 (if @move-right?
rlm@60 350 (.applyTorque control
rlm@60 351 (.mult (.getPhysicsRotation control)
rlm@61 352 (Vector3f. 0 -10 0))))
rlm@60 353 (if @roll-left?
rlm@60 354 (.applyTorque control
rlm@60 355 (.mult (.getPhysicsRotation control)
rlm@61 356 (Vector3f. -1 0 0))))
rlm@60 357 (if @roll-right?
rlm@60 358 (.applyTorque control
rlm@60 359 (.mult (.getPhysicsRotation control)
rlm@61 360 (Vector3f. 1 0 0))))
rlm@60 361
rlm@130 362 (if (= 0 (rem (swap! time inc) 20))
rlm@130 363 (prop-view
rlm@130 364 (joint-proprioception joint)))))))
rlm@130 365
rlm@130 366
rlm@64 367 #+end_src
rlm@56 368
rlm@130 369 #+results: test-body
rlm@130 370 : #'cortex.test.body/test-proprioception
rlm@130 371
rlm@60 372
rlm@63 373 * COMMENT code-limbo
rlm@61 374 #+begin_src clojure
rlm@61 375 ;;(.loadModel
rlm@61 376 ;; (doto (asset-manager)
rlm@61 377 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 378 ;; "Models/person/person.blend")
rlm@61 379
rlm@64 380
rlm@64 381 (defn load-blender-model
rlm@64 382 "Load a .blend file using an asset folder relative path."
rlm@64 383 [^String model]
rlm@64 384 (.loadModel
rlm@64 385 (doto (asset-manager)
rlm@64 386 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@64 387 model))
rlm@64 388
rlm@64 389
rlm@61 390 (defn view-model [^String model]
rlm@61 391 (view
rlm@61 392 (.loadModel
rlm@61 393 (doto (asset-manager)
rlm@61 394 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 395 model)))
rlm@61 396
rlm@61 397 (defn load-blender-scene [^String model]
rlm@61 398 (.loadModel
rlm@61 399 (doto (asset-manager)
rlm@61 400 (.registerLoader BlenderLoader (into-array String ["blend"])))
rlm@61 401 model))
rlm@61 402
rlm@61 403 (defn worm
rlm@61 404 []
rlm@61 405 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
rlm@61 406
rlm@61 407 (defn oto
rlm@61 408 []
rlm@61 409 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
rlm@61 410
rlm@61 411 (defn sinbad
rlm@61 412 []
rlm@61 413 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
rlm@61 414
rlm@61 415 (defn worm-blender
rlm@61 416 []
rlm@61 417 (first (seq (.getChildren (load-blender-model
rlm@61 418 "Models/anim2/simple-worm.blend")))))
rlm@61 419
rlm@61 420 (defn body
rlm@61 421 "given a node with a SkeletonControl, will produce a body sutiable
rlm@61 422 for AI control with movement and proprioception."
rlm@61 423 [node]
rlm@61 424 (let [skeleton-control (.getControl node SkeletonControl)
rlm@61 425 krc (KinematicRagdollControl.)]
rlm@61 426 (comment
rlm@61 427 (dorun
rlm@61 428 (map #(.addBoneName krc %)
rlm@61 429 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
rlm@61 430 ;;"mid2" "mid3" "tail" "head"]
rlm@61 431 )))
rlm@61 432 (.addControl node krc)
rlm@61 433 (.setRagdollMode krc)
rlm@61 434 )
rlm@61 435 node
rlm@61 436 )
rlm@61 437 (defn show-skeleton [node]
rlm@61 438 (let [sd
rlm@61 439
rlm@61 440 (doto
rlm@61 441 (SkeletonDebugger. "aurellem-skel-debug"
rlm@61 442 (skel node))
rlm@61 443 (.setMaterial (green-x-ray)))]
rlm@61 444 (.attachChild node sd)
rlm@61 445 node))
rlm@61 446
rlm@61 447
rlm@61 448
rlm@61 449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 450
rlm@61 451 ;; this could be a good way to give objects special properties like
rlm@61 452 ;; being eyes and the like
rlm@61 453
rlm@61 454 (.getUserData
rlm@61 455 (.getChild
rlm@61 456 (load-blender-model "Models/property/test.blend") 0)
rlm@61 457 "properties")
rlm@61 458
rlm@61 459 ;; the properties are saved along with the blender file.
rlm@61 460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 461
rlm@61 462
rlm@61 463
rlm@61 464
rlm@61 465 (defn init-debug-skel-node
rlm@61 466 [f debug-node skeleton]
rlm@61 467 (let [bones
rlm@61 468 (map #(.getBone skeleton %)
rlm@61 469 (range (.getBoneCount skeleton)))]
rlm@61 470 (dorun (map #(.setUserControl % true) bones))
rlm@61 471 (dorun (map (fn [b]
rlm@61 472 (println (.getName b)
rlm@61 473 " -- " (f b)))
rlm@61 474 bones))
rlm@61 475 (dorun
rlm@61 476 (map #(.attachChild
rlm@61 477 debug-node
rlm@61 478 (doto
rlm@61 479 (sphere 0.1
rlm@61 480 :position (f %)
rlm@61 481 :physical? false)
rlm@61 482 (.setMaterial (green-x-ray))))
rlm@61 483 bones)))
rlm@61 484 debug-node)
rlm@61 485
rlm@61 486 (import jme3test.bullet.PhysicsTestHelper)
rlm@61 487
rlm@61 488
rlm@61 489 (defn test-zzz [the-worm world value]
rlm@61 490 (if (not value)
rlm@61 491 (let [skeleton (skel the-worm)]
rlm@61 492 (println-repl "enabling bones")
rlm@61 493 (dorun
rlm@61 494 (map
rlm@61 495 #(.setUserControl (.getBone skeleton %) true)
rlm@61 496 (range (.getBoneCount skeleton))))
rlm@61 497
rlm@61 498
rlm@61 499 (let [b (.getBone skeleton 2)]
rlm@61 500 (println-repl "moving " (.getName b))
rlm@61 501 (println-repl (.getLocalPosition b))
rlm@61 502 (.setUserTransforms b
rlm@61 503 Vector3f/UNIT_X
rlm@61 504 Quaternion/IDENTITY
rlm@61 505 ;;(doto (Quaternion.)
rlm@61 506 ;; (.fromAngles (/ Math/PI 2)
rlm@61 507 ;; 0
rlm@61 508 ;; 0
rlm@61 509
rlm@61 510 (Vector3f. 1 1 1))
rlm@61 511 )
rlm@61 512
rlm@61 513 (println-repl "hi! <3"))))
rlm@61 514
rlm@61 515
rlm@61 516 (defn test-ragdoll []
rlm@61 517
rlm@61 518 (let [the-worm
rlm@61 519
rlm@61 520 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
rlm@61 521 (doto (show-skeleton (worm-blender))
rlm@61 522 (.setLocalTranslation (Vector3f. 0 10 0))
rlm@61 523 ;;(worm)
rlm@61 524 ;;(oto)
rlm@61 525 ;;(sinbad)
rlm@61 526 )
rlm@61 527 ]
rlm@61 528
rlm@61 529
rlm@61 530 (.start
rlm@61 531 (world
rlm@61 532 (doto (Node.)
rlm@61 533 (.attachChild the-worm))
rlm@61 534 {"key-return" (fire-cannon-ball)
rlm@61 535 "key-space" (partial test-zzz the-worm)
rlm@61 536 }
rlm@61 537 (fn [world]
rlm@61 538 (light-up-everything world)
rlm@61 539 (PhysicsTestHelper/createPhysicsTestWorld
rlm@61 540 (.getRootNode world)
rlm@61 541 (asset-manager)
rlm@61 542 (.getPhysicsSpace
rlm@61 543 (.getState (.getStateManager world) BulletAppState)))
rlm@61 544 (set-gravity world Vector3f/ZERO)
rlm@61 545 ;;(.setTimer world (NanoTimer.))
rlm@61 546 ;;(org.lwjgl.input.Mouse/setGrabbed false)
rlm@61 547 )
rlm@61 548 no-op
rlm@61 549 )
rlm@61 550
rlm@61 551
rlm@61 552 )))
rlm@61 553
rlm@61 554
rlm@61 555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 556 ;;; here is the ragdoll stuff
rlm@61 557
rlm@61 558 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
rlm@61 559 (def mesh worm-mesh)
rlm@61 560
rlm@61 561 (.getFloatBuffer mesh VertexBuffer$Type/Position)
rlm@61 562 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 563 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 564
rlm@61 565
rlm@61 566 (defn position [index]
rlm@61 567 (.get
rlm@61 568 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
rlm@61 569 index))
rlm@61 570
rlm@61 571 (defn bones [index]
rlm@61 572 (.get
rlm@61 573 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 574 index))
rlm@61 575
rlm@61 576 (defn bone-weights [index]
rlm@61 577 (.get
rlm@61 578 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 579 index))
rlm@61 580
rlm@61 581
rlm@61 582
rlm@61 583 (defn vertex-bones [vertex]
rlm@61 584 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 585
rlm@61 586 (defn vertex-weights [vertex]
rlm@61 587 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 588
rlm@61 589 (defn vertex-position [index]
rlm@61 590 (let [offset (* index 3)]
rlm@61 591 (Vector3f. (position offset)
rlm@61 592 (position (inc offset))
rlm@61 593 (position (inc(inc offset))))))
rlm@61 594
rlm@61 595 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
rlm@61 596
rlm@61 597 (defn bone-control-color [index]
rlm@61 598 (get {[1 0 0 0] ColorRGBA/Red
rlm@61 599 [1 2 0 0] ColorRGBA/Magenta
rlm@61 600 [2 0 0 0] ColorRGBA/Blue}
rlm@61 601 (vertex-bones index)
rlm@61 602 ColorRGBA/White))
rlm@61 603
rlm@61 604 (defn influence-color [index bone-num]
rlm@61 605 (get
rlm@61 606 {(float 0) ColorRGBA/Blue
rlm@61 607 (float 0.5) ColorRGBA/Green
rlm@61 608 (float 1) ColorRGBA/Red}
rlm@61 609 ;; find the weight of the desired bone
rlm@61 610 ((zipmap (vertex-bones index)(vertex-weights index))
rlm@61 611 bone-num)
rlm@61 612 ColorRGBA/Blue))
rlm@61 613
rlm@61 614 (def worm-vertices (set (map vertex-info (range 60))))
rlm@61 615
rlm@61 616
rlm@61 617 (defn test-info []
rlm@61 618 (let [points (Node.)]
rlm@61 619 (dorun
rlm@61 620 (map #(.attachChild points %)
rlm@61 621 (map #(sphere 0.01
rlm@61 622 :position (vertex-position %)
rlm@61 623 :color (influence-color % 1)
rlm@61 624 :physical? false)
rlm@61 625 (range 60))))
rlm@61 626 (view points)))
rlm@61 627
rlm@61 628
rlm@61 629 (defrecord JointControl [joint physics-space]
rlm@61 630 PhysicsControl
rlm@61 631 (setPhysicsSpace [this space]
rlm@61 632 (dosync
rlm@61 633 (ref-set (:physics-space this) space))
rlm@61 634 (.addJoint space (:joint this)))
rlm@61 635 (update [this tpf])
rlm@61 636 (setSpatial [this spatial])
rlm@61 637 (render [this rm vp])
rlm@61 638 (getPhysicsSpace [this] (deref (:physics-space this)))
rlm@61 639 (isEnabled [this] true)
rlm@61 640 (setEnabled [this state]))
rlm@61 641
rlm@61 642 (defn add-joint
rlm@61 643 "Add a joint to a particular object. When the object is added to the
rlm@61 644 PhysicsSpace of a simulation, the joint will also be added"
rlm@61 645 [object joint]
rlm@61 646 (let [control (JointControl. joint (ref nil))]
rlm@61 647 (.addControl object control))
rlm@61 648 object)
rlm@61 649
rlm@61 650
rlm@61 651 (defn hinge-world
rlm@61 652 []
rlm@61 653 (let [sphere1 (sphere)
rlm@61 654 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
rlm@61 655 joint (Point2PointJoint.
rlm@61 656 (.getControl sphere1 RigidBodyControl)
rlm@61 657 (.getControl sphere2 RigidBodyControl)
rlm@61 658 Vector3f/ZERO (Vector3f. 3 3 3))]
rlm@61 659 (add-joint sphere1 joint)
rlm@61 660 (doto (Node. "hinge-world")
rlm@61 661 (.attachChild sphere1)
rlm@61 662 (.attachChild sphere2))))
rlm@61 663
rlm@61 664
rlm@61 665 (defn test-joint []
rlm@61 666 (view (hinge-world)))
rlm@61 667
rlm@61 668 ;; (defn copier-gen []
rlm@61 669 ;; (let [count (atom 0)]
rlm@61 670 ;; (fn [in]
rlm@61 671 ;; (swap! count inc)
rlm@61 672 ;; (clojure.contrib.duck-streams/copy
rlm@61 673 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
rlm@61 674 ;; ;;/home/r/tmp/mao-test/clojure-images
rlm@61 675 ;; (format "%08d.png" @count)))))))
rlm@61 676 ;; (defn decrease-framerate []
rlm@61 677 ;; (map
rlm@61 678 ;; (copier-gen)
rlm@61 679 ;; (sort
rlm@61 680 ;; (map first
rlm@61 681 ;; (partition
rlm@61 682 ;; 4
rlm@61 683 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
rlm@61 684 ;; (file-seq
rlm@61 685 ;; (file-str
rlm@61 686 ;; "/home/r/media/anime/mao-temp/images"))))))))
rlm@61 687
rlm@61 688
rlm@61 689
rlm@61 690 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 691
rlm@61 692 (defn proprioception
rlm@61 693 "Create a proprioception map that reports the rotations of the
rlm@61 694 various limbs of the creature's body"
rlm@61 695 [creature]
rlm@61 696 [#^Node creature]
rlm@61 697 (let [
rlm@61 698 nodes (node-seq creature)
rlm@61 699 joints
rlm@61 700 (map
rlm@61 701 :joint
rlm@61 702 (filter
rlm@61 703 #(isa? (class %) JointControl)
rlm@61 704 (reduce
rlm@61 705 concat
rlm@61 706 (map (fn [node]
rlm@61 707 (map (fn [num] (.getControl node num))
rlm@61 708 (range (.getNumControls node))))
rlm@61 709 nodes))))]
rlm@61 710 (fn []
rlm@61 711 (reduce concat (map relative-positions (list (first joints)))))))
rlm@61 712
rlm@61 713
rlm@63 714 (defn skel [node]
rlm@63 715 (doto
rlm@63 716 (.getSkeleton
rlm@63 717 (.getControl node SkeletonControl))
rlm@63 718 ;; this is necessary to force the skeleton to have accurate world
rlm@63 719 ;; transforms before it is rendered to the screen.
rlm@63 720 (.resetAndUpdate)))
rlm@63 721
rlm@63 722 (defn green-x-ray []
rlm@63 723 (doto (Material. (asset-manager)
rlm@63 724 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@63 725 (.setColor "Color" ColorRGBA/Green)
rlm@63 726 (-> (.getAdditionalRenderState)
rlm@63 727 (.setDepthTest false))))
rlm@63 728
rlm@63 729 (defn test-worm []
rlm@63 730 (.start
rlm@63 731 (world
rlm@63 732 (doto (Node.)
rlm@63 733 ;;(.attachChild (point-worm))
rlm@63 734 (.attachChild (load-blender-model
rlm@63 735 "Models/anim2/joint-worm.blend"))
rlm@63 736
rlm@63 737 (.attachChild (box 10 1 10
rlm@63 738 :position (Vector3f. 0 -2 0) :mass 0
rlm@63 739 :color (ColorRGBA/Gray))))
rlm@63 740 {
rlm@63 741 "key-space" (fire-cannon-ball)
rlm@63 742 }
rlm@63 743 (fn [world]
rlm@63 744 (enable-debug world)
rlm@63 745 (light-up-everything world)
rlm@63 746 ;;(.setTimer world (NanoTimer.))
rlm@63 747 )
rlm@63 748 no-op)))
rlm@63 749
rlm@63 750
rlm@63 751
rlm@63 752 ;; defunct movement stuff
rlm@63 753 (defn torque-controls [control]
rlm@63 754 (let [torques
rlm@63 755 (concat
rlm@63 756 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
rlm@63 757 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
rlm@63 758 [Vector3f/UNIT_X])]
rlm@63 759 (map (fn [torque-axis]
rlm@63 760 (fn [torque]
rlm@63 761 (.applyTorque
rlm@63 762 control
rlm@63 763 (.mult (.mult (.getPhysicsRotation control)
rlm@63 764 torque-axis)
rlm@63 765 (float
rlm@63 766 (* (.getMass control) torque))))))
rlm@63 767 torques)))
rlm@63 768
rlm@63 769 (defn motor-map
rlm@63 770 "Take a creature and generate a function that will enable fine
rlm@63 771 grained control over all the creature's limbs."
rlm@63 772 [#^Node creature]
rlm@63 773 (let [controls (keep #(.getControl % RigidBodyControl)
rlm@63 774 (node-seq creature))
rlm@63 775 limb-controls (reduce concat (map torque-controls controls))
rlm@63 776 body-control (partial map #(%1 %2) limb-controls)]
rlm@63 777 body-control))
rlm@63 778
rlm@63 779 (defn test-motor-map
rlm@63 780 "see how torque works."
rlm@63 781 []
rlm@63 782 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
rlm@63 783 :mass 1 :color ColorRGBA/Green)
rlm@63 784 motor-map (motor-map finger)]
rlm@63 785 (world
rlm@63 786 (nodify [finger
rlm@63 787 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@63 788 :color ColorRGBA/Gray)])
rlm@63 789 standard-debug-controls
rlm@63 790 (fn [world]
rlm@63 791 (set-gravity world Vector3f/ZERO)
rlm@63 792 (light-up-everything world)
rlm@63 793 (.setTimer world (NanoTimer.)))
rlm@63 794 (fn [_ _]
rlm@63 795 (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 796 #+end_src
rlm@0 797
rlm@0 798
rlm@0 799
rlm@0 800
rlm@0 801
rlm@0 802
rlm@0 803
rlm@73 804 * COMMENT generate Source
rlm@44 805 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@64 806 <<proprioception>>
rlm@64 807 <<motor-control>>
rlm@0 808 #+end_src
rlm@64 809
rlm@69 810 #+begin_src clojure :tangle ../src/cortex/test/body.clj
rlm@64 811 <<test-body>>
rlm@64 812 #+end_src
rlm@64 813
rlm@64 814
rlm@0 815