diff org/test-creature.org @ 160:33278bf028e7

refactored joints
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:47:05 -0700
parents 75b6c2ebbf8e
children e401dafa5966
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Fri Feb 03 06:41:16 2012 -0700
     1.2 +++ b/org/test-creature.org	Fri Feb 03 06:47:05 2012 -0700
     1.3 @@ -71,164 +71,14 @@
     1.4       (.registerLoader BlenderModelLoader (into-array String ["blend"])))
     1.5     model))
     1.6  
     1.7 -(defn blender-to-jme
     1.8 -  "Convert from Blender coordinates to JME coordinates"
     1.9 -  [#^Vector3f in]
    1.10 -  (Vector3f. (.getX in)
    1.11 -             (.getZ in)
    1.12 -             (- (.getY in))))
    1.13 +(declare blender-creature)
    1.14  
    1.15 -
    1.16 -(defmulti joint-dispatch
    1.17 -  "Translate blender pseudo-joints into real JME joints."
    1.18 -  (fn [constraints & _] 
    1.19 -    (:type constraints)))
    1.20 -
    1.21 -(defmethod joint-dispatch :point
    1.22 -  [constraints control-a control-b pivot-a pivot-b rotation]
    1.23 -  (println-repl "creating POINT2POINT joint")
    1.24 -  ;; bullet's point2point joints are BROKEN, so we must use the
    1.25 -  ;; generic 6DOF joint instead of an actual Point2Point joint!
    1.26 -
    1.27 -  ;; should be able to do this:
    1.28 -  (comment 
    1.29 -    (Point2PointJoint.
    1.30 -     control-a
    1.31 -     control-b
    1.32 -     pivot-a
    1.33 -     pivot-b))
    1.34 -
    1.35 -  ;; but instead we must do this:
    1.36 -  (println-repl "substuting 6DOF joint for POINT2POINT joint!")
    1.37 -  (doto
    1.38 -      (SixDofJoint.
    1.39 -       control-a
    1.40 -       control-b
    1.41 -       pivot-a
    1.42 -       pivot-b
    1.43 -       false)
    1.44 -    (.setLinearLowerLimit Vector3f/ZERO)
    1.45 -    (.setLinearUpperLimit Vector3f/ZERO)
    1.46 -    ;;(.setAngularLowerLimit (Vector3f. 1 1 1))
    1.47 -    ;;(.setAngularUpperLimit (Vector3f. 0 0 0))
    1.48 -
    1.49 -))
    1.50 -
    1.51 -
    1.52 -(defmethod joint-dispatch :hinge
    1.53 -  [constraints control-a control-b pivot-a pivot-b rotation]
    1.54 -  (println-repl "creating HINGE joint")
    1.55 -  (let [axis
    1.56 -        (if-let
    1.57 -            [axis (:axis constraints)]
    1.58 -          axis
    1.59 -          Vector3f/UNIT_X)
    1.60 -        [limit-1 limit-2] (:limit constraints)
    1.61 -        hinge-axis
    1.62 -        (.mult
    1.63 -         rotation
    1.64 -         (blender-to-jme axis))]
    1.65 -    (doto
    1.66 -        (HingeJoint.
    1.67 -         control-a
    1.68 -         control-b
    1.69 -         pivot-a
    1.70 -         pivot-b
    1.71 -         hinge-axis
    1.72 -         hinge-axis)
    1.73 -      (.setLimit limit-1 limit-2))))
    1.74 -
    1.75 -(defmethod joint-dispatch :cone
    1.76 -  [constraints control-a control-b pivot-a pivot-b rotation]
    1.77 -  (let [limit-xz (:limit-xz constraints)
    1.78 -        limit-xy (:limit-xy constraints)
    1.79 -        twist    (:twist constraints)]
    1.80 -    
    1.81 -    (println-repl "creating CONE joint")
    1.82 -    (println-repl rotation)
    1.83 -    (println-repl
    1.84 -     "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
    1.85 -    (println-repl
    1.86 -     "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
    1.87 -    (println-repl
    1.88 -     "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
    1.89 -    (doto
    1.90 -        (ConeJoint.
    1.91 -         control-a
    1.92 -         control-b
    1.93 -         pivot-a
    1.94 -         pivot-b
    1.95 -         rotation
    1.96 -         rotation)
    1.97 -      (.setLimit (float limit-xz)
    1.98 -                 (float limit-xy)
    1.99 -                 (float twist)))))
   1.100 -
   1.101 -(defn connect
   1.102 -  "here are some examples:
   1.103 -   {:type :point}
   1.104 -   {:type :hinge  :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
   1.105 -   (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
   1.106 -
   1.107 -   {:type :cone :limit-xz 0]
   1.108 -                :limit-xy 0]
   1.109 -                :twist 0]}   (use XZY rotation mode in blender!)"
   1.110 -  [#^Node obj-a #^Node obj-b #^Node joint]
   1.111 -  (let [control-a (.getControl obj-a RigidBodyControl)
   1.112 -        control-b (.getControl obj-b RigidBodyControl)
   1.113 -        joint-center (.getWorldTranslation joint)
   1.114 -        joint-rotation (.toRotationMatrix (.getWorldRotation joint))
   1.115 -        pivot-a (world-to-local obj-a joint-center)
   1.116 -        pivot-b (world-to-local obj-b joint-center)]
   1.117 -   
   1.118 -    (if-let [constraints
   1.119 -             (map-vals
   1.120 -              eval
   1.121 -              (read-string
   1.122 -               (meta-data joint "joint")))]
   1.123 -      ;; A side-effect of creating a joint registers
   1.124 -      ;; it with both physics objects which in turn
   1.125 -      ;; will register the joint with the physics system
   1.126 -      ;; when the simulation is started.
   1.127 -      (do
   1.128 -        (println-repl "creating joint between"
   1.129 -                      (.getName obj-a) "and" (.getName obj-b))
   1.130 -        (joint-dispatch constraints
   1.131 -                        control-a control-b
   1.132 -                        pivot-a pivot-b
   1.133 -                        joint-rotation))
   1.134 -      (println-repl "could not find joint meta-data!"))))
   1.135 -
   1.136 -
   1.137 -
   1.138 -
   1.139 -(defn assemble-creature [#^Node pieces joints]
   1.140 -  (dorun
   1.141 -   (map
   1.142 -    (fn [geom]
   1.143 -      (let [physics-control
   1.144 -            (RigidBodyControl.
   1.145 -             (HullCollisionShape.
   1.146 -              (.getMesh geom))
   1.147 -             (if-let [mass (meta-data geom "mass")]
   1.148 -               (do
   1.149 -                 (println-repl
   1.150 -                  "setting" (.getName geom) "mass to" (float mass))
   1.151 -                 (float mass))
   1.152 -               (float 1)))]
   1.153 -               
   1.154 -        (.addControl geom physics-control)))
   1.155 -    (filter #(isa? (class %) Geometry )
   1.156 -            (node-seq pieces))))
   1.157 -  (dorun
   1.158 -   (map
   1.159 -    (fn [joint]
   1.160 -      (let [[obj-a obj-b] (joint-targets pieces joint)]
   1.161 -        (connect obj-a obj-b joint)))
   1.162 -    joints))
   1.163 -  pieces)
   1.164 -
   1.165 -(declare blender-creature)
   1.166 +(defn blender-creature
   1.167 +  "Return a creature with all joints in place."
   1.168 +  [blender-path]
   1.169 +  (let [model (load-blender-model blender-path)
   1.170 +        joints (creature-joints model)]
   1.171 +    (assemble-creature model joints)))
   1.172  
   1.173  (def hand "Models/creature1/one.blend")
   1.174  
   1.175 @@ -271,12 +121,6 @@
   1.176  
   1.177  
   1.178  
   1.179 -(defn blender-creature
   1.180 -  "Return a creature with all joints in place."
   1.181 -  [blender-path]
   1.182 -  (let [model (load-blender-model blender-path)
   1.183 -        joints (creature-joints model)]
   1.184 -    (assemble-creature model joints)))
   1.185  
   1.186  (defn gray-scale [num]
   1.187    (+ num