changeset 74:fb810a2c50c2

trying to get the hand to work
author Robert McIntyre <rlm@mit.edu>
date Wed, 28 Dec 2011 06:44:36 -0700
parents 257a86328adb
children f4c77512808e
files assets/Models/creature1/one.blend assets/Models/creature1/try-again.blend org/body.org org/test-creature.org org/util.org
diffstat 5 files changed, 134 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/one.blend has changed
     2.1 Binary file assets/Models/creature1/try-again.blend has changed
     3.1 --- a/org/body.org	Wed Dec 28 00:00:23 2011 -0700
     3.2 +++ b/org/body.org	Wed Dec 28 06:44:36 2011 -0700
     3.3 @@ -730,8 +730,6 @@
     3.4         (.setTimer world (NanoTimer.)))
     3.5       (fn [_ _]
     3.6         (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
     3.7 -
     3.8 -
     3.9  #+end_src
    3.10  
    3.11  
     4.1 --- a/org/test-creature.org	Wed Dec 28 00:00:23 2011 -0700
     4.2 +++ b/org/test-creature.org	Wed Dec 28 06:44:36 2011 -0700
     4.3 @@ -61,13 +61,132 @@
     4.4       (.registerLoader BlenderModelLoader (into-array String ["blend"])))
     4.5     model))
     4.6  
     4.7 +(defn meta-data [blender-node key]
     4.8 +  (if-let [data (.getUserData blender-node "properties")]
     4.9 +    (.findValue data key)
    4.10 +    nil))
    4.11  
    4.12 -(defn apply-skeleton
    4.13 -  "Given an imported blender model, apply the armature using the
    4.14 -  following pattern: if two bones are connected and have the same
    4.15 -  names as two shapes, connect the shapes with a joint constraint."
    4.16 -  [armature-name creature]
    4.17 +(defn hand2 []
    4.18 +  (load-blender-model "Models/creature1/try-again.blend"))
    4.19 +
    4.20 +(defn hand []
    4.21 +  (load-blender-model "Models/creature1/one.blend"))
    4.22 +
    4.23 +
    4.24    
    4.25 +(def hand-names
    4.26 +  #{
    4.27 +    "middle-1"
    4.28 +    "middle-2"
    4.29 +    "middle-3"
    4.30 +    "palm"
    4.31 +    "pinky-1"
    4.32 +    "pinky-2"
    4.33 +    "pinky-3"
    4.34 +    "pointer-1"
    4.35 +    "pointer-2"
    4.36 +    "pointer-3"
    4.37 +    "ring-1"
    4.38 +    "ring-2"
    4.39 +    "ring-3"
    4.40 +    "thumb-1"
    4.41 +    "thumb-2"})
    4.42 +
    4.43 +(defn hand-pieces []
    4.44 +  (filter
    4.45 +   (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand))))
    4.46 +
    4.47 +(defn hand-joints []
    4.48 +  (map #(.getWorldTranslation %)
    4.49 +       (filter #(re-matches #"joint\.\d\d\d" (.getName %))
    4.50 +               (node-seq (hand)))))
    4.51 +
    4.52 +(defn worm-pieces []
    4.53 +  (filter
    4.54 +   (comp #{"worm-2" "worm-1"}
    4.55 +         #(apply str (drop-last (.getName %))))
    4.56 +   (node-seq (hand2))))
    4.57 +
    4.58 +(defn worm-joints []
    4.59 +  [Vector3f/ZERO])
    4.60 +
    4.61 +
    4.62 +
    4.63 +(defn find-joint
    4.64 +  [#^Node parts #^Vector3f joint-position]
    4.65 +  (loop [radius (float 0.01)]
    4.66 +    (let [results (CollisionResults.)]
    4.67 +      (.collideWith
    4.68 +       parts
    4.69 +       (BoundingBox. joint-position radius radius radius)
    4.70 +       results)
    4.71 +      (let [targets
    4.72 +            (distinct
    4.73 +             (map  #(.getGeometry %) results))]
    4.74 +        (if (>= (count targets) 2)
    4.75 +          (take 2 targets)
    4.76 +          (recur (float (* radius 2))))))))
    4.77 +
    4.78 +
    4.79 +
    4.80 +(defn connect-at-point
    4.81 +  [obj-a obj-b point]
    4.82 +  (let [center-a (.getWorldTranslation obj-a)
    4.83 +        center-b (.getWorldTranslation obj-b)
    4.84 +        pivot-a (.subtract point center-a)
    4.85 +        pivot-b (.subtract point center-b)
    4.86 +        ;; A side-effect of creating a joint registers
    4.87 +        ;; it with both physics objects which in turn
    4.88 +        ;; will register the joint with the physics system
    4.89 +        ;; when the simulation is started.
    4.90 +        joint (Point2PointJoint.
    4.91 +               (.getControl obj-a RigidBodyControl)
    4.92 +               (.getControl obj-b RigidBodyControl)
    4.93 +               pivot-a
    4.94 +               pivot-b)]
    4.95 +    obj-a))
    4.96 +
    4.97 +
    4.98 +(defn physical-hand [#^Node pieces joints]
    4.99 +  ;; Make each piece a physical entity in the simulation.
   4.100 +  (dorun
   4.101 +   (map
   4.102 +    (fn [geom]
   4.103 +      (let [physics-control
   4.104 +            (RigidBodyControl.
   4.105 +             (HullCollisionShape.
   4.106 +              (.getMesh geom))
   4.107 +             ;; TODO: fix this.
   4.108 +             (float 1.0))]
   4.109 +        (.addControl geom physics-control)))
   4.110 +    (filter #(isa? (class %) Geometry )
   4.111 +            (node-seq pieces))))
   4.112 +  (dorun
   4.113 +   (map
   4.114 +    (fn [joint-position]
   4.115 +      (let [[geom-a geom-b] (find-joint pieces joint-position)]
   4.116 +        (connect-at-point geom-a geom-b joint-position)))
   4.117 +      joints))
   4.118 +   pieces)
   4.119 +
   4.120 +
   4.121 +(defn the-hand! [] (physical-hand  (hand) (hand-joints)))
   4.122 +
   4.123 +
   4.124 +(defn test-hand []
   4.125 +  (world
   4.126 +   (nodify [(the-hand!)
   4.127 +            (box 10 0.1 10 :position (Vector3f. 0 -10 0)
   4.128 +                 :color ColorRGBA/Gray
   4.129 +                 :mass 0)])
   4.130 +   standard-debug-controls
   4.131 +   enable-debug
   4.132 +   no-op))
   4.133 +
   4.134 +
   4.135 +
   4.136 +
   4.137 +
   4.138  
   4.139  
   4.140  #+end_src
     5.1 --- a/org/util.org	Wed Dec 28 00:00:23 2011 -0700
     5.2 +++ b/org/util.org	Wed Dec 28 06:44:36 2011 -0700
     5.3 @@ -274,6 +274,16 @@
     5.4  		     :shape (Sphere. 32 32 (float r)))))) 
     5.5    ([] (sphere 0.5)))
     5.6  
     5.7 +(defn green-x-ray 
     5.8 +  "A usefull material for debuging -- it can be seen no matter what
     5.9 +  object occuldes it."
    5.10 +  []
    5.11 +  (doto (Material. (asset-manager)
    5.12 +                   "Common/MatDefs/Misc/Unshaded.j3md")
    5.13 +    (.setColor "Color" ColorRGBA/Green)
    5.14 +    (-> (.getAdditionalRenderState)
    5.15 +        (.setDepthTest false))))
    5.16 +
    5.17  (defn node-seq
    5.18    "Take a node and return a seq of all its children
    5.19     recursively. There will be no nodes left in the resulting