annotate org/body.org @ 135:421cc43441ae

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