annotate org/body.org @ 176:026f69582022

organized imports in cortex.body
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 06:52:47 -0700
parents 0b9ae09eaec3
children d5c597a7aed4
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@175 8 * Making a solid, connected body.
rlm@175 9 #+name: joints
rlm@0 10 #+begin_src clojure
rlm@44 11 (ns cortex.body
rlm@176 12 "Assemble a physical creature using the definitions found in a
rlm@176 13 specially prepared blender file. Creates rigid bodies and joints so
rlm@176 14 that a creature can have a physical presense in the simulation."
rlm@176 15 {:author "Robert McIntyre"}
rlm@160 16 (:use (cortex world util sense))
rlm@176 17 (:use clojure.contrib.def)
rlm@64 18 (:import
rlm@176 19 (com.jme3.math Vector3f Quaternion Vector2f Matrix3f)
rlm@176 20 (com.jme3.bullet.joints
rlm@176 21 SixDofJoint Point2PointJoint HingeJoint ConeJoint)
rlm@135 22 com.jme3.bullet.control.RigidBodyControl
rlm@135 23 com.jme3.collision.CollisionResults
rlm@150 24 com.jme3.bounding.BoundingBox
rlm@176 25 com.jme3.scene.Node
rlm@176 26 com.jme3.scene.Geometry
rlm@176 27 com.jme3.bullet.collision.shapes.HullCollisionShape))
rlm@160 28
rlm@135 29 (defn joint-targets
rlm@135 30 "Return the two closest two objects to the joint object, ordered
rlm@135 31 from bottom to top according to the joint's rotation."
rlm@135 32 [#^Node parts #^Node joint]
rlm@135 33 (loop [radius (float 0.01)]
rlm@135 34 (let [results (CollisionResults.)]
rlm@135 35 (.collideWith
rlm@135 36 parts
rlm@135 37 (BoundingBox. (.getWorldTranslation joint)
rlm@135 38 radius radius radius)
rlm@135 39 results)
rlm@135 40 (let [targets
rlm@135 41 (distinct
rlm@135 42 (map #(.getGeometry %) results))]
rlm@135 43 (if (>= (count targets) 2)
rlm@135 44 (sort-by
rlm@135 45 #(let [v
rlm@135 46 (jme-to-blender
rlm@135 47 (.mult
rlm@135 48 (.inverse (.getWorldRotation joint))
rlm@135 49 (.subtract (.getWorldTranslation %)
rlm@135 50 (.getWorldTranslation joint))))]
rlm@135 51 (println-repl (.getName %) ":" v)
rlm@135 52 (.dot (Vector3f. 1 1 1)
rlm@135 53 v))
rlm@135 54 (take 2 targets))
rlm@135 55 (recur (float (* radius 2))))))))
rlm@135 56
rlm@160 57 (defmulti joint-dispatch
rlm@160 58 "Translate blender pseudo-joints into real JME joints."
rlm@160 59 (fn [constraints & _]
rlm@160 60 (:type constraints)))
rlm@141 61
rlm@160 62 (defmethod joint-dispatch :point
rlm@160 63 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@160 64 (println-repl "creating POINT2POINT joint")
rlm@160 65 ;; bullet's point2point joints are BROKEN, so we must use the
rlm@160 66 ;; generic 6DOF joint instead of an actual Point2Point joint!
rlm@141 67
rlm@160 68 ;; should be able to do this:
rlm@160 69 (comment
rlm@160 70 (Point2PointJoint.
rlm@160 71 control-a
rlm@160 72 control-b
rlm@160 73 pivot-a
rlm@160 74 pivot-b))
rlm@141 75
rlm@160 76 ;; but instead we must do this:
rlm@160 77 (println-repl "substuting 6DOF joint for POINT2POINT joint!")
rlm@160 78 (doto
rlm@160 79 (SixDofJoint.
rlm@160 80 control-a
rlm@160 81 control-b
rlm@160 82 pivot-a
rlm@160 83 pivot-b
rlm@160 84 false)
rlm@160 85 (.setLinearLowerLimit Vector3f/ZERO)
rlm@160 86 (.setLinearUpperLimit Vector3f/ZERO)
rlm@160 87 ;;(.setAngularLowerLimit (Vector3f. 1 1 1))
rlm@160 88 ;;(.setAngularUpperLimit (Vector3f. 0 0 0))
rlm@141 89
rlm@160 90 ))
rlm@160 91
rlm@160 92
rlm@160 93 (defmethod joint-dispatch :hinge
rlm@160 94 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@160 95 (println-repl "creating HINGE joint")
rlm@160 96 (let [axis
rlm@160 97 (if-let
rlm@160 98 [axis (:axis constraints)]
rlm@160 99 axis
rlm@160 100 Vector3f/UNIT_X)
rlm@160 101 [limit-1 limit-2] (:limit constraints)
rlm@160 102 hinge-axis
rlm@160 103 (.mult
rlm@160 104 rotation
rlm@160 105 (blender-to-jme axis))]
rlm@160 106 (doto
rlm@160 107 (HingeJoint.
rlm@160 108 control-a
rlm@160 109 control-b
rlm@160 110 pivot-a
rlm@160 111 pivot-b
rlm@160 112 hinge-axis
rlm@160 113 hinge-axis)
rlm@160 114 (.setLimit limit-1 limit-2))))
rlm@160 115
rlm@160 116 (defmethod joint-dispatch :cone
rlm@160 117 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@160 118 (let [limit-xz (:limit-xz constraints)
rlm@160 119 limit-xy (:limit-xy constraints)
rlm@160 120 twist (:twist constraints)]
rlm@160 121
rlm@160 122 (println-repl "creating CONE joint")
rlm@160 123 (println-repl rotation)
rlm@160 124 (println-repl
rlm@160 125 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@160 126 (println-repl
rlm@160 127 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@160 128 (println-repl
rlm@160 129 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@160 130 (doto
rlm@160 131 (ConeJoint.
rlm@160 132 control-a
rlm@160 133 control-b
rlm@160 134 pivot-a
rlm@160 135 pivot-b
rlm@160 136 rotation
rlm@160 137 rotation)
rlm@160 138 (.setLimit (float limit-xz)
rlm@160 139 (float limit-xy)
rlm@160 140 (float twist)))))
rlm@160 141
rlm@160 142 (defn connect
rlm@175 143 "Create a joint between 'obj-a and 'obj-b at the location of
rlm@175 144 'joint. The type of joint is determined by the metadata on 'joint.
rlm@175 145
rlm@175 146 Here are some examples:
rlm@160 147 {:type :point}
rlm@160 148 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@160 149 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@160 150
rlm@160 151 {:type :cone :limit-xz 0]
rlm@160 152 :limit-xy 0]
rlm@160 153 :twist 0]} (use XZY rotation mode in blender!)"
rlm@160 154 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@160 155 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@160 156 control-b (.getControl obj-b RigidBodyControl)
rlm@160 157 joint-center (.getWorldTranslation joint)
rlm@160 158 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@160 159 pivot-a (world-to-local obj-a joint-center)
rlm@160 160 pivot-b (world-to-local obj-b joint-center)]
rlm@160 161
rlm@160 162 (if-let [constraints
rlm@160 163 (map-vals
rlm@160 164 eval
rlm@160 165 (read-string
rlm@160 166 (meta-data joint "joint")))]
rlm@160 167 ;; A side-effect of creating a joint registers
rlm@160 168 ;; it with both physics objects which in turn
rlm@160 169 ;; will register the joint with the physics system
rlm@160 170 ;; when the simulation is started.
rlm@160 171 (do
rlm@160 172 (println-repl "creating joint between"
rlm@160 173 (.getName obj-a) "and" (.getName obj-b))
rlm@160 174 (joint-dispatch constraints
rlm@160 175 control-a control-b
rlm@160 176 pivot-a pivot-b
rlm@160 177 joint-rotation))
rlm@160 178 (println-repl "could not find joint meta-data!"))))
rlm@160 179
rlm@175 180 (defvar
rlm@175 181 ^{:arglists '([creature])}
rlm@175 182 joints
rlm@175 183 (sense-nodes "joints")
rlm@175 184 "Return the children of the creature's \"joints\" node.")
rlm@160 185
rlm@175 186 (defn physical!
rlm@175 187 "Iterate through the nodes in creature and make them real physical
rlm@175 188 objects in the simulation."
rlm@175 189 [#^Node creature]
rlm@160 190 (dorun
rlm@160 191 (map
rlm@160 192 (fn [geom]
rlm@160 193 (let [physics-control
rlm@160 194 (RigidBodyControl.
rlm@160 195 (HullCollisionShape.
rlm@160 196 (.getMesh geom))
rlm@160 197 (if-let [mass (meta-data geom "mass")]
rlm@160 198 (do
rlm@160 199 (println-repl
rlm@160 200 "setting" (.getName geom) "mass to" (float mass))
rlm@160 201 (float mass))
rlm@160 202 (float 1)))]
rlm@160 203
rlm@160 204 (.addControl geom physics-control)))
rlm@160 205 (filter #(isa? (class %) Geometry )
rlm@175 206 (node-seq creature)))))
rlm@175 207
rlm@175 208 (defn joints!
rlm@175 209 "Connect the solid parts of the creature with physical joints. The
rlm@175 210 joints are taken from the \"joints\" node in the creature."
rlm@175 211 [#^Node creature]
rlm@160 212 (dorun
rlm@160 213 (map
rlm@160 214 (fn [joint]
rlm@175 215 (let [[obj-a obj-b] (joint-targets creature joint)]
rlm@160 216 (connect obj-a obj-b joint)))
rlm@175 217 (joints creature))))
rlm@160 218
rlm@175 219 (defn body!
rlm@175 220 "Endow the creature with a physical body connected with joints. The
rlm@175 221 particulars of the joints and the masses of each pody part are
rlm@175 222 determined in blender."
rlm@175 223 [#^Node creature]
rlm@175 224 (physical! creature)
rlm@175 225 (joints! creature))
rlm@64 226 #+end_src
rlm@63 227
rlm@175 228 #+results: joints
rlm@175 229 : #'cortex.body/body!
rlm@175 230
rlm@133 231 #+results: proprioception
rlm@145 232 : #'cortex.body/proprioception-debug-window
rlm@133 233
rlm@64 234 * Examples
rlm@63 235
rlm@69 236 #+name: test-body
rlm@64 237 #+begin_src clojure
rlm@69 238 (ns cortex.test.body
rlm@64 239 (:use (cortex world util body))
rlm@135 240 (:require cortex.silly)
rlm@64 241 (:import
rlm@64 242 com.jme3.math.Vector3f
rlm@64 243 com.jme3.math.ColorRGBA
rlm@64 244 com.jme3.bullet.joints.Point2PointJoint
rlm@64 245 com.jme3.bullet.control.RigidBodyControl
rlm@145 246 com.jme3.system.NanoTimer
rlm@145 247 com.jme3.math.Quaternion))
rlm@63 248
rlm@64 249 (defn worm-segments
rlm@64 250 "Create multiple evenly spaced box segments. They're fabulous!"
rlm@64 251 [segment-length num-segments interstitial-space radius]
rlm@64 252 (letfn [(nth-segment
rlm@64 253 [n]
rlm@64 254 (box segment-length radius radius :mass 0.1
rlm@64 255 :position
rlm@64 256 (Vector3f.
rlm@64 257 (* 2 n (+ interstitial-space segment-length)) 0 0)
rlm@64 258 :name (str "worm-segment" n)
rlm@64 259 :color (ColorRGBA/randomColor)))]
rlm@64 260 (map nth-segment (range num-segments))))
rlm@63 261
rlm@64 262 (defn connect-at-midpoint
rlm@64 263 "Connect two physics objects with a Point2Point joint constraint at
rlm@64 264 the point equidistant from both objects' centers."
rlm@64 265 [segmentA segmentB]
rlm@64 266 (let [centerA (.getWorldTranslation segmentA)
rlm@64 267 centerB (.getWorldTranslation segmentB)
rlm@64 268 midpoint (.mult (.add centerA centerB) (float 0.5))
rlm@64 269 pivotA (.subtract midpoint centerA)
rlm@64 270 pivotB (.subtract midpoint centerB)
rlm@64 271
rlm@64 272 ;; A side-effect of creating a joint registers
rlm@64 273 ;; it with both physics objects which in turn
rlm@64 274 ;; will register the joint with the physics system
rlm@64 275 ;; when the simulation is started.
rlm@64 276 joint (Point2PointJoint.
rlm@64 277 (.getControl segmentA RigidBodyControl)
rlm@64 278 (.getControl segmentB RigidBodyControl)
rlm@64 279 pivotA
rlm@64 280 pivotB)]
rlm@64 281 segmentB))
rlm@63 282
rlm@64 283 (defn eve-worm
rlm@72 284 "Create a worm-like body bound by invisible joint constraints."
rlm@64 285 []
rlm@64 286 (let [segments (worm-segments 0.2 5 0.1 0.1)]
rlm@64 287 (dorun (map (partial apply connect-at-midpoint)
rlm@64 288 (partition 2 1 segments)))
rlm@64 289 (nodify "worm" segments)))
rlm@63 290
rlm@64 291 (defn worm-pattern
rlm@64 292 "This is a simple, mindless motor control pattern that drives the
rlm@64 293 second segment of the worm's body at an offset angle with
rlm@64 294 sinusoidally varying strength."
rlm@64 295 [time]
rlm@64 296 (let [angle (* Math/PI (/ 9 20))
rlm@63 297 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
rlm@63 298 [Vector3f/ZERO
rlm@63 299 (.mult
rlm@63 300 direction
rlm@63 301 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
rlm@63 302 Vector3f/ZERO
rlm@63 303 Vector3f/ZERO
rlm@63 304 Vector3f/ZERO]))
rlm@60 305
rlm@64 306 (defn test-motor-control
rlm@69 307 "Testing motor-control:
rlm@69 308 You should see a multi-segmented worm-like object fall onto the
rlm@64 309 table and begin writhing and moving."
rlm@60 310 []
rlm@64 311 (let [worm (eve-worm)
rlm@60 312 time (atom 0)
rlm@63 313 worm-motor-map (vector-motor-control worm)]
rlm@60 314 (world
rlm@60 315 (nodify [worm
rlm@60 316 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 317 :color ColorRGBA/Gray)])
rlm@60 318 standard-debug-controls
rlm@60 319 (fn [world]
rlm@60 320 (enable-debug world)
rlm@60 321 (light-up-everything world)
rlm@63 322 (comment
rlm@63 323 (com.aurellem.capture.Capture/captureVideo
rlm@63 324 world
rlm@63 325 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
rlm@63 326 )
rlm@60 327
rlm@60 328 (fn [_ _]
rlm@60 329 (swap! time inc)
rlm@64 330 (Thread/sleep 20)
rlm@60 331 (dorun (worm-motor-map
rlm@60 332 (worm-pattern @time)))))))
rlm@60 333
rlm@130 334
rlm@135 335
rlm@130 336 (defn join-at-point [obj-a obj-b world-pivot]
rlm@130 337 (cortex.silly/joint-dispatch
rlm@130 338 {:type :point}
rlm@130 339 (.getControl obj-a RigidBodyControl)
rlm@130 340 (.getControl obj-b RigidBodyControl)
rlm@130 341 (cortex.silly/world-to-local obj-a world-pivot)
rlm@130 342 (cortex.silly/world-to-local obj-b world-pivot)
rlm@130 343 nil
rlm@130 344 ))
rlm@130 345
rlm@133 346 (import com.jme3.bullet.collision.PhysicsCollisionObject)
rlm@130 347
rlm@130 348 (defn blab-* []
rlm@130 349 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
rlm@130 350 :mass 0 :color ColorRGBA/Green)
rlm@130 351 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
rlm@130 352 :mass 1 :color ColorRGBA/Red)
rlm@130 353 connection-point (Vector3f. 1.2 0 0)
rlm@130 354 root (nodify [hand finger])]
rlm@130 355
rlm@130 356 (join-at-point hand finger (Vector3f. 1.2 0 0))
rlm@130 357
rlm@130 358 (.setCollisionGroup
rlm@130 359 (.getControl hand RigidBodyControl)
rlm@130 360 PhysicsCollisionObject/COLLISION_GROUP_NONE)
rlm@130 361 (world
rlm@130 362 root
rlm@130 363 standard-debug-controls
rlm@130 364 (fn [world]
rlm@130 365 (enable-debug world)
rlm@130 366 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
rlm@130 367 (set-gravity world Vector3f/ZERO)
rlm@130 368 )
rlm@130 369 no-op)))
rlm@133 370 (comment
rlm@133 371
rlm@133 372 (defn proprioception-debug-window
rlm@133 373 []
rlm@133 374 (let [time (atom 0)]
rlm@133 375 (fn [prop-data]
rlm@133 376 (if (= 0 (rem (swap! time inc) 40))
rlm@133 377 (println-repl prop-data)))))
rlm@133 378 )
rlm@133 379
rlm@131 380 (comment
rlm@131 381 (dorun
rlm@131 382 (map
rlm@131 383 (comp
rlm@131 384 println-repl
rlm@131 385 (fn [[p y r]]
rlm@131 386 (format
rlm@131 387 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
rlm@131 388 p y r)))
rlm@131 389 prop-data)))
rlm@131 390
rlm@130 391
rlm@130 392
rlm@137 393
rlm@64 394 (defn test-proprioception
rlm@69 395 "Testing proprioception:
rlm@69 396 You should see two foating bars, and a printout of pitch, yaw, and
rlm@64 397 roll. Pressing key-r/key-t should move the blue bar up and down and
rlm@64 398 change only the value of pitch. key-f/key-g moves it side to side
rlm@64 399 and changes yaw. key-v/key-b will spin the blue segment clockwise
rlm@64 400 and counterclockwise, and only affect roll."
rlm@60 401 []
rlm@145 402 (let [hand (box 0.2 1 0.2 :position (Vector3f. 0 0 0)
rlm@142 403 :mass 0 :color ColorRGBA/Green :name "hand")
rlm@145 404 finger (box 0.2 1 0.2 :position (Vector3f. 0 2.4 0)
rlm@132 405 :mass 1 :color ColorRGBA/Red :name "finger")
rlm@133 406 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
rlm@145 407 :position (Vector3f. 0 1.2 0)
rlm@145 408 :rotation (doto (Quaternion.)
rlm@145 409 (.fromAngleAxis
rlm@145 410 (/ Math/PI 2)
rlm@145 411 (Vector3f. 0 0 1)))
rlm@133 412 :physical? false)
rlm@145 413 joint (join-at-point hand finger (Vector3f. 0 1.2 0 ))
rlm@135 414 creature (nodify [hand finger joint-node])
rlm@145 415 finger-control (.getControl finger RigidBodyControl)
rlm@145 416 hand-control (.getControl hand RigidBodyControl)]
rlm@145 417
rlm@145 418
rlm@145 419 (let
rlm@135 420 ;; *******************************************
rlm@137 421
rlm@145 422 [floor (box 10 10 10 :position (Vector3f. 0 -15 0)
rlm@135 423 :mass 0 :color ColorRGBA/Gray)
rlm@137 424
rlm@137 425 root (nodify [creature floor])
rlm@133 426 prop (joint-proprioception creature joint-node)
rlm@139 427 prop-view (proprioception-debug-window)
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@145 474 [1 1 10 10 10 10]
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@145 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@145 911 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
rlm@145 912 0]))))))
rlm@145 913
rlm@145 914 (defn joint-proprioception [#^Node parts #^Node joint]
rlm@145 915 (let [[obj-a obj-b] (joint-targets parts joint)
rlm@145 916 joint-rot (.getWorldRotation joint)
rlm@145 917 pre-inv-a (.inverse (.getWorldRotation obj-a))
rlm@145 918 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))
rlm@145 919 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))
rlm@145 920 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))
rlm@145 921
rlm@145 922 x Vector3f/UNIT_Y
rlm@145 923 y Vector3f/UNIT_Z
rlm@145 924 z Vector3f/UNIT_X
rlm@145 925
rlm@145 926
rlm@145 927 tmp-rot-a (.getWorldRotation obj-a)]
rlm@145 928 (println-repl "x:" (.mult tmp-rot-a x))
rlm@145 929 (println-repl "y:" (.mult tmp-rot-a y))
rlm@145 930 (println-repl "z:" (.mult tmp-rot-a z))
rlm@145 931 (println-repl "rot-a" (.getWorldRotation obj-a))
rlm@145 932 (println-repl "rot-b" (.getWorldRotation obj-b))
rlm@145 933 (println-repl "joint-rot" joint-rot)
rlm@145 934 ;; this function will report proprioceptive information for the
rlm@145 935 ;; joint.
rlm@145 936 (fn []
rlm@145 937 ;; x is the "twist" axis, y and z are the "bend" axes
rlm@145 938 (let [rot-a (.getWorldRotation obj-a)
rlm@145 939 ;;inv-a (.inverse rot-a)
rlm@145 940 rot-b (.getWorldRotation obj-b)
rlm@145 941 ;;relative (.mult rot-b inv-a)
rlm@145 942 basis (doto (Matrix3f.)
rlm@145 943 (.setColumn 0 (.mult rot-a x))
rlm@145 944 (.setColumn 1 (.mult rot-a y))
rlm@145 945 (.setColumn 2 (.mult rot-a z)))
rlm@145 946 rotation-about-joint
rlm@145 947 (doto (Quaternion.)
rlm@145 948 (.fromRotationMatrix
rlm@145 949 (.mult (.invert basis)
rlm@145 950 (.toRotationMatrix rot-b))))
rlm@145 951 [yaw roll pitch]
rlm@145 952 (seq (.toAngles rotation-about-joint nil))]
rlm@145 953 ;;return euler angles of the quaternion around the new basis
rlm@145 954 [yaw roll pitch]))))
rlm@145 955
rlm@61 956 #+end_src
rlm@0 957
rlm@0 958
rlm@0 959
rlm@0 960
rlm@0 961
rlm@0 962
rlm@0 963
rlm@73 964 * COMMENT generate Source
rlm@44 965 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@175 966 <<joints>>
rlm@0 967 #+end_src
rlm@64 968
rlm@69 969 #+begin_src clojure :tangle ../src/cortex/test/body.clj
rlm@64 970 <<test-body>>
rlm@64 971 #+end_src
rlm@64 972
rlm@64 973
rlm@0 974