view org/test-creature.org @ 80:7af5ef686539

got a finger working!
author Robert McIntyre <rlm@mit.edu>
date Thu, 05 Jan 2012 02:35:07 -0700
parents 01dbdb0d5500
children 10f495560c59
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-xy (:limit-xz constraints)
172 limit-yz (:limit-xy constraints)
173 twist (:twist constraints)]
175 (println-repl "creating CONE joint")
176 (let [vector-1
177 (.mult (.getWorldRotation joint)
178 Vector3f/UNIT_X)
179 vector-2
180 (.normalize
181 (.subtract
182 (.getWorldTranslation joint)
183 (.getWorldTranslation obj-a)))
184 ]
185 (println-repl
186 "vector-1 :" vector-1)
187 (println-repl
188 "vector-2 :" vector-2)
192 (doto
193 (ConeJoint.
194 control-a
195 control-b
196 pivot-a
197 pivot-b
199 ;;(doto (Matrix3f.)
200 ;; (.fromStartEndVectors
201 ;; Vector3f/UNIT_X
202 ;; (.normalize
203 ;; (.subtract
204 ;; (.getWorldTranslation joint)
205 ;; (.getWorldTranslation obj-a)))))
207 (.toRotationMatrix (.getWorldRotation joint))
209 (.toRotationMatrix
210 (.fromAngleAxis (Quaternion.)
211 (.angleBetween Vector3f/UNIT_X pivot-a)
212 (.cross Vector3f/UNIT_X pivot-a)))
214 ;; (doto (Matrix3f.)
215 ;; (.fromStartEndVectors
216 ;; Vector3f/UNIT_X
217 ;; (.normalize
218 ;; vector-1)))
220 ;; (doto (Matrix3f.)
221 ;; (.fromStartEndVectors
222 ;; Vector3f/UNIT_X
223 ;; vector-2))
224 )
225 (.setLimit (float limit-xy)
226 (float limit-yz)
227 (float twist))))))
228 true
229 (println-repl
230 "joint-type" joint-type "not recognized")))
232 (println-repl "could not find joint meta-data!")))))
234 (defn assemble-creature [#^Node pieces joints]
235 (dorun
236 (map
237 (fn [geom]
238 (let [physics-control
239 (RigidBodyControl.
240 (HullCollisionShape.
241 (.getMesh geom))
242 (if-let [mass (meta-data geom "mass")]
243 (do
244 (println-repl
245 "setting" (.getName geom) "mass to" (float mass))
246 (float mass))
247 (float 1)))]
249 (.addControl geom physics-control)))
250 (filter #(isa? (class %) Geometry )
251 (node-seq pieces))))
253 (dorun
254 (map
255 (fn [joint]
256 (let [[obj-a obj-b]
257 (joint-targets pieces joint)]
258 (connect obj-a obj-b joint)))
259 joints))
260 pieces)
262 (defn blender-creature [blender-path]
263 (let [model (load-blender-model blender-path)
264 joints
265 (if-let [joint-node (.getChild model "joints")]
266 (seq (.getChildren joint-node))
267 (do (println-repl "could not find joints node")
268 []))]
269 (assemble-creature model joints)))
271 (def hand "Models/creature1/one.blend")
273 (def worm "Models/creature1/try-again.blend")
275 (defn x-ray [#^ColorRGBA color]
276 (doto (Material. (asset-manager)
277 "Common/MatDefs/Misc/Unshaded.j3md")
278 (.setColor "Color" color)
279 (-> (.getAdditionalRenderState)
280 (.setDepthTest false))))
282 (defn test-creature [thing]
283 (let [x-axis
284 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
285 y-axis
286 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
287 z-axis
288 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
289 (world
290 (nodify [(blender-creature thing)
291 (box 10 2 10 :position (Vector3f. 0 -5.5 0)
292 :color ColorRGBA/Gray :mass 0)
293 x-axis y-axis z-axis
294 ])
295 standard-debug-controls
296 (comp light-up-everything enable-debug
297 (fn [world]
298 (.setTimer world (NanoTimer.))
299 (set-gravity world (Vector3f. 0 0 0))
300 (speed-up world)
301 world
302 ))
303 no-op)))
305 (defn world-setup [joint]
306 (let [top (doto
307 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
308 :position (Vector3f. 0 7 0))
309 (.addControl
310 (RigidBodyControl.
311 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))
312 bottom (doto
313 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
314 :position (Vector3f. 0 -1 0))
315 (.addControl
316 (RigidBodyControl.
317 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
318 table (box 10 2 10 :position (Vector3f. 0 -6 0)
319 :color ColorRGBA/Gray :mass 0)
320 a (.getControl top RigidBodyControl)
321 b (.getControl bottom RigidBodyControl)]
322 (cond
323 (= joint :point)
324 (doto
325 (Point2PointJoint. a b
326 (Vector3f. 0 -2 0)
327 (Vector3f. 0 2 0))
328 (.setCollisionBetweenLinkedBodys false))
329 (= joint :hinge)
330 (doto
331 (HingeJoint.
332 a b
333 (Vector3f. 0 -2 0)
334 (Vector3f. 0 2 0)
335 (Vector3f. 0 0 1)
336 (Vector3f. 0 0 1)
338 )
339 (.setCollisionBetweenLinkedBodys false)
340 ;;(.setLimit (- Math/PI) Math/PI)
341 )
342 (= joint :cone)
343 ;; note to self -- jbullet does NOT implement cone joints
344 ;; correctly. You must use plain ol' bullet for this to work.
345 ;; It's faster anyway, so whatever.
347 (doto (ConeJoint.
348 a b
349 (Vector3f. 0 -5 0)
350 (Vector3f. 0 2 0)
352 (doto (Matrix3f.)
353 (.fromStartEndVectors Vector3f/UNIT_X
354 Vector3f/UNIT_Y))
355 (doto (Matrix3f.)
356 (.fromStartEndVectors Vector3f/UNIT_X
357 (.normalize
358 (Vector3f. 0 0 -1))))
359 )
360 ;;(.setAngularOnly true)
362 (.setCollisionBetweenLinkedBodys false)
363 (.setLimit (* 1 (/ Math/PI 4))
364 (* 1 (/ Math/PI 2))
365 (* 0 (/ Math/PI 4)))
367 )
368 (= joint :six)
369 (doto
370 (SixDofJoint.
371 a b
372 (Vector3f. 0 -2 0)
373 (Vector3f. 0 2 0)
374 ;;(doto (Matrix3f.)
375 ;; (.fromStartEndVectors Vector3f/UNIT_X
376 ;; Vector3f/UNIT_Y))
377 ;;(doto (Matrix3f.)
378 ;; (.fromStartEndVectors Vector3f/UNIT_X
379 ;; Vector3f/UNIT_Y))
380 true)
381 (.setCollisionBetweenLinkedBodys false)
382 (.setAngularLowerLimit (Vector3f. 0
383 (- (/ Math/PI 2))
384 0))
386 (.setAngularUpperLimit (Vector3f. 0
387 (/ Math/PI 2)
388 0))
389 (.setLinearLowerLimit (Vector3f. 0 0 0))
390 (.setLinearUpperLimit (Vector3f. 0 0 0))
392 )
398 )
400 [top bottom table]))
403 (defn test-joint [joint]
404 (let [[top bottom floor] (world-setup joint)
405 control (.getControl top RigidBodyControl)
406 move-up? (atom false)
407 move-down? (atom false)
408 move-left? (atom false)
409 move-right? (atom false)
410 roll-left? (atom false)
411 roll-right? (atom false)
412 timer (atom 0)]
414 (world
415 (nodify [top bottom floor])
416 (merge standard-debug-controls
417 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
418 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
419 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
420 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
421 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
422 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
424 (fn [world]
425 (light-up-everything world)
426 (enable-debug world)
427 (set-gravity world (Vector3f. 0 0 0))
428 )
430 (fn [world _]
431 (if (zero? (rem (swap! timer inc) 100))
432 (do
433 ;; (println-repl @timer)
434 (.attachChild (.getRootNode world)
435 (sphere 0.05 :color ColorRGBA/Yellow
436 :position (.getWorldTranslation top)
437 :physical? false))))
438 (if @move-up?
439 (.applyTorque control
440 (.mult (.getPhysicsRotation control)
441 (Vector3f. 0 0 10))))
442 (if @move-down?
443 (.applyTorque control
444 (.mult (.getPhysicsRotation control)
445 (Vector3f. 0 0 -10))))
446 (if @move-left?
447 (.applyTorque control
448 (.mult (.getPhysicsRotation control)
449 (Vector3f. 0 10 0))))
450 (if @move-right?
451 (.applyTorque control
452 (.mult (.getPhysicsRotation control)
453 (Vector3f. 0 -10 0))))
454 (if @roll-left?
455 (.applyTorque control
456 (.mult (.getPhysicsRotation control)
457 (Vector3f. -1 0 0))))
458 (if @roll-right?
459 (.applyTorque control
460 (.mult (.getPhysicsRotation control)
461 (Vector3f. 1 0 0))))))))
462 #+end_src
465 * COMMENT purgatory
466 #+begin_src clojure
467 (defn bullet-trans []
468 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
469 :position (Vector3f. -10 5 0))
470 obj-b (sphere 0.5 :color ColorRGBA/Blue
471 :position (Vector3f. -10 -5 0)
472 :mass 0)
473 control-a (.getControl obj-a RigidBodyControl)
474 control-b (.getControl obj-b RigidBodyControl)
475 swivel
476 (.toRotationMatrix
477 (doto (Quaternion.)
478 (.fromAngleAxis (/ Math/PI 2)
479 Vector3f/UNIT_X)))]
480 (doto
481 (ConeJoint.
482 control-a control-b
483 (Vector3f. 0 5 0)
484 (Vector3f. 0 -5 0)
485 swivel swivel)
486 (.setLimit (* 0.6 (/ Math/PI 4))
487 (/ Math/PI 4)
488 (* Math/PI 0.8)))
489 (world (nodify
490 [obj-a obj-b])
491 standard-debug-controls
492 enable-debug
493 no-op)))
496 (defn bullet-trans* []
497 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
498 :position (Vector3f. 5 0 0)
499 :mass 90)
500 obj-b (sphere 0.5 :color ColorRGBA/Blue
501 :position (Vector3f. -5 0 0)
502 :mass 0)
503 control-a (.getControl obj-a RigidBodyControl)
504 control-b (.getControl obj-b RigidBodyControl)
505 move-up? (atom nil)
506 move-down? (atom nil)
507 move-left? (atom nil)
508 move-right? (atom nil)
509 roll-left? (atom nil)
510 roll-right? (atom nil)
511 force 100
512 swivel
513 (.toRotationMatrix
514 (doto (Quaternion.)
515 (.fromAngleAxis (/ Math/PI 2)
516 Vector3f/UNIT_X)))
517 x-move
518 (doto (Matrix3f.)
519 (.fromStartEndVectors Vector3f/UNIT_X
520 (.normalize (Vector3f. 1 1 0))))
522 timer (atom 0)]
523 (doto
524 (ConeJoint.
525 control-a control-b
526 (Vector3f. -8 0 0)
527 (Vector3f. 2 0 0)
528 ;;swivel swivel
529 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
530 x-move Matrix3f/IDENTITY
531 )
532 (.setCollisionBetweenLinkedBodys false)
533 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
534 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
535 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
536 (world (nodify
537 [obj-a obj-b])
538 (merge standard-debug-controls
539 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
540 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
541 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
542 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
543 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
544 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
546 (fn [world]
547 (enable-debug world)
548 (set-gravity world Vector3f/ZERO)
549 )
551 (fn [world _]
553 (if @move-up?
554 (.applyForce control-a
555 (Vector3f. force 0 0)
556 (Vector3f. 0 0 0)))
557 (if @move-down?
558 (.applyForce control-a
559 (Vector3f. (- force) 0 0)
560 (Vector3f. 0 0 0)))
561 (if @move-left?
562 (.applyForce control-a
563 (Vector3f. 0 force 0)
564 (Vector3f. 0 0 0)))
565 (if @move-right?
566 (.applyForce control-a
567 (Vector3f. 0 (- force) 0)
568 (Vector3f. 0 0 0)))
570 (if @roll-left?
571 (.applyForce control-a
572 (Vector3f. 0 0 force)
573 (Vector3f. 0 0 0)))
574 (if @roll-right?
575 (.applyForce control-a
576 (Vector3f. 0 0 (- force))
577 (Vector3f. 0 0 0)))
579 (if (zero? (rem (swap! timer inc) 100))
580 (.attachChild
581 (.getRootNode world)
582 (sphere 0.05 :color ColorRGBA/Yellow
583 :physical? false :position
584 (.getWorldTranslation obj-a)))))
585 )
586 ))
590 #+end_src
593 * COMMENT generate source
594 #+begin_src clojure :tangle ../src/cortex/silly.clj
595 <<body-1>>
596 #+end_src