annotate org/body.org @ 142:ccd057319c2a

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