diff org/test-creature.org @ 147:72801a20b8e5

First stab at blender muscles. (gee that sounds a little violent...)
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 00:48:34 -0700
parents 68226790d1fa
children 511112c44b16
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Thu Feb 02 13:22:14 2012 -0700
     1.2 +++ b/org/test-creature.org	Fri Feb 03 00:48:34 2012 -0700
     1.3 @@ -80,7 +80,8 @@
     1.4   - [X] proprioception sensor map in the style of the other senses -- day
     1.5   - [ ] refactor integration code to distribute to each of the senses
     1.6         -- day
     1.7 - - [ ] create video showing all the senses for Winston -- 2 days 
     1.8 + - [ ] create video showing all the senses for Winston -- 2 days
     1.9 + - [ ] send package to friends for critiques -- 2 days
    1.10   - [ ] write summary of project for Winston            \
    1.11   - [ ] project proposals for Winston                    \
    1.12   - [ ] additional senses to be implemented for Winston   |  -- 2 days 
    1.13 @@ -1077,14 +1078,55 @@
    1.14  ;; represent relative roll, pitch, and yaw.  Write display code for
    1.15  ;; this though.
    1.16  
    1.17 -(defn muscle-fibre-values
    1.18 -  "Take the first row of the image and return the low-order bytes."
    1.19 +(defn muscle-fiber-values
    1.20 +  "get motor pool strengths"
    1.21    [#^BufferedImage image]
    1.22 -  (let [width (.getWidth image)]
    1.23 -    (for [x (range width)]
    1.24 -      (bit-and
    1.25 -       0xFF
    1.26 -       (.getRGB image x 0)))))
    1.27 +  (vec
    1.28 +   (let [width (.getWidth image)]
    1.29 +     (for [x (range width)]
    1.30 +       (- 255
    1.31 +          (bit-and
    1.32 +           0x0000FF
    1.33 +           (.getRGB image x 0)))))))
    1.34 +
    1.35 +
    1.36 +(defn creature-muscles 
    1.37 + "Return the children of the creature's \"muscles\" node."
    1.38 + [#^Node creature]
    1.39 + (if-let [muscle-node (.getChild creature "muscles")]
    1.40 +   (seq (.getChildren muscle-node))
    1.41 +   (do (println-repl "could not find muscles node") [])))
    1.42 +
    1.43 +(defn single-muscle [#^Node parts #^Node muscle]
    1.44 +  (let [target (closest-node parts muscle)
    1.45 +        axis
    1.46 +        (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
    1.47 +        strength (meta-data muscle "strength")
    1.48 +        image-name (read-string (meta-data muscle "muscle"))
    1.49 +        image 
    1.50 +        (ImageToAwt/convert
    1.51 +          (.getImage (.loadTexture (asset-manager) image-name))
    1.52 +          false false 0)
    1.53 +        fibers (muscle-fiber-values image)
    1.54 +        fiber-integral (reductions + fibers)
    1.55 +        force-index (vec
    1.56 +                     (map
    1.57 +                      #(float (* strength (/ % (last
    1.58 +                                                fiber-integral))))
    1.59 +                      fiber-integral))
    1.60 +        control (.getControl target RigidBodyControl)]
    1.61 +    (fn [n]
    1.62 +      (let [pool-index (min n (count fibers))]
    1.63 +        (.applyTorque control (force-index n))))))
    1.64 +
    1.65 +
    1.66 +(defn enable-muscles
    1.67 +  "Must be called on a creature after RigidBodyControls have been
    1.68 +   created." 
    1.69 +  [#^Node creature]
    1.70 +  (let [muscles (creature-muscles creature)]
    1.71 +    (for [muscle muscles]
    1.72 +      (single-muscle creature muscle))))
    1.73  
    1.74  (defn test-creature [thing]
    1.75    (let [x-axis