view org/test-creature.org @ 82:6b4ca076285e

making some progress on cone joints...
author Robert McIntyre <rlm@mit.edu>
date Fri, 06 Jan 2012 02:03:43 -0700
parents 10f495560c59
children 14b604e955ed
line wrap: on
line source
1 #+title: First attempt at a creature!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
9 * Intro
10 So far, I've made the following senses --
11 - Vision
12 - Hearing
13 - Touch
14 - Proprioception
16 And one effector:
17 - Movement
19 However, the code so far has only enabled these senses, but has not
20 actually implemented them. For example, there is still a lot of work
21 to be done for vision. I need to be able to create an /eyeball/ in
22 simulation that can be moved around and see the world from different
23 angles. I also need to determine weather to use log-polar or cartesian
24 for the visual input, and I need to determine how/wether to
25 disceritise the visual input.
27 I also want to be able to visualize both the sensors and the
28 effectors in pretty pictures. This semi-retarted creature will by my
29 first attempt at bringing everything together.
31 * The creature's body
33 Still going to do an eve-like body in blender, but due to problems
34 importing the joints, etc into jMonkeyEngine3, I',m going to do all
35 the connecting here in clojure code, using the names of the individual
36 components and trial and error. Later, I'll maybe make some sort of
37 creature-building modifications to blender that support whatever
38 discreitized senses I'm going to make.
40 #+name: body-1
41 #+begin_src clojure
42 (ns cortex.silly
43 "let's play!"
44 {:author "Robert McIntyre"})
46 ;; TODO remove this!
47 (require 'cortex.import)
48 (cortex.import/mega-import-jme3)
49 (use '(cortex world util body hearing touch vision))
51 (rlm.rlm-commands/help)
53 (defn load-blender-model
54 "Load a .blend file using an asset folder relative path."
55 [^String model]
56 (.loadModel
57 (doto (asset-manager)
58 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
59 model))
61 (defn meta-data [blender-node key]
62 (if-let [data (.getUserData blender-node "properties")]
63 (.findValue data key)
64 nil))
66 (defn blender-to-jme
67 "Convert from Blender coordinates to JME coordinates"
68 [#^Vector3f in]
69 (Vector3f. (.getX in)
70 (.getZ in)
71 (- (.getY in))))
73 (defn jme-to-blender
74 "Convert from JME coordinates to Blender coordinates"
75 [#^Vector3f in]
76 (Vector3f. (.getX in)
77 (- (.getZ in))
78 (.getY in)))
80 (defn joint-targets
81 "Return the two closest two objects to the joint object, ordered
82 from bottom to top according to the joint's rotation."
83 [#^Node parts #^Node joint]
84 ;;(println (meta-data joint "joint"))
85 (.getWorldRotation joint)
86 (loop [radius (float 0.01)]
87 (let [results (CollisionResults.)]
88 (.collideWith
89 parts
90 (BoundingBox. (.getWorldTranslation joint)
91 radius radius radius)
92 results)
93 (let [targets
94 (distinct
95 (map #(.getGeometry %) results))]
96 (if (>= (count targets) 2)
97 (sort-by
98 #(let [v
99 (jme-to-blender
100 (.mult
101 (.inverse (.getWorldRotation joint))
102 (.subtract (.getWorldTranslation %)
103 (.getWorldTranslation joint))))]
104 (println-repl (.getName %) ":" v)
105 (.dot (Vector3f. 1 1 1)
106 v))
107 (take 2 targets))
108 (recur (float (* radius 2))))))))
110 (defn connect
111 "here are some examples:
112 {:type :point}
113 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
114 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
116 {:type :cone :limit-xz 0]
117 :limit-xy 0]
118 :twist 0]} (use XZY rotation mode in blender!)"
119 [#^Node obj-a #^Node obj-b #^Node joint]
120 (let [center-a (.getWorldTranslation obj-a)
121 center-b (.getWorldTranslation obj-b)
122 joint-center (.getWorldTranslation joint)
123 pivot-a (.subtract joint-center center-a)
124 pivot-b (.subtract joint-center center-b)
125 control-a (.getControl obj-a RigidBodyControl)
126 control-b (.getControl obj-b RigidBodyControl)]
127 ;; A side-effect of creating a joint registers
128 ;; it with both physics objects which in turn
129 ;; will register the joint with the physics system
130 ;; when the simulation is started.
131 (if-let [constraints
132 (map-vals
133 eval
134 (read-string
135 (meta-data joint "joint")))]
137 (let [joint-type (:type constraints)]
138 (println-repl "creating joint between"
139 (.getName obj-a) "and" (.getName obj-b))
140 (cond (= :point joint-type)
141 (do
142 (println-repl "creating POINT joint")
143 (Point2PointJoint.
144 control-a
145 control-b
146 pivot-a
147 pivot-b))
148 (= :hinge joint-type)
149 (do
150 (println-repl "creating HINGE joint")
151 (let [axis (if-let
152 [axis (:axis constraints)]
153 axis
154 Vector3f/UNIT_X)
155 [limit-1 limit-2] (:limit constraints)
156 hinge-axis
157 (.mult
158 (.getWorldRotation joint)
159 (blender-to-jme axis))]
160 (doto
161 (HingeJoint.
162 control-a
163 control-b
164 pivot-a
165 pivot-b
166 hinge-axis
167 hinge-axis)
168 (.setLimit limit-1 limit-2))))
169 (= :cone joint-type)
170 (do
171 (let [limit-xz (:limit-xz constraints)
172 limit-xy (:limit-xy constraints)
173 twist (:twist constraints)]
175 (println-repl "creating CONE joint")
176 (let [frame-a
177 (.toRotationMatrix
178 (doto (Quaternion.)
179 (.fromAngleAxis
180 (float
181 (.angleBetween
182 pivot-a Vector3f/UNIT_X))
183 (.cross (.normalize pivot-a)
184 Vector3f/UNIT_X))))
185 ]
186 (println-repl
187 "angle between pivot-a (" pivot-a ") and UNIT_X is"
188 (.angleBetween Vector3f/UNIT_X (.normalize pivot-a)))
192 (doto
193 (ConeJoint.
194 control-a
195 control-b
196 pivot-a
197 pivot-b
200 ;; ;; frame-in-A
201 frame-a
202 frame-a
204 ;; (.toRotationMatrix
205 ;; (doto (Quaternion.)
206 ;; (.fromAngles
207 ;; 0 0 (* -1 (/ Math/PI 2)))))
210 ;; ;; frame-in-B
211 ;; (.toRotationMatrix
212 ;; (doto (Quaternion.)
213 ;; (.fromAngles
214 ;; 0 0 (* -1.2 (/ Math/PI 2)))))
217 )
218 (.setLimit (float limit-xz)
219 (float limit-xy)
220 (float twist))))))
221 true
222 (println-repl
223 "joint-type" joint-type "not recognized")))
225 (println-repl "could not find joint meta-data!"))))
227 (defn assemble-creature [#^Node pieces joints]
228 (dorun
229 (map
230 (fn [geom]
231 (let [physics-control
232 (RigidBodyControl.
233 (HullCollisionShape.
234 (.getMesh geom))
235 (if-let [mass (meta-data geom "mass")]
236 (do
237 (println-repl
238 "setting" (.getName geom) "mass to" (float mass))
239 (float mass))
240 (float 1)))]
242 (.addControl geom physics-control)))
243 (filter #(isa? (class %) Geometry )
244 (node-seq pieces))))
246 (dorun
247 (map
248 (fn [joint]
249 (let [[obj-a obj-b]
250 (joint-targets pieces joint)]
251 (connect obj-a obj-b joint)))
252 joints))
253 pieces)
255 (defn blender-creature [blender-path]
256 (let [model (load-blender-model blender-path)
257 joints
258 (if-let [joint-node (.getChild model "joints")]
259 (seq (.getChildren joint-node))
260 (do (println-repl "could not find joints node")
261 []))]
262 (assemble-creature model joints)))
264 (def hand "Models/creature1/one.blend")
266 (def worm "Models/creature1/try-again.blend")
268 (defn x-ray [#^ColorRGBA color]
269 (doto (Material. (asset-manager)
270 "Common/MatDefs/Misc/Unshaded.j3md")
271 (.setColor "Color" color)
272 (-> (.getAdditionalRenderState)
273 (.setDepthTest false))))
275 (defn test-creature [thing]
276 (let [x-axis
277 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
278 y-axis
279 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
280 z-axis
281 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
282 (world
283 (nodify [(blender-creature thing)
284 (box 10 2 10 :position (Vector3f. 0 -9 0)
285 :color ColorRGBA/Gray :mass 0)
286 x-axis y-axis z-axis
287 ])
288 standard-debug-controls
289 (comp light-up-everything enable-debug
290 (fn [world]
291 (.setTimer world (NanoTimer.))
292 (set-gravity world (Vector3f. 0 0 0))
293 (speed-up world)
294 world
295 ))
296 no-op)))
298 (defn world-setup [joint]
299 (let [top (doto
300 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
301 :position (Vector3f. 0 7 0))
302 (.addControl
303 (RigidBodyControl.
304 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))
305 bottom (doto
306 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
307 :position (Vector3f. 0 -1 0))
308 (.addControl
309 (RigidBodyControl.
310 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
311 table (box 10 2 10 :position (Vector3f. 0 -6 0)
312 :color ColorRGBA/Gray :mass 0)
313 a (.getControl top RigidBodyControl)
314 b (.getControl bottom RigidBodyControl)]
315 (cond
316 (= joint :point)
317 (doto
318 (Point2PointJoint. a b
319 (Vector3f. 0 -2 0)
320 (Vector3f. 0 2 0))
321 (.setCollisionBetweenLinkedBodys false))
322 (= joint :hinge)
323 (doto
324 (HingeJoint.
325 a b
326 (Vector3f. 0 -2 0)
327 (Vector3f. 0 2 0)
328 (Vector3f. 0 0 1)
329 (Vector3f. 0 0 1)
331 )
332 (.setCollisionBetweenLinkedBodys false)
333 ;;(.setLimit (- Math/PI) Math/PI)
334 )
335 (= joint :cone)
336 ;; note to self -- jbullet does NOT implement cone joints
337 ;; correctly. You must use plain ol' bullet for this to work.
338 ;; It's faster anyway, so whatever.
340 (doto (ConeJoint.
341 a b
342 (Vector3f. 0 -5 0)
343 (Vector3f. 0 2 0)
345 (doto (Matrix3f.)
346 (.fromStartEndVectors Vector3f/UNIT_X
347 Vector3f/UNIT_Y))
348 (doto (Matrix3f.)
349 (.fromStartEndVectors Vector3f/UNIT_X
350 (.normalize
351 (Vector3f. 0 0 -1))))
352 )
353 ;;(.setAngularOnly true)
355 (.setCollisionBetweenLinkedBodys false)
356 (.setLimit (* 1 (/ Math/PI 4))
357 (* 1 (/ Math/PI 2))
358 (* 0 (/ Math/PI 4)))
360 )
361 (= joint :six)
362 (doto
363 (SixDofJoint.
364 a b
365 (Vector3f. 0 -2 0)
366 (Vector3f. 0 2 0)
367 ;;(doto (Matrix3f.)
368 ;; (.fromStartEndVectors Vector3f/UNIT_X
369 ;; Vector3f/UNIT_Y))
370 ;;(doto (Matrix3f.)
371 ;; (.fromStartEndVectors Vector3f/UNIT_X
372 ;; Vector3f/UNIT_Y))
373 true)
374 (.setCollisionBetweenLinkedBodys false)
375 (.setAngularLowerLimit (Vector3f. 0
376 (- (/ Math/PI 2))
377 0))
379 (.setAngularUpperLimit (Vector3f. 0
380 (/ Math/PI 2)
381 0))
382 (.setLinearLowerLimit (Vector3f. 0 0 0))
383 (.setLinearUpperLimit (Vector3f. 0 0 0))
385 )
391 )
393 [top bottom table]))
396 (defn test-joint [joint]
397 (let [[top bottom floor] (world-setup joint)
398 control (.getControl top RigidBodyControl)
399 move-up? (atom false)
400 move-down? (atom false)
401 move-left? (atom false)
402 move-right? (atom false)
403 roll-left? (atom false)
404 roll-right? (atom false)
405 timer (atom 0)]
407 (world
408 (nodify [top bottom floor])
409 (merge standard-debug-controls
410 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
411 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
412 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
413 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
414 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
415 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
417 (fn [world]
418 (light-up-everything world)
419 (enable-debug world)
420 (set-gravity world (Vector3f. 0 0 0))
421 )
423 (fn [world _]
424 (if (zero? (rem (swap! timer inc) 100))
425 (do
426 ;; (println-repl @timer)
427 (.attachChild (.getRootNode world)
428 (sphere 0.05 :color ColorRGBA/Yellow
429 :position (.getWorldTranslation top)
430 :physical? false))))
431 (if @move-up?
432 (.applyTorque control
433 (.mult (.getPhysicsRotation control)
434 (Vector3f. 0 0 10))))
435 (if @move-down?
436 (.applyTorque control
437 (.mult (.getPhysicsRotation control)
438 (Vector3f. 0 0 -10))))
439 (if @move-left?
440 (.applyTorque control
441 (.mult (.getPhysicsRotation control)
442 (Vector3f. 0 10 0))))
443 (if @move-right?
444 (.applyTorque control
445 (.mult (.getPhysicsRotation control)
446 (Vector3f. 0 -10 0))))
447 (if @roll-left?
448 (.applyTorque control
449 (.mult (.getPhysicsRotation control)
450 (Vector3f. -1 0 0))))
451 (if @roll-right?
452 (.applyTorque control
453 (.mult (.getPhysicsRotation control)
454 (Vector3f. 1 0 0))))))))
455 #+end_src
458 * COMMENT purgatory
459 #+begin_src clojure
460 (defn bullet-trans []
461 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
462 :position (Vector3f. -10 5 0))
463 obj-b (sphere 0.5 :color ColorRGBA/Blue
464 :position (Vector3f. -10 -5 0)
465 :mass 0)
466 control-a (.getControl obj-a RigidBodyControl)
467 control-b (.getControl obj-b RigidBodyControl)
468 swivel
469 (.toRotationMatrix
470 (doto (Quaternion.)
471 (.fromAngleAxis (/ Math/PI 2)
472 Vector3f/UNIT_X)))]
473 (doto
474 (ConeJoint.
475 control-a control-b
476 (Vector3f. 0 5 0)
477 (Vector3f. 0 -5 0)
478 swivel swivel)
479 (.setLimit (* 0.6 (/ Math/PI 4))
480 (/ Math/PI 4)
481 (* Math/PI 0.8)))
482 (world (nodify
483 [obj-a obj-b])
484 standard-debug-controls
485 enable-debug
486 no-op)))
489 (defn bullet-trans* []
490 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
491 :position (Vector3f. 5 0 0)
492 :mass 90)
493 obj-b (sphere 0.5 :color ColorRGBA/Blue
494 :position (Vector3f. -5 0 0)
495 :mass 0)
496 control-a (.getControl obj-a RigidBodyControl)
497 control-b (.getControl obj-b RigidBodyControl)
498 move-up? (atom nil)
499 move-down? (atom nil)
500 move-left? (atom nil)
501 move-right? (atom nil)
502 roll-left? (atom nil)
503 roll-right? (atom nil)
504 force 100
505 swivel
506 (.toRotationMatrix
507 (doto (Quaternion.)
508 (.fromAngleAxis (/ Math/PI 2)
509 Vector3f/UNIT_X)))
510 x-move
511 (doto (Matrix3f.)
512 (.fromStartEndVectors Vector3f/UNIT_X
513 (.normalize (Vector3f. 1 1 0))))
515 timer (atom 0)]
516 (doto
517 (ConeJoint.
518 control-a control-b
519 (Vector3f. -8 0 0)
520 (Vector3f. 2 0 0)
521 ;;swivel swivel
522 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
523 x-move Matrix3f/IDENTITY
524 )
525 (.setCollisionBetweenLinkedBodys false)
526 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
527 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
528 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
529 (world (nodify
530 [obj-a obj-b])
531 (merge standard-debug-controls
532 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
533 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
534 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
535 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
536 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
537 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
539 (fn [world]
540 (enable-debug world)
541 (set-gravity world Vector3f/ZERO)
542 )
544 (fn [world _]
546 (if @move-up?
547 (.applyForce control-a
548 (Vector3f. force 0 0)
549 (Vector3f. 0 0 0)))
550 (if @move-down?
551 (.applyForce control-a
552 (Vector3f. (- force) 0 0)
553 (Vector3f. 0 0 0)))
554 (if @move-left?
555 (.applyForce control-a
556 (Vector3f. 0 force 0)
557 (Vector3f. 0 0 0)))
558 (if @move-right?
559 (.applyForce control-a
560 (Vector3f. 0 (- force) 0)
561 (Vector3f. 0 0 0)))
563 (if @roll-left?
564 (.applyForce control-a
565 (Vector3f. 0 0 force)
566 (Vector3f. 0 0 0)))
567 (if @roll-right?
568 (.applyForce control-a
569 (Vector3f. 0 0 (- force))
570 (Vector3f. 0 0 0)))
572 (if (zero? (rem (swap! timer inc) 100))
573 (.attachChild
574 (.getRootNode world)
575 (sphere 0.05 :color ColorRGBA/Yellow
576 :physical? false :position
577 (.getWorldTranslation obj-a)))))
578 )
579 ))
583 #+end_src
586 * COMMENT generate source
587 #+begin_src clojure :tangle ../src/cortex/silly.clj
588 <<body-1>>
589 #+end_src