view org/test-creature.org @ 79:01dbdb0d5500

offset cone joints achieved!
author Robert McIntyre <rlm@mit.edu>
date Wed, 04 Jan 2012 21:03:25 -0700
parents 77b506ac64f3
children 7af5ef686539
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 (doto
177 (ConeJoint.
178 control-a
179 control-b
180 pivot-a
181 pivot-b
183 (doto (Matrix3f.)
184 (.fromStartEndVectors
185 Vector3f/UNIT_X
186 (.normalize
187 (.subtract
188 (.getWorldTranslation joint)
189 (.getWorldTranslation obj-a)))))
190 (doto (Matrix3f.)
191 (.set (.getWorldRotation joint)))
193 )
194 (.setLimit (float limit-xy)
195 (float limit-yz)
196 (float twist)))))
197 true
198 (println-repl
199 "joint-type" joint-type "not recognized")))
201 (println-repl "could not find joint meta-data!")))))
203 (defn assemble-creature [#^Node pieces joints]
204 (dorun
205 (map
206 (fn [geom]
207 (let [physics-control
208 (RigidBodyControl.
209 (HullCollisionShape.
210 (.getMesh geom))
211 (if-let [mass (meta-data geom "mass")]
212 (do
213 (println-repl
214 "setting" (.getName geom) "mass to" (float mass))
215 (float mass))
216 (float 1)))]
218 (.addControl geom physics-control)))
219 (filter #(isa? (class %) Geometry )
220 (node-seq pieces))))
222 (dorun
223 (map
224 (fn [joint]
225 (let [[obj-a obj-b]
226 (joint-targets pieces joint)]
227 (connect obj-a obj-b joint)))
228 joints))
229 pieces)
231 (defn blender-creature [blender-path]
232 (let [model (load-blender-model blender-path)
233 joints
234 (if-let [joint-node (.getChild model "joints")]
235 (seq (.getChildren joint-node))
236 (do (println-repl "could not find joints node")
237 []))]
238 (assemble-creature model joints)))
240 (def hand "Models/creature1/one.blend")
242 (def worm "Models/creature1/try-again.blend")
244 (defn test-creature [thing]
245 (world
246 (nodify [(blender-creature thing)
247 (box 10 2 10 :position (Vector3f. 0 -5.5 0)
248 :color ColorRGBA/Gray :mass 0)])
249 standard-debug-controls
250 (comp light-up-everything enable-debug
251 (fn [world]
252 (.setTimer world (NanoTimer.))
253 ;;(set-gravity world (Vector3f. 0 0 0))
254 (speed-up world)
255 world
256 ))
257 no-op))
259 (defn world-setup [joint]
260 (let [top (doto
261 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
262 :position (Vector3f. 0 7 0))
263 (.addControl
264 (RigidBodyControl.
265 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))
266 bottom (doto
267 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
268 :position (Vector3f. 0 -1 0))
269 (.addControl
270 (RigidBodyControl.
271 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
272 table (box 10 2 10 :position (Vector3f. 0 -6 0)
273 :color ColorRGBA/Gray :mass 0)
274 a (.getControl top RigidBodyControl)
275 b (.getControl bottom RigidBodyControl)]
276 (cond
277 (= joint :point)
278 (doto
279 (Point2PointJoint. a b
280 (Vector3f. 0 -2 0)
281 (Vector3f. 0 2 0))
282 (.setCollisionBetweenLinkedBodys false))
283 (= joint :hinge)
284 (doto
285 (HingeJoint.
286 a b
287 (Vector3f. 0 -2 0)
288 (Vector3f. 0 2 0)
289 (Vector3f. 0 0 1)
290 (Vector3f. 0 0 1)
292 )
293 (.setCollisionBetweenLinkedBodys false)
294 ;;(.setLimit (- Math/PI) Math/PI)
295 )
296 (= joint :cone)
297 ;; note to self -- jbullet does NOT implement cone joints
298 ;; correctly. You must use plain ol' bullet for this to work.
299 ;; It's faster anyway, so whatever.
301 (doto (ConeJoint.
302 a b
303 (Vector3f. 0 -5 0)
304 (Vector3f. 0 2 0)
306 (doto (Matrix3f.)
307 (.fromStartEndVectors Vector3f/UNIT_X
308 Vector3f/UNIT_Y))
309 (doto (Matrix3f.)
310 (.fromStartEndVectors Vector3f/UNIT_X
311 (.normalize
312 (Vector3f. 5 5 0))))
313 )
314 ;;(.setAngularOnly true)
316 (.setCollisionBetweenLinkedBodys false)
317 (.setLimit (* 1 (/ Math/PI 4))
318 (* 1 (/ Math/PI 2))
319 (* 0 (/ Math/PI 4)))
321 )
322 (= joint :six)
323 (doto
324 (SixDofJoint.
325 a b
326 (Vector3f. 0 -2 0)
327 (Vector3f. 0 2 0)
328 ;;(doto (Matrix3f.)
329 ;; (.fromStartEndVectors Vector3f/UNIT_X
330 ;; Vector3f/UNIT_Y))
331 ;;(doto (Matrix3f.)
332 ;; (.fromStartEndVectors Vector3f/UNIT_X
333 ;; Vector3f/UNIT_Y))
334 true)
335 (.setCollisionBetweenLinkedBodys false)
336 (.setAngularLowerLimit (Vector3f. 0
337 (- (/ Math/PI 2))
338 0))
340 (.setAngularUpperLimit (Vector3f. 0
341 (/ Math/PI 2)
342 0))
343 (.setLinearLowerLimit (Vector3f. 0 0 0))
344 (.setLinearUpperLimit (Vector3f. 0 0 0))
346 )
352 )
354 [top bottom table]))
357 (defn test-joint [joint]
358 (let [[top bottom floor] (world-setup joint)
359 control (.getControl top RigidBodyControl)
360 move-up? (atom false)
361 move-down? (atom false)
362 move-left? (atom false)
363 move-right? (atom false)
364 roll-left? (atom false)
365 roll-right? (atom false)
366 timer (atom 0)]
368 (world
369 (nodify [top bottom floor])
370 (merge standard-debug-controls
371 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
372 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
373 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
374 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
375 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
376 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
378 (fn [world]
379 (light-up-everything world)
380 (enable-debug world)
381 (set-gravity world (Vector3f. 0 0 0))
382 )
384 (fn [world _]
385 (if (zero? (rem (swap! timer inc) 100))
386 (do
387 ;; (println-repl @timer)
388 (.attachChild (.getRootNode world)
389 (sphere 0.05 :color ColorRGBA/Yellow
390 :position (.getWorldTranslation top)
391 :physical? false))))
392 (if @move-up?
393 (.applyTorque control
394 (.mult (.getPhysicsRotation control)
395 (Vector3f. 0 0 10))))
396 (if @move-down?
397 (.applyTorque control
398 (.mult (.getPhysicsRotation control)
399 (Vector3f. 0 0 -10))))
400 (if @move-left?
401 (.applyTorque control
402 (.mult (.getPhysicsRotation control)
403 (Vector3f. 0 10 0))))
404 (if @move-right?
405 (.applyTorque control
406 (.mult (.getPhysicsRotation control)
407 (Vector3f. 0 -10 0))))
408 (if @roll-left?
409 (.applyTorque control
410 (.mult (.getPhysicsRotation control)
411 (Vector3f. -1 0 0))))
412 (if @roll-right?
413 (.applyTorque control
414 (.mult (.getPhysicsRotation control)
415 (Vector3f. 1 0 0))))))))
416 #+end_src
419 * COMMENT purgatory
420 #+begin_src clojure
421 (defn bullet-trans []
422 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
423 :position (Vector3f. -10 5 0))
424 obj-b (sphere 0.5 :color ColorRGBA/Blue
425 :position (Vector3f. -10 -5 0)
426 :mass 0)
427 control-a (.getControl obj-a RigidBodyControl)
428 control-b (.getControl obj-b RigidBodyControl)
429 swivel
430 (.toRotationMatrix
431 (doto (Quaternion.)
432 (.fromAngleAxis (/ Math/PI 2)
433 Vector3f/UNIT_X)))]
434 (doto
435 (ConeJoint.
436 control-a control-b
437 (Vector3f. 0 5 0)
438 (Vector3f. 0 -5 0)
439 swivel swivel)
440 (.setLimit (* 0.6 (/ Math/PI 4))
441 (/ Math/PI 4)
442 (* Math/PI 0.8)))
443 (world (nodify
444 [obj-a obj-b])
445 standard-debug-controls
446 enable-debug
447 no-op)))
450 (defn bullet-trans* []
451 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
452 :position (Vector3f. 5 0 0)
453 :mass 90)
454 obj-b (sphere 0.5 :color ColorRGBA/Blue
455 :position (Vector3f. -5 0 0)
456 :mass 0)
457 control-a (.getControl obj-a RigidBodyControl)
458 control-b (.getControl obj-b RigidBodyControl)
459 move-up? (atom nil)
460 move-down? (atom nil)
461 move-left? (atom nil)
462 move-right? (atom nil)
463 roll-left? (atom nil)
464 roll-right? (atom nil)
465 force 100
466 swivel
467 (.toRotationMatrix
468 (doto (Quaternion.)
469 (.fromAngleAxis (/ Math/PI 2)
470 Vector3f/UNIT_X)))
471 x-move
472 (doto (Matrix3f.)
473 (.fromStartEndVectors Vector3f/UNIT_X
474 (.normalize (Vector3f. 1 1 0))))
476 timer (atom 0)]
477 (doto
478 (ConeJoint.
479 control-a control-b
480 (Vector3f. -8 0 0)
481 (Vector3f. 2 0 0)
482 ;;swivel swivel
483 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
484 x-move Matrix3f/IDENTITY
485 )
486 (.setCollisionBetweenLinkedBodys false)
487 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
488 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
489 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
490 (world (nodify
491 [obj-a obj-b])
492 (merge standard-debug-controls
493 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
494 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
495 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
496 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
497 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
498 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
500 (fn [world]
501 (enable-debug world)
502 (set-gravity world Vector3f/ZERO)
503 )
505 (fn [world _]
507 (if @move-up?
508 (.applyForce control-a
509 (Vector3f. force 0 0)
510 (Vector3f. 0 0 0)))
511 (if @move-down?
512 (.applyForce control-a
513 (Vector3f. (- force) 0 0)
514 (Vector3f. 0 0 0)))
515 (if @move-left?
516 (.applyForce control-a
517 (Vector3f. 0 force 0)
518 (Vector3f. 0 0 0)))
519 (if @move-right?
520 (.applyForce control-a
521 (Vector3f. 0 (- force) 0)
522 (Vector3f. 0 0 0)))
524 (if @roll-left?
525 (.applyForce control-a
526 (Vector3f. 0 0 force)
527 (Vector3f. 0 0 0)))
528 (if @roll-right?
529 (.applyForce control-a
530 (Vector3f. 0 0 (- force))
531 (Vector3f. 0 0 0)))
533 (if (zero? (rem (swap! timer inc) 100))
534 (.attachChild
535 (.getRootNode world)
536 (sphere 0.05 :color ColorRGBA/Yellow
537 :physical? false :position
538 (.getWorldTranslation obj-a)))))
539 )
540 ))
544 #+end_src
547 * COMMENT generate source
548 #+begin_src clojure :tangle ../src/cortex/silly.clj
549 <<body-1>>
550 #+end_src