Mercurial > cortex
comparison org/test-creature.org @ 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 |
comparison
equal
deleted
inserted
replaced
73:257a86328adb | 74:fb810a2c50c2 |
---|---|
59 (.loadModel | 59 (.loadModel |
60 (doto (asset-manager) | 60 (doto (asset-manager) |
61 (.registerLoader BlenderModelLoader (into-array String ["blend"]))) | 61 (.registerLoader BlenderModelLoader (into-array String ["blend"]))) |
62 model)) | 62 model)) |
63 | 63 |
64 | 64 (defn meta-data [blender-node key] |
65 (defn apply-skeleton | 65 (if-let [data (.getUserData blender-node "properties")] |
66 "Given an imported blender model, apply the armature using the | 66 (.findValue data key) |
67 following pattern: if two bones are connected and have the same | 67 nil)) |
68 names as two shapes, connect the shapes with a joint constraint." | 68 |
69 [armature-name creature] | 69 (defn hand2 [] |
70 (load-blender-model "Models/creature1/try-again.blend")) | |
71 | |
72 (defn hand [] | |
73 (load-blender-model "Models/creature1/one.blend")) | |
74 | |
75 | |
70 | 76 |
77 (def hand-names | |
78 #{ | |
79 "middle-1" | |
80 "middle-2" | |
81 "middle-3" | |
82 "palm" | |
83 "pinky-1" | |
84 "pinky-2" | |
85 "pinky-3" | |
86 "pointer-1" | |
87 "pointer-2" | |
88 "pointer-3" | |
89 "ring-1" | |
90 "ring-2" | |
91 "ring-3" | |
92 "thumb-1" | |
93 "thumb-2"}) | |
94 | |
95 (defn hand-pieces [] | |
96 (filter | |
97 (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand)))) | |
98 | |
99 (defn hand-joints [] | |
100 (map #(.getWorldTranslation %) | |
101 (filter #(re-matches #"joint\.\d\d\d" (.getName %)) | |
102 (node-seq (hand))))) | |
103 | |
104 (defn worm-pieces [] | |
105 (filter | |
106 (comp #{"worm-2" "worm-1"} | |
107 #(apply str (drop-last (.getName %)))) | |
108 (node-seq (hand2)))) | |
109 | |
110 (defn worm-joints [] | |
111 [Vector3f/ZERO]) | |
112 | |
113 | |
114 | |
115 (defn find-joint | |
116 [#^Node parts #^Vector3f joint-position] | |
117 (loop [radius (float 0.01)] | |
118 (let [results (CollisionResults.)] | |
119 (.collideWith | |
120 parts | |
121 (BoundingBox. joint-position radius radius radius) | |
122 results) | |
123 (let [targets | |
124 (distinct | |
125 (map #(.getGeometry %) results))] | |
126 (if (>= (count targets) 2) | |
127 (take 2 targets) | |
128 (recur (float (* radius 2)))))))) | |
129 | |
130 | |
131 | |
132 (defn connect-at-point | |
133 [obj-a obj-b point] | |
134 (let [center-a (.getWorldTranslation obj-a) | |
135 center-b (.getWorldTranslation obj-b) | |
136 pivot-a (.subtract point center-a) | |
137 pivot-b (.subtract point center-b) | |
138 ;; A side-effect of creating a joint registers | |
139 ;; it with both physics objects which in turn | |
140 ;; will register the joint with the physics system | |
141 ;; when the simulation is started. | |
142 joint (Point2PointJoint. | |
143 (.getControl obj-a RigidBodyControl) | |
144 (.getControl obj-b RigidBodyControl) | |
145 pivot-a | |
146 pivot-b)] | |
147 obj-a)) | |
148 | |
149 | |
150 (defn physical-hand [#^Node pieces joints] | |
151 ;; Make each piece a physical entity in the simulation. | |
152 (dorun | |
153 (map | |
154 (fn [geom] | |
155 (let [physics-control | |
156 (RigidBodyControl. | |
157 (HullCollisionShape. | |
158 (.getMesh geom)) | |
159 ;; TODO: fix this. | |
160 (float 1.0))] | |
161 (.addControl geom physics-control))) | |
162 (filter #(isa? (class %) Geometry ) | |
163 (node-seq pieces)))) | |
164 (dorun | |
165 (map | |
166 (fn [joint-position] | |
167 (let [[geom-a geom-b] (find-joint pieces joint-position)] | |
168 (connect-at-point geom-a geom-b joint-position))) | |
169 joints)) | |
170 pieces) | |
171 | |
172 | |
173 (defn the-hand! [] (physical-hand (hand) (hand-joints))) | |
174 | |
175 | |
176 (defn test-hand [] | |
177 (world | |
178 (nodify [(the-hand!) | |
179 (box 10 0.1 10 :position (Vector3f. 0 -10 0) | |
180 :color ColorRGBA/Gray | |
181 :mass 0)]) | |
182 standard-debug-controls | |
183 enable-debug | |
184 no-op)) | |
185 | |
186 | |
187 | |
188 | |
189 | |
71 | 190 |
72 | 191 |
73 #+end_src | 192 #+end_src |
74 | 193 |
75 | 194 |