annotate org/body.org @ 155:95bf55614211

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