Mercurial > cortex
comparison org/test-creature.org @ 78:77b506ac64f3
trying to make offset cone joints
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 04 Jan 2012 20:34:00 -0700 |
parents | 1f84f425e05d |
children | 01dbdb0d5500 |
comparison
equal
deleted
inserted
replaced
77:1f84f425e05d | 78:77b506ac64f3 |
---|---|
46 ;; TODO remove this! | 46 ;; TODO remove this! |
47 (require 'cortex.import) | 47 (require 'cortex.import) |
48 (cortex.import/mega-import-jme3) | 48 (cortex.import/mega-import-jme3) |
49 (use '(cortex world util body hearing touch vision)) | 49 (use '(cortex world util body hearing touch vision)) |
50 | 50 |
51 (use '[clojure.contrib [seq :only [find-first]]]) | |
52 | |
53 | |
54 (rlm.rlm-commands/help) | 51 (rlm.rlm-commands/help) |
55 | 52 |
56 (defn load-blender-model | 53 (defn load-blender-model |
57 "Load a .blend file using an asset folder relative path." | 54 "Load a .blend file using an asset folder relative path." |
58 [^String model] | 55 [^String model] |
64 (defn meta-data [blender-node key] | 61 (defn meta-data [blender-node key] |
65 (if-let [data (.getUserData blender-node "properties")] | 62 (if-let [data (.getUserData blender-node "properties")] |
66 (.findValue data key) | 63 (.findValue data key) |
67 nil)) | 64 nil)) |
68 | 65 |
69 | |
70 (defn hand [] | |
71 (load-blender-model "Models/creature1/one.blend")) | |
72 | |
73 | |
74 | |
75 (def hand-names | |
76 #{ | |
77 "middle-1" | |
78 "middle-2" | |
79 "middle-3" | |
80 "palm" | |
81 "pinky-1" | |
82 "pinky-2" | |
83 "pinky-3" | |
84 "pointer-1" | |
85 "pointer-2" | |
86 "pointer-3" | |
87 "ring-1" | |
88 "ring-2" | |
89 "ring-3" | |
90 "thumb-1" | |
91 "thumb-2"}) | |
92 | |
93 (defn hand-pieces [] | |
94 (filter | |
95 (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand)))) | |
96 | |
97 (defn hand-joints [] | |
98 (map #(.getWorldTranslation %) | |
99 (filter #(re-matches #"joint\.\d\d\d" (.getName %)) | |
100 (node-seq (hand))))) | |
101 (defn worm [] | |
102 (load-blender-model "Models/creature1/try-again.blend")) | |
103 | |
104 | |
105 (defn worm-pieces [] | |
106 (filter | |
107 (comp #{"worm-2" "worm-1"} | |
108 #(apply str (drop-last (.getName %)))) | |
109 (node-seq (worm)))) | |
110 | |
111 (defn worm-joints [] | |
112 (filter #(re-matches #"joint\.\d\d\d" (.getName %)) | |
113 (node-seq (worm)))) | |
114 | |
115 (defn bullet-trans [] | |
116 (let [obj-a (sphere 0.5 :color ColorRGBA/Red | |
117 :position (Vector3f. -10 5 0)) | |
118 obj-b (sphere 0.5 :color ColorRGBA/Blue | |
119 :position (Vector3f. -10 -5 0) | |
120 :mass 0) | |
121 control-a (.getControl obj-a RigidBodyControl) | |
122 control-b (.getControl obj-b RigidBodyControl) | |
123 swivel | |
124 (.toRotationMatrix | |
125 (doto (Quaternion.) | |
126 (.fromAngleAxis (/ Math/PI 2) | |
127 Vector3f/UNIT_X)))] | |
128 (doto | |
129 (ConeJoint. | |
130 control-a control-b | |
131 (Vector3f. 0 5 0) | |
132 (Vector3f. 0 -5 0) | |
133 swivel swivel) | |
134 (.setLimit (* 0.6 (/ Math/PI 4)) | |
135 (/ Math/PI 4) | |
136 (* Math/PI 0.8))) | |
137 (world (nodify | |
138 [obj-a obj-b]) | |
139 standard-debug-controls | |
140 enable-debug | |
141 no-op))) | |
142 | |
143 | |
144 (defn bullet-trans* [] | |
145 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red | |
146 :position (Vector3f. 5 0 0) | |
147 :mass 90) | |
148 obj-b (sphere 0.5 :color ColorRGBA/Blue | |
149 :position (Vector3f. -5 0 0) | |
150 :mass 0) | |
151 control-a (.getControl obj-a RigidBodyControl) | |
152 control-b (.getControl obj-b RigidBodyControl) | |
153 move-up? (atom nil) | |
154 move-down? (atom nil) | |
155 move-left? (atom nil) | |
156 move-right? (atom nil) | |
157 roll-left? (atom nil) | |
158 roll-right? (atom nil) | |
159 force 100 | |
160 swivel | |
161 (.toRotationMatrix | |
162 (doto (Quaternion.) | |
163 (.fromAngleAxis (/ Math/PI 2) | |
164 Vector3f/UNIT_X))) | |
165 x-move | |
166 (doto (Matrix3f.) | |
167 (.fromStartEndVectors Vector3f/UNIT_X | |
168 (.normalize (Vector3f. 1 1 0)))) | |
169 | |
170 timer (atom 0)] | |
171 (doto | |
172 (ConeJoint. | |
173 control-a control-b | |
174 (Vector3f. -8 0 0) | |
175 (Vector3f. 2 0 0) | |
176 ;;swivel swivel | |
177 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY | |
178 x-move Matrix3f/IDENTITY | |
179 ) | |
180 (.setCollisionBetweenLinkedBodys false) | |
181 (.setLimit (* 1 (/ Math/PI 4)) ;; twist | |
182 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane | |
183 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane | |
184 (world (nodify | |
185 [obj-a obj-b]) | |
186 (merge standard-debug-controls | |
187 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) | |
188 "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) | |
189 "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) | |
190 "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) | |
191 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) | |
192 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) | |
193 | |
194 (fn [world] | |
195 (enable-debug world) | |
196 (set-gravity world Vector3f/ZERO) | |
197 ) | |
198 | |
199 (fn [world _] | |
200 | |
201 (if @move-up? | |
202 (.applyForce control-a | |
203 (Vector3f. force 0 0) | |
204 (Vector3f. 0 0 0))) | |
205 (if @move-down? | |
206 (.applyForce control-a | |
207 (Vector3f. (- force) 0 0) | |
208 (Vector3f. 0 0 0))) | |
209 (if @move-left? | |
210 (.applyForce control-a | |
211 (Vector3f. 0 force 0) | |
212 (Vector3f. 0 0 0))) | |
213 (if @move-right? | |
214 (.applyForce control-a | |
215 (Vector3f. 0 (- force) 0) | |
216 (Vector3f. 0 0 0))) | |
217 | |
218 (if @roll-left? | |
219 (.applyForce control-a | |
220 (Vector3f. 0 0 force) | |
221 (Vector3f. 0 0 0))) | |
222 (if @roll-right? | |
223 (.applyForce control-a | |
224 (Vector3f. 0 0 (- force)) | |
225 (Vector3f. 0 0 0))) | |
226 | |
227 (if (zero? (rem (swap! timer inc) 100)) | |
228 (.attachChild | |
229 (.getRootNode world) | |
230 (sphere 0.05 :color ColorRGBA/Yellow | |
231 :physical? false :position | |
232 (.getWorldTranslation obj-a))))) | |
233 ) | |
234 )) | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 (defn world-setup [joint] | |
247 (let [top (doto | |
248 (sphere 0.1 :physical? false :color ColorRGBA/Yellow | |
249 :position (Vector3f. 0 7 0)) | |
250 (.addControl | |
251 (RigidBodyControl. | |
252 (CapsuleCollisionShape. 0.5 1.5 1) (float 15)))) | |
253 bottom (doto | |
254 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray | |
255 :position (Vector3f. 0 -1 0)) | |
256 (.addControl | |
257 (RigidBodyControl. | |
258 (CapsuleCollisionShape. 0.5 1.5 1) (float 0)))) | |
259 table (box 10 2 10 :position (Vector3f. 0 -6 0) | |
260 :color ColorRGBA/Gray :mass 0) | |
261 a (.getControl top RigidBodyControl) | |
262 b (.getControl bottom RigidBodyControl)] | |
263 (cond | |
264 (= joint :point) | |
265 (doto | |
266 (Point2PointJoint. a b | |
267 (Vector3f. 0 -2 0) | |
268 (Vector3f. 0 2 0)) | |
269 (.setCollisionBetweenLinkedBodys false)) | |
270 (= joint :hinge) | |
271 (doto | |
272 (HingeJoint. | |
273 a b | |
274 (Vector3f. 0 -2 0) | |
275 (Vector3f. 0 2 0) | |
276 (Vector3f. 0 0 1) | |
277 (Vector3f. 0 0 1) | |
278 | |
279 ) | |
280 (.setCollisionBetweenLinkedBodys false) | |
281 ;;(.setLimit (- Math/PI) Math/PI) | |
282 ) | |
283 (= joint :cone) | |
284 ;; note to self -- jbullet does NOT implement cone joints | |
285 ;; correctly. You must use plain ol' bullet for this to work. | |
286 ;; It's faster anyway, so whatever. | |
287 | |
288 (doto (ConeJoint. | |
289 a b | |
290 (Vector3f. 0 -5 0) | |
291 (Vector3f. 0 2 0) | |
292 | |
293 (doto (Matrix3f.) | |
294 (.fromStartEndVectors Vector3f/UNIT_X | |
295 Vector3f/UNIT_Y)) | |
296 (doto (Matrix3f.) | |
297 (.fromStartEndVectors Vector3f/UNIT_X | |
298 Vector3f/UNIT_Y)) | |
299 ) | |
300 ;;(.setAngularOnly true) | |
301 | |
302 (.setCollisionBetweenLinkedBodys false) | |
303 (.setLimit (* 1 (/ Math/PI 4)) | |
304 (* 1 (/ Math/PI 4)) | |
305 (* 0 (/ Math/PI 4))) | |
306 | |
307 ) | |
308 (= joint :six) | |
309 (doto | |
310 (SixDofJoint. | |
311 a b | |
312 (Vector3f. 0 -2 0) | |
313 (Vector3f. 0 2 0) | |
314 ;;(doto (Matrix3f.) | |
315 ;; (.fromStartEndVectors Vector3f/UNIT_X | |
316 ;; Vector3f/UNIT_Y)) | |
317 ;;(doto (Matrix3f.) | |
318 ;; (.fromStartEndVectors Vector3f/UNIT_X | |
319 ;; Vector3f/UNIT_Y)) | |
320 true) | |
321 (.setCollisionBetweenLinkedBodys false) | |
322 (.setAngularLowerLimit (Vector3f. 0 | |
323 (- (/ Math/PI 2)) | |
324 0)) | |
325 | |
326 (.setAngularUpperLimit (Vector3f. 0 | |
327 (/ Math/PI 2) | |
328 0)) | |
329 (.setLinearLowerLimit (Vector3f. 0 0 0)) | |
330 (.setLinearUpperLimit (Vector3f. 0 0 0)) | |
331 | |
332 ) | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 ) | |
339 | |
340 [top bottom table])) | |
341 | |
342 (defn speed-up [world] | |
343 (.setMoveSpeed (.getFlyByCamera world) | |
344 (float 100)) | |
345 (.setRotationSpeed (.getFlyByCamera world) | |
346 (float 20)) | |
347 world) | |
348 | |
349 | |
350 (defn test-joint [joint] | |
351 (let [[top bottom floor] (world-setup joint) | |
352 control (.getControl top RigidBodyControl) | |
353 move-up? (atom false) | |
354 move-down? (atom false) | |
355 move-left? (atom false) | |
356 move-right? (atom false) | |
357 roll-left? (atom false) | |
358 roll-right? (atom false) | |
359 timer (atom 0)] | |
360 | |
361 (world | |
362 (nodify [top bottom floor]) | |
363 (merge standard-debug-controls | |
364 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) | |
365 "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) | |
366 "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) | |
367 "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) | |
368 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) | |
369 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) | |
370 | |
371 (fn [world] | |
372 (light-up-everything world) | |
373 (enable-debug world) | |
374 (set-gravity world (Vector3f. 0 0 0)) | |
375 ) | |
376 | |
377 (fn [world _] | |
378 (if (zero? (rem (swap! timer inc) 100)) | |
379 (do | |
380 ;; (println-repl @timer) | |
381 (.attachChild (.getRootNode world) | |
382 (sphere 0.05 :color ColorRGBA/Yellow | |
383 :position (.getWorldTranslation top) | |
384 :physical? false)))) | |
385 (if @move-up? | |
386 (.applyTorque control | |
387 (.mult (.getPhysicsRotation control) | |
388 (Vector3f. 0 0 10)))) | |
389 (if @move-down? | |
390 (.applyTorque control | |
391 (.mult (.getPhysicsRotation control) | |
392 (Vector3f. 0 0 -10)))) | |
393 (if @move-left? | |
394 (.applyTorque control | |
395 (.mult (.getPhysicsRotation control) | |
396 (Vector3f. 0 10 0)))) | |
397 (if @move-right? | |
398 (.applyTorque control | |
399 (.mult (.getPhysicsRotation control) | |
400 (Vector3f. 0 -10 0)))) | |
401 (if @roll-left? | |
402 (.applyTorque control | |
403 (.mult (.getPhysicsRotation control) | |
404 (Vector3f. -1 0 0)))) | |
405 (if @roll-right? | |
406 (.applyTorque control | |
407 (.mult (.getPhysicsRotation control) | |
408 (Vector3f. 1 0 0)))))))) | |
409 | |
410 | |
411 | |
412 (defn run [joint] (.start (test-joint joint))) | |
413 (defn look [joint] (view (nodify (world-setup joint)))) | |
414 | |
415 (defn blender-to-jme | 66 (defn blender-to-jme |
416 "Convert from Blender coordinates to JME coordinates" | 67 "Convert from Blender coordinates to JME coordinates" |
417 [#^Vector3f in] | 68 [#^Vector3f in] |
418 (Vector3f. (.getX in) | 69 (Vector3f. (.getX in) |
419 (.getZ in) | 70 (.getZ in) |
420 (- (.getY in)))) | 71 (- (.getY in)))) |
421 | |
422 | 72 |
423 (defn joint-targets | 73 (defn joint-targets |
424 "Return the two closest two objects to the joint object, ordered | 74 "Return the two closest two objects to the joint object, ordered |
425 from bottom to top according to the joint's rotation." | 75 from bottom to top according to the joint's rotation." |
426 [#^Node parts #^Node joint] | 76 [#^Node parts #^Node joint] |
452 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} | 102 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} |
453 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) | 103 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) |
454 | 104 |
455 {:type :cone :limit-xz 0] | 105 {:type :cone :limit-xz 0] |
456 :limit-yz 0] | 106 :limit-yz 0] |
457 :twist 0]} | 107 :twist 0]}" |
458 " | |
459 ([#^Node obj-a #^Node obj-b #^Node joint] | 108 ([#^Node obj-a #^Node obj-b #^Node joint] |
460 (let [center-a (.getWorldTranslation obj-a) | 109 (let [center-a (.getWorldTranslation obj-a) |
461 center-b (.getWorldTranslation obj-b) | 110 center-b (.getWorldTranslation obj-b) |
462 joint-center (.getWorldTranslation joint) | 111 joint-center (.getWorldTranslation joint) |
463 pivot-a (.subtract joint-center center-a) | 112 pivot-a (.subtract joint-center center-a) |
470 ;; when the simulation is started. | 119 ;; when the simulation is started. |
471 (if-let [constraints | 120 (if-let [constraints |
472 (map-vals | 121 (map-vals |
473 eval | 122 eval |
474 (read-string | 123 (read-string |
475 (meta-data (first (worm-joints)) "joint")))] | 124 (meta-data joint "joint")))] |
476 | 125 |
477 (let [joint-type (:type constraints)] | 126 (let [joint-type (:type constraints)] |
127 (println-repl "creating joint between" | |
128 (.getName obj-a) "and" (.getName obj-b)) | |
478 (cond (= :point joint-type) | 129 (cond (= :point joint-type) |
479 (do | 130 (do |
480 (println-repl "creating POINT joint") | 131 (println-repl "creating POINT joint") |
481 (Point2PointJoint. | 132 (Point2PointJoint. |
482 control-a | 133 control-a |
533 (.setLimit (float limit-xy) | 184 (.setLimit (float limit-xy) |
534 (float limit-yz) | 185 (float limit-yz) |
535 (float twist))))) | 186 (float twist))))) |
536 true | 187 true |
537 (println-repl | 188 (println-repl |
538 "joint-type " joint-type " not recognized"))) | 189 "joint-type" joint-type "not recognized"))) |
539 | 190 |
540 (println-repl "could not find joint meta-data!"))))) | 191 (println-repl "could not find joint meta-data!"))))) |
541 | 192 |
542 | 193 (defn assemble-creature [#^Node pieces joints] |
543 | |
544 (defn physical-worm [#^Node pieces joints] | |
545 (dorun | 194 (dorun |
546 (map | 195 (map |
547 (fn [geom] | 196 (fn [geom] |
548 (let [physics-control | 197 (let [physics-control |
549 (RigidBodyControl. | 198 (RigidBodyControl. |
550 (HullCollisionShape. | 199 (HullCollisionShape. |
551 (.getMesh geom)) | 200 (.getMesh geom)) |
552 (if-let [mass (meta-data geom "mass")] | 201 (if-let [mass (meta-data geom "mass")] |
553 (do | 202 (do |
554 (println-repl | 203 (println-repl |
555 "setting mass to " (float mass)) | 204 "setting" (.getName geom) "mass to" (float mass)) |
556 (float mass)) | 205 (float mass)) |
557 (float 1)))] | 206 (float 1)))] |
558 | 207 |
559 (.addControl geom physics-control))) | 208 (.addControl geom physics-control))) |
560 (filter #(isa? (class %) Geometry ) | 209 (filter #(isa? (class %) Geometry ) |
567 (joint-targets pieces joint)] | 216 (joint-targets pieces joint)] |
568 (connect obj-a obj-b joint))) | 217 (connect obj-a obj-b joint))) |
569 joints)) | 218 joints)) |
570 pieces) | 219 pieces) |
571 | 220 |
572 (defn the-worm [] | 221 (defn blender-creature [blender-path] |
573 (physical-worm (worm) (worm-joints))) | 222 (let [model (load-blender-model blender-path) |
574 | 223 joints |
575 (defn test-worm [] | 224 (if-let [joint-node (.getChild model "joints")] |
225 (seq (.getChildren joint-node)) | |
226 (do (println-repl "could not find joints node") | |
227 []))] | |
228 (assemble-creature model joints))) | |
229 | |
230 (def hand "Models/creature1/one.blend") | |
231 | |
232 (def worm "Models/creature1/try-again.blend") | |
233 | |
234 (defn test-creature [thing] | |
576 (world | 235 (world |
577 (nodify [(the-worm) | 236 (nodify [(blender-creature thing) |
578 (box 10 2 10 :position (Vector3f. 0 -5 0) | 237 (box 10 2 10 :position (Vector3f. 0 -5.5 0) |
579 :color ColorRGBA/Gray :mass 0)]) | 238 :color ColorRGBA/Gray :mass 0)]) |
580 standard-debug-controls | 239 standard-debug-controls |
581 (comp light-up-everything enable-debug | 240 (comp light-up-everything enable-debug |
582 (fn [world] | 241 (fn [world] |
583 (.setTimer world (NanoTimer.)) | 242 (.setTimer world (NanoTimer.)) |
243 ;;(set-gravity world (Vector3f. 0 0 0)) | |
584 (speed-up world) | 244 (speed-up world) |
585 ;;(set-gravity world (Vector3f. 0 0 0)) | |
586 world | 245 world |
587 )) | 246 )) |
588 no-op)) | 247 no-op)) |
589 | 248 |
249 (defn world-setup [joint] | |
250 (let [top (doto | |
251 (sphere 0.1 :physical? false :color ColorRGBA/Yellow | |
252 :position (Vector3f. 0 7 0)) | |
253 (.addControl | |
254 (RigidBodyControl. | |
255 (CapsuleCollisionShape. 0.5 1.5 1) (float 15)))) | |
256 bottom (doto | |
257 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray | |
258 :position (Vector3f. 0 -1 0)) | |
259 (.addControl | |
260 (RigidBodyControl. | |
261 (CapsuleCollisionShape. 0.5 1.5 1) (float 0)))) | |
262 table (box 10 2 10 :position (Vector3f. 0 -6 0) | |
263 :color ColorRGBA/Gray :mass 0) | |
264 a (.getControl top RigidBodyControl) | |
265 b (.getControl bottom RigidBodyControl)] | |
266 (cond | |
267 (= joint :point) | |
268 (doto | |
269 (Point2PointJoint. a b | |
270 (Vector3f. 0 -2 0) | |
271 (Vector3f. 0 2 0)) | |
272 (.setCollisionBetweenLinkedBodys false)) | |
273 (= joint :hinge) | |
274 (doto | |
275 (HingeJoint. | |
276 a b | |
277 (Vector3f. 0 -2 0) | |
278 (Vector3f. 0 2 0) | |
279 (Vector3f. 0 0 1) | |
280 (Vector3f. 0 0 1) | |
281 | |
282 ) | |
283 (.setCollisionBetweenLinkedBodys false) | |
284 ;;(.setLimit (- Math/PI) Math/PI) | |
285 ) | |
286 (= joint :cone) | |
287 ;; note to self -- jbullet does NOT implement cone joints | |
288 ;; correctly. You must use plain ol' bullet for this to work. | |
289 ;; It's faster anyway, so whatever. | |
290 | |
291 (doto (ConeJoint. | |
292 a b | |
293 (Vector3f. 0 -5 0) | |
294 (Vector3f. 0 2 0) | |
295 | |
296 (doto (Matrix3f.) | |
297 (.fromStartEndVectors Vector3f/UNIT_X | |
298 Vector3f/UNIT_Y)) | |
299 (doto (Matrix3f.) | |
300 (.fromStartEndVectors Vector3f/UNIT_X | |
301 (.normalize | |
302 (Vector3f. 5 5 0)))) | |
303 ) | |
304 ;;(.setAngularOnly true) | |
305 | |
306 (.setCollisionBetweenLinkedBodys false) | |
307 (.setLimit (* 1 (/ Math/PI 4)) | |
308 (* 1 (/ Math/PI 2)) | |
309 (* 0 (/ Math/PI 4))) | |
310 | |
311 ) | |
312 (= joint :six) | |
313 (doto | |
314 (SixDofJoint. | |
315 a b | |
316 (Vector3f. 0 -2 0) | |
317 (Vector3f. 0 2 0) | |
318 ;;(doto (Matrix3f.) | |
319 ;; (.fromStartEndVectors Vector3f/UNIT_X | |
320 ;; Vector3f/UNIT_Y)) | |
321 ;;(doto (Matrix3f.) | |
322 ;; (.fromStartEndVectors Vector3f/UNIT_X | |
323 ;; Vector3f/UNIT_Y)) | |
324 true) | |
325 (.setCollisionBetweenLinkedBodys false) | |
326 (.setAngularLowerLimit (Vector3f. 0 | |
327 (- (/ Math/PI 2)) | |
328 0)) | |
329 | |
330 (.setAngularUpperLimit (Vector3f. 0 | |
331 (/ Math/PI 2) | |
332 0)) | |
333 (.setLinearLowerLimit (Vector3f. 0 0 0)) | |
334 (.setLinearUpperLimit (Vector3f. 0 0 0)) | |
335 | |
336 ) | |
337 | |
338 | |
339 | |
340 | |
341 | |
342 ) | |
343 | |
344 [top bottom table])) | |
345 | |
346 | |
347 (defn test-joint [joint] | |
348 (let [[top bottom floor] (world-setup joint) | |
349 control (.getControl top RigidBodyControl) | |
350 move-up? (atom false) | |
351 move-down? (atom false) | |
352 move-left? (atom false) | |
353 move-right? (atom false) | |
354 roll-left? (atom false) | |
355 roll-right? (atom false) | |
356 timer (atom 0)] | |
357 | |
358 (world | |
359 (nodify [top bottom floor]) | |
360 (merge standard-debug-controls | |
361 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) | |
362 "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) | |
363 "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) | |
364 "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) | |
365 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) | |
366 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) | |
367 | |
368 (fn [world] | |
369 (light-up-everything world) | |
370 (enable-debug world) | |
371 (set-gravity world (Vector3f. 0 0 0)) | |
372 ) | |
373 | |
374 (fn [world _] | |
375 (if (zero? (rem (swap! timer inc) 100)) | |
376 (do | |
377 ;; (println-repl @timer) | |
378 (.attachChild (.getRootNode world) | |
379 (sphere 0.05 :color ColorRGBA/Yellow | |
380 :position (.getWorldTranslation top) | |
381 :physical? false)))) | |
382 (if @move-up? | |
383 (.applyTorque control | |
384 (.mult (.getPhysicsRotation control) | |
385 (Vector3f. 0 0 10)))) | |
386 (if @move-down? | |
387 (.applyTorque control | |
388 (.mult (.getPhysicsRotation control) | |
389 (Vector3f. 0 0 -10)))) | |
390 (if @move-left? | |
391 (.applyTorque control | |
392 (.mult (.getPhysicsRotation control) | |
393 (Vector3f. 0 10 0)))) | |
394 (if @move-right? | |
395 (.applyTorque control | |
396 (.mult (.getPhysicsRotation control) | |
397 (Vector3f. 0 -10 0)))) | |
398 (if @roll-left? | |
399 (.applyTorque control | |
400 (.mult (.getPhysicsRotation control) | |
401 (Vector3f. -1 0 0)))) | |
402 (if @roll-right? | |
403 (.applyTorque control | |
404 (.mult (.getPhysicsRotation control) | |
405 (Vector3f. 1 0 0)))))))) | |
590 #+end_src | 406 #+end_src |
591 | 407 |
592 #+results: body-1 | 408 |
593 : #'cortex.silly/test-try-again | 409 * COMMENT purgatory |
594 | 410 #+begin_src clojure |
411 (defn bullet-trans [] | |
412 (let [obj-a (sphere 0.5 :color ColorRGBA/Red | |
413 :position (Vector3f. -10 5 0)) | |
414 obj-b (sphere 0.5 :color ColorRGBA/Blue | |
415 :position (Vector3f. -10 -5 0) | |
416 :mass 0) | |
417 control-a (.getControl obj-a RigidBodyControl) | |
418 control-b (.getControl obj-b RigidBodyControl) | |
419 swivel | |
420 (.toRotationMatrix | |
421 (doto (Quaternion.) | |
422 (.fromAngleAxis (/ Math/PI 2) | |
423 Vector3f/UNIT_X)))] | |
424 (doto | |
425 (ConeJoint. | |
426 control-a control-b | |
427 (Vector3f. 0 5 0) | |
428 (Vector3f. 0 -5 0) | |
429 swivel swivel) | |
430 (.setLimit (* 0.6 (/ Math/PI 4)) | |
431 (/ Math/PI 4) | |
432 (* Math/PI 0.8))) | |
433 (world (nodify | |
434 [obj-a obj-b]) | |
435 standard-debug-controls | |
436 enable-debug | |
437 no-op))) | |
438 | |
439 | |
440 (defn bullet-trans* [] | |
441 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red | |
442 :position (Vector3f. 5 0 0) | |
443 :mass 90) | |
444 obj-b (sphere 0.5 :color ColorRGBA/Blue | |
445 :position (Vector3f. -5 0 0) | |
446 :mass 0) | |
447 control-a (.getControl obj-a RigidBodyControl) | |
448 control-b (.getControl obj-b RigidBodyControl) | |
449 move-up? (atom nil) | |
450 move-down? (atom nil) | |
451 move-left? (atom nil) | |
452 move-right? (atom nil) | |
453 roll-left? (atom nil) | |
454 roll-right? (atom nil) | |
455 force 100 | |
456 swivel | |
457 (.toRotationMatrix | |
458 (doto (Quaternion.) | |
459 (.fromAngleAxis (/ Math/PI 2) | |
460 Vector3f/UNIT_X))) | |
461 x-move | |
462 (doto (Matrix3f.) | |
463 (.fromStartEndVectors Vector3f/UNIT_X | |
464 (.normalize (Vector3f. 1 1 0)))) | |
465 | |
466 timer (atom 0)] | |
467 (doto | |
468 (ConeJoint. | |
469 control-a control-b | |
470 (Vector3f. -8 0 0) | |
471 (Vector3f. 2 0 0) | |
472 ;;swivel swivel | |
473 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY | |
474 x-move Matrix3f/IDENTITY | |
475 ) | |
476 (.setCollisionBetweenLinkedBodys false) | |
477 (.setLimit (* 1 (/ Math/PI 4)) ;; twist | |
478 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane | |
479 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane | |
480 (world (nodify | |
481 [obj-a obj-b]) | |
482 (merge standard-debug-controls | |
483 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?)) | |
484 "key-t" (fn [_ pressed?] (reset! move-down? pressed?)) | |
485 "key-f" (fn [_ pressed?] (reset! move-left? pressed?)) | |
486 "key-g" (fn [_ pressed?] (reset! move-right? pressed?)) | |
487 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?)) | |
488 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))}) | |
489 | |
490 (fn [world] | |
491 (enable-debug world) | |
492 (set-gravity world Vector3f/ZERO) | |
493 ) | |
494 | |
495 (fn [world _] | |
496 | |
497 (if @move-up? | |
498 (.applyForce control-a | |
499 (Vector3f. force 0 0) | |
500 (Vector3f. 0 0 0))) | |
501 (if @move-down? | |
502 (.applyForce control-a | |
503 (Vector3f. (- force) 0 0) | |
504 (Vector3f. 0 0 0))) | |
505 (if @move-left? | |
506 (.applyForce control-a | |
507 (Vector3f. 0 force 0) | |
508 (Vector3f. 0 0 0))) | |
509 (if @move-right? | |
510 (.applyForce control-a | |
511 (Vector3f. 0 (- force) 0) | |
512 (Vector3f. 0 0 0))) | |
513 | |
514 (if @roll-left? | |
515 (.applyForce control-a | |
516 (Vector3f. 0 0 force) | |
517 (Vector3f. 0 0 0))) | |
518 (if @roll-right? | |
519 (.applyForce control-a | |
520 (Vector3f. 0 0 (- force)) | |
521 (Vector3f. 0 0 0))) | |
522 | |
523 (if (zero? (rem (swap! timer inc) 100)) | |
524 (.attachChild | |
525 (.getRootNode world) | |
526 (sphere 0.05 :color ColorRGBA/Yellow | |
527 :physical? false :position | |
528 (.getWorldTranslation obj-a))))) | |
529 ) | |
530 )) | |
531 | |
532 | |
533 | |
534 #+end_src | |
595 | 535 |
596 | 536 |
597 * COMMENT generate source | 537 * COMMENT generate source |
598 #+begin_src clojure :tangle ../src/cortex/silly.clj | 538 #+begin_src clojure :tangle ../src/cortex/silly.clj |
599 <<body-1>> | 539 <<body-1>> |