Mercurial > cortex
view org/body.org @ 309:5d448182c807
added/verified YouTube backup links for all videos.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 18 Feb 2012 11:42:34 -0700 |
parents | 7e7f8d6d9ec5 |
children | b46ccfd128a2 |
line wrap: on
line source
1 #+title: Building a Body2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating a body (movement, touch, proprioception) in jMonkeyEngine3.5 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org8 * Design Constraints10 I use [[www.blender.org/][blender]] to design bodies. The design of the bodies is11 determined by the requirements of the AI that will use them. The12 bodies must be easy for an AI to sense and control, and they must be13 relatively simple for jMonkeyEngine to compute.15 # I'm a secret test! :P16 ** Bag of Bones18 How to create such a body? One option I ultimately rejected is to use19 blender's [[http://wiki.blender.org/index.php/Doc:2.6/Manual/Rigging/Armatures][armature]] system. The idea would have been to define a mesh20 which describes the creature's entire body. To this you add an21 skeleton which deforms this mesh. This technique is used extensively22 to model humans and create realistic animations. It is hard to use for23 my purposes because it is difficult to update the creature's Physics24 Collision Mesh in tandem with its Geometric Mesh under the influence25 of the armature. Without this the creature will not be able to grab26 things in its environment, and it won't be able to tell where its27 physical body is by using its eyes. Also, armatures do not specify28 any rotational limits for a joint, making it hard to model elbows,29 shoulders, etc.31 ** EVE33 Instead of using the human-like "deformable bag of bones" approach, I34 decided to base my body plans on the robot EVE from the movie wall-E.36 #+caption: EVE from the movie WALL-E. This body plan turns out to be much better suited to my purposes than a more human-like one.37 [[../images/Eve.jpg]]39 EVE's body is composed of several rigid components that are held40 together by invisible joint constraints. This is what I mean by41 "eve-like". The main reason that I use eve-style bodies is so that42 there will be correspondence between the AI's vision and the physical43 presence of its body. Each individual section is simulated by a44 separate rigid body that corresponds exactly with its visual45 representation and does not change. Sections are connected by46 invisible joints that are well supported in jMonkeyEngine. Bullet, the47 physics backend for jMonkeyEngine, can efficiently simulate hundreds48 of rigid bodies connected by joints. Sections do not have to stay as49 one piece forever; they can be dynamically replaced with multiple50 sections to simulate splitting in two. This could be used to simulate51 retractable claws or EVE's hands, which are able to coalesce into one52 object in the movie.54 * Solidifying the Body56 Here is a hand designed eve-style in blender.58 #+attr_html: width="755"59 [[../images/hand-screenshot0.png]]61 If we load it directly into jMonkeyEngine, we get this:63 #+name: test-164 #+begin_src clojure65 (def hand-path "Models/test-creature/hand.blend")67 (defn hand [] (load-blender-model hand-path))69 (defn setup [world]70 (let [cam (.getCamera world)]71 (println-repl cam)72 (.setLocation73 cam (Vector3f.74 -6.9015837, 8.644911, 5.6043186))75 (.setRotation76 cam77 (Quaternion.78 0.14046453, 0.85894054, -0.34301838, 0.3533118)))79 (light-up-everything world)80 (.setTimer world (RatchetTimer. 60))81 world)83 (defn test-hand-184 ([] (test-hand-1 false))85 ([record?]86 (world (hand)87 standard-debug-controls88 (fn [world]89 (if record?90 (Capture/captureVideo91 world92 (File. "/home/r/proj/cortex/render/body/1")))93 (setup world)) no-op)))94 #+end_src97 #+begin_src clojure :results silent98 (.start (cortex.test.body/test-one))99 #+end_src101 #+begin_html102 <div class="figure">103 <center>104 <video controls="controls" width="640">105 <source src="../video/ghost-hand.ogg" type="video/ogg"106 preload="none" poster="../images/aurellem-1280x480.png" />107 </video>108 <br> <a href="http://youtu.be/9LZpwTIhjzE"> YouTube </a>109 </center>110 <p>The hand model directly loaded from blender. It has no physical111 presence in the simulation. </p>112 </div>113 #+end_html115 You will notice that the hand has no physical presence -- it's a116 hologram through which everything passes. Therefore, the first thing117 to do is to make it solid. Blender has physics simulation on par with118 jMonkeyEngine (they both use bullet as their physics backend), but it119 can be difficult to translate between the two systems, so for now I120 specify the mass of each object as meta-data in blender and construct121 the physics shape based on the mesh in jMonkeyEngine.123 #+name: body-1124 #+begin_src clojure125 (defn physical!126 "Iterate through the nodes in creature and make them real physical127 objects in the simulation."128 [#^Node creature]129 (dorun130 (map131 (fn [geom]132 (let [physics-control133 (RigidBodyControl.134 (HullCollisionShape.135 (.getMesh geom))136 (if-let [mass (meta-data geom "mass")]137 (do138 (println-repl139 "setting" (.getName geom) "mass to" (float mass))140 (float mass))141 (float 1)))]142 (.addControl geom physics-control)))143 (filter #(isa? (class %) Geometry )144 (node-seq creature)))))145 #+end_src147 =physical!)= iterates through a creature's node structure, creating148 CollisionShapes for each geometry with the mass specified in that149 geometry's meta-data.151 #+name: test-2152 #+begin_src clojure153 (in-ns 'cortex.test.body)155 (def gravity-control156 {"key-g" (fn [world _]157 (set-gravity world (Vector3f. 0 -9.81 0)))158 "key-u" (fn [world _] (set-gravity world Vector3f/ZERO))})161 (defn floor []162 (box 10 3 10 :position (Vector3f. 0 -10 0)163 :color ColorRGBA/Gray :mass 0))165 (defn test-hand-2166 ([] (test-hand-2 false))167 ([record?]168 (world169 (nodify170 [(doto (hand)171 (physical!))172 (floor)])173 (merge standard-debug-controls gravity-control)174 (fn [world]175 (if record?176 (Capture/captureVideo177 world (File. "/home/r/proj/cortex/render/body/2")))178 (set-gravity world Vector3f/ZERO)179 (setup world))180 no-op)))181 #+end_src183 #+begin_html184 <div class="figure">185 <center>186 <video controls="controls" width="640">187 <source src="../video/crumbly-hand.ogg" type="video/ogg"188 preload="none" poster="../images/aurellem-1280x480.png" />189 </video>190 <br> <a href="http://youtu.be/GEA1SACwpPg"> YouTube </a>191 </center>192 <p>The hand now has a physical presence, but there is nothing to hold193 it together.</p>194 </div>195 #+end_html197 Now that's some progress.199 * Joints201 Obviously, an AI is not going to be doing much while lying in pieces202 on the floor. So, the next step to making a proper body is to connect203 those pieces together with joints. jMonkeyEngine has a large array of204 joints available via bullet, such as Point2Point, Cone, Hinge, and a205 generic Six Degree of Freedom joint, with or without spring206 restitution.208 Although it should be possible to specify the joints using blender's209 physics system, and then automatically import them with jMonkeyEngine,210 the support isn't there yet, and there are a few problems with bullet211 itself that need to be solved before it can happen.213 So, I will use the same system for specifying joints as I will do for214 some senses. Each joint is specified by an empty node whose parent215 has the name "joints". Their orientation and meta-data determine what216 joint is created.218 #+attr_html: width="755"219 #+caption: Joints hack in blender. Each empty node here will be transformed into a joint in jMonkeyEngine220 [[../images/hand-screenshot1.png]]222 The empty node in the upper right, highlighted in yellow, is the223 parent node of all the empties which represent joints. The following224 functions must do three things to translate these into real joints:226 - Find the children of the "joints" node.227 - Determine the two spatials the joint it meant to connect.228 - Create the joint based on the meta-data of the empty node.230 ** Finding the Joints232 The higher order function =sense-nodes= from =cortex.sense= simplifies233 the first task.235 #+name: joints-2236 #+begin_src clojure237 (defvar238 ^{:arglists '([creature])}239 joints240 (sense-nodes "joints")241 "Return the children of the creature's \"joints\" node.")242 #+end_src244 ** Joint Targets and Orientation246 This technique for finding a joint's targets is very similar to247 =cortex.sense/closest-node=. A small cube, centered around the248 empty-node, grows exponentially until it intersects two /physical/249 objects. The objects are ordered according to the joint's rotation,250 with the first one being the object that has more negative coordinates251 in the joint's reference frame. Since the objects must be physical,252 the empty-node itself escapes detection. Because the objects must be253 physical, =joint-targets= must be called /after/ =physical!= is254 called.256 #+name: joints-3257 #+begin_src clojure258 (defn joint-targets259 "Return the two closest two objects to the joint object, ordered260 from bottom to top according to the joint's rotation."261 [#^Node parts #^Node joint]262 (loop [radius (float 0.01)]263 (let [results (CollisionResults.)]264 (.collideWith265 parts266 (BoundingBox. (.getWorldTranslation joint)267 radius radius radius) results)268 (let [targets269 (distinct270 (map #(.getGeometry %) results))]271 (if (>= (count targets) 2)272 (sort-by273 #(let [joint-ref-frame-position274 (jme-to-blender275 (.mult276 (.inverse (.getWorldRotation joint))277 (.subtract (.getWorldTranslation %)278 (.getWorldTranslation joint))))]279 (.dot (Vector3f. 1 1 1) joint-ref-frame-position))280 (take 2 targets))281 (recur (float (* radius 2))))))))282 #+end_src284 ** Generating Joints286 This section of code iterates through all the different ways of287 specifying joints using blender meta-data and converts each one to the288 appropriate jMonkeyEngine joint.290 #+name: joints-4291 #+begin_src clojure292 (defmulti joint-dispatch293 "Translate blender pseudo-joints into real JME joints."294 (fn [constraints & _]295 (:type constraints)))297 (defmethod joint-dispatch :point298 [constraints control-a control-b pivot-a pivot-b rotation]299 (println-repl "creating POINT2POINT joint")300 ;; bullet's point2point joints are BROKEN, so we must use the301 ;; generic 6DOF joint instead of an actual Point2Point joint!303 ;; should be able to do this:304 (comment305 (Point2PointJoint.306 control-a307 control-b308 pivot-a309 pivot-b))311 ;; but instead we must do this:312 (println-repl "substituting 6DOF joint for POINT2POINT joint!")313 (doto314 (SixDofJoint.315 control-a316 control-b317 pivot-a318 pivot-b319 false)320 (.setLinearLowerLimit Vector3f/ZERO)321 (.setLinearUpperLimit Vector3f/ZERO)))323 (defmethod joint-dispatch :hinge324 [constraints control-a control-b pivot-a pivot-b rotation]325 (println-repl "creating HINGE joint")326 (let [axis327 (if-let328 [axis (:axis constraints)]329 axis330 Vector3f/UNIT_X)331 [limit-1 limit-2] (:limit constraints)332 hinge-axis333 (.mult334 rotation335 (blender-to-jme axis))]336 (doto337 (HingeJoint.338 control-a339 control-b340 pivot-a341 pivot-b342 hinge-axis343 hinge-axis)344 (.setLimit limit-1 limit-2))))346 (defmethod joint-dispatch :cone347 [constraints control-a control-b pivot-a pivot-b rotation]348 (let [limit-xz (:limit-xz constraints)349 limit-xy (:limit-xy constraints)350 twist (:twist constraints)]352 (println-repl "creating CONE joint")353 (println-repl rotation)354 (println-repl355 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))356 (println-repl357 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))358 (println-repl359 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))360 (doto361 (ConeJoint.362 control-a363 control-b364 pivot-a365 pivot-b366 rotation367 rotation)368 (.setLimit (float limit-xz)369 (float limit-xy)370 (float twist)))))372 (defn connect373 "Create a joint between 'obj-a and 'obj-b at the location of374 'joint. The type of joint is determined by the metadata on 'joint.376 Here are some examples:377 {:type :point}378 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}379 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)381 {:type :cone :limit-xz 0]382 :limit-xy 0]383 :twist 0]} (use XZY rotation mode in blender!)"384 [#^Node obj-a #^Node obj-b #^Node joint]385 (let [control-a (.getControl obj-a RigidBodyControl)386 control-b (.getControl obj-b RigidBodyControl)387 joint-center (.getWorldTranslation joint)388 joint-rotation (.toRotationMatrix (.getWorldRotation joint))389 pivot-a (world-to-local obj-a joint-center)390 pivot-b (world-to-local obj-b joint-center)]392 (if-let [constraints393 (map-vals394 eval395 (read-string396 (meta-data joint "joint")))]397 ;; A side-effect of creating a joint registers398 ;; it with both physics objects which in turn399 ;; will register the joint with the physics system400 ;; when the simulation is started.401 (do402 (println-repl "creating joint between"403 (.getName obj-a) "and" (.getName obj-b))404 (joint-dispatch constraints405 control-a control-b406 pivot-a pivot-b407 joint-rotation))408 (println-repl "could not find joint meta-data!"))))409 #+end_src411 Creating joints is now a matter of applying =connect= to each joint412 node.414 #+name: joints-5415 #+begin_src clojure416 (defn joints!417 "Connect the solid parts of the creature with physical joints. The418 joints are taken from the \"joints\" node in the creature."419 [#^Node creature]420 (dorun421 (map422 (fn [joint]423 (let [[obj-a obj-b] (joint-targets creature joint)]424 (connect obj-a obj-b joint)))425 (joints creature))))426 #+end_src428 ** Round 3430 Now we can test the hand in all its glory.432 #+name: test-3433 #+begin_src clojure434 (in-ns 'cortex.test.body)436 (def debug-control437 {"key-h" (fn [world val]438 (if val (enable-debug world)))})440 (defn test-hand-3441 ([] (test-hand-3 false))442 ([record?]443 (world444 (nodify445 [(doto (hand)446 (physical!)447 (joints!))448 (floor)])449 (merge standard-debug-controls debug-control450 gravity-control)451 (comp452 #(Capture/captureVideo453 % (File. "/home/r/proj/cortex/render/body/3"))454 #(do (set-gravity % Vector3f/ZERO) %)455 setup)456 no-op)))457 #+end_src459 =physical!= makes the hand solid, then =joints!= connects each460 piece together.462 #+begin_html463 <div class="figure">464 <center>465 <video controls="controls" width="640">466 <source src="../video/full-hand.ogg" type="video/ogg"467 preload="none" poster="../images/aurellem-1280x480.png" />468 </video>469 <br> <a href="http://youtu.be/4affLfwSPP4"> YouTube </a>470 </center>471 <p>Now the hand is physical and has joints.</p>472 </div>473 #+end_html475 The joints are visualized as green connections between each segment476 for debug purposes. You can see that they correspond to the empty477 nodes in the blender file.479 * Wrap-Up!481 It is convenient to combine =physical!= and =joints!= into one482 function that completely creates the creature's physical body.484 #+name: joints-6485 #+begin_src clojure486 (defn body!487 "Endow the creature with a physical body connected with joints. The488 particulars of the joints and the masses of each body part are489 determined in blender."490 [#^Node creature]491 (physical! creature)492 (joints! creature))493 #+end_src495 * The Worm497 Going forward, I will use a model that is less complicated than the498 hand. It has two segments and one joint, and I call it the worm. All499 of the senses described in the following posts will be applied to this500 worm.502 #+name: test-4503 #+begin_src clojure504 (in-ns 'cortex.test.body)506 (defn worm []507 (load-blender-model508 "Models/test-creature/worm.blend"))510 (defn test-worm511 ([] (test-worm false))512 ([record?]513 (let [timer (RatchetTimer. 60)]514 (world515 (nodify516 [(doto (worm)517 (body!))518 (floor)])519 (merge standard-debug-controls debug-control)520 #(do521 (speed-up %)522 (light-up-everything %)523 (.setTimer % timer)524 (cortex.util/display-dialated-time % timer)525 (if record?526 (Capture/captureVideo527 % (File. "/home/r/proj/cortex/render/body/4"))))528 no-op))))529 #+end_src531 #+begin_html532 <div class="figure">533 <center>534 <video controls="controls" width="640">535 <source src="../video/worm-1.ogg" type="video/ogg"536 preload="none" poster="../images/aurellem-1280x480.png" />537 </video>538 <br> <a href="http://youtu.be/rFVXI0T3iSE"> YouTube </a>539 </center>540 <p>This worm model will be the platform onto which future senses will541 be grafted.</p>542 </div>543 #+end_html545 * Headers546 #+name: body-header547 #+begin_src clojure548 (ns cortex.body549 "Assemble a physical creature using the definitions found in a550 specially prepared blender file. Creates rigid bodies and joints so551 that a creature can have a physical presence in the simulation."552 {:author "Robert McIntyre"}553 (:use (cortex world util sense))554 (:use clojure.contrib.def)555 (:import556 (com.jme3.math Vector3f Quaternion Vector2f Matrix3f)557 (com.jme3.bullet.joints558 SixDofJoint Point2PointJoint HingeJoint ConeJoint)559 com.jme3.bullet.control.RigidBodyControl560 com.jme3.collision.CollisionResults561 com.jme3.bounding.BoundingBox562 com.jme3.scene.Node563 com.jme3.scene.Geometry564 com.jme3.bullet.collision.shapes.HullCollisionShape))565 #+end_src567 #+name: test-header568 #+begin_src clojure569 (ns cortex.test.body570 (:use (cortex world util body))571 (:import572 (com.aurellem.capture Capture RatchetTimer)573 (com.jme3.math Quaternion Vector3f ColorRGBA)574 java.io.File))575 #+end_src577 * Source578 - [[../src/cortex/body.clj][cortex.body]]579 - [[../src/cortex/test/body.clj][cortex.test.body]]580 - [[../assets/Models/test-creature/hand.blend][hand.blend]]581 - [[../assets/Models/test-creature/palm.png][UV-map-1]]582 - [[../assets/Models/test-creature/worm.blend][worm.blend]]583 - [[../assets/Models/test-creature/retina-small.png][UV-map-1]]584 - [[../assets/Models/test-creature/tip.png][UV-map-2]]585 #+html: <ul> <li> <a href="../org/body.org">This org file</a> </li> </ul>586 - [[http://hg.bortreb.com ][source-repository]]588 * Next589 The body I have made here exists without any senses or effectors. In590 the [[./vision.org][next post]], I'll give the creature eyes.592 * COMMENT Generate Source593 #+begin_src clojure :tangle ../src/cortex/body.clj594 <<body-header>>595 <<body-1>>596 <<joints-2>>597 <<joints-3>>598 <<joints-4>>599 <<joints-5>>600 <<joints-6>>601 #+end_src603 #+begin_src clojure :tangle ../src/cortex/test/body.clj604 <<test-header>>605 <<test-1>>606 <<test-2>>607 <<test-3>>608 <<test-4>>609 #+end_src