annotate org/body.org @ 144:48f9cba082eb

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