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@202
|
39 The main reason that I use eve-style bodies is so that there will be
|
rlm@202
|
40 correspondence between the AI's vision and the physical presence of
|
rlm@203
|
41 its body. Each individual section is simulated by a separate rigid
|
rlm@203
|
42 body that corresponds exactly with its visual representation and does
|
rlm@203
|
43 not change. Sections are connected by invisible joints that are well
|
rlm@203
|
44 supported in jMonkyeEngine. Bullet, the physics backend for
|
rlm@203
|
45 jMonkeyEngine, can efficiently simulate hundreds of rigid bodies
|
rlm@203
|
46 connected by joints. Sections do not have to stay as one piece
|
rlm@203
|
47 forever; they can be dynamically replaced with multiple sections to
|
rlm@203
|
48 simulate splitting in two. This could be used to simulate retractable
|
rlm@203
|
49 claws or EVE's hands, which could coalece into one object in the
|
rlm@203
|
50 movie.
|
rlm@202
|
51
|
rlm@202
|
52 * Solidifying the Body
|
rlm@202
|
53
|
rlm@202
|
54 Here is a hand designed eve-style in blender.
|
rlm@202
|
55
|
rlm@203
|
56 #+attr_html: width="755"
|
rlm@202
|
57 [[../images/hand-screenshot0.png]]
|
rlm@202
|
58
|
rlm@202
|
59 If we load it directly into jMonkeyEngine, we get this:
|
rlm@202
|
60
|
rlm@202
|
61 #+name: test-0
|
rlm@202
|
62 #+begin_src clojure
|
rlm@202
|
63 (ns cortex.test.body
|
rlm@202
|
64 (:use (cortex world util body))
|
rlm@202
|
65 (:import (com.aurellem.capture Capture RatchetTimer)
|
rlm@202
|
66 (com.jme3.math Quaternion Vector3f)
|
rlm@202
|
67 java.io.File))
|
rlm@202
|
68
|
rlm@202
|
69 (def hand-path "Models/test-creature/hand.blend")
|
rlm@202
|
70
|
rlm@202
|
71 (defn hand [] (load-blender-model hand-path))
|
rlm@202
|
72
|
rlm@202
|
73 (defn setup [world]
|
rlm@202
|
74 (let [cam (.getCamera world)]
|
rlm@202
|
75 (println-repl cam)
|
rlm@202
|
76 (.setLocation
|
rlm@202
|
77 cam (Vector3f.
|
rlm@202
|
78 -6.9015837, 8.644911, 5.6043186))
|
rlm@202
|
79 (.setRotation
|
rlm@202
|
80 cam
|
rlm@202
|
81 (Quaternion.
|
rlm@202
|
82 0.14046453, 0.85894054, -0.34301838, 0.3533118)))
|
rlm@202
|
83 (light-up-everything world)
|
rlm@202
|
84 (.setTimer world (RatchetTimer. 60))
|
rlm@202
|
85 world)
|
rlm@202
|
86
|
rlm@202
|
87 (defn test-one []
|
rlm@202
|
88 (world (hand)
|
rlm@202
|
89 standard-debug-controls
|
rlm@202
|
90 (comp
|
rlm@202
|
91 #(Capture/captureVideo
|
rlm@202
|
92 % (File. "/home/r/proj/cortex/render/body/1"))
|
rlm@202
|
93 setup)
|
rlm@202
|
94 no-op))
|
rlm@202
|
95 #+end_src
|
rlm@202
|
96
|
rlm@202
|
97
|
rlm@202
|
98 #+begin_src clojure :results silent
|
rlm@202
|
99 (.start (cortex.test.body/test-one))
|
rlm@202
|
100 #+end_src
|
rlm@202
|
101
|
rlm@202
|
102 #+begin_html
|
rlm@203
|
103 <div class="figure">
|
rlm@203
|
104 <center>
|
rlm@203
|
105 <video controls="controls" width="640">
|
rlm@202
|
106 <source src="../video/ghost-hand.ogg" type="video/ogg"
|
rlm@202
|
107 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@202
|
108 </video>
|
rlm@203
|
109 </center>
|
rlm@203
|
110 <p>The hand model directly loaded from blender. It has no physical
|
rlm@203
|
111 presense 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@202
|
116 hologram through witch 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@202
|
120 specify the mass of each object in blender and construct the physics
|
rlm@202
|
121 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@202
|
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@203
|
151 #+name: test-1
|
rlm@0
|
152 #+begin_src clojure
|
rlm@202
|
153 (in-ns 'cortex.test.body)
|
rlm@160
|
154
|
rlm@202
|
155 (def normal-gravity
|
rlm@202
|
156 {"key-g" (fn [world _]
|
rlm@202
|
157 (set-gravity world (Vector3f. 0 -9.81 0)))})
|
rlm@202
|
158
|
rlm@202
|
159 (defn floor []
|
rlm@202
|
160 (box 10 3 10 :position (Vector3f. 0 -10 0)
|
rlm@202
|
161 :color ColorRGBA/Gray :mass 0))
|
rlm@202
|
162
|
rlm@202
|
163 (defn test-two []
|
rlm@202
|
164 (world (nodify
|
rlm@202
|
165 [(doto (hand)
|
rlm@202
|
166 (physical!))
|
rlm@202
|
167 (floor)])
|
rlm@202
|
168 (merge standard-debug-controls normal-gravity)
|
rlm@202
|
169 (comp
|
rlm@202
|
170 #(Capture/captureVideo
|
rlm@202
|
171 % (File. "/home/r/proj/cortex/render/body/2"))
|
rlm@202
|
172 #(do (set-gravity % Vector3f/ZERO) %)
|
rlm@202
|
173 setup)
|
rlm@202
|
174 no-op))
|
rlm@202
|
175 #+end_src
|
rlm@202
|
176
|
rlm@202
|
177 #+begin_html
|
rlm@203
|
178 <div class="figure">
|
rlm@203
|
179 <center>
|
rlm@203
|
180 <video controls="controls" width="640">
|
rlm@202
|
181 <source src="../video/crumbly-hand.ogg" type="video/ogg"
|
rlm@202
|
182 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@202
|
183 </video>
|
rlm@203
|
184 </center>
|
rlm@203
|
185 <p>The hand now has a physical presence, but there is nothing to hold
|
rlm@203
|
186 it together.</p>
|
rlm@203
|
187 </div>
|
rlm@202
|
188 #+end_html
|
rlm@202
|
189
|
rlm@202
|
190 Now that's some progress.
|
rlm@202
|
191
|
rlm@202
|
192
|
rlm@202
|
193 * Joints
|
rlm@202
|
194
|
rlm@202
|
195 Obviously, an AI is not going to be doing much just lying in pieces on
|
rlm@202
|
196 the floor. So, the next step to making a proper body is to connect
|
rlm@202
|
197 those pieces together with joints. jMonkeyEngine has a large array of
|
rlm@202
|
198 joints available via bullet, such as Point2Point, Cone, Hinge, and a
|
rlm@202
|
199 generic Six Degree of Freedom joint, with or without spring
|
rlm@202
|
200 restitution.
|
rlm@202
|
201
|
rlm@202
|
202 Although it should be possible to specify the joints using blender's
|
rlm@202
|
203 physics system, and then automatically import them with jMonkeyEngine,
|
rlm@202
|
204 the support isn't there yet, and there are a few problems with bullet
|
rlm@202
|
205 itself that need to be solved before it can happen.
|
rlm@202
|
206
|
rlm@202
|
207 So, I will use the same system for specifying joints as I will do for
|
rlm@202
|
208 some senses. Each joint is specified by an empty node whose parent
|
rlm@202
|
209 has the name "joints". Their orientation and meta-data determine what
|
rlm@202
|
210 joint is created.
|
rlm@202
|
211
|
rlm@203
|
212 #+attr_html: width="755"
|
rlm@203
|
213 #+caption: joints hack in blender. Each empty node here will be transformed into a joint in jMonkeyEngine
|
rlm@202
|
214 [[../images/hand-screenshot1.png]]
|
rlm@202
|
215
|
rlm@203
|
216 The empty node in the upper right, highlighted in yellow, is the
|
rlm@203
|
217 parent node of all the emptys which represent joints. The following
|
rlm@203
|
218 functions must do three things to translate these into real joints:
|
rlm@202
|
219
|
rlm@203
|
220 - Find the children of the "joints" node.
|
rlm@203
|
221 - Determine the two spatials the joint it meant to connect.
|
rlm@203
|
222 - Create the joint based on the meta-data of the empty node.
|
rlm@202
|
223
|
rlm@203
|
224 ** Finding the Joints
|
rlm@203
|
225 #+name: joints-2
|
rlm@203
|
226 #+begin_src clojure
|
rlm@203
|
227 (defvar
|
rlm@203
|
228 ^{:arglists '([creature])}
|
rlm@203
|
229 joints
|
rlm@203
|
230 (sense-nodes "joints")
|
rlm@203
|
231 "Return the children of the creature's \"joints\" node.")
|
rlm@203
|
232 #+end_src
|
rlm@202
|
233
|
rlm@203
|
234 The higher order function =(sense-nodes)= from cortex.sense makes our
|
rlm@203
|
235 first task very easy.
|
rlm@203
|
236
|
rlm@203
|
237 ** Joint Targets and Orientation
|
rlm@203
|
238
|
rlm@203
|
239 This technique for finding a joint's targets is very similiar to
|
rlm@203
|
240 =(cortex.sense/closest-node)=. A small cube, centered around the
|
rlm@203
|
241 empty-node, grows exponentially until it intersects two /physical/
|
rlm@203
|
242 objects. The objects are ordered according to the joint's rotation,
|
rlm@203
|
243 with the first one being the object that has more negative coordinates
|
rlm@203
|
244 in the joint's reference frame. Since the objects must be physical,
|
rlm@203
|
245 the empty-node itself escapes detection. Because the objects must be
|
rlm@203
|
246 physical, =(joint-targets)= must be called /after/ =(physical!)= is
|
rlm@203
|
247 called.
|
rlm@203
|
248
|
rlm@203
|
249 #+name: joints-3
|
rlm@202
|
250 #+begin_src clojure
|
rlm@135
|
251 (defn joint-targets
|
rlm@135
|
252 "Return the two closest two objects to the joint object, ordered
|
rlm@135
|
253 from bottom to top according to the joint's rotation."
|
rlm@135
|
254 [#^Node parts #^Node joint]
|
rlm@135
|
255 (loop [radius (float 0.01)]
|
rlm@135
|
256 (let [results (CollisionResults.)]
|
rlm@135
|
257 (.collideWith
|
rlm@135
|
258 parts
|
rlm@135
|
259 (BoundingBox. (.getWorldTranslation joint)
|
rlm@135
|
260 radius radius radius)
|
rlm@135
|
261 results)
|
rlm@135
|
262 (let [targets
|
rlm@135
|
263 (distinct
|
rlm@135
|
264 (map #(.getGeometry %) results))]
|
rlm@135
|
265 (if (>= (count targets) 2)
|
rlm@135
|
266 (sort-by
|
rlm@135
|
267 #(let [v
|
rlm@135
|
268 (jme-to-blender
|
rlm@135
|
269 (.mult
|
rlm@135
|
270 (.inverse (.getWorldRotation joint))
|
rlm@135
|
271 (.subtract (.getWorldTranslation %)
|
rlm@135
|
272 (.getWorldTranslation joint))))]
|
rlm@135
|
273 (println-repl (.getName %) ":" v)
|
rlm@135
|
274 (.dot (Vector3f. 1 1 1)
|
rlm@135
|
275 v))
|
rlm@135
|
276 (take 2 targets))
|
rlm@135
|
277 (recur (float (* radius 2))))))))
|
rlm@203
|
278 #+end_src
|
rlm@135
|
279
|
rlm@203
|
280 ** Generating Joints
|
rlm@203
|
281
|
rlm@203
|
282 This long chunk of code iterates through all the different ways of
|
rlm@203
|
283 specifying joints using blender meta-data and converts each one to the
|
rlm@203
|
284 appropriate jMonkyeEngine joint.
|
rlm@203
|
285
|
rlm@203
|
286 #+name: joints-4
|
rlm@203
|
287 #+begin_src clojure
|
rlm@160
|
288 (defmulti joint-dispatch
|
rlm@160
|
289 "Translate blender pseudo-joints into real JME joints."
|
rlm@160
|
290 (fn [constraints & _]
|
rlm@160
|
291 (:type constraints)))
|
rlm@141
|
292
|
rlm@160
|
293 (defmethod joint-dispatch :point
|
rlm@160
|
294 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
295 (println-repl "creating POINT2POINT joint")
|
rlm@160
|
296 ;; bullet's point2point joints are BROKEN, so we must use the
|
rlm@160
|
297 ;; generic 6DOF joint instead of an actual Point2Point joint!
|
rlm@141
|
298
|
rlm@160
|
299 ;; should be able to do this:
|
rlm@160
|
300 (comment
|
rlm@160
|
301 (Point2PointJoint.
|
rlm@160
|
302 control-a
|
rlm@160
|
303 control-b
|
rlm@160
|
304 pivot-a
|
rlm@160
|
305 pivot-b))
|
rlm@141
|
306
|
rlm@160
|
307 ;; but instead we must do this:
|
rlm@160
|
308 (println-repl "substuting 6DOF joint for POINT2POINT joint!")
|
rlm@160
|
309 (doto
|
rlm@160
|
310 (SixDofJoint.
|
rlm@160
|
311 control-a
|
rlm@160
|
312 control-b
|
rlm@160
|
313 pivot-a
|
rlm@160
|
314 pivot-b
|
rlm@160
|
315 false)
|
rlm@160
|
316 (.setLinearLowerLimit Vector3f/ZERO)
|
rlm@203
|
317 (.setLinearUpperLimit Vector3f/ZERO)))
|
rlm@160
|
318
|
rlm@160
|
319 (defmethod joint-dispatch :hinge
|
rlm@160
|
320 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
321 (println-repl "creating HINGE joint")
|
rlm@160
|
322 (let [axis
|
rlm@160
|
323 (if-let
|
rlm@160
|
324 [axis (:axis constraints)]
|
rlm@160
|
325 axis
|
rlm@160
|
326 Vector3f/UNIT_X)
|
rlm@160
|
327 [limit-1 limit-2] (:limit constraints)
|
rlm@160
|
328 hinge-axis
|
rlm@160
|
329 (.mult
|
rlm@160
|
330 rotation
|
rlm@160
|
331 (blender-to-jme axis))]
|
rlm@160
|
332 (doto
|
rlm@160
|
333 (HingeJoint.
|
rlm@160
|
334 control-a
|
rlm@160
|
335 control-b
|
rlm@160
|
336 pivot-a
|
rlm@160
|
337 pivot-b
|
rlm@160
|
338 hinge-axis
|
rlm@160
|
339 hinge-axis)
|
rlm@160
|
340 (.setLimit limit-1 limit-2))))
|
rlm@160
|
341
|
rlm@160
|
342 (defmethod joint-dispatch :cone
|
rlm@160
|
343 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@160
|
344 (let [limit-xz (:limit-xz constraints)
|
rlm@160
|
345 limit-xy (:limit-xy constraints)
|
rlm@160
|
346 twist (:twist constraints)]
|
rlm@160
|
347
|
rlm@160
|
348 (println-repl "creating CONE joint")
|
rlm@160
|
349 (println-repl rotation)
|
rlm@160
|
350 (println-repl
|
rlm@160
|
351 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
|
rlm@160
|
352 (println-repl
|
rlm@160
|
353 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
|
rlm@160
|
354 (println-repl
|
rlm@160
|
355 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
|
rlm@160
|
356 (doto
|
rlm@160
|
357 (ConeJoint.
|
rlm@160
|
358 control-a
|
rlm@160
|
359 control-b
|
rlm@160
|
360 pivot-a
|
rlm@160
|
361 pivot-b
|
rlm@160
|
362 rotation
|
rlm@160
|
363 rotation)
|
rlm@160
|
364 (.setLimit (float limit-xz)
|
rlm@160
|
365 (float limit-xy)
|
rlm@160
|
366 (float twist)))))
|
rlm@160
|
367
|
rlm@160
|
368 (defn connect
|
rlm@175
|
369 "Create a joint between 'obj-a and 'obj-b at the location of
|
rlm@175
|
370 'joint. The type of joint is determined by the metadata on 'joint.
|
rlm@175
|
371
|
rlm@175
|
372 Here are some examples:
|
rlm@160
|
373 {:type :point}
|
rlm@160
|
374 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@160
|
375 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@160
|
376
|
rlm@160
|
377 {:type :cone :limit-xz 0]
|
rlm@160
|
378 :limit-xy 0]
|
rlm@160
|
379 :twist 0]} (use XZY rotation mode in blender!)"
|
rlm@160
|
380 [#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@160
|
381 (let [control-a (.getControl obj-a RigidBodyControl)
|
rlm@160
|
382 control-b (.getControl obj-b RigidBodyControl)
|
rlm@160
|
383 joint-center (.getWorldTranslation joint)
|
rlm@160
|
384 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
|
rlm@160
|
385 pivot-a (world-to-local obj-a joint-center)
|
rlm@160
|
386 pivot-b (world-to-local obj-b joint-center)]
|
rlm@160
|
387
|
rlm@160
|
388 (if-let [constraints
|
rlm@160
|
389 (map-vals
|
rlm@160
|
390 eval
|
rlm@160
|
391 (read-string
|
rlm@160
|
392 (meta-data joint "joint")))]
|
rlm@160
|
393 ;; A side-effect of creating a joint registers
|
rlm@160
|
394 ;; it with both physics objects which in turn
|
rlm@160
|
395 ;; will register the joint with the physics system
|
rlm@160
|
396 ;; when the simulation is started.
|
rlm@160
|
397 (do
|
rlm@160
|
398 (println-repl "creating joint between"
|
rlm@160
|
399 (.getName obj-a) "and" (.getName obj-b))
|
rlm@160
|
400 (joint-dispatch constraints
|
rlm@160
|
401 control-a control-b
|
rlm@160
|
402 pivot-a pivot-b
|
rlm@160
|
403 joint-rotation))
|
rlm@160
|
404 (println-repl "could not find joint meta-data!"))))
|
rlm@203
|
405 #+end_src
|
rlm@160
|
406
|
rlm@203
|
407 Creating joints is now a matter applying =(connect)= to each joint
|
rlm@203
|
408 node.
|
rlm@160
|
409
|
rlm@203
|
410 #+begin_src clojure
|
rlm@175
|
411 (defn joints!
|
rlm@175
|
412 "Connect the solid parts of the creature with physical joints. The
|
rlm@175
|
413 joints are taken from the \"joints\" node in the creature."
|
rlm@175
|
414 [#^Node creature]
|
rlm@160
|
415 (dorun
|
rlm@160
|
416 (map
|
rlm@160
|
417 (fn [joint]
|
rlm@175
|
418 (let [[obj-a obj-b] (joint-targets creature joint)]
|
rlm@160
|
419 (connect obj-a obj-b joint)))
|
rlm@175
|
420 (joints creature))))
|
rlm@203
|
421 #+end_src
|
rlm@160
|
422
|
rlm@203
|
423
|
rlm@203
|
424 ** Round 3
|
rlm@203
|
425
|
rlm@203
|
426 Now we can test the hand in all its glory.
|
rlm@203
|
427
|
rlm@203
|
428 #+begin_src clojure
|
rlm@203
|
429 (in-ns 'cortex.test.body)
|
rlm@203
|
430
|
rlm@203
|
431 (def debug-control
|
rlm@203
|
432 {"key-h" (fn [world val]
|
rlm@203
|
433 (if val (enable-debug world)))
|
rlm@203
|
434
|
rlm@203
|
435 "key-u" (fn [world _] (set-gravity world Vector3f/ZERO))
|
rlm@203
|
436 })
|
rlm@203
|
437
|
rlm@203
|
438 (defn test-three []
|
rlm@203
|
439 (world (nodify
|
rlm@203
|
440 [(doto (hand)
|
rlm@203
|
441 (physical!)
|
rlm@203
|
442 (joints!) )
|
rlm@203
|
443 (floor)])
|
rlm@203
|
444 (merge standard-debug-controls debug-control
|
rlm@203
|
445 normal-gravity)
|
rlm@203
|
446 (comp
|
rlm@203
|
447 #(Capture/captureVideo
|
rlm@203
|
448 % (File. "/home/r/proj/cortex/render/body/3"))
|
rlm@203
|
449 #(do (set-gravity % Vector3f/ZERO) %)
|
rlm@203
|
450 setup)
|
rlm@203
|
451 no-op))
|
rlm@203
|
452 #+end_src
|
rlm@203
|
453
|
rlm@203
|
454 =(physical!)= makes the hand solid, then =(joints!)= connects each
|
rlm@203
|
455 piece together.
|
rlm@203
|
456
|
rlm@203
|
457
|
rlm@203
|
458 #+begin_html
|
rlm@203
|
459 <div class="figure">
|
rlm@203
|
460 <center>
|
rlm@203
|
461 <video controls="controls" width="640">
|
rlm@203
|
462 <source src="../video/full-hand.ogg" type="video/ogg"
|
rlm@203
|
463 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@203
|
464 </video>
|
rlm@203
|
465 </center>
|
rlm@203
|
466 <p>Now the hand is physical and has joints.</p>
|
rlm@203
|
467 </div>
|
rlm@203
|
468 #+end_html
|
rlm@203
|
469
|
rlm@203
|
470 The joints are visualized as green connections between each segment
|
rlm@203
|
471 for debug purposes. You can see that they correspond to the empty
|
rlm@203
|
472 nodes in the blender file.
|
rlm@203
|
473
|
rlm@203
|
474 * Wrap-Up!
|
rlm@203
|
475
|
rlm@203
|
476 It is convienent to combine =(physical!)= and =(joints!)= into one
|
rlm@203
|
477 function that completely creates the creature's physical body.
|
rlm@203
|
478
|
rlm@203
|
479 #+name: joints-4
|
rlm@203
|
480 #+begin_src clojure
|
rlm@175
|
481 (defn body!
|
rlm@175
|
482 "Endow the creature with a physical body connected with joints. The
|
rlm@175
|
483 particulars of the joints and the masses of each pody part are
|
rlm@175
|
484 determined in blender."
|
rlm@175
|
485 [#^Node creature]
|
rlm@175
|
486 (physical! creature)
|
rlm@175
|
487 (joints! creature))
|
rlm@64
|
488 #+end_src
|
rlm@63
|
489
|
rlm@202
|
490 * Bookkeeping
|
rlm@175
|
491
|
rlm@203
|
492 Header; here for completeness.
|
rlm@203
|
493
|
rlm@202
|
494 #+name: body-0
|
rlm@202
|
495 #+begin_src clojure
|
rlm@202
|
496 (ns cortex.body
|
rlm@202
|
497 "Assemble a physical creature using the definitions found in a
|
rlm@202
|
498 specially prepared blender file. Creates rigid bodies and joints so
|
rlm@202
|
499 that a creature can have a physical presense in the simulation."
|
rlm@202
|
500 {:author "Robert McIntyre"}
|
rlm@202
|
501 (:use (cortex world util sense))
|
rlm@202
|
502 (:use clojure.contrib.def)
|
rlm@202
|
503 (:import
|
rlm@202
|
504 (com.jme3.math Vector3f Quaternion Vector2f Matrix3f)
|
rlm@202
|
505 (com.jme3.bullet.joints
|
rlm@202
|
506 SixDofJoint Point2PointJoint HingeJoint ConeJoint)
|
rlm@202
|
507 com.jme3.bullet.control.RigidBodyControl
|
rlm@202
|
508 com.jme3.collision.CollisionResults
|
rlm@202
|
509 com.jme3.bounding.BoundingBox
|
rlm@202
|
510 com.jme3.scene.Node
|
rlm@202
|
511 com.jme3.scene.Geometry
|
rlm@202
|
512 com.jme3.bullet.collision.shapes.HullCollisionShape))
|
rlm@202
|
513 #+end_src
|
rlm@133
|
514
|
rlm@202
|
515 * Source
|
rlm@202
|
516
|
rlm@203
|
517 Dylan -- I'll fill these in later
|
rlm@203
|
518 - cortex.body
|
rlm@203
|
519 - cortex.test.body
|
rlm@203
|
520 - blender files
|
rlm@203
|
521
|
rlm@202
|
522 * COMMENT Examples
|
rlm@63
|
523
|
rlm@69
|
524 #+name: test-body
|
rlm@64
|
525 #+begin_src clojure
|
rlm@69
|
526 (ns cortex.test.body
|
rlm@64
|
527 (:use (cortex world util body))
|
rlm@135
|
528 (:require cortex.silly)
|
rlm@64
|
529 (:import
|
rlm@64
|
530 com.jme3.math.Vector3f
|
rlm@64
|
531 com.jme3.math.ColorRGBA
|
rlm@64
|
532 com.jme3.bullet.joints.Point2PointJoint
|
rlm@64
|
533 com.jme3.bullet.control.RigidBodyControl
|
rlm@145
|
534 com.jme3.system.NanoTimer
|
rlm@145
|
535 com.jme3.math.Quaternion))
|
rlm@63
|
536
|
rlm@64
|
537 (defn worm-segments
|
rlm@64
|
538 "Create multiple evenly spaced box segments. They're fabulous!"
|
rlm@64
|
539 [segment-length num-segments interstitial-space radius]
|
rlm@64
|
540 (letfn [(nth-segment
|
rlm@64
|
541 [n]
|
rlm@64
|
542 (box segment-length radius radius :mass 0.1
|
rlm@64
|
543 :position
|
rlm@64
|
544 (Vector3f.
|
rlm@64
|
545 (* 2 n (+ interstitial-space segment-length)) 0 0)
|
rlm@64
|
546 :name (str "worm-segment" n)
|
rlm@64
|
547 :color (ColorRGBA/randomColor)))]
|
rlm@64
|
548 (map nth-segment (range num-segments))))
|
rlm@63
|
549
|
rlm@64
|
550 (defn connect-at-midpoint
|
rlm@64
|
551 "Connect two physics objects with a Point2Point joint constraint at
|
rlm@64
|
552 the point equidistant from both objects' centers."
|
rlm@64
|
553 [segmentA segmentB]
|
rlm@64
|
554 (let [centerA (.getWorldTranslation segmentA)
|
rlm@64
|
555 centerB (.getWorldTranslation segmentB)
|
rlm@64
|
556 midpoint (.mult (.add centerA centerB) (float 0.5))
|
rlm@64
|
557 pivotA (.subtract midpoint centerA)
|
rlm@64
|
558 pivotB (.subtract midpoint centerB)
|
rlm@64
|
559
|
rlm@64
|
560 ;; A side-effect of creating a joint registers
|
rlm@64
|
561 ;; it with both physics objects which in turn
|
rlm@64
|
562 ;; will register the joint with the physics system
|
rlm@64
|
563 ;; when the simulation is started.
|
rlm@64
|
564 joint (Point2PointJoint.
|
rlm@64
|
565 (.getControl segmentA RigidBodyControl)
|
rlm@64
|
566 (.getControl segmentB RigidBodyControl)
|
rlm@64
|
567 pivotA
|
rlm@64
|
568 pivotB)]
|
rlm@64
|
569 segmentB))
|
rlm@63
|
570
|
rlm@64
|
571 (defn eve-worm
|
rlm@72
|
572 "Create a worm-like body bound by invisible joint constraints."
|
rlm@64
|
573 []
|
rlm@64
|
574 (let [segments (worm-segments 0.2 5 0.1 0.1)]
|
rlm@64
|
575 (dorun (map (partial apply connect-at-midpoint)
|
rlm@64
|
576 (partition 2 1 segments)))
|
rlm@64
|
577 (nodify "worm" segments)))
|
rlm@63
|
578
|
rlm@64
|
579 (defn worm-pattern
|
rlm@64
|
580 "This is a simple, mindless motor control pattern that drives the
|
rlm@64
|
581 second segment of the worm's body at an offset angle with
|
rlm@64
|
582 sinusoidally varying strength."
|
rlm@64
|
583 [time]
|
rlm@64
|
584 (let [angle (* Math/PI (/ 9 20))
|
rlm@63
|
585 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
|
rlm@63
|
586 [Vector3f/ZERO
|
rlm@63
|
587 (.mult
|
rlm@63
|
588 direction
|
rlm@63
|
589 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
|
rlm@63
|
590 Vector3f/ZERO
|
rlm@63
|
591 Vector3f/ZERO
|
rlm@63
|
592 Vector3f/ZERO]))
|
rlm@60
|
593
|
rlm@64
|
594 (defn test-motor-control
|
rlm@69
|
595 "Testing motor-control:
|
rlm@69
|
596 You should see a multi-segmented worm-like object fall onto the
|
rlm@64
|
597 table and begin writhing and moving."
|
rlm@60
|
598 []
|
rlm@64
|
599 (let [worm (eve-worm)
|
rlm@60
|
600 time (atom 0)
|
rlm@63
|
601 worm-motor-map (vector-motor-control worm)]
|
rlm@60
|
602 (world
|
rlm@60
|
603 (nodify [worm
|
rlm@60
|
604 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
|
rlm@60
|
605 :color ColorRGBA/Gray)])
|
rlm@60
|
606 standard-debug-controls
|
rlm@60
|
607 (fn [world]
|
rlm@60
|
608 (enable-debug world)
|
rlm@60
|
609 (light-up-everything world)
|
rlm@63
|
610 (comment
|
rlm@63
|
611 (com.aurellem.capture.Capture/captureVideo
|
rlm@63
|
612 world
|
rlm@63
|
613 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
|
rlm@63
|
614 )
|
rlm@60
|
615
|
rlm@60
|
616 (fn [_ _]
|
rlm@60
|
617 (swap! time inc)
|
rlm@64
|
618 (Thread/sleep 20)
|
rlm@60
|
619 (dorun (worm-motor-map
|
rlm@60
|
620 (worm-pattern @time)))))))
|
rlm@60
|
621
|
rlm@130
|
622
|
rlm@135
|
623
|
rlm@130
|
624 (defn join-at-point [obj-a obj-b world-pivot]
|
rlm@130
|
625 (cortex.silly/joint-dispatch
|
rlm@130
|
626 {:type :point}
|
rlm@130
|
627 (.getControl obj-a RigidBodyControl)
|
rlm@130
|
628 (.getControl obj-b RigidBodyControl)
|
rlm@130
|
629 (cortex.silly/world-to-local obj-a world-pivot)
|
rlm@130
|
630 (cortex.silly/world-to-local obj-b world-pivot)
|
rlm@130
|
631 nil
|
rlm@130
|
632 ))
|
rlm@130
|
633
|
rlm@133
|
634 (import com.jme3.bullet.collision.PhysicsCollisionObject)
|
rlm@130
|
635
|
rlm@130
|
636 (defn blab-* []
|
rlm@130
|
637 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
|
rlm@130
|
638 :mass 0 :color ColorRGBA/Green)
|
rlm@130
|
639 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
|
rlm@130
|
640 :mass 1 :color ColorRGBA/Red)
|
rlm@130
|
641 connection-point (Vector3f. 1.2 0 0)
|
rlm@130
|
642 root (nodify [hand finger])]
|
rlm@130
|
643
|
rlm@130
|
644 (join-at-point hand finger (Vector3f. 1.2 0 0))
|
rlm@130
|
645
|
rlm@130
|
646 (.setCollisionGroup
|
rlm@130
|
647 (.getControl hand RigidBodyControl)
|
rlm@130
|
648 PhysicsCollisionObject/COLLISION_GROUP_NONE)
|
rlm@130
|
649 (world
|
rlm@130
|
650 root
|
rlm@130
|
651 standard-debug-controls
|
rlm@130
|
652 (fn [world]
|
rlm@130
|
653 (enable-debug world)
|
rlm@130
|
654 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
|
rlm@130
|
655 (set-gravity world Vector3f/ZERO)
|
rlm@130
|
656 )
|
rlm@130
|
657 no-op)))
|
rlm@133
|
658 (comment
|
rlm@133
|
659
|
rlm@133
|
660 (defn proprioception-debug-window
|
rlm@133
|
661 []
|
rlm@133
|
662 (let [time (atom 0)]
|
rlm@133
|
663 (fn [prop-data]
|
rlm@133
|
664 (if (= 0 (rem (swap! time inc) 40))
|
rlm@133
|
665 (println-repl prop-data)))))
|
rlm@133
|
666 )
|
rlm@133
|
667
|
rlm@131
|
668 (comment
|
rlm@131
|
669 (dorun
|
rlm@131
|
670 (map
|
rlm@131
|
671 (comp
|
rlm@131
|
672 println-repl
|
rlm@131
|
673 (fn [[p y r]]
|
rlm@131
|
674 (format
|
rlm@131
|
675 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
|
rlm@131
|
676 p y r)))
|
rlm@131
|
677 prop-data)))
|
rlm@131
|
678
|
rlm@130
|
679
|
rlm@130
|
680
|
rlm@137
|
681
|
rlm@64
|
682 (defn test-proprioception
|
rlm@69
|
683 "Testing proprioception:
|
rlm@69
|
684 You should see two foating bars, and a printout of pitch, yaw, and
|
rlm@64
|
685 roll. Pressing key-r/key-t should move the blue bar up and down and
|
rlm@64
|
686 change only the value of pitch. key-f/key-g moves it side to side
|
rlm@64
|
687 and changes yaw. key-v/key-b will spin the blue segment clockwise
|
rlm@64
|
688 and counterclockwise, and only affect roll."
|
rlm@60
|
689 []
|
rlm@145
|
690 (let [hand (box 0.2 1 0.2 :position (Vector3f. 0 0 0)
|
rlm@142
|
691 :mass 0 :color ColorRGBA/Green :name "hand")
|
rlm@145
|
692 finger (box 0.2 1 0.2 :position (Vector3f. 0 2.4 0)
|
rlm@132
|
693 :mass 1 :color ColorRGBA/Red :name "finger")
|
rlm@133
|
694 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
|
rlm@145
|
695 :position (Vector3f. 0 1.2 0)
|
rlm@145
|
696 :rotation (doto (Quaternion.)
|
rlm@145
|
697 (.fromAngleAxis
|
rlm@145
|
698 (/ Math/PI 2)
|
rlm@145
|
699 (Vector3f. 0 0 1)))
|
rlm@133
|
700 :physical? false)
|
rlm@145
|
701 joint (join-at-point hand finger (Vector3f. 0 1.2 0 ))
|
rlm@135
|
702 creature (nodify [hand finger joint-node])
|
rlm@145
|
703 finger-control (.getControl finger RigidBodyControl)
|
rlm@145
|
704 hand-control (.getControl hand RigidBodyControl)]
|
rlm@145
|
705
|
rlm@145
|
706
|
rlm@145
|
707 (let
|
rlm@135
|
708 ;; *******************************************
|
rlm@137
|
709
|
rlm@145
|
710 [floor (box 10 10 10 :position (Vector3f. 0 -15 0)
|
rlm@135
|
711 :mass 0 :color ColorRGBA/Gray)
|
rlm@137
|
712
|
rlm@137
|
713 root (nodify [creature floor])
|
rlm@133
|
714 prop (joint-proprioception creature joint-node)
|
rlm@139
|
715 prop-view (proprioception-debug-window)
|
rlm@139
|
716
|
rlm@139
|
717 controls
|
rlm@139
|
718 (merge standard-debug-controls
|
rlm@140
|
719 {"key-o"
|
rlm@139
|
720 (fn [_ _] (.setEnabled finger-control true))
|
rlm@140
|
721 "key-p"
|
rlm@139
|
722 (fn [_ _] (.setEnabled finger-control false))
|
rlm@140
|
723 "key-k"
|
rlm@140
|
724 (fn [_ _] (.setEnabled hand-control true))
|
rlm@140
|
725 "key-l"
|
rlm@140
|
726 (fn [_ _] (.setEnabled hand-control false))
|
rlm@139
|
727 "key-i"
|
rlm@139
|
728 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))
|
rlm@142
|
729 "key-period"
|
rlm@142
|
730 (fn [world _]
|
rlm@142
|
731 (.setEnabled finger-control false)
|
rlm@142
|
732 (.setEnabled hand-control false)
|
rlm@142
|
733 (.rotate creature (doto (Quaternion.)
|
rlm@142
|
734 (.fromAngleAxis
|
rlm@142
|
735 (float (/ Math/PI 15))
|
rlm@142
|
736 (Vector3f. 0 0 -1))))
|
rlm@142
|
737
|
rlm@142
|
738 (.setEnabled finger-control true)
|
rlm@142
|
739 (.setEnabled hand-control true)
|
rlm@142
|
740 (set-gravity world (Vector3f. 0 0 0))
|
rlm@142
|
741 )
|
rlm@142
|
742
|
rlm@142
|
743
|
rlm@139
|
744 }
|
rlm@139
|
745 )
|
rlm@130
|
746
|
rlm@139
|
747 ]
|
rlm@139
|
748 (comment
|
rlm@139
|
749 (.setCollisionGroup
|
rlm@139
|
750 (.getControl hand RigidBodyControl)
|
rlm@139
|
751 PhysicsCollisionObject/COLLISION_GROUP_NONE)
|
rlm@139
|
752 )
|
rlm@140
|
753 (apply
|
rlm@140
|
754 world
|
rlm@140
|
755 (with-movement
|
rlm@140
|
756 hand
|
rlm@140
|
757 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]
|
rlm@140
|
758 [10 10 10 10 1 1]
|
rlm@140
|
759 (with-movement
|
rlm@140
|
760 finger
|
rlm@140
|
761 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]
|
rlm@145
|
762 [1 1 10 10 10 10]
|
rlm@140
|
763 [root
|
rlm@140
|
764 controls
|
rlm@140
|
765 (fn [world]
|
rlm@140
|
766 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
|
rlm@140
|
767 (set-gravity world (Vector3f. 0 0 0))
|
rlm@140
|
768 (light-up-everything world))
|
rlm@145
|
769 (fn [_ _] (prop-view (list (prop))))]))))))
|
rlm@138
|
770
|
rlm@64
|
771 #+end_src
|
rlm@56
|
772
|
rlm@130
|
773 #+results: test-body
|
rlm@130
|
774 : #'cortex.test.body/test-proprioception
|
rlm@130
|
775
|
rlm@60
|
776
|
rlm@63
|
777 * COMMENT code-limbo
|
rlm@61
|
778 #+begin_src clojure
|
rlm@61
|
779 ;;(.loadModel
|
rlm@61
|
780 ;; (doto (asset-manager)
|
rlm@61
|
781 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@61
|
782 ;; "Models/person/person.blend")
|
rlm@61
|
783
|
rlm@64
|
784
|
rlm@64
|
785 (defn load-blender-model
|
rlm@64
|
786 "Load a .blend file using an asset folder relative path."
|
rlm@64
|
787 [^String model]
|
rlm@64
|
788 (.loadModel
|
rlm@64
|
789 (doto (asset-manager)
|
rlm@64
|
790 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@64
|
791 model))
|
rlm@64
|
792
|
rlm@64
|
793
|
rlm@61
|
794 (defn view-model [^String model]
|
rlm@61
|
795 (view
|
rlm@61
|
796 (.loadModel
|
rlm@61
|
797 (doto (asset-manager)
|
rlm@61
|
798 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@61
|
799 model)))
|
rlm@61
|
800
|
rlm@61
|
801 (defn load-blender-scene [^String model]
|
rlm@61
|
802 (.loadModel
|
rlm@61
|
803 (doto (asset-manager)
|
rlm@61
|
804 (.registerLoader BlenderLoader (into-array String ["blend"])))
|
rlm@61
|
805 model))
|
rlm@61
|
806
|
rlm@61
|
807 (defn worm
|
rlm@61
|
808 []
|
rlm@61
|
809 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
|
rlm@61
|
810
|
rlm@61
|
811 (defn oto
|
rlm@61
|
812 []
|
rlm@61
|
813 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
|
rlm@61
|
814
|
rlm@61
|
815 (defn sinbad
|
rlm@61
|
816 []
|
rlm@61
|
817 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
|
rlm@61
|
818
|
rlm@61
|
819 (defn worm-blender
|
rlm@61
|
820 []
|
rlm@61
|
821 (first (seq (.getChildren (load-blender-model
|
rlm@61
|
822 "Models/anim2/simple-worm.blend")))))
|
rlm@61
|
823
|
rlm@61
|
824 (defn body
|
rlm@61
|
825 "given a node with a SkeletonControl, will produce a body sutiable
|
rlm@61
|
826 for AI control with movement and proprioception."
|
rlm@61
|
827 [node]
|
rlm@61
|
828 (let [skeleton-control (.getControl node SkeletonControl)
|
rlm@61
|
829 krc (KinematicRagdollControl.)]
|
rlm@61
|
830 (comment
|
rlm@61
|
831 (dorun
|
rlm@61
|
832 (map #(.addBoneName krc %)
|
rlm@61
|
833 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
|
rlm@61
|
834 ;;"mid2" "mid3" "tail" "head"]
|
rlm@61
|
835 )))
|
rlm@61
|
836 (.addControl node krc)
|
rlm@61
|
837 (.setRagdollMode krc)
|
rlm@61
|
838 )
|
rlm@61
|
839 node
|
rlm@61
|
840 )
|
rlm@61
|
841 (defn show-skeleton [node]
|
rlm@61
|
842 (let [sd
|
rlm@61
|
843
|
rlm@61
|
844 (doto
|
rlm@61
|
845 (SkeletonDebugger. "aurellem-skel-debug"
|
rlm@61
|
846 (skel node))
|
rlm@61
|
847 (.setMaterial (green-x-ray)))]
|
rlm@61
|
848 (.attachChild node sd)
|
rlm@61
|
849 node))
|
rlm@61
|
850
|
rlm@61
|
851
|
rlm@61
|
852
|
rlm@61
|
853 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
854
|
rlm@61
|
855 ;; this could be a good way to give objects special properties like
|
rlm@61
|
856 ;; being eyes and the like
|
rlm@61
|
857
|
rlm@61
|
858 (.getUserData
|
rlm@61
|
859 (.getChild
|
rlm@61
|
860 (load-blender-model "Models/property/test.blend") 0)
|
rlm@61
|
861 "properties")
|
rlm@61
|
862
|
rlm@61
|
863 ;; the properties are saved along with the blender file.
|
rlm@61
|
864 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
865
|
rlm@61
|
866
|
rlm@61
|
867
|
rlm@61
|
868
|
rlm@61
|
869 (defn init-debug-skel-node
|
rlm@61
|
870 [f debug-node skeleton]
|
rlm@61
|
871 (let [bones
|
rlm@61
|
872 (map #(.getBone skeleton %)
|
rlm@61
|
873 (range (.getBoneCount skeleton)))]
|
rlm@61
|
874 (dorun (map #(.setUserControl % true) bones))
|
rlm@61
|
875 (dorun (map (fn [b]
|
rlm@61
|
876 (println (.getName b)
|
rlm@61
|
877 " -- " (f b)))
|
rlm@61
|
878 bones))
|
rlm@61
|
879 (dorun
|
rlm@61
|
880 (map #(.attachChild
|
rlm@61
|
881 debug-node
|
rlm@61
|
882 (doto
|
rlm@61
|
883 (sphere 0.1
|
rlm@61
|
884 :position (f %)
|
rlm@61
|
885 :physical? false)
|
rlm@61
|
886 (.setMaterial (green-x-ray))))
|
rlm@61
|
887 bones)))
|
rlm@61
|
888 debug-node)
|
rlm@61
|
889
|
rlm@61
|
890 (import jme3test.bullet.PhysicsTestHelper)
|
rlm@61
|
891
|
rlm@61
|
892
|
rlm@61
|
893 (defn test-zzz [the-worm world value]
|
rlm@61
|
894 (if (not value)
|
rlm@61
|
895 (let [skeleton (skel the-worm)]
|
rlm@61
|
896 (println-repl "enabling bones")
|
rlm@61
|
897 (dorun
|
rlm@61
|
898 (map
|
rlm@61
|
899 #(.setUserControl (.getBone skeleton %) true)
|
rlm@61
|
900 (range (.getBoneCount skeleton))))
|
rlm@61
|
901
|
rlm@61
|
902
|
rlm@61
|
903 (let [b (.getBone skeleton 2)]
|
rlm@61
|
904 (println-repl "moving " (.getName b))
|
rlm@61
|
905 (println-repl (.getLocalPosition b))
|
rlm@61
|
906 (.setUserTransforms b
|
rlm@61
|
907 Vector3f/UNIT_X
|
rlm@61
|
908 Quaternion/IDENTITY
|
rlm@61
|
909 ;;(doto (Quaternion.)
|
rlm@61
|
910 ;; (.fromAngles (/ Math/PI 2)
|
rlm@61
|
911 ;; 0
|
rlm@61
|
912 ;; 0
|
rlm@61
|
913
|
rlm@61
|
914 (Vector3f. 1 1 1))
|
rlm@61
|
915 )
|
rlm@61
|
916
|
rlm@61
|
917 (println-repl "hi! <3"))))
|
rlm@61
|
918
|
rlm@61
|
919
|
rlm@61
|
920 (defn test-ragdoll []
|
rlm@61
|
921
|
rlm@61
|
922 (let [the-worm
|
rlm@61
|
923
|
rlm@61
|
924 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
|
rlm@61
|
925 (doto (show-skeleton (worm-blender))
|
rlm@61
|
926 (.setLocalTranslation (Vector3f. 0 10 0))
|
rlm@61
|
927 ;;(worm)
|
rlm@61
|
928 ;;(oto)
|
rlm@61
|
929 ;;(sinbad)
|
rlm@61
|
930 )
|
rlm@61
|
931 ]
|
rlm@61
|
932
|
rlm@61
|
933
|
rlm@61
|
934 (.start
|
rlm@61
|
935 (world
|
rlm@61
|
936 (doto (Node.)
|
rlm@61
|
937 (.attachChild the-worm))
|
rlm@61
|
938 {"key-return" (fire-cannon-ball)
|
rlm@61
|
939 "key-space" (partial test-zzz the-worm)
|
rlm@61
|
940 }
|
rlm@61
|
941 (fn [world]
|
rlm@61
|
942 (light-up-everything world)
|
rlm@61
|
943 (PhysicsTestHelper/createPhysicsTestWorld
|
rlm@61
|
944 (.getRootNode world)
|
rlm@61
|
945 (asset-manager)
|
rlm@61
|
946 (.getPhysicsSpace
|
rlm@61
|
947 (.getState (.getStateManager world) BulletAppState)))
|
rlm@61
|
948 (set-gravity world Vector3f/ZERO)
|
rlm@61
|
949 ;;(.setTimer world (NanoTimer.))
|
rlm@61
|
950 ;;(org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@61
|
951 )
|
rlm@61
|
952 no-op
|
rlm@61
|
953 )
|
rlm@61
|
954
|
rlm@61
|
955
|
rlm@61
|
956 )))
|
rlm@61
|
957
|
rlm@61
|
958
|
rlm@61
|
959 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
960 ;;; here is the ragdoll stuff
|
rlm@61
|
961
|
rlm@61
|
962 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
|
rlm@61
|
963 (def mesh worm-mesh)
|
rlm@61
|
964
|
rlm@61
|
965 (.getFloatBuffer mesh VertexBuffer$Type/Position)
|
rlm@61
|
966 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@61
|
967 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@61
|
968
|
rlm@61
|
969
|
rlm@61
|
970 (defn position [index]
|
rlm@61
|
971 (.get
|
rlm@61
|
972 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
|
rlm@61
|
973 index))
|
rlm@61
|
974
|
rlm@61
|
975 (defn bones [index]
|
rlm@61
|
976 (.get
|
rlm@61
|
977 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@61
|
978 index))
|
rlm@61
|
979
|
rlm@61
|
980 (defn bone-weights [index]
|
rlm@61
|
981 (.get
|
rlm@61
|
982 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@61
|
983 index))
|
rlm@61
|
984
|
rlm@61
|
985
|
rlm@61
|
986
|
rlm@61
|
987 (defn vertex-bones [vertex]
|
rlm@61
|
988 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@61
|
989
|
rlm@61
|
990 (defn vertex-weights [vertex]
|
rlm@61
|
991 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@61
|
992
|
rlm@61
|
993 (defn vertex-position [index]
|
rlm@61
|
994 (let [offset (* index 3)]
|
rlm@61
|
995 (Vector3f. (position offset)
|
rlm@61
|
996 (position (inc offset))
|
rlm@61
|
997 (position (inc(inc offset))))))
|
rlm@61
|
998
|
rlm@61
|
999 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
|
rlm@61
|
1000
|
rlm@61
|
1001 (defn bone-control-color [index]
|
rlm@61
|
1002 (get {[1 0 0 0] ColorRGBA/Red
|
rlm@61
|
1003 [1 2 0 0] ColorRGBA/Magenta
|
rlm@61
|
1004 [2 0 0 0] ColorRGBA/Blue}
|
rlm@61
|
1005 (vertex-bones index)
|
rlm@61
|
1006 ColorRGBA/White))
|
rlm@61
|
1007
|
rlm@61
|
1008 (defn influence-color [index bone-num]
|
rlm@61
|
1009 (get
|
rlm@61
|
1010 {(float 0) ColorRGBA/Blue
|
rlm@61
|
1011 (float 0.5) ColorRGBA/Green
|
rlm@61
|
1012 (float 1) ColorRGBA/Red}
|
rlm@61
|
1013 ;; find the weight of the desired bone
|
rlm@61
|
1014 ((zipmap (vertex-bones index)(vertex-weights index))
|
rlm@61
|
1015 bone-num)
|
rlm@61
|
1016 ColorRGBA/Blue))
|
rlm@61
|
1017
|
rlm@61
|
1018 (def worm-vertices (set (map vertex-info (range 60))))
|
rlm@61
|
1019
|
rlm@61
|
1020
|
rlm@61
|
1021 (defn test-info []
|
rlm@61
|
1022 (let [points (Node.)]
|
rlm@61
|
1023 (dorun
|
rlm@61
|
1024 (map #(.attachChild points %)
|
rlm@61
|
1025 (map #(sphere 0.01
|
rlm@61
|
1026 :position (vertex-position %)
|
rlm@61
|
1027 :color (influence-color % 1)
|
rlm@61
|
1028 :physical? false)
|
rlm@61
|
1029 (range 60))))
|
rlm@61
|
1030 (view points)))
|
rlm@61
|
1031
|
rlm@61
|
1032
|
rlm@61
|
1033 (defrecord JointControl [joint physics-space]
|
rlm@61
|
1034 PhysicsControl
|
rlm@61
|
1035 (setPhysicsSpace [this space]
|
rlm@61
|
1036 (dosync
|
rlm@61
|
1037 (ref-set (:physics-space this) space))
|
rlm@61
|
1038 (.addJoint space (:joint this)))
|
rlm@61
|
1039 (update [this tpf])
|
rlm@61
|
1040 (setSpatial [this spatial])
|
rlm@61
|
1041 (render [this rm vp])
|
rlm@61
|
1042 (getPhysicsSpace [this] (deref (:physics-space this)))
|
rlm@61
|
1043 (isEnabled [this] true)
|
rlm@61
|
1044 (setEnabled [this state]))
|
rlm@61
|
1045
|
rlm@61
|
1046 (defn add-joint
|
rlm@61
|
1047 "Add a joint to a particular object. When the object is added to the
|
rlm@61
|
1048 PhysicsSpace of a simulation, the joint will also be added"
|
rlm@61
|
1049 [object joint]
|
rlm@61
|
1050 (let [control (JointControl. joint (ref nil))]
|
rlm@61
|
1051 (.addControl object control))
|
rlm@61
|
1052 object)
|
rlm@61
|
1053
|
rlm@61
|
1054
|
rlm@61
|
1055 (defn hinge-world
|
rlm@61
|
1056 []
|
rlm@61
|
1057 (let [sphere1 (sphere)
|
rlm@61
|
1058 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
|
rlm@61
|
1059 joint (Point2PointJoint.
|
rlm@61
|
1060 (.getControl sphere1 RigidBodyControl)
|
rlm@61
|
1061 (.getControl sphere2 RigidBodyControl)
|
rlm@61
|
1062 Vector3f/ZERO (Vector3f. 3 3 3))]
|
rlm@61
|
1063 (add-joint sphere1 joint)
|
rlm@61
|
1064 (doto (Node. "hinge-world")
|
rlm@61
|
1065 (.attachChild sphere1)
|
rlm@61
|
1066 (.attachChild sphere2))))
|
rlm@61
|
1067
|
rlm@61
|
1068
|
rlm@61
|
1069 (defn test-joint []
|
rlm@61
|
1070 (view (hinge-world)))
|
rlm@61
|
1071
|
rlm@61
|
1072 ;; (defn copier-gen []
|
rlm@61
|
1073 ;; (let [count (atom 0)]
|
rlm@61
|
1074 ;; (fn [in]
|
rlm@61
|
1075 ;; (swap! count inc)
|
rlm@61
|
1076 ;; (clojure.contrib.duck-streams/copy
|
rlm@61
|
1077 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
|
rlm@61
|
1078 ;; ;;/home/r/tmp/mao-test/clojure-images
|
rlm@61
|
1079 ;; (format "%08d.png" @count)))))))
|
rlm@61
|
1080 ;; (defn decrease-framerate []
|
rlm@61
|
1081 ;; (map
|
rlm@61
|
1082 ;; (copier-gen)
|
rlm@61
|
1083 ;; (sort
|
rlm@61
|
1084 ;; (map first
|
rlm@61
|
1085 ;; (partition
|
rlm@61
|
1086 ;; 4
|
rlm@61
|
1087 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
|
rlm@61
|
1088 ;; (file-seq
|
rlm@61
|
1089 ;; (file-str
|
rlm@61
|
1090 ;; "/home/r/media/anime/mao-temp/images"))))))))
|
rlm@61
|
1091
|
rlm@61
|
1092
|
rlm@61
|
1093
|
rlm@61
|
1094 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
1095
|
rlm@61
|
1096 (defn proprioception
|
rlm@61
|
1097 "Create a proprioception map that reports the rotations of the
|
rlm@61
|
1098 various limbs of the creature's body"
|
rlm@61
|
1099 [creature]
|
rlm@61
|
1100 [#^Node creature]
|
rlm@61
|
1101 (let [
|
rlm@61
|
1102 nodes (node-seq creature)
|
rlm@61
|
1103 joints
|
rlm@61
|
1104 (map
|
rlm@61
|
1105 :joint
|
rlm@61
|
1106 (filter
|
rlm@61
|
1107 #(isa? (class %) JointControl)
|
rlm@61
|
1108 (reduce
|
rlm@61
|
1109 concat
|
rlm@61
|
1110 (map (fn [node]
|
rlm@61
|
1111 (map (fn [num] (.getControl node num))
|
rlm@61
|
1112 (range (.getNumControls node))))
|
rlm@61
|
1113 nodes))))]
|
rlm@61
|
1114 (fn []
|
rlm@61
|
1115 (reduce concat (map relative-positions (list (first joints)))))))
|
rlm@61
|
1116
|
rlm@61
|
1117
|
rlm@63
|
1118 (defn skel [node]
|
rlm@63
|
1119 (doto
|
rlm@63
|
1120 (.getSkeleton
|
rlm@63
|
1121 (.getControl node SkeletonControl))
|
rlm@63
|
1122 ;; this is necessary to force the skeleton to have accurate world
|
rlm@63
|
1123 ;; transforms before it is rendered to the screen.
|
rlm@63
|
1124 (.resetAndUpdate)))
|
rlm@63
|
1125
|
rlm@63
|
1126 (defn green-x-ray []
|
rlm@63
|
1127 (doto (Material. (asset-manager)
|
rlm@63
|
1128 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@63
|
1129 (.setColor "Color" ColorRGBA/Green)
|
rlm@63
|
1130 (-> (.getAdditionalRenderState)
|
rlm@63
|
1131 (.setDepthTest false))))
|
rlm@63
|
1132
|
rlm@63
|
1133 (defn test-worm []
|
rlm@63
|
1134 (.start
|
rlm@63
|
1135 (world
|
rlm@63
|
1136 (doto (Node.)
|
rlm@63
|
1137 ;;(.attachChild (point-worm))
|
rlm@63
|
1138 (.attachChild (load-blender-model
|
rlm@63
|
1139 "Models/anim2/joint-worm.blend"))
|
rlm@63
|
1140
|
rlm@63
|
1141 (.attachChild (box 10 1 10
|
rlm@63
|
1142 :position (Vector3f. 0 -2 0) :mass 0
|
rlm@63
|
1143 :color (ColorRGBA/Gray))))
|
rlm@63
|
1144 {
|
rlm@63
|
1145 "key-space" (fire-cannon-ball)
|
rlm@63
|
1146 }
|
rlm@63
|
1147 (fn [world]
|
rlm@63
|
1148 (enable-debug world)
|
rlm@63
|
1149 (light-up-everything world)
|
rlm@63
|
1150 ;;(.setTimer world (NanoTimer.))
|
rlm@63
|
1151 )
|
rlm@63
|
1152 no-op)))
|
rlm@63
|
1153
|
rlm@63
|
1154
|
rlm@63
|
1155
|
rlm@63
|
1156 ;; defunct movement stuff
|
rlm@63
|
1157 (defn torque-controls [control]
|
rlm@63
|
1158 (let [torques
|
rlm@63
|
1159 (concat
|
rlm@63
|
1160 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
|
rlm@63
|
1161 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
|
rlm@63
|
1162 [Vector3f/UNIT_X])]
|
rlm@63
|
1163 (map (fn [torque-axis]
|
rlm@63
|
1164 (fn [torque]
|
rlm@63
|
1165 (.applyTorque
|
rlm@63
|
1166 control
|
rlm@63
|
1167 (.mult (.mult (.getPhysicsRotation control)
|
rlm@63
|
1168 torque-axis)
|
rlm@63
|
1169 (float
|
rlm@63
|
1170 (* (.getMass control) torque))))))
|
rlm@63
|
1171 torques)))
|
rlm@63
|
1172
|
rlm@63
|
1173 (defn motor-map
|
rlm@63
|
1174 "Take a creature and generate a function that will enable fine
|
rlm@63
|
1175 grained control over all the creature's limbs."
|
rlm@63
|
1176 [#^Node creature]
|
rlm@63
|
1177 (let [controls (keep #(.getControl % RigidBodyControl)
|
rlm@63
|
1178 (node-seq creature))
|
rlm@63
|
1179 limb-controls (reduce concat (map torque-controls controls))
|
rlm@63
|
1180 body-control (partial map #(%1 %2) limb-controls)]
|
rlm@63
|
1181 body-control))
|
rlm@63
|
1182
|
rlm@63
|
1183 (defn test-motor-map
|
rlm@63
|
1184 "see how torque works."
|
rlm@63
|
1185 []
|
rlm@63
|
1186 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
|
rlm@63
|
1187 :mass 1 :color ColorRGBA/Green)
|
rlm@63
|
1188 motor-map (motor-map finger)]
|
rlm@63
|
1189 (world
|
rlm@63
|
1190 (nodify [finger
|
rlm@63
|
1191 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
|
rlm@63
|
1192 :color ColorRGBA/Gray)])
|
rlm@63
|
1193 standard-debug-controls
|
rlm@63
|
1194 (fn [world]
|
rlm@63
|
1195 (set-gravity world Vector3f/ZERO)
|
rlm@63
|
1196 (light-up-everything world)
|
rlm@63
|
1197 (.setTimer world (NanoTimer.)))
|
rlm@63
|
1198 (fn [_ _]
|
rlm@145
|
1199 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
|
rlm@145
|
1200 0]))))))
|
rlm@145
|
1201
|
rlm@145
|
1202 (defn joint-proprioception [#^Node parts #^Node joint]
|
rlm@145
|
1203 (let [[obj-a obj-b] (joint-targets parts joint)
|
rlm@145
|
1204 joint-rot (.getWorldRotation joint)
|
rlm@145
|
1205 pre-inv-a (.inverse (.getWorldRotation obj-a))
|
rlm@145
|
1206 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))
|
rlm@145
|
1207 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))
|
rlm@145
|
1208 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))
|
rlm@145
|
1209
|
rlm@145
|
1210 x Vector3f/UNIT_Y
|
rlm@145
|
1211 y Vector3f/UNIT_Z
|
rlm@145
|
1212 z Vector3f/UNIT_X
|
rlm@145
|
1213
|
rlm@145
|
1214
|
rlm@145
|
1215 tmp-rot-a (.getWorldRotation obj-a)]
|
rlm@145
|
1216 (println-repl "x:" (.mult tmp-rot-a x))
|
rlm@145
|
1217 (println-repl "y:" (.mult tmp-rot-a y))
|
rlm@145
|
1218 (println-repl "z:" (.mult tmp-rot-a z))
|
rlm@145
|
1219 (println-repl "rot-a" (.getWorldRotation obj-a))
|
rlm@145
|
1220 (println-repl "rot-b" (.getWorldRotation obj-b))
|
rlm@145
|
1221 (println-repl "joint-rot" joint-rot)
|
rlm@145
|
1222 ;; this function will report proprioceptive information for the
|
rlm@145
|
1223 ;; joint.
|
rlm@145
|
1224 (fn []
|
rlm@145
|
1225 ;; x is the "twist" axis, y and z are the "bend" axes
|
rlm@145
|
1226 (let [rot-a (.getWorldRotation obj-a)
|
rlm@145
|
1227 ;;inv-a (.inverse rot-a)
|
rlm@145
|
1228 rot-b (.getWorldRotation obj-b)
|
rlm@145
|
1229 ;;relative (.mult rot-b inv-a)
|
rlm@145
|
1230 basis (doto (Matrix3f.)
|
rlm@145
|
1231 (.setColumn 0 (.mult rot-a x))
|
rlm@145
|
1232 (.setColumn 1 (.mult rot-a y))
|
rlm@145
|
1233 (.setColumn 2 (.mult rot-a z)))
|
rlm@145
|
1234 rotation-about-joint
|
rlm@145
|
1235 (doto (Quaternion.)
|
rlm@145
|
1236 (.fromRotationMatrix
|
rlm@145
|
1237 (.mult (.invert basis)
|
rlm@145
|
1238 (.toRotationMatrix rot-b))))
|
rlm@145
|
1239 [yaw roll pitch]
|
rlm@145
|
1240 (seq (.toAngles rotation-about-joint nil))]
|
rlm@145
|
1241 ;;return euler angles of the quaternion around the new basis
|
rlm@145
|
1242 [yaw roll pitch]))))
|
rlm@145
|
1243
|
rlm@61
|
1244 #+end_src
|
rlm@0
|
1245
|
rlm@0
|
1246
|
rlm@0
|
1247
|
rlm@0
|
1248
|
rlm@0
|
1249
|
rlm@0
|
1250
|
rlm@0
|
1251
|
rlm@73
|
1252 * COMMENT generate Source
|
rlm@44
|
1253 #+begin_src clojure :tangle ../src/cortex/body.clj
|
rlm@175
|
1254 <<joints>>
|
rlm@0
|
1255 #+end_src
|
rlm@64
|
1256
|
rlm@69
|
1257 #+begin_src clojure :tangle ../src/cortex/test/body.clj
|
rlm@202
|
1258 <<test-0>>
|
rlm@64
|
1259 #+end_src
|
rlm@64
|
1260
|
rlm@64
|
1261
|
rlm@0
|
1262
|