rlm@202
|
1 #+title: Building a Body
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@273
|
3 #+email: rlm@mit.edu
|
rlm@306
|
4 #+description: Simulating a body (movement, touch, proprioception) in jMonkeyEngine3.
|
rlm@4
|
5 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
6 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@4
|
7
|
rlm@202
|
8 * Design Constraints
|
rlm@202
|
9
|
rlm@202
|
10 I use [[www.blender.org/][blender]] to design bodies. The design of the bodies is
|
rlm@202
|
11 determined by the requirements of the AI that will use them. The
|
rlm@202
|
12 bodies must be easy for an AI to sense and control, and they must be
|
rlm@202
|
13 relatively simple for jMonkeyEngine to compute.
|
rlm@202
|
14
|
ocsenave@251
|
15 # I'm a secret test! :P
|
rlm@202
|
16 ** Bag of Bones
|
rlm@202
|
17
|
rlm@202
|
18 How to create such a body? One option I ultimately rejected is to use
|
rlm@202
|
19 blender's [[http://wiki.blender.org/index.php/Doc:2.6/Manual/Rigging/Armatures][armature]] system. The idea would have been to define a mesh
|
rlm@202
|
20 which describes the creature's entire body. To this you add an
|
rlm@273
|
21 skeleton which deforms this mesh. This technique is used extensively
|
rlm@202
|
22 to model humans and create realistic animations. It is hard to use for
|
rlm@202
|
23 my purposes because it is difficult to update the creature's Physics
|
rlm@202
|
24 Collision Mesh in tandem with its Geometric Mesh under the influence
|
rlm@306
|
25 of the armature. Without this the creature will not be able to grab
|
rlm@202
|
26 things in its environment, and it won't be able to tell where its
|
rlm@202
|
27 physical body is by using its eyes. Also, armatures do not specify
|
rlm@202
|
28 any rotational limits for a joint, making it hard to model elbows,
|
rlm@202
|
29 shoulders, etc.
|
rlm@202
|
30
|
rlm@202
|
31 ** EVE
|
rlm@202
|
32
|
rlm@202
|
33 Instead of using the human-like "deformable bag of bones" approach, I
|
rlm@202
|
34 decided to base my body plans on the robot EVE from the movie wall-E.
|
rlm@202
|
35
|
rlm@202
|
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.
|
rlm@202
|
37 [[../images/Eve.jpg]]
|
rlm@202
|
38
|
rlm@204
|
39 EVE's body is composed of several rigid components that are held
|
rlm@204
|
40 together by invisible joint constraints. This is what I mean by
|
rlm@204
|
41 "eve-like". The main reason that I use eve-style bodies is so that
|
rlm@204
|
42 there will be correspondence between the AI's vision and the physical
|
rlm@204
|
43 presence of its body. Each individual section is simulated by a
|
rlm@204
|
44 separate rigid body that corresponds exactly with its visual
|
rlm@204
|
45 representation and does not change. Sections are connected by
|
rlm@306
|
46 invisible joints that are well supported in jMonkeyEngine. Bullet, the
|
rlm@204
|
47 physics backend for jMonkeyEngine, can efficiently simulate hundreds
|
rlm@204
|
48 of rigid bodies connected by joints. Sections do not have to stay as
|
rlm@204
|
49 one piece forever; they can be dynamically replaced with multiple
|
rlm@204
|
50 sections to simulate splitting in two. This could be used to simulate
|
rlm@306
|
51 retractable claws or EVE's hands, which are able to coalesce into one
|
rlm@209
|
52 object in the movie.
|
rlm@202
|
53
|
rlm@202
|
54 * Solidifying the Body
|
rlm@202
|
55
|
rlm@202
|
56 Here is a hand designed eve-style in blender.
|
rlm@202
|
57
|
rlm@203
|
58 #+attr_html: width="755"
|
rlm@202
|
59 [[../images/hand-screenshot0.png]]
|
rlm@202
|
60
|
rlm@202
|
61 If we load it directly into jMonkeyEngine, we get this:
|
rlm@202
|
62
|
rlm@205
|
63 #+name: test-1
|
rlm@202
|
64 #+begin_src clojure
|
rlm@202
|
65 (def hand-path "Models/test-creature/hand.blend")
|
rlm@202
|
66
|
rlm@202
|
67 (defn hand [] (load-blender-model hand-path))
|
rlm@202
|
68
|
rlm@202
|
69 (defn setup [world]
|
rlm@202
|
70 (let [cam (.getCamera world)]
|
rlm@202
|
71 (println-repl cam)
|
rlm@202
|
72 (.setLocation
|
rlm@202
|
73 cam (Vector3f.
|
rlm@202
|
74 -6.9015837, 8.644911, 5.6043186))
|
rlm@202
|
75 (.setRotation
|
rlm@202
|
76 cam
|
rlm@202
|
77 (Quaternion.
|
rlm@202
|
78 0.14046453, 0.85894054, -0.34301838, 0.3533118)))
|
rlm@202
|
79 (light-up-everything world)
|
rlm@202
|
80 (.setTimer world (RatchetTimer. 60))
|
rlm@202
|
81 world)
|
rlm@202
|
82
|
rlm@283
|
83 (defn test-hand-1
|
rlm@283
|
84 ([] (test-hand-1 false))
|
rlm@283
|
85 ([record?]
|
rlm@283
|
86 (world (hand)
|
rlm@283
|
87 standard-debug-controls
|
rlm@283
|
88 (fn [world]
|
rlm@283
|
89 (if record?
|
rlm@283
|
90 (Capture/captureVideo
|
rlm@283
|
91 world
|
rlm@283
|
92 (File. "/home/r/proj/cortex/render/body/1")))
|
rlm@283
|
93 (setup world)) no-op)))
|
rlm@202
|
94 #+end_src
|
rlm@202
|
95
|
rlm@202
|
96
|
rlm@202
|
97 #+begin_src clojure :results silent
|
rlm@202
|
98 (.start (cortex.test.body/test-one))
|
rlm@202
|
99 #+end_src
|
rlm@202
|
100
|
rlm@202
|
101 #+begin_html
|
rlm@203
|
102 <div class="figure">
|
rlm@203
|
103 <center>
|
rlm@203
|
104 <video controls="controls" width="640">
|
rlm@202
|
105 <source src="../video/ghost-hand.ogg" type="video/ogg"
|
rlm@202
|
106 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@202
|
107 </video>
|
rlm@309
|
108 <br> <a href="http://youtu.be/9LZpwTIhjzE"> YouTube </a>
|
rlm@203
|
109 </center>
|
rlm@203
|
110 <p>The hand model directly loaded from blender. It has no physical
|
rlm@306
|
111 presence in the simulation. </p>
|
rlm@203
|
112 </div>
|
rlm@202
|
113 #+end_html
|
rlm@202
|
114
|
rlm@202
|
115 You will notice that the hand has no physical presence -- it's a
|
rlm@204
|
116 hologram through which everything passes. Therefore, the first thing
|
rlm@202
|
117 to do is to make it solid. Blender has physics simulation on par with
|
rlm@202
|
118 jMonkeyEngine (they both use bullet as their physics backend), but it
|
rlm@202
|
119 can be difficult to translate between the two systems, so for now I
|
rlm@209
|
120 specify the mass of each object as meta-data in blender and construct
|
rlm@209
|
121 the physics shape based on the mesh in jMonkeyEngine.
|
rlm@202
|
122
|
rlm@203
|
123 #+name: body-1
|
rlm@202
|
124 #+begin_src clojure
|
rlm@202
|
125 (defn physical!
|
rlm@202
|
126 "Iterate through the nodes in creature and make them real physical
|
rlm@202
|
127 objects in the simulation."
|
rlm@202
|
128 [#^Node creature]
|
rlm@202
|
129 (dorun
|
rlm@202
|
130 (map
|
rlm@202
|
131 (fn [geom]
|
rlm@202
|
132 (let [physics-control
|
rlm@202
|
133 (RigidBodyControl.
|
rlm@202
|
134 (HullCollisionShape.
|
rlm@202
|
135 (.getMesh geom))
|
rlm@202
|
136 (if-let [mass (meta-data geom "mass")]
|
rlm@202
|
137 (do
|
rlm@202
|
138 (println-repl
|
rlm@202
|
139 "setting" (.getName geom) "mass to" (float mass))
|
rlm@202
|
140 (float mass))
|
rlm@202
|
141 (float 1)))]
|
rlm@202
|
142 (.addControl geom physics-control)))
|
rlm@202
|
143 (filter #(isa? (class %) Geometry )
|
rlm@202
|
144 (node-seq creature)))))
|
rlm@202
|
145 #+end_src
|
rlm@202
|
146
|
rlm@273
|
147 =physical!)= iterates through a creature's node structure, creating
|
rlm@202
|
148 CollisionShapes for each geometry with the mass specified in that
|
rlm@202
|
149 geometry's meta-data.
|
rlm@202
|
150
|
rlm@205
|
151 #+name: test-2
|
rlm@0
|
152 #+begin_src clojure
|
rlm@202
|
153 (in-ns 'cortex.test.body)
|
rlm@160
|
154
|
rlm@209
|
155 (def gravity-control
|
rlm@202
|
156 {"key-g" (fn [world _]
|
rlm@209
|
157 (set-gravity world (Vector3f. 0 -9.81 0)))
|
rlm@209
|
158 "key-u" (fn [world _] (set-gravity world Vector3f/ZERO))})
|
rlm@209
|
159
|
rlm@202
|
160
|
rlm@202
|
161 (defn floor []
|
rlm@202
|
162 (box 10 3 10 :position (Vector3f. 0 -10 0)
|
rlm@202
|
163 :color ColorRGBA/Gray :mass 0))
|
rlm@202
|
164
|
rlm@283
|
165 (defn test-hand-2
|
rlm@283
|
166 ([] (test-hand-2 false))
|
rlm@283
|
167 ([record?]
|
rlm@283
|
168 (world
|
rlm@283
|
169 (nodify
|
rlm@283
|
170 [(doto (hand)
|
rlm@283
|
171 (physical!))
|
rlm@283
|
172 (floor)])
|
rlm@283
|
173 (merge standard-debug-controls gravity-control)
|
rlm@283
|
174 (fn [world]
|
rlm@283
|
175 (if record?
|
rlm@283
|
176 (Capture/captureVideo
|
rlm@283
|
177 world (File. "/home/r/proj/cortex/render/body/2")))
|
rlm@283
|
178 (set-gravity world Vector3f/ZERO)
|
rlm@283
|
179 (setup world))
|
rlm@283
|
180 no-op)))
|
rlm@202
|
181 #+end_src
|
rlm@202
|
182
|
rlm@202
|
183 #+begin_html
|
rlm@203
|
184 <div class="figure">
|
rlm@203
|
185 <center>
|
rlm@203
|
186 <video controls="controls" width="640">
|
rlm@202
|
187 <source src="../video/crumbly-hand.ogg" type="video/ogg"
|
rlm@202
|
188 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@202
|
189 </video>
|
rlm@309
|
190 <br> <a href="http://youtu.be/GEA1SACwpPg"> YouTube </a>
|
rlm@203
|
191 </center>
|
rlm@203
|
192 <p>The hand now has a physical presence, but there is nothing to hold
|
rlm@203
|
193 it together.</p>
|
rlm@203
|
194 </div>
|
rlm@202
|
195 #+end_html
|
rlm@202
|
196
|
rlm@202
|
197 Now that's some progress.
|
rlm@202
|
198
|
rlm@202
|
199 * Joints
|
rlm@202
|
200
|
rlm@209
|
201 Obviously, an AI is not going to be doing much while lying in pieces
|
rlm@209
|
202 on the floor. So, the next step to making a proper body is to connect
|
rlm@202
|
203 those pieces together with joints. jMonkeyEngine has a large array of
|
rlm@202
|
204 joints available via bullet, such as Point2Point, Cone, Hinge, and a
|
rlm@202
|
205 generic Six Degree of Freedom joint, with or without spring
|
rlm@202
|
206 restitution.
|
rlm@202
|
207
|
rlm@202
|
208 Although it should be possible to specify the joints using blender's
|
rlm@202
|
209 physics system, and then automatically import them with jMonkeyEngine,
|
rlm@202
|
210 the support isn't there yet, and there are a few problems with bullet
|
rlm@202
|
211 itself that need to be solved before it can happen.
|
rlm@202
|
212
|
rlm@202
|
213 So, I will use the same system for specifying joints as I will do for
|
rlm@202
|
214 some senses. Each joint is specified by an empty node whose parent
|
rlm@202
|
215 has the name "joints". Their orientation and meta-data determine what
|
rlm@202
|
216 joint is created.
|
rlm@202
|
217
|
rlm@203
|
218 #+attr_html: width="755"
|
rlm@209
|
219 #+caption: Joints hack in blender. Each empty node here will be transformed into a joint in jMonkeyEngine
|
rlm@202
|
220 [[../images/hand-screenshot1.png]]
|
rlm@202
|
221
|
rlm@203
|
222 The empty node in the upper right, highlighted in yellow, is the
|
rlm@306
|
223 parent node of all the empties which represent joints. The following
|
rlm@203
|
224 functions must do three things to translate these into real joints:
|
rlm@202
|
225
|
rlm@203
|
226 - Find the children of the "joints" node.
|
rlm@203
|
227 - Determine the two spatials the joint it meant to connect.
|
rlm@203
|
228 - Create the joint based on the meta-data of the empty node.
|
rlm@202
|
229
|
rlm@203
|
230 ** Finding the Joints
|
rlm@209
|
231
|
rlm@273
|
232 The higher order function =sense-nodes= from =cortex.sense= simplifies
|
rlm@209
|
233 the first task.
|
rlm@209
|
234
|
rlm@203
|
235 #+name: joints-2
|
rlm@203
|
236 #+begin_src clojure
|
rlm@203
|
237 (defvar
|
rlm@203
|
238 ^{:arglists '([creature])}
|
rlm@203
|
239 joints
|
rlm@203
|
240 (sense-nodes "joints")
|
rlm@203
|
241 "Return the children of the creature's \"joints\" node.")
|
rlm@203
|
242 #+end_src
|
rlm@202
|
243
|
rlm@203
|
244 ** Joint Targets and Orientation
|
rlm@203
|
245
|
rlm@306
|
246 This technique for finding a joint's targets is very similar to
|
rlm@273
|
247 =cortex.sense/closest-node=. A small cube, centered around the
|
rlm@203
|
248 empty-node, grows exponentially until it intersects two /physical/
|
rlm@203
|
249 objects. The objects are ordered according to the joint's rotation,
|
rlm@203
|
250 with the first one being the object that has more negative coordinates
|
rlm@203
|
251 in the joint's reference frame. Since the objects must be physical,
|
rlm@203
|
252 the empty-node itself escapes detection. Because the objects must be
|
rlm@273
|
253 physical, =joint-targets= must be called /after/ =physical!= is
|
rlm@203
|
254 called.
|
rlm@203
|
255
|
rlm@203
|
256 #+name: joints-3
|
rlm@202
|
257 #+begin_src clojure
|
rlm@135
|
258 (defn joint-targets
|
rlm@135
|
259 "Return the two closest two objects to the joint object, ordered
|
rlm@135
|
260 from bottom to top according to the joint's rotation."
|
rlm@135
|
261 [#^Node parts #^Node joint]
|
rlm@135
|
262 (loop [radius (float 0.01)]
|
rlm@135
|
263 (let [results (CollisionResults.)]
|
rlm@135
|
264 (.collideWith
|
rlm@135
|
265 parts
|
rlm@135
|
266 (BoundingBox. (.getWorldTranslation joint)
|
rlm@209
|
267 radius radius radius) results)
|
rlm@135
|
268 (let [targets
|
rlm@135
|
269 (distinct
|
rlm@135
|
270 (map #(.getGeometry %) results))]
|
rlm@135
|
271 (if (>= (count targets) 2)
|
rlm@135
|
272 (sort-by
|
rlm@209
|
273 #(let [joint-ref-frame-position
|
rlm@135
|
274 (jme-to-blender
|
rlm@135
|
275 (.mult
|
rlm@135
|
276 (.inverse (.getWorldRotation joint))
|
rlm@135
|
277 (.subtract (.getWorldTranslation %)
|
rlm@135
|
278 (.getWorldTranslation joint))))]
|
rlm@209
|
279 (.dot (Vector3f. 1 1 1) joint-ref-frame-position))
|
rlm@135
|
280 (take 2 targets))
|
rlm@135
|
281 (recur (float (* radius 2))))))))
|
rlm@203
|
282 #+end_src
|
rlm@135
|
283
|
rlm@203
|
284 ** Generating Joints
|
rlm@203
|
285
|
rlm@209
|
286 This section of code iterates through all the different ways of
|
rlm@203
|
287 specifying joints using blender meta-data and converts each one to the
|
rlm@306
|
288 appropriate jMonkeyEngine joint.
|
rlm@203
|
289
|
rlm@203
|
290 #+name: joints-4
|
rlm@203
|
291 #+begin_src clojure
|
rlm@160
|
292 (defmulti joint-dispatch
|
rlm@160
|
293 "Translate blender pseudo-joints into real JME joints."
|
rlm@160
|
294 (fn [constraints & _]
|
rlm@160
|
295 (:type constraints)))
|
rlm@141
|
296
|
rlm@160
|
297 (defmethod joint-dispatch :point
|
rlm@160
|
298 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
299 (println-repl "creating POINT2POINT joint")
|
rlm@160
|
300 ;; bullet's point2point joints are BROKEN, so we must use the
|
rlm@160
|
301 ;; generic 6DOF joint instead of an actual Point2Point joint!
|
rlm@141
|
302
|
rlm@160
|
303 ;; should be able to do this:
|
rlm@160
|
304 (comment
|
rlm@160
|
305 (Point2PointJoint.
|
rlm@160
|
306 control-a
|
rlm@160
|
307 control-b
|
rlm@160
|
308 pivot-a
|
rlm@160
|
309 pivot-b))
|
rlm@141
|
310
|
rlm@160
|
311 ;; but instead we must do this:
|
rlm@306
|
312 (println-repl "substituting 6DOF joint for POINT2POINT joint!")
|
rlm@160
|
313 (doto
|
rlm@160
|
314 (SixDofJoint.
|
rlm@160
|
315 control-a
|
rlm@160
|
316 control-b
|
rlm@160
|
317 pivot-a
|
rlm@160
|
318 pivot-b
|
rlm@160
|
319 false)
|
rlm@160
|
320 (.setLinearLowerLimit Vector3f/ZERO)
|
rlm@203
|
321 (.setLinearUpperLimit Vector3f/ZERO)))
|
rlm@160
|
322
|
rlm@160
|
323 (defmethod joint-dispatch :hinge
|
rlm@160
|
324 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
325 (println-repl "creating HINGE joint")
|
rlm@160
|
326 (let [axis
|
rlm@160
|
327 (if-let
|
rlm@160
|
328 [axis (:axis constraints)]
|
rlm@160
|
329 axis
|
rlm@160
|
330 Vector3f/UNIT_X)
|
rlm@160
|
331 [limit-1 limit-2] (:limit constraints)
|
rlm@160
|
332 hinge-axis
|
rlm@160
|
333 (.mult
|
rlm@160
|
334 rotation
|
rlm@160
|
335 (blender-to-jme axis))]
|
rlm@160
|
336 (doto
|
rlm@160
|
337 (HingeJoint.
|
rlm@160
|
338 control-a
|
rlm@160
|
339 control-b
|
rlm@160
|
340 pivot-a
|
rlm@160
|
341 pivot-b
|
rlm@160
|
342 hinge-axis
|
rlm@160
|
343 hinge-axis)
|
rlm@160
|
344 (.setLimit limit-1 limit-2))))
|
rlm@160
|
345
|
rlm@160
|
346 (defmethod joint-dispatch :cone
|
rlm@160
|
347 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
348 (let [limit-xz (:limit-xz constraints)
|
rlm@160
|
349 limit-xy (:limit-xy constraints)
|
rlm@160
|
350 twist (:twist constraints)]
|
rlm@160
|
351
|
rlm@160
|
352 (println-repl "creating CONE joint")
|
rlm@160
|
353 (println-repl rotation)
|
rlm@160
|
354 (println-repl
|
rlm@160
|
355 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
|
rlm@160
|
356 (println-repl
|
rlm@160
|
357 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
|
rlm@160
|
358 (println-repl
|
rlm@160
|
359 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
|
rlm@160
|
360 (doto
|
rlm@160
|
361 (ConeJoint.
|
rlm@160
|
362 control-a
|
rlm@160
|
363 control-b
|
rlm@160
|
364 pivot-a
|
rlm@160
|
365 pivot-b
|
rlm@160
|
366 rotation
|
rlm@160
|
367 rotation)
|
rlm@160
|
368 (.setLimit (float limit-xz)
|
rlm@160
|
369 (float limit-xy)
|
rlm@160
|
370 (float twist)))))
|
rlm@160
|
371
|
rlm@160
|
372 (defn connect
|
rlm@175
|
373 "Create a joint between 'obj-a and 'obj-b at the location of
|
rlm@175
|
374 'joint. The type of joint is determined by the metadata on 'joint.
|
rlm@175
|
375
|
rlm@175
|
376 Here are some examples:
|
rlm@160
|
377 {:type :point}
|
rlm@160
|
378 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@160
|
379 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@160
|
380
|
rlm@160
|
381 {:type :cone :limit-xz 0]
|
rlm@160
|
382 :limit-xy 0]
|
rlm@160
|
383 :twist 0]} (use XZY rotation mode in blender!)"
|
rlm@160
|
384 [#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@160
|
385 (let [control-a (.getControl obj-a RigidBodyControl)
|
rlm@160
|
386 control-b (.getControl obj-b RigidBodyControl)
|
rlm@160
|
387 joint-center (.getWorldTranslation joint)
|
rlm@160
|
388 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
|
rlm@160
|
389 pivot-a (world-to-local obj-a joint-center)
|
rlm@160
|
390 pivot-b (world-to-local obj-b joint-center)]
|
rlm@160
|
391
|
rlm@160
|
392 (if-let [constraints
|
rlm@160
|
393 (map-vals
|
rlm@160
|
394 eval
|
rlm@160
|
395 (read-string
|
rlm@160
|
396 (meta-data joint "joint")))]
|
rlm@160
|
397 ;; A side-effect of creating a joint registers
|
rlm@160
|
398 ;; it with both physics objects which in turn
|
rlm@160
|
399 ;; will register the joint with the physics system
|
rlm@160
|
400 ;; when the simulation is started.
|
rlm@160
|
401 (do
|
rlm@160
|
402 (println-repl "creating joint between"
|
rlm@160
|
403 (.getName obj-a) "and" (.getName obj-b))
|
rlm@160
|
404 (joint-dispatch constraints
|
rlm@160
|
405 control-a control-b
|
rlm@160
|
406 pivot-a pivot-b
|
rlm@160
|
407 joint-rotation))
|
rlm@160
|
408 (println-repl "could not find joint meta-data!"))))
|
rlm@203
|
409 #+end_src
|
rlm@160
|
410
|
rlm@273
|
411 Creating joints is now a matter of applying =connect= to each joint
|
rlm@203
|
412 node.
|
rlm@160
|
413
|
rlm@205
|
414 #+name: joints-5
|
rlm@203
|
415 #+begin_src clojure
|
rlm@175
|
416 (defn joints!
|
rlm@175
|
417 "Connect the solid parts of the creature with physical joints. The
|
rlm@175
|
418 joints are taken from the \"joints\" node in the creature."
|
rlm@175
|
419 [#^Node creature]
|
rlm@160
|
420 (dorun
|
rlm@160
|
421 (map
|
rlm@160
|
422 (fn [joint]
|
rlm@175
|
423 (let [[obj-a obj-b] (joint-targets creature joint)]
|
rlm@160
|
424 (connect obj-a obj-b joint)))
|
rlm@175
|
425 (joints creature))))
|
rlm@203
|
426 #+end_src
|
rlm@160
|
427
|
rlm@203
|
428 ** Round 3
|
rlm@203
|
429
|
rlm@203
|
430 Now we can test the hand in all its glory.
|
rlm@203
|
431
|
rlm@205
|
432 #+name: test-3
|
rlm@203
|
433 #+begin_src clojure
|
rlm@203
|
434 (in-ns 'cortex.test.body)
|
rlm@203
|
435
|
rlm@203
|
436 (def debug-control
|
rlm@203
|
437 {"key-h" (fn [world val]
|
rlm@209
|
438 (if val (enable-debug world)))})
|
rlm@203
|
439
|
rlm@283
|
440 (defn test-hand-3
|
rlm@283
|
441 ([] (test-hand-3 false))
|
rlm@283
|
442 ([record?]
|
rlm@283
|
443 (world
|
rlm@283
|
444 (nodify
|
rlm@283
|
445 [(doto (hand)
|
rlm@283
|
446 (physical!)
|
rlm@283
|
447 (joints!))
|
rlm@283
|
448 (floor)])
|
rlm@283
|
449 (merge standard-debug-controls debug-control
|
rlm@283
|
450 gravity-control)
|
rlm@283
|
451 (comp
|
rlm@283
|
452 #(Capture/captureVideo
|
rlm@283
|
453 % (File. "/home/r/proj/cortex/render/body/3"))
|
rlm@283
|
454 #(do (set-gravity % Vector3f/ZERO) %)
|
rlm@283
|
455 setup)
|
rlm@283
|
456 no-op)))
|
rlm@203
|
457 #+end_src
|
rlm@203
|
458
|
rlm@273
|
459 =physical!= makes the hand solid, then =joints!= connects each
|
rlm@203
|
460 piece together.
|
rlm@203
|
461
|
rlm@203
|
462 #+begin_html
|
rlm@203
|
463 <div class="figure">
|
rlm@203
|
464 <center>
|
rlm@203
|
465 <video controls="controls" width="640">
|
rlm@203
|
466 <source src="../video/full-hand.ogg" type="video/ogg"
|
rlm@203
|
467 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@203
|
468 </video>
|
rlm@309
|
469 <br> <a href="http://youtu.be/4affLfwSPP4"> YouTube </a>
|
rlm@203
|
470 </center>
|
rlm@203
|
471 <p>Now the hand is physical and has joints.</p>
|
rlm@203
|
472 </div>
|
rlm@203
|
473 #+end_html
|
rlm@203
|
474
|
rlm@203
|
475 The joints are visualized as green connections between each segment
|
rlm@203
|
476 for debug purposes. You can see that they correspond to the empty
|
rlm@203
|
477 nodes in the blender file.
|
rlm@203
|
478
|
rlm@203
|
479 * Wrap-Up!
|
rlm@203
|
480
|
rlm@306
|
481 It is convenient to combine =physical!= and =joints!= into one
|
rlm@203
|
482 function that completely creates the creature's physical body.
|
rlm@203
|
483
|
rlm@205
|
484 #+name: joints-6
|
rlm@203
|
485 #+begin_src clojure
|
rlm@175
|
486 (defn body!
|
rlm@175
|
487 "Endow the creature with a physical body connected with joints. The
|
rlm@306
|
488 particulars of the joints and the masses of each body part are
|
rlm@175
|
489 determined in blender."
|
rlm@175
|
490 [#^Node creature]
|
rlm@175
|
491 (physical! creature)
|
rlm@175
|
492 (joints! creature))
|
rlm@64
|
493 #+end_src
|
rlm@63
|
494
|
rlm@205
|
495 * The Worm
|
rlm@205
|
496
|
rlm@205
|
497 Going forward, I will use a model that is less complicated than the
|
rlm@205
|
498 hand. It has two segments and one joint, and I call it the worm. All
|
rlm@205
|
499 of the senses described in the following posts will be applied to this
|
rlm@205
|
500 worm.
|
rlm@205
|
501
|
rlm@205
|
502 #+name: test-4
|
rlm@205
|
503 #+begin_src clojure
|
rlm@205
|
504 (in-ns 'cortex.test.body)
|
rlm@205
|
505
|
rlm@215
|
506 (defn worm []
|
rlm@215
|
507 (load-blender-model
|
rlm@215
|
508 "Models/test-creature/worm.blend"))
|
rlm@215
|
509
|
rlm@283
|
510 (defn test-worm
|
rlm@283
|
511 ([] (test-worm false))
|
rlm@283
|
512 ([record?]
|
rlm@283
|
513 (let [timer (RatchetTimer. 60)]
|
rlm@283
|
514 (world
|
rlm@283
|
515 (nodify
|
rlm@283
|
516 [(doto (worm)
|
rlm@283
|
517 (body!))
|
rlm@283
|
518 (floor)])
|
rlm@283
|
519 (merge standard-debug-controls debug-control)
|
rlm@283
|
520 #(do
|
rlm@283
|
521 (speed-up %)
|
rlm@283
|
522 (light-up-everything %)
|
rlm@283
|
523 (.setTimer % timer)
|
rlm@283
|
524 (cortex.util/display-dialated-time % timer)
|
rlm@283
|
525 (if record?
|
rlm@283
|
526 (Capture/captureVideo
|
rlm@283
|
527 % (File. "/home/r/proj/cortex/render/body/4"))))
|
rlm@283
|
528 no-op))))
|
rlm@205
|
529 #+end_src
|
rlm@205
|
530
|
rlm@205
|
531 #+begin_html
|
rlm@205
|
532 <div class="figure">
|
rlm@205
|
533 <center>
|
rlm@205
|
534 <video controls="controls" width="640">
|
rlm@205
|
535 <source src="../video/worm-1.ogg" type="video/ogg"
|
rlm@205
|
536 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@205
|
537 </video>
|
rlm@309
|
538 <br> <a href="http://youtu.be/rFVXI0T3iSE"> YouTube </a>
|
rlm@205
|
539 </center>
|
rlm@205
|
540 <p>This worm model will be the platform onto which future senses will
|
rlm@205
|
541 be grafted.</p>
|
rlm@205
|
542 </div>
|
rlm@205
|
543 #+end_html
|
rlm@205
|
544
|
rlm@209
|
545 * Headers
|
rlm@205
|
546 #+name: body-header
|
rlm@202
|
547 #+begin_src clojure
|
rlm@202
|
548 (ns cortex.body
|
rlm@202
|
549 "Assemble a physical creature using the definitions found in a
|
rlm@202
|
550 specially prepared blender file. Creates rigid bodies and joints so
|
rlm@306
|
551 that a creature can have a physical presence in the simulation."
|
rlm@202
|
552 {:author "Robert McIntyre"}
|
rlm@202
|
553 (:use (cortex world util sense))
|
rlm@202
|
554 (:use clojure.contrib.def)
|
rlm@202
|
555 (:import
|
rlm@202
|
556 (com.jme3.math Vector3f Quaternion Vector2f Matrix3f)
|
rlm@202
|
557 (com.jme3.bullet.joints
|
rlm@202
|
558 SixDofJoint Point2PointJoint HingeJoint ConeJoint)
|
rlm@202
|
559 com.jme3.bullet.control.RigidBodyControl
|
rlm@202
|
560 com.jme3.collision.CollisionResults
|
rlm@202
|
561 com.jme3.bounding.BoundingBox
|
rlm@202
|
562 com.jme3.scene.Node
|
rlm@202
|
563 com.jme3.scene.Geometry
|
rlm@202
|
564 com.jme3.bullet.collision.shapes.HullCollisionShape))
|
rlm@202
|
565 #+end_src
|
rlm@133
|
566
|
rlm@205
|
567 #+name: test-header
|
rlm@205
|
568 #+begin_src clojure
|
rlm@205
|
569 (ns cortex.test.body
|
rlm@205
|
570 (:use (cortex world util body))
|
rlm@205
|
571 (:import
|
rlm@205
|
572 (com.aurellem.capture Capture RatchetTimer)
|
rlm@205
|
573 (com.jme3.math Quaternion Vector3f ColorRGBA)
|
rlm@205
|
574 java.io.File))
|
rlm@205
|
575 #+end_src
|
rlm@205
|
576
|
rlm@202
|
577 * Source
|
rlm@207
|
578 - [[../src/cortex/body.clj][cortex.body]]
|
rlm@207
|
579 - [[../src/cortex/test/body.clj][cortex.test.body]]
|
rlm@207
|
580 - [[../assets/Models/test-creature/hand.blend][hand.blend]]
|
rlm@209
|
581 - [[../assets/Models/test-creature/palm.png][UV-map-1]]
|
rlm@207
|
582 - [[../assets/Models/test-creature/worm.blend][worm.blend]]
|
rlm@207
|
583 - [[../assets/Models/test-creature/retina-small.png][UV-map-1]]
|
rlm@207
|
584 - [[../assets/Models/test-creature/tip.png][UV-map-2]]
|
rlm@211
|
585 #+html: <ul> <li> <a href="../org/body.org">This org file</a> </li> </ul>
|
rlm@217
|
586 - [[http://hg.bortreb.com ][source-repository]]
|
rlm@211
|
587
|
rlm@211
|
588 * Next
|
rlm@211
|
589 The body I have made here exists without any senses or effectors. In
|
rlm@211
|
590 the [[./vision.org][next post]], I'll give the creature eyes.
|
rlm@63
|
591
|
rlm@206
|
592 * COMMENT Generate Source
|
rlm@44
|
593 #+begin_src clojure :tangle ../src/cortex/body.clj
|
rlm@205
|
594 <<body-header>>
|
rlm@205
|
595 <<body-1>>
|
rlm@205
|
596 <<joints-2>>
|
rlm@205
|
597 <<joints-3>>
|
rlm@205
|
598 <<joints-4>>
|
rlm@205
|
599 <<joints-5>>
|
rlm@205
|
600 <<joints-6>>
|
rlm@0
|
601 #+end_src
|
rlm@64
|
602
|
rlm@69
|
603 #+begin_src clojure :tangle ../src/cortex/test/body.clj
|
rlm@205
|
604 <<test-header>>
|
rlm@205
|
605 <<test-1>>
|
rlm@205
|
606 <<test-2>>
|
rlm@205
|
607 <<test-3>>
|
rlm@205
|
608 <<test-4>>
|
rlm@64
|
609 #+end_src
|
rlm@64
|
610
|
rlm@64
|
611
|
rlm@0
|
612
|
rlm@206
|
613
|