changeset 81:10f495560c59

correcting problem with joints
author Robert McIntyre <rlm@mit.edu>
date Thu, 05 Jan 2012 21:31:34 -0700
parents 7af5ef686539
children 6b4ca076285e
files assets/Models/creature1/one.blend org/test-creature.org
diffstat 2 files changed, 121 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/one.blend has changed
     2.1 --- a/org/test-creature.org	Thu Jan 05 02:35:07 2012 -0700
     2.2 +++ b/org/test-creature.org	Thu Jan 05 21:31:34 2012 -0700
     2.3 @@ -116,120 +116,128 @@
     2.4     {:type :cone   :limit-xz 0]
     2.5                    :limit-xy 0]
     2.6                    :twist 0]}   (use XZY rotation mode in blender!)"
     2.7 -  ([#^Node obj-a #^Node obj-b #^Node joint]
     2.8 -     (let [center-a (.getWorldTranslation obj-a)
     2.9 -           center-b (.getWorldTranslation obj-b)
    2.10 -           joint-center (.getWorldTranslation joint)
    2.11 -           pivot-a (.subtract joint-center center-a)
    2.12 -           pivot-b (.subtract joint-center center-b)
    2.13 -           control-a (.getControl obj-a RigidBodyControl)
    2.14 -           control-b (.getControl obj-b RigidBodyControl)]
    2.15 -           ;; A side-effect of creating a joint registers
    2.16 -           ;; it with both physics objects which in turn
    2.17 -           ;; will register the joint with the physics system
    2.18 -           ;; when the simulation is started.
    2.19 -       (if-let [constraints
    2.20 -                (map-vals
    2.21 -                 eval
    2.22 -                 (read-string
    2.23 -                  (meta-data joint "joint")))]
    2.24 +  [#^Node obj-a #^Node obj-b #^Node joint]
    2.25 +  (let [center-a (.getWorldTranslation obj-a)
    2.26 +        center-b (.getWorldTranslation obj-b)
    2.27 +        joint-center (.getWorldTranslation joint)
    2.28 +        pivot-a (.subtract joint-center center-a)
    2.29 +        pivot-b (.subtract joint-center center-b)
    2.30 +        control-a (.getControl obj-a RigidBodyControl)
    2.31 +        control-b (.getControl obj-b RigidBodyControl)]
    2.32 +    ;; A side-effect of creating a joint registers
    2.33 +    ;; it with both physics objects which in turn
    2.34 +    ;; will register the joint with the physics system
    2.35 +    ;; when the simulation is started.
    2.36 +    (if-let [constraints
    2.37 +             (map-vals
    2.38 +              eval
    2.39 +              (read-string
    2.40 +               (meta-data joint "joint")))]
    2.41  
    2.42 -         (let [joint-type (:type constraints)]
    2.43 -           (println-repl "creating joint between"
    2.44 -                         (.getName obj-a) "and" (.getName obj-b))
    2.45 -           (cond (= :point joint-type)
    2.46 -                 (do
    2.47 -                   (println-repl "creating POINT joint")
    2.48 -                   (Point2PointJoint.
    2.49 -                    control-a
    2.50 -                    control-b
    2.51 -                    pivot-a
    2.52 -                    pivot-b))
    2.53 -                 (= :hinge joint-type)
    2.54 -                 (do
    2.55 -                   (println-repl "creating HINGE joint")
    2.56 -                   (let [axis (if-let
    2.57 -                                  [axis (:axis constraints)]
    2.58 -                                axis
    2.59 -                                Vector3f/UNIT_X)
    2.60 -                         [limit-1 limit-2] (:limit constraints)
    2.61 -                         hinge-axis
    2.62 -                         (.mult
    2.63 -                          (.getWorldRotation joint)
    2.64 -                          (blender-to-jme axis))]
    2.65 -                     (doto
    2.66 -                         (HingeJoint.
    2.67 -                          control-a
    2.68 -                          control-b
    2.69 -                          pivot-a
    2.70 -                          pivot-b
    2.71 -                          hinge-axis
    2.72 -                          hinge-axis)
    2.73 -                       (.setLimit limit-1 limit-2))))
    2.74 -                 (= :cone joint-type)
    2.75 -                 (do
    2.76 -                   (let [limit-xy (:limit-xz constraints)
    2.77 -                         limit-yz (:limit-xy constraints)
    2.78 -                         twist    (:twist constraints)]
    2.79 -                     
    2.80 -                     (println-repl "creating CONE joint")
    2.81 -                     (let [vector-1
    2.82 -                           (.mult (.getWorldRotation joint)
    2.83 -                                  Vector3f/UNIT_X)
    2.84 -                           vector-2
    2.85 -                           (.normalize
    2.86 -                            (.subtract
    2.87 -                             (.getWorldTranslation joint)
    2.88 -                             (.getWorldTranslation obj-a)))
    2.89 -                           ]
    2.90 -                       (println-repl
    2.91 -                        "vector-1 :" vector-1)
    2.92 -                       (println-repl
    2.93 -                        "vector-2 :" vector-2)
    2.94 -                        
    2.95 -                       
    2.96 -                       
    2.97 -                       (doto
    2.98 -                           (ConeJoint.
    2.99 -                            control-a
   2.100 -                            control-b
   2.101 -                            pivot-a
   2.102 -                            pivot-b
   2.103 -                            
   2.104 -                            ;;(doto (Matrix3f.)
   2.105 -                            ;;  (.fromStartEndVectors
   2.106 -                            ;;   Vector3f/UNIT_X
   2.107 -                            ;;   (.normalize
   2.108 -                            ;;    (.subtract
   2.109 -                            ;;     (.getWorldTranslation joint)
   2.110 -                            ;;     (.getWorldTranslation obj-a)))))
   2.111 -                            
   2.112 -                            (.toRotationMatrix (.getWorldRotation joint))
   2.113 +      (let [joint-type (:type constraints)]
   2.114 +        (println-repl "creating joint between"
   2.115 +                      (.getName obj-a) "and" (.getName obj-b))
   2.116 +        (cond (= :point joint-type)
   2.117 +              (do
   2.118 +                (println-repl "creating POINT joint")
   2.119 +                (Point2PointJoint.
   2.120 +                 control-a
   2.121 +                 control-b
   2.122 +                 pivot-a
   2.123 +                 pivot-b))
   2.124 +              (= :hinge joint-type)
   2.125 +              (do
   2.126 +                (println-repl "creating HINGE joint")
   2.127 +                (let [axis (if-let
   2.128 +                               [axis (:axis constraints)]
   2.129 +                             axis
   2.130 +                             Vector3f/UNIT_X)
   2.131 +                      [limit-1 limit-2] (:limit constraints)
   2.132 +                      hinge-axis
   2.133 +                      (.mult
   2.134 +                       (.getWorldRotation joint)
   2.135 +                       (blender-to-jme axis))]
   2.136 +                  (doto
   2.137 +                      (HingeJoint.
   2.138 +                       control-a
   2.139 +                       control-b
   2.140 +                       pivot-a
   2.141 +                       pivot-b
   2.142 +                       hinge-axis
   2.143 +                       hinge-axis)
   2.144 +                    (.setLimit limit-1 limit-2))))
   2.145 +              (= :cone joint-type)
   2.146 +              (do
   2.147 +                (let [limit-xz (:limit-xz constraints)
   2.148 +                      limit-xy (:limit-xy constraints)
   2.149 +                      twist    (:twist constraints)]
   2.150 +                  
   2.151 +                  (println-repl "creating CONE joint")
   2.152 +                  (let [vector-1
   2.153 +                        (.mult (.getWorldRotation joint)
   2.154 +                               Vector3f/UNIT_X)
   2.155 +                        vector-2
   2.156 +                        (.normalize
   2.157 +                         (.subtract
   2.158 +                          (.getWorldTranslation joint)
   2.159 +                          (.getWorldTranslation obj-a)))
   2.160 +                        ]
   2.161 +                    (println-repl
   2.162 +                     "vector-1 :" vector-1)
   2.163 +                    (println-repl
   2.164 +                     "vector-2 :" vector-2)
   2.165 +                    
   2.166 +                    
   2.167 +                    
   2.168 +                    (doto
   2.169 +                        (ConeJoint.
   2.170 +                         control-a
   2.171 +                         control-b
   2.172 +                         pivot-a
   2.173 +                         pivot-b
   2.174 +                         
   2.175 +                         ;;(doto (Matrix3f.)
   2.176 +                         ;;  (.fromStartEndVectors
   2.177 +                         ;;   Vector3f/UNIT_X
   2.178 +                         ;;   (.normalize
   2.179 +                         ;;    (.subtract
   2.180 +                         ;;     (.getWorldTranslation joint)
   2.181 +                         ;;     (.getWorldTranslation obj-a)))))
   2.182  
   2.183 -                            (.toRotationMatrix
   2.184 -                             (.fromAngleAxis (Quaternion.)
   2.185 -                              (.angleBetween Vector3f/UNIT_X pivot-a)
   2.186 -                              (.cross Vector3f/UNIT_X pivot-a)))
   2.187 -                            
   2.188 -                            ;; (doto (Matrix3f.)
   2.189 -                            ;;   (.fromStartEndVectors
   2.190 -                            ;;    Vector3f/UNIT_X
   2.191 -                            ;;    (.normalize
   2.192 -                            ;;     vector-1)))
   2.193 -                            
   2.194 -                            ;; (doto (Matrix3f.)
   2.195 -                            ;;   (.fromStartEndVectors
   2.196 -                            ;;    Vector3f/UNIT_X
   2.197 -                            ;;    vector-2))
   2.198 -                            )
   2.199 -                         (.setLimit (float limit-xy)
   2.200 -                                    (float limit-yz)
   2.201 -                                    (float twist))))))
   2.202 -                   true
   2.203 -                   (println-repl
   2.204 -                    "joint-type" joint-type "not recognized")))
   2.205 -           
   2.206 -           (println-repl "could not find joint meta-data!")))))
   2.207 +
   2.208 +                         (.toRotationMatrix
   2.209 +                          (.getWorldRotation joint))
   2.210 +
   2.211 +                         
   2.212 +                         
   2.213 +                         (.toRotationMatrix
   2.214 +                          (.fromAngleAxis
   2.215 +                           (Quaternion.)
   2.216 +                           (.angleBetween Vector3f/UNIT_X pivot-a)
   2.217 +                           (.cross Vector3f/UNIT_X pivot-a)))
   2.218 +                         
   2.219 +
   2.220 +                         
   2.221 +                         
   2.222 +                         ;; (doto (Matrix3f.)
   2.223 +                         ;;   (.fromStartEndVectors
   2.224 +                         ;;    Vector3f/UNIT_X
   2.225 +                         ;;    (.normalize
   2.226 +                         ;;     vector-1)))
   2.227 +                         
   2.228 +                         ;; (doto (Matrix3f.)
   2.229 +                         ;;   (.fromStartEndVectors
   2.230 +                         ;;    Vector3f/UNIT_X
   2.231 +                         ;;    vector-2))
   2.232 +                         )
   2.233 +                      (.setLimit (float limit-xz)
   2.234 +                                 (float limit-xy)
   2.235 +                                 (float twist))))))
   2.236 +              true
   2.237 +              (println-repl
   2.238 +               "joint-type" joint-type "not recognized")))
   2.239 +      
   2.240 +      (println-repl "could not find joint meta-data!"))))
   2.241  
   2.242  (defn assemble-creature [#^Node pieces joints]
   2.243    (dorun
   2.244 @@ -288,7 +296,7 @@
   2.245          (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
   2.246    (world
   2.247     (nodify [(blender-creature thing)
   2.248 -            (box 10 2 10 :position (Vector3f. 0 -5.5 0)
   2.249 +            (box 10 2 10 :position (Vector3f. 0 -9 0)
   2.250                   :color ColorRGBA/Gray :mass 0)
   2.251              x-axis y-axis z-axis
   2.252              ])