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