Mercurial > cortex
diff org/test-creature.org @ 88:3e929630a25f
multimethod dispatch works
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 07 Jan 2012 05:24:14 -0700 |
parents | af1bb43661f9 |
children | cd5151b5e7c3 |
line wrap: on
line diff
1.1 --- a/org/test-creature.org Sat Jan 07 05:06:58 2012 -0700 1.2 +++ b/org/test-creature.org Sat Jan 07 05:24:14 2012 -0700 1.3 @@ -123,18 +123,11 @@ 1.4 of object." 1.5 [#^Spatial object world-coordinate] 1.6 (let [out (Vector3f.)] 1.7 - (.worldToLocal object world-coordinate out) out)) 1.8 - 1.9 -(defmulti zz 1.10 - (fn [a b _ _ _ _ _] 1.11 - 1.12 - (:type a))) 1.13 -(defmethod zz :p [a b] (println "hi")) 1.14 - 1.15 + (.worldToLocal object world-coordinate out) out)) 1.16 1.17 (defmulti joint-dispatch 1.18 "Translate blender pseudo-joints into real JME joints." 1.19 - (fn [constraints _ _ _ _ _] 1.20 + (fn [constraints & _] 1.21 (:type constraints))) 1.22 1.23 (defmethod joint-dispatch :point 1.24 @@ -199,7 +192,7 @@ 1.25 1.26 1.27 1.28 -(defn connect* 1.29 +(defn connect 1.30 "here are some examples: 1.31 {:type :point} 1.32 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} 1.33 @@ -234,101 +227,6 @@ 1.34 1.35 (println-repl "could not find joint meta-data!")))) 1.36 1.37 - 1.38 - 1.39 - 1.40 -(defn connect 1.41 - "here are some examples: 1.42 - {:type :point} 1.43 - {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} 1.44 - (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) 1.45 - 1.46 - {:type :cone :limit-xz 0] 1.47 - :limit-xy 0] 1.48 - :twist 0]} (use XZY rotation mode in blender!)" 1.49 - [#^Node obj-a #^Node obj-b #^Node joint] 1.50 - (let [control-a (.getControl obj-a RigidBodyControl) 1.51 - control-b (.getControl obj-b RigidBodyControl) 1.52 - joint-center (.getWorldTranslation joint) 1.53 - joint-rotation (.toRotationMatrix (.getWorldRotation joint)) 1.54 - pivot-a (world-to-local obj-a joint-center) 1.55 - pivot-b (world-to-local obj-b joint-center)] 1.56 - ;; A side-effect of creating a joint registers 1.57 - ;; it with both physics objects which in turn 1.58 - ;; will register the joint with the physics system 1.59 - ;; when the simulation is started. 1.60 - (if-let [constraints 1.61 - (map-vals 1.62 - eval 1.63 - (read-string 1.64 - (meta-data joint "joint")))] 1.65 - 1.66 - (let [joint-type (:type constraints)] 1.67 - (println-repl "creating joint between" 1.68 - (.getName obj-a) "and" (.getName obj-b)) 1.69 - (cond (= :point joint-type) 1.70 - (do 1.71 - (println-repl "creating POINT joint") 1.72 - (Point2PointJoint. 1.73 - control-a 1.74 - control-b 1.75 - pivot-a 1.76 - pivot-b)) 1.77 - (= :hinge joint-type) 1.78 - (do 1.79 - (println-repl "creating HINGE joint") 1.80 - (let [axis (if-let 1.81 - [axis (:axis constraints)] 1.82 - axis 1.83 - Vector3f/UNIT_X) 1.84 - [limit-1 limit-2] (:limit constraints) 1.85 - hinge-axis 1.86 - (.mult 1.87 - (.getWorldRotation joint) 1.88 - (blender-to-jme axis))] 1.89 - (doto 1.90 - (HingeJoint. 1.91 - control-a 1.92 - control-b 1.93 - pivot-a 1.94 - pivot-b 1.95 - hinge-axis 1.96 - hinge-axis) 1.97 - (.setLimit limit-1 limit-2)))) 1.98 - (= :cone joint-type) 1.99 - (do 1.100 - (let [limit-xz (:limit-xz constraints) 1.101 - limit-xy (:limit-xy constraints) 1.102 - twist (:twist constraints)] 1.103 - 1.104 - 1.105 - (println-repl "creating CONE joint") 1.106 - (println-repl joint-rotation) 1.107 - (println-repl 1.108 - "UNIT_X --> " (.mult joint-rotation (Vector3f. 1 0 0))) 1.109 - (println-repl 1.110 - "UNIT_Y --> " (.mult joint-rotation (Vector3f. 0 1 0))) 1.111 - (println-repl 1.112 - "UNIT_Z --> " (.mult joint-rotation (Vector3f. 0 0 1))) 1.113 - (doto 1.114 - (ConeJoint. 1.115 - control-a 1.116 - control-b 1.117 - pivot-a 1.118 - pivot-b 1.119 - joint-rotation 1.120 - joint-rotation 1.121 - ) 1.122 - (.setLimit (float limit-xz) 1.123 - (float limit-xy) 1.124 - (float twist))))) 1.125 - true 1.126 - (println-repl 1.127 - "joint-type" joint-type "not recognized"))) 1.128 - 1.129 - (println-repl "could not find joint meta-data!")))) 1.130 - 1.131 - 1.132 (defn assemble-creature [#^Node pieces joints] 1.133 (dorun 1.134 (map 1.135 @@ -353,7 +251,7 @@ 1.136 (fn [joint] 1.137 (let [[obj-a obj-b] 1.138 (joint-targets pieces joint)] 1.139 - (connect* obj-a obj-b joint))) 1.140 + (connect obj-a obj-b joint))) 1.141 joints)) 1.142 pieces) 1.143