comparison 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
comparison
equal deleted inserted replaced
87:af1bb43661f9 88:3e929630a25f
121 "Convert the world coordinates into coordinates relative to the 121 "Convert the world coordinates into coordinates relative to the
122 object (i.e. local coordinates), taking into account the rotation 122 object (i.e. local coordinates), taking into account the rotation
123 of object." 123 of object."
124 [#^Spatial object world-coordinate] 124 [#^Spatial object world-coordinate]
125 (let [out (Vector3f.)] 125 (let [out (Vector3f.)]
126 (.worldToLocal object world-coordinate out) out)) 126 (.worldToLocal object world-coordinate out) out))
127
128 (defmulti zz
129 (fn [a b _ _ _ _ _]
130
131 (:type a)))
132 (defmethod zz :p [a b] (println "hi"))
133
134 127
135 (defmulti joint-dispatch 128 (defmulti joint-dispatch
136 "Translate blender pseudo-joints into real JME joints." 129 "Translate blender pseudo-joints into real JME joints."
137 (fn [constraints _ _ _ _ _] 130 (fn [constraints & _]
138 (:type constraints))) 131 (:type constraints)))
139 132
140 (defmethod joint-dispatch :point 133 (defmethod joint-dispatch :point
141 [constraints control-a control-b pivot-a pivot-b rotation] 134 [constraints control-a control-b pivot-a pivot-b rotation]
142 (println-repl "creating POINT2POINT joint") 135 (println-repl "creating POINT2POINT joint")
197 (float limit-xy) 190 (float limit-xy)
198 (float twist))))) 191 (float twist)))))
199 192
200 193
201 194
202 (defn connect* 195 (defn connect
203 "here are some examples: 196 "here are some examples:
204 {:type :point} 197 {:type :point}
205 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} 198 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
206 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) 199 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
207 200
232 pivot-a pivot-b 225 pivot-a pivot-b
233 joint-rotation)) 226 joint-rotation))
234 227
235 (println-repl "could not find joint meta-data!")))) 228 (println-repl "could not find joint meta-data!"))))
236 229
237
238
239
240 (defn connect
241 "here are some examples:
242 {:type :point}
243 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
244 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
245
246 {:type :cone :limit-xz 0]
247 :limit-xy 0]
248 :twist 0]} (use XZY rotation mode in blender!)"
249 [#^Node obj-a #^Node obj-b #^Node joint]
250 (let [control-a (.getControl obj-a RigidBodyControl)
251 control-b (.getControl obj-b RigidBodyControl)
252 joint-center (.getWorldTranslation joint)
253 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
254 pivot-a (world-to-local obj-a joint-center)
255 pivot-b (world-to-local obj-b joint-center)]
256 ;; A side-effect of creating a joint registers
257 ;; it with both physics objects which in turn
258 ;; will register the joint with the physics system
259 ;; when the simulation is started.
260 (if-let [constraints
261 (map-vals
262 eval
263 (read-string
264 (meta-data joint "joint")))]
265
266 (let [joint-type (:type constraints)]
267 (println-repl "creating joint between"
268 (.getName obj-a) "and" (.getName obj-b))
269 (cond (= :point joint-type)
270 (do
271 (println-repl "creating POINT joint")
272 (Point2PointJoint.
273 control-a
274 control-b
275 pivot-a
276 pivot-b))
277 (= :hinge joint-type)
278 (do
279 (println-repl "creating HINGE joint")
280 (let [axis (if-let
281 [axis (:axis constraints)]
282 axis
283 Vector3f/UNIT_X)
284 [limit-1 limit-2] (:limit constraints)
285 hinge-axis
286 (.mult
287 (.getWorldRotation joint)
288 (blender-to-jme axis))]
289 (doto
290 (HingeJoint.
291 control-a
292 control-b
293 pivot-a
294 pivot-b
295 hinge-axis
296 hinge-axis)
297 (.setLimit limit-1 limit-2))))
298 (= :cone joint-type)
299 (do
300 (let [limit-xz (:limit-xz constraints)
301 limit-xy (:limit-xy constraints)
302 twist (:twist constraints)]
303
304
305 (println-repl "creating CONE joint")
306 (println-repl joint-rotation)
307 (println-repl
308 "UNIT_X --> " (.mult joint-rotation (Vector3f. 1 0 0)))
309 (println-repl
310 "UNIT_Y --> " (.mult joint-rotation (Vector3f. 0 1 0)))
311 (println-repl
312 "UNIT_Z --> " (.mult joint-rotation (Vector3f. 0 0 1)))
313 (doto
314 (ConeJoint.
315 control-a
316 control-b
317 pivot-a
318 pivot-b
319 joint-rotation
320 joint-rotation
321 )
322 (.setLimit (float limit-xz)
323 (float limit-xy)
324 (float twist)))))
325 true
326 (println-repl
327 "joint-type" joint-type "not recognized")))
328
329 (println-repl "could not find joint meta-data!"))))
330
331
332 (defn assemble-creature [#^Node pieces joints] 230 (defn assemble-creature [#^Node pieces joints]
333 (dorun 231 (dorun
334 (map 232 (map
335 (fn [geom] 233 (fn [geom]
336 (let [physics-control 234 (let [physics-control
351 (dorun 249 (dorun
352 (map 250 (map
353 (fn [joint] 251 (fn [joint]
354 (let [[obj-a obj-b] 252 (let [[obj-a obj-b]
355 (joint-targets pieces joint)] 253 (joint-targets pieces joint)]
356 (connect* obj-a obj-b joint))) 254 (connect obj-a obj-b joint)))
357 joints)) 255 joints))
358 pieces) 256 pieces)
359 257
360 (defn blender-creature [blender-path] 258 (defn blender-creature [blender-path]
361 (let [model (load-blender-model blender-path) 259 (let [model (load-blender-model blender-path)