annotate org/body.org @ 157:84c67be00abe

refactored proprioception
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:21:39 -0700
parents e1232043656a
children 33278bf028e7
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@135 18 com.jme3.bullet.control.RigidBodyControl
rlm@135 19 com.jme3.collision.CollisionResults
rlm@150 20 com.jme3.bounding.BoundingBox
rlm@150 21 com.jme3.scene.Node))
rlm@133 22
rlm@135 23 (defn jme-to-blender
rlm@135 24 "Convert from JME coordinates to Blender coordinates"
rlm@135 25 [#^Vector3f in]
rlm@135 26 (Vector3f. (.getX in)
rlm@135 27 (- (.getZ in))
rlm@135 28 (.getY in)))
rlm@135 29
rlm@135 30 (defn joint-targets
rlm@135 31 "Return the two closest two objects to the joint object, ordered
rlm@135 32 from bottom to top according to the joint's rotation."
rlm@135 33 [#^Node parts #^Node joint]
rlm@135 34 (loop [radius (float 0.01)]
rlm@135 35 (let [results (CollisionResults.)]
rlm@135 36 (.collideWith
rlm@135 37 parts
rlm@135 38 (BoundingBox. (.getWorldTranslation joint)
rlm@135 39 radius radius radius)
rlm@135 40 results)
rlm@135 41 (let [targets
rlm@135 42 (distinct
rlm@135 43 (map #(.getGeometry %) results))]
rlm@135 44 (if (>= (count targets) 2)
rlm@135 45 (sort-by
rlm@135 46 #(let [v
rlm@135 47 (jme-to-blender
rlm@135 48 (.mult
rlm@135 49 (.inverse (.getWorldRotation joint))
rlm@135 50 (.subtract (.getWorldTranslation %)
rlm@135 51 (.getWorldTranslation joint))))]
rlm@135 52 (println-repl (.getName %) ":" v)
rlm@135 53 (.dot (Vector3f. 1 1 1)
rlm@135 54 v))
rlm@135 55 (take 2 targets))
rlm@135 56 (recur (float (* radius 2))))))))
rlm@135 57
rlm@135 58 (defn creature-joints
rlm@135 59 "Return the children of the creature's \"joints\" node."
rlm@135 60 [#^Node creature]
rlm@135 61 (if-let [joint-node (.getChild creature "joints")]
rlm@135 62 (seq (.getChildren joint-node))
rlm@135 63 (do (println-repl "could not find JOINTS node") [])))
rlm@135 64
rlm@141 65 (defn tap [obj direction force]
rlm@141 66 (let [control (.getControl obj RigidBodyControl)]
rlm@141 67 (.applyTorque
rlm@141 68 control
rlm@141 69 (.mult (.getPhysicsRotation control)
rlm@141 70 (.mult (.normalize direction) (float force))))))
rlm@141 71
rlm@141 72
rlm@141 73 (defn with-movement
rlm@141 74 [object
rlm@141 75 [up down left right roll-up roll-down :as keyboard]
rlm@141 76 forces
rlm@141 77 [root-node
rlm@141 78 keymap
rlm@141 79 intilization
rlm@141 80 world-loop]]
rlm@141 81 (let [add-keypress
rlm@141 82 (fn [state keymap key]
rlm@141 83 (merge keymap
rlm@141 84 {key
rlm@141 85 (fn [_ pressed?]
rlm@141 86 (reset! state pressed?))}))
rlm@141 87 move-up? (atom false)
rlm@141 88 move-down? (atom false)
rlm@141 89 move-left? (atom false)
rlm@141 90 move-right? (atom false)
rlm@141 91 roll-left? (atom false)
rlm@141 92 roll-right? (atom false)
rlm@141 93
rlm@141 94 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)
rlm@141 95 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)
rlm@141 96 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]
rlm@141 97 atoms [move-left? move-right? move-up? move-down?
rlm@141 98 roll-left? roll-right?]
rlm@141 99
rlm@141 100 keymap* (reduce merge
rlm@141 101 (map #(add-keypress %1 keymap %2)
rlm@141 102 atoms
rlm@141 103 keyboard))
rlm@141 104
rlm@141 105 splice-loop (fn []
rlm@141 106 (dorun
rlm@141 107 (map
rlm@141 108 (fn [sym direction force]
rlm@141 109 (if @sym
rlm@141 110 (tap object direction force)))
rlm@141 111 atoms directions forces)))
rlm@141 112
rlm@141 113 world-loop* (fn [world tpf]
rlm@141 114 (world-loop world tpf)
rlm@141 115 (splice-loop))]
rlm@145 116 [root-node
rlm@145 117 keymap*
rlm@145 118 intilization
rlm@145 119 world-loop*]))
rlm@141 120
rlm@145 121 (import java.awt.image.BufferedImage)
rlm@141 122
rlm@145 123 (defn draw-sprite [image sprite x y color ]
rlm@145 124 (dorun
rlm@145 125 (for [[u v] sprite]
rlm@145 126 (.setRGB image (+ u x) (+ v y) color))))
rlm@145 127
rlm@145 128 (defn view-angle
rlm@145 129 "create a debug view of an angle"
rlm@145 130 [color]
rlm@145 131 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
rlm@145 132 previous (atom [25 25])
rlm@145 133 sprite [[0 0] [0 1]
rlm@145 134 [0 -1] [-1 0] [1 0]]]
rlm@145 135 (fn [angle]
rlm@145 136 (let [angle (float angle)]
rlm@145 137 (let [position
rlm@145 138 [(+ 25 (int (* 20 (Math/cos angle))))
rlm@145 139 (+ 25 (int (* -20 (Math/sin angle))))]]
rlm@145 140 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
rlm@145 141 (draw-sprite image sprite (position 0) (position 1) color)
rlm@145 142 (reset! previous position))
rlm@145 143 image))))
rlm@145 144
rlm@145 145 (defn proprioception-debug-window
rlm@145 146 []
rlm@145 147 (let [heading (view-angle 0xFF0000)
rlm@145 148 pitch (view-angle 0x00FF00)
rlm@145 149 roll (view-angle 0xFFFFFF)
rlm@145 150 v-heading (view-image)
rlm@145 151 v-pitch (view-image)
rlm@145 152 v-roll (view-image)
rlm@145 153 ]
rlm@145 154 (fn [prop-data]
rlm@145 155 (dorun
rlm@145 156 (map
rlm@145 157 (fn [[h p r]]
rlm@145 158 (v-heading (heading h))
rlm@145 159 (v-pitch (pitch p))
rlm@145 160 (v-roll (roll r)))
rlm@145 161 prop-data)))))
rlm@145 162
rlm@145 163
rlm@64 164 #+end_src
rlm@63 165
rlm@133 166 #+results: proprioception
rlm@145 167 : #'cortex.body/proprioception-debug-window
rlm@133 168
rlm@65 169 * Motor Control
rlm@66 170 #+name: motor-control
rlm@64 171 #+begin_src clojure
rlm@64 172 (in-ns 'cortex.body)
rlm@63 173
rlm@63 174 ;; surprisingly enough, terristerial creatures only move by using
rlm@63 175 ;; torque applied about their joints. There's not a single straight
rlm@63 176 ;; line of force in the human body at all! (A straight line of force
rlm@63 177 ;; would correspond to some sort of jet or rocket propulseion.)
rlm@63 178
rlm@63 179 (defn vector-motor-control
rlm@63 180 "Create a function that accepts a sequence of Vector3f objects that
rlm@63 181 describe the torque to be applied to each part of the body."
rlm@63 182 [body]
rlm@63 183 (let [nodes (node-seq body)
rlm@63 184 controls (keep #(.getControl % RigidBodyControl) nodes)]
rlm@63 185 (fn [torques]
rlm@63 186 (map #(.applyTorque %1 %2)
rlm@63 187 controls torques))))
rlm@64 188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@64 189 #+end_src
rlm@64 190
rlm@64 191 ## note -- might want to add a lower dimensional, discrete version of
rlm@64 192 ## this if it proves useful from a x-modal clustering perspective.
rlm@63 193
rlm@64 194 * Examples
rlm@63 195
rlm@69 196 #+name: test-body
rlm@64 197 #+begin_src clojure
rlm@69 198 (ns cortex.test.body
rlm@64 199 (:use (cortex world util body))
rlm@135 200 (:require cortex.silly)
rlm@64 201 (:import
rlm@64 202 com.jme3.math.Vector3f
rlm@64 203 com.jme3.math.ColorRGBA
rlm@64 204 com.jme3.bullet.joints.Point2PointJoint
rlm@64 205 com.jme3.bullet.control.RigidBodyControl
rlm@145 206 com.jme3.system.NanoTimer
rlm@145 207 com.jme3.math.Quaternion))
rlm@63 208
rlm@64 209 (defn worm-segments
rlm@64 210 "Create multiple evenly spaced box segments. They're fabulous!"
rlm@64 211 [segment-length num-segments interstitial-space radius]
rlm@64 212 (letfn [(nth-segment
rlm@64 213 [n]
rlm@64 214 (box segment-length radius radius :mass 0.1
rlm@64 215 :position
rlm@64 216 (Vector3f.
rlm@64 217 (* 2 n (+ interstitial-space segment-length)) 0 0)
rlm@64 218 :name (str "worm-segment" n)
rlm@64 219 :color (ColorRGBA/randomColor)))]
rlm@64 220 (map nth-segment (range num-segments))))
rlm@63 221
rlm@64 222 (defn connect-at-midpoint
rlm@64 223 "Connect two physics objects with a Point2Point joint constraint at
rlm@64 224 the point equidistant from both objects' centers."
rlm@64 225 [segmentA segmentB]
rlm@64 226 (let [centerA (.getWorldTranslation segmentA)
rlm@64 227 centerB (.getWorldTranslation segmentB)
rlm@64 228 midpoint (.mult (.add centerA centerB) (float 0.5))
rlm@64 229 pivotA (.subtract midpoint centerA)
rlm@64 230 pivotB (.subtract midpoint centerB)
rlm@64 231
rlm@64 232 ;; A side-effect of creating a joint registers
rlm@64 233 ;; it with both physics objects which in turn
rlm@64 234 ;; will register the joint with the physics system
rlm@64 235 ;; when the simulation is started.
rlm@64 236 joint (Point2PointJoint.
rlm@64 237 (.getControl segmentA RigidBodyControl)
rlm@64 238 (.getControl segmentB RigidBodyControl)
rlm@64 239 pivotA
rlm@64 240 pivotB)]
rlm@64 241 segmentB))
rlm@63 242
rlm@64 243 (defn eve-worm
rlm@72 244 "Create a worm-like body bound by invisible joint constraints."
rlm@64 245 []
rlm@64 246 (let [segments (worm-segments 0.2 5 0.1 0.1)]
rlm@64 247 (dorun (map (partial apply connect-at-midpoint)
rlm@64 248 (partition 2 1 segments)))
rlm@64 249 (nodify "worm" segments)))
rlm@63 250
rlm@64 251 (defn worm-pattern
rlm@64 252 "This is a simple, mindless motor control pattern that drives the
rlm@64 253 second segment of the worm's body at an offset angle with
rlm@64 254 sinusoidally varying strength."
rlm@64 255 [time]
rlm@64 256 (let [angle (* Math/PI (/ 9 20))
rlm@63 257 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
rlm@63 258 [Vector3f/ZERO
rlm@63 259 (.mult
rlm@63 260 direction
rlm@63 261 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
rlm@63 262 Vector3f/ZERO
rlm@63 263 Vector3f/ZERO
rlm@63 264 Vector3f/ZERO]))
rlm@60 265
rlm@64 266 (defn test-motor-control
rlm@69 267 "Testing motor-control:
rlm@69 268 You should see a multi-segmented worm-like object fall onto the
rlm@64 269 table and begin writhing and moving."
rlm@60 270 []
rlm@64 271 (let [worm (eve-worm)
rlm@60 272 time (atom 0)
rlm@63 273 worm-motor-map (vector-motor-control worm)]
rlm@60 274 (world
rlm@60 275 (nodify [worm
rlm@60 276 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 277 :color ColorRGBA/Gray)])
rlm@60 278 standard-debug-controls
rlm@60 279 (fn [world]
rlm@60 280 (enable-debug world)
rlm@60 281 (light-up-everything world)
rlm@63 282 (comment
rlm@63 283 (com.aurellem.capture.Capture/captureVideo
rlm@63 284 world
rlm@63 285 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
rlm@63 286 )
rlm@60 287
rlm@60 288 (fn [_ _]
rlm@60 289 (swap! time inc)
rlm@64 290 (Thread/sleep 20)
rlm@60 291 (dorun (worm-motor-map
rlm@60 292 (worm-pattern @time)))))))
rlm@60 293
rlm@130 294
rlm@135 295
rlm@130 296 (defn join-at-point [obj-a obj-b world-pivot]
rlm@130 297 (cortex.silly/joint-dispatch
rlm@130 298 {:type :point}
rlm@130 299 (.getControl obj-a RigidBodyControl)
rlm@130 300 (.getControl obj-b RigidBodyControl)
rlm@130 301 (cortex.silly/world-to-local obj-a world-pivot)
rlm@130 302 (cortex.silly/world-to-local obj-b world-pivot)
rlm@130 303 nil
rlm@130 304 ))
rlm@130 305
rlm@133 306 (import com.jme3.bullet.collision.PhysicsCollisionObject)
rlm@130 307
rlm@130 308 (defn blab-* []
rlm@130 309 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
rlm@130 310 :mass 0 :color ColorRGBA/Green)
rlm@130 311 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
rlm@130 312 :mass 1 :color ColorRGBA/Red)
rlm@130 313 connection-point (Vector3f. 1.2 0 0)
rlm@130 314 root (nodify [hand finger])]
rlm@130 315
rlm@130 316 (join-at-point hand finger (Vector3f. 1.2 0 0))
rlm@130 317
rlm@130 318 (.setCollisionGroup
rlm@130 319 (.getControl hand RigidBodyControl)
rlm@130 320 PhysicsCollisionObject/COLLISION_GROUP_NONE)
rlm@130 321 (world
rlm@130 322 root
rlm@130 323 standard-debug-controls
rlm@130 324 (fn [world]
rlm@130 325 (enable-debug world)
rlm@130 326 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
rlm@130 327 (set-gravity world Vector3f/ZERO)
rlm@130 328 )
rlm@130 329 no-op)))
rlm@133 330 (comment
rlm@133 331
rlm@133 332 (defn proprioception-debug-window
rlm@133 333 []
rlm@133 334 (let [time (atom 0)]
rlm@133 335 (fn [prop-data]
rlm@133 336 (if (= 0 (rem (swap! time inc) 40))
rlm@133 337 (println-repl prop-data)))))
rlm@133 338 )
rlm@133 339
rlm@131 340 (comment
rlm@131 341 (dorun
rlm@131 342 (map
rlm@131 343 (comp
rlm@131 344 println-repl
rlm@131 345 (fn [[p y r]]
rlm@131 346 (format
rlm@131 347 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
rlm@131 348 p y r)))
rlm@131 349 prop-data)))
rlm@131 350
rlm@130 351
rlm@130 352
rlm@137 353
rlm@64 354 (defn test-proprioception
rlm@69 355 "Testing proprioception:
rlm@69 356 You should see two foating bars, and a printout of pitch, yaw, and
rlm@64 357 roll. Pressing key-r/key-t should move the blue bar up and down and
rlm@64 358 change only the value of pitch. key-f/key-g moves it side to side
rlm@64 359 and changes yaw. key-v/key-b will spin the blue segment clockwise
rlm@64 360 and counterclockwise, and only affect roll."
rlm@60 361 []
rlm@145 362 (let [hand (box 0.2 1 0.2 :position (Vector3f. 0 0 0)
rlm@142 363 :mass 0 :color ColorRGBA/Green :name "hand")
rlm@145 364 finger (box 0.2 1 0.2 :position (Vector3f. 0 2.4 0)
rlm@132 365 :mass 1 :color ColorRGBA/Red :name "finger")
rlm@133 366 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
rlm@145 367 :position (Vector3f. 0 1.2 0)
rlm@145 368 :rotation (doto (Quaternion.)
rlm@145 369 (.fromAngleAxis
rlm@145 370 (/ Math/PI 2)
rlm@145 371 (Vector3f. 0 0 1)))
rlm@133 372 :physical? false)
rlm@145 373 joint (join-at-point hand finger (Vector3f. 0 1.2 0 ))
rlm@135 374 creature (nodify [hand finger joint-node])
rlm@145 375 finger-control (.getControl finger RigidBodyControl)
rlm@145 376 hand-control (.getControl hand RigidBodyControl)]
rlm@145 377
rlm@145 378
rlm@145 379 (let
rlm@135 380 ;; *******************************************
rlm@137 381
rlm@145 382 [floor (box 10 10 10 :position (Vector3f. 0 -15 0)
rlm@135 383 :mass 0 :color ColorRGBA/Gray)
rlm@137 384
rlm@137 385 root (nodify [creature floor])
rlm@133 386 prop (joint-proprioception creature joint-node)
rlm@139 387 prop-view (proprioception-debug-window)
rlm@139 388
rlm@139 389 controls
rlm@139 390 (merge standard-debug-controls
rlm@140 391 {"key-o"
rlm@139 392 (fn [_ _] (.setEnabled finger-control true))
rlm@140 393 "key-p"
rlm@139 394 (fn [_ _] (.setEnabled finger-control false))
rlm@140 395 "key-k"
rlm@140 396 (fn [_ _] (.setEnabled hand-control true))
rlm@140 397 "key-l"
rlm@140 398 (fn [_ _] (.setEnabled hand-control false))
rlm@139 399 "key-i"
rlm@139 400 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))
rlm@142 401 "key-period"
rlm@142 402 (fn [world _]
rlm@142 403 (.setEnabled finger-control false)
rlm@142 404 (.setEnabled hand-control false)
rlm@142 405 (.rotate creature (doto (Quaternion.)
rlm@142 406 (.fromAngleAxis
rlm@142 407 (float (/ Math/PI 15))
rlm@142 408 (Vector3f. 0 0 -1))))
rlm@142 409
rlm@142 410 (.setEnabled finger-control true)
rlm@142 411 (.setEnabled hand-control true)
rlm@142 412 (set-gravity world (Vector3f. 0 0 0))
rlm@142 413 )
rlm@142 414
rlm@142 415
rlm@139 416 }
rlm@139 417 )
rlm@130 418
rlm@139 419 ]
rlm@139 420 (comment
rlm@139 421 (.setCollisionGroup
rlm@139 422 (.getControl hand RigidBodyControl)
rlm@139 423 PhysicsCollisionObject/COLLISION_GROUP_NONE)
rlm@139 424 )
rlm@140 425 (apply
rlm@140 426 world
rlm@140 427 (with-movement
rlm@140 428 hand
rlm@140 429 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]
rlm@140 430 [10 10 10 10 1 1]
rlm@140 431 (with-movement
rlm@140 432 finger
rlm@140 433 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
rlm@145 434 [1 1 10 10 10 10]
rlm@140 435 [root
rlm@140 436 controls
rlm@140 437 (fn [world]
rlm@140 438 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
rlm@140 439 (set-gravity world (Vector3f. 0 0 0))
rlm@140 440 (light-up-everything world))
rlm@145 441 (fn [_ _] (prop-view (list (prop))))]))))))
rlm@138 442
rlm@64 443 #+end_src
rlm@56 444
rlm@130 445 #+results: test-body
rlm@130 446 : #'cortex.test.body/test-proprioception
rlm@130 447
rlm@60 448
rlm@63 449 * COMMENT code-limbo
rlm@61 450 #+begin_src clojure
rlm@61 451 ;;(.loadModel
rlm@61 452 ;; (doto (asset-manager)
rlm@61 453 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 454 ;; "Models/person/person.blend")
rlm@61 455
rlm@64 456
rlm@64 457 (defn load-blender-model
rlm@64 458 "Load a .blend file using an asset folder relative path."
rlm@64 459 [^String model]
rlm@64 460 (.loadModel
rlm@64 461 (doto (asset-manager)
rlm@64 462 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@64 463 model))
rlm@64 464
rlm@64 465
rlm@61 466 (defn view-model [^String model]
rlm@61 467 (view
rlm@61 468 (.loadModel
rlm@61 469 (doto (asset-manager)
rlm@61 470 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 471 model)))
rlm@61 472
rlm@61 473 (defn load-blender-scene [^String model]
rlm@61 474 (.loadModel
rlm@61 475 (doto (asset-manager)
rlm@61 476 (.registerLoader BlenderLoader (into-array String ["blend"])))
rlm@61 477 model))
rlm@61 478
rlm@61 479 (defn worm
rlm@61 480 []
rlm@61 481 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
rlm@61 482
rlm@61 483 (defn oto
rlm@61 484 []
rlm@61 485 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
rlm@61 486
rlm@61 487 (defn sinbad
rlm@61 488 []
rlm@61 489 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
rlm@61 490
rlm@61 491 (defn worm-blender
rlm@61 492 []
rlm@61 493 (first (seq (.getChildren (load-blender-model
rlm@61 494 "Models/anim2/simple-worm.blend")))))
rlm@61 495
rlm@61 496 (defn body
rlm@61 497 "given a node with a SkeletonControl, will produce a body sutiable
rlm@61 498 for AI control with movement and proprioception."
rlm@61 499 [node]
rlm@61 500 (let [skeleton-control (.getControl node SkeletonControl)
rlm@61 501 krc (KinematicRagdollControl.)]
rlm@61 502 (comment
rlm@61 503 (dorun
rlm@61 504 (map #(.addBoneName krc %)
rlm@61 505 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
rlm@61 506 ;;"mid2" "mid3" "tail" "head"]
rlm@61 507 )))
rlm@61 508 (.addControl node krc)
rlm@61 509 (.setRagdollMode krc)
rlm@61 510 )
rlm@61 511 node
rlm@61 512 )
rlm@61 513 (defn show-skeleton [node]
rlm@61 514 (let [sd
rlm@61 515
rlm@61 516 (doto
rlm@61 517 (SkeletonDebugger. "aurellem-skel-debug"
rlm@61 518 (skel node))
rlm@61 519 (.setMaterial (green-x-ray)))]
rlm@61 520 (.attachChild node sd)
rlm@61 521 node))
rlm@61 522
rlm@61 523
rlm@61 524
rlm@61 525 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 526
rlm@61 527 ;; this could be a good way to give objects special properties like
rlm@61 528 ;; being eyes and the like
rlm@61 529
rlm@61 530 (.getUserData
rlm@61 531 (.getChild
rlm@61 532 (load-blender-model "Models/property/test.blend") 0)
rlm@61 533 "properties")
rlm@61 534
rlm@61 535 ;; the properties are saved along with the blender file.
rlm@61 536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 537
rlm@61 538
rlm@61 539
rlm@61 540
rlm@61 541 (defn init-debug-skel-node
rlm@61 542 [f debug-node skeleton]
rlm@61 543 (let [bones
rlm@61 544 (map #(.getBone skeleton %)
rlm@61 545 (range (.getBoneCount skeleton)))]
rlm@61 546 (dorun (map #(.setUserControl % true) bones))
rlm@61 547 (dorun (map (fn [b]
rlm@61 548 (println (.getName b)
rlm@61 549 " -- " (f b)))
rlm@61 550 bones))
rlm@61 551 (dorun
rlm@61 552 (map #(.attachChild
rlm@61 553 debug-node
rlm@61 554 (doto
rlm@61 555 (sphere 0.1
rlm@61 556 :position (f %)
rlm@61 557 :physical? false)
rlm@61 558 (.setMaterial (green-x-ray))))
rlm@61 559 bones)))
rlm@61 560 debug-node)
rlm@61 561
rlm@61 562 (import jme3test.bullet.PhysicsTestHelper)
rlm@61 563
rlm@61 564
rlm@61 565 (defn test-zzz [the-worm world value]
rlm@61 566 (if (not value)
rlm@61 567 (let [skeleton (skel the-worm)]
rlm@61 568 (println-repl "enabling bones")
rlm@61 569 (dorun
rlm@61 570 (map
rlm@61 571 #(.setUserControl (.getBone skeleton %) true)
rlm@61 572 (range (.getBoneCount skeleton))))
rlm@61 573
rlm@61 574
rlm@61 575 (let [b (.getBone skeleton 2)]
rlm@61 576 (println-repl "moving " (.getName b))
rlm@61 577 (println-repl (.getLocalPosition b))
rlm@61 578 (.setUserTransforms b
rlm@61 579 Vector3f/UNIT_X
rlm@61 580 Quaternion/IDENTITY
rlm@61 581 ;;(doto (Quaternion.)
rlm@61 582 ;; (.fromAngles (/ Math/PI 2)
rlm@61 583 ;; 0
rlm@61 584 ;; 0
rlm@61 585
rlm@61 586 (Vector3f. 1 1 1))
rlm@61 587 )
rlm@61 588
rlm@61 589 (println-repl "hi! <3"))))
rlm@61 590
rlm@61 591
rlm@61 592 (defn test-ragdoll []
rlm@61 593
rlm@61 594 (let [the-worm
rlm@61 595
rlm@61 596 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
rlm@61 597 (doto (show-skeleton (worm-blender))
rlm@61 598 (.setLocalTranslation (Vector3f. 0 10 0))
rlm@61 599 ;;(worm)
rlm@61 600 ;;(oto)
rlm@61 601 ;;(sinbad)
rlm@61 602 )
rlm@61 603 ]
rlm@61 604
rlm@61 605
rlm@61 606 (.start
rlm@61 607 (world
rlm@61 608 (doto (Node.)
rlm@61 609 (.attachChild the-worm))
rlm@61 610 {"key-return" (fire-cannon-ball)
rlm@61 611 "key-space" (partial test-zzz the-worm)
rlm@61 612 }
rlm@61 613 (fn [world]
rlm@61 614 (light-up-everything world)
rlm@61 615 (PhysicsTestHelper/createPhysicsTestWorld
rlm@61 616 (.getRootNode world)
rlm@61 617 (asset-manager)
rlm@61 618 (.getPhysicsSpace
rlm@61 619 (.getState (.getStateManager world) BulletAppState)))
rlm@61 620 (set-gravity world Vector3f/ZERO)
rlm@61 621 ;;(.setTimer world (NanoTimer.))
rlm@61 622 ;;(org.lwjgl.input.Mouse/setGrabbed false)
rlm@61 623 )
rlm@61 624 no-op
rlm@61 625 )
rlm@61 626
rlm@61 627
rlm@61 628 )))
rlm@61 629
rlm@61 630
rlm@61 631 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 632 ;;; here is the ragdoll stuff
rlm@61 633
rlm@61 634 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
rlm@61 635 (def mesh worm-mesh)
rlm@61 636
rlm@61 637 (.getFloatBuffer mesh VertexBuffer$Type/Position)
rlm@61 638 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 639 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 640
rlm@61 641
rlm@61 642 (defn position [index]
rlm@61 643 (.get
rlm@61 644 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
rlm@61 645 index))
rlm@61 646
rlm@61 647 (defn bones [index]
rlm@61 648 (.get
rlm@61 649 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 650 index))
rlm@61 651
rlm@61 652 (defn bone-weights [index]
rlm@61 653 (.get
rlm@61 654 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 655 index))
rlm@61 656
rlm@61 657
rlm@61 658
rlm@61 659 (defn vertex-bones [vertex]
rlm@61 660 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 661
rlm@61 662 (defn vertex-weights [vertex]
rlm@61 663 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 664
rlm@61 665 (defn vertex-position [index]
rlm@61 666 (let [offset (* index 3)]
rlm@61 667 (Vector3f. (position offset)
rlm@61 668 (position (inc offset))
rlm@61 669 (position (inc(inc offset))))))
rlm@61 670
rlm@61 671 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
rlm@61 672
rlm@61 673 (defn bone-control-color [index]
rlm@61 674 (get {[1 0 0 0] ColorRGBA/Red
rlm@61 675 [1 2 0 0] ColorRGBA/Magenta
rlm@61 676 [2 0 0 0] ColorRGBA/Blue}
rlm@61 677 (vertex-bones index)
rlm@61 678 ColorRGBA/White))
rlm@61 679
rlm@61 680 (defn influence-color [index bone-num]
rlm@61 681 (get
rlm@61 682 {(float 0) ColorRGBA/Blue
rlm@61 683 (float 0.5) ColorRGBA/Green
rlm@61 684 (float 1) ColorRGBA/Red}
rlm@61 685 ;; find the weight of the desired bone
rlm@61 686 ((zipmap (vertex-bones index)(vertex-weights index))
rlm@61 687 bone-num)
rlm@61 688 ColorRGBA/Blue))
rlm@61 689
rlm@61 690 (def worm-vertices (set (map vertex-info (range 60))))
rlm@61 691
rlm@61 692
rlm@61 693 (defn test-info []
rlm@61 694 (let [points (Node.)]
rlm@61 695 (dorun
rlm@61 696 (map #(.attachChild points %)
rlm@61 697 (map #(sphere 0.01
rlm@61 698 :position (vertex-position %)
rlm@61 699 :color (influence-color % 1)
rlm@61 700 :physical? false)
rlm@61 701 (range 60))))
rlm@61 702 (view points)))
rlm@61 703
rlm@61 704
rlm@61 705 (defrecord JointControl [joint physics-space]
rlm@61 706 PhysicsControl
rlm@61 707 (setPhysicsSpace [this space]
rlm@61 708 (dosync
rlm@61 709 (ref-set (:physics-space this) space))
rlm@61 710 (.addJoint space (:joint this)))
rlm@61 711 (update [this tpf])
rlm@61 712 (setSpatial [this spatial])
rlm@61 713 (render [this rm vp])
rlm@61 714 (getPhysicsSpace [this] (deref (:physics-space this)))
rlm@61 715 (isEnabled [this] true)
rlm@61 716 (setEnabled [this state]))
rlm@61 717
rlm@61 718 (defn add-joint
rlm@61 719 "Add a joint to a particular object. When the object is added to the
rlm@61 720 PhysicsSpace of a simulation, the joint will also be added"
rlm@61 721 [object joint]
rlm@61 722 (let [control (JointControl. joint (ref nil))]
rlm@61 723 (.addControl object control))
rlm@61 724 object)
rlm@61 725
rlm@61 726
rlm@61 727 (defn hinge-world
rlm@61 728 []
rlm@61 729 (let [sphere1 (sphere)
rlm@61 730 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
rlm@61 731 joint (Point2PointJoint.
rlm@61 732 (.getControl sphere1 RigidBodyControl)
rlm@61 733 (.getControl sphere2 RigidBodyControl)
rlm@61 734 Vector3f/ZERO (Vector3f. 3 3 3))]
rlm@61 735 (add-joint sphere1 joint)
rlm@61 736 (doto (Node. "hinge-world")
rlm@61 737 (.attachChild sphere1)
rlm@61 738 (.attachChild sphere2))))
rlm@61 739
rlm@61 740
rlm@61 741 (defn test-joint []
rlm@61 742 (view (hinge-world)))
rlm@61 743
rlm@61 744 ;; (defn copier-gen []
rlm@61 745 ;; (let [count (atom 0)]
rlm@61 746 ;; (fn [in]
rlm@61 747 ;; (swap! count inc)
rlm@61 748 ;; (clojure.contrib.duck-streams/copy
rlm@61 749 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
rlm@61 750 ;; ;;/home/r/tmp/mao-test/clojure-images
rlm@61 751 ;; (format "%08d.png" @count)))))))
rlm@61 752 ;; (defn decrease-framerate []
rlm@61 753 ;; (map
rlm@61 754 ;; (copier-gen)
rlm@61 755 ;; (sort
rlm@61 756 ;; (map first
rlm@61 757 ;; (partition
rlm@61 758 ;; 4
rlm@61 759 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
rlm@61 760 ;; (file-seq
rlm@61 761 ;; (file-str
rlm@61 762 ;; "/home/r/media/anime/mao-temp/images"))))))))
rlm@61 763
rlm@61 764
rlm@61 765
rlm@61 766 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 767
rlm@61 768 (defn proprioception
rlm@61 769 "Create a proprioception map that reports the rotations of the
rlm@61 770 various limbs of the creature's body"
rlm@61 771 [creature]
rlm@61 772 [#^Node creature]
rlm@61 773 (let [
rlm@61 774 nodes (node-seq creature)
rlm@61 775 joints
rlm@61 776 (map
rlm@61 777 :joint
rlm@61 778 (filter
rlm@61 779 #(isa? (class %) JointControl)
rlm@61 780 (reduce
rlm@61 781 concat
rlm@61 782 (map (fn [node]
rlm@61 783 (map (fn [num] (.getControl node num))
rlm@61 784 (range (.getNumControls node))))
rlm@61 785 nodes))))]
rlm@61 786 (fn []
rlm@61 787 (reduce concat (map relative-positions (list (first joints)))))))
rlm@61 788
rlm@61 789
rlm@63 790 (defn skel [node]
rlm@63 791 (doto
rlm@63 792 (.getSkeleton
rlm@63 793 (.getControl node SkeletonControl))
rlm@63 794 ;; this is necessary to force the skeleton to have accurate world
rlm@63 795 ;; transforms before it is rendered to the screen.
rlm@63 796 (.resetAndUpdate)))
rlm@63 797
rlm@63 798 (defn green-x-ray []
rlm@63 799 (doto (Material. (asset-manager)
rlm@63 800 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@63 801 (.setColor "Color" ColorRGBA/Green)
rlm@63 802 (-> (.getAdditionalRenderState)
rlm@63 803 (.setDepthTest false))))
rlm@63 804
rlm@63 805 (defn test-worm []
rlm@63 806 (.start
rlm@63 807 (world
rlm@63 808 (doto (Node.)
rlm@63 809 ;;(.attachChild (point-worm))
rlm@63 810 (.attachChild (load-blender-model
rlm@63 811 "Models/anim2/joint-worm.blend"))
rlm@63 812
rlm@63 813 (.attachChild (box 10 1 10
rlm@63 814 :position (Vector3f. 0 -2 0) :mass 0
rlm@63 815 :color (ColorRGBA/Gray))))
rlm@63 816 {
rlm@63 817 "key-space" (fire-cannon-ball)
rlm@63 818 }
rlm@63 819 (fn [world]
rlm@63 820 (enable-debug world)
rlm@63 821 (light-up-everything world)
rlm@63 822 ;;(.setTimer world (NanoTimer.))
rlm@63 823 )
rlm@63 824 no-op)))
rlm@63 825
rlm@63 826
rlm@63 827
rlm@63 828 ;; defunct movement stuff
rlm@63 829 (defn torque-controls [control]
rlm@63 830 (let [torques
rlm@63 831 (concat
rlm@63 832 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
rlm@63 833 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
rlm@63 834 [Vector3f/UNIT_X])]
rlm@63 835 (map (fn [torque-axis]
rlm@63 836 (fn [torque]
rlm@63 837 (.applyTorque
rlm@63 838 control
rlm@63 839 (.mult (.mult (.getPhysicsRotation control)
rlm@63 840 torque-axis)
rlm@63 841 (float
rlm@63 842 (* (.getMass control) torque))))))
rlm@63 843 torques)))
rlm@63 844
rlm@63 845 (defn motor-map
rlm@63 846 "Take a creature and generate a function that will enable fine
rlm@63 847 grained control over all the creature's limbs."
rlm@63 848 [#^Node creature]
rlm@63 849 (let [controls (keep #(.getControl % RigidBodyControl)
rlm@63 850 (node-seq creature))
rlm@63 851 limb-controls (reduce concat (map torque-controls controls))
rlm@63 852 body-control (partial map #(%1 %2) limb-controls)]
rlm@63 853 body-control))
rlm@63 854
rlm@63 855 (defn test-motor-map
rlm@63 856 "see how torque works."
rlm@63 857 []
rlm@63 858 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
rlm@63 859 :mass 1 :color ColorRGBA/Green)
rlm@63 860 motor-map (motor-map finger)]
rlm@63 861 (world
rlm@63 862 (nodify [finger
rlm@63 863 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@63 864 :color ColorRGBA/Gray)])
rlm@63 865 standard-debug-controls
rlm@63 866 (fn [world]
rlm@63 867 (set-gravity world Vector3f/ZERO)
rlm@63 868 (light-up-everything world)
rlm@63 869 (.setTimer world (NanoTimer.)))
rlm@63 870 (fn [_ _]
rlm@145 871 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
rlm@145 872 0]))))))
rlm@145 873
rlm@145 874 (defn joint-proprioception [#^Node parts #^Node joint]
rlm@145 875 (let [[obj-a obj-b] (joint-targets parts joint)
rlm@145 876 joint-rot (.getWorldRotation joint)
rlm@145 877 pre-inv-a (.inverse (.getWorldRotation obj-a))
rlm@145 878 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))
rlm@145 879 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))
rlm@145 880 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))
rlm@145 881
rlm@145 882 x Vector3f/UNIT_Y
rlm@145 883 y Vector3f/UNIT_Z
rlm@145 884 z Vector3f/UNIT_X
rlm@145 885
rlm@145 886
rlm@145 887 tmp-rot-a (.getWorldRotation obj-a)]
rlm@145 888 (println-repl "x:" (.mult tmp-rot-a x))
rlm@145 889 (println-repl "y:" (.mult tmp-rot-a y))
rlm@145 890 (println-repl "z:" (.mult tmp-rot-a z))
rlm@145 891 (println-repl "rot-a" (.getWorldRotation obj-a))
rlm@145 892 (println-repl "rot-b" (.getWorldRotation obj-b))
rlm@145 893 (println-repl "joint-rot" joint-rot)
rlm@145 894 ;; this function will report proprioceptive information for the
rlm@145 895 ;; joint.
rlm@145 896 (fn []
rlm@145 897 ;; x is the "twist" axis, y and z are the "bend" axes
rlm@145 898 (let [rot-a (.getWorldRotation obj-a)
rlm@145 899 ;;inv-a (.inverse rot-a)
rlm@145 900 rot-b (.getWorldRotation obj-b)
rlm@145 901 ;;relative (.mult rot-b inv-a)
rlm@145 902 basis (doto (Matrix3f.)
rlm@145 903 (.setColumn 0 (.mult rot-a x))
rlm@145 904 (.setColumn 1 (.mult rot-a y))
rlm@145 905 (.setColumn 2 (.mult rot-a z)))
rlm@145 906 rotation-about-joint
rlm@145 907 (doto (Quaternion.)
rlm@145 908 (.fromRotationMatrix
rlm@145 909 (.mult (.invert basis)
rlm@145 910 (.toRotationMatrix rot-b))))
rlm@145 911 [yaw roll pitch]
rlm@145 912 (seq (.toAngles rotation-about-joint nil))]
rlm@145 913 ;;return euler angles of the quaternion around the new basis
rlm@145 914 [yaw roll pitch]))))
rlm@145 915
rlm@61 916 #+end_src
rlm@0 917
rlm@0 918
rlm@0 919
rlm@0 920
rlm@0 921
rlm@0 922
rlm@0 923
rlm@73 924 * COMMENT generate Source
rlm@44 925 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@64 926 <<proprioception>>
rlm@64 927 <<motor-control>>
rlm@0 928 #+end_src
rlm@64 929
rlm@69 930 #+begin_src clojure :tangle ../src/cortex/test/body.clj
rlm@64 931 <<test-body>>
rlm@64 932 #+end_src
rlm@64 933
rlm@64 934
rlm@0 935