annotate org/body.org @ 136:47a4d74761f0

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