rlm@0
|
1 #+title: The 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@58
|
8 * Body
|
rlm@0
|
9
|
rlm@0
|
10 #+srcname: body-main
|
rlm@0
|
11 #+begin_src clojure
|
rlm@44
|
12 (ns cortex.body
|
rlm@44
|
13 (use (cortex world util import)))
|
rlm@44
|
14
|
rlm@0
|
15 (use 'clojure.contrib.def)
|
rlm@0
|
16 (cortex.import/mega-import-jme3)
|
rlm@0
|
17 (rlm.rlm-commands/help)
|
rlm@0
|
18
|
rlm@49
|
19 (defn load-blender-model
|
rlm@49
|
20 [^String model]
|
rlm@49
|
21 (.loadModel
|
rlm@49
|
22 (doto (asset-manager)
|
rlm@49
|
23 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@49
|
24 model))
|
rlm@49
|
25
|
rlm@52
|
26 (defn skel [node]
|
rlm@52
|
27 (doto
|
rlm@52
|
28 (.getSkeleton
|
rlm@52
|
29 (.getControl node SkeletonControl))
|
rlm@52
|
30 ;; this is necessary to force the skeleton to have accurate world
|
rlm@52
|
31 ;; transforms before it is rendered to the screen.
|
rlm@52
|
32 (.resetAndUpdate)))
|
rlm@52
|
33
|
rlm@50
|
34
|
rlm@52
|
35 (defn green-x-ray []
|
rlm@51
|
36 (doto (Material. (asset-manager)
|
rlm@51
|
37 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@51
|
38 (.setColor "Color" ColorRGBA/Green)
|
rlm@52
|
39 (-> (.getAdditionalRenderState)
|
rlm@52
|
40 (.setDepthTest false))))
|
rlm@51
|
41
|
rlm@51
|
42
|
rlm@58
|
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@58
|
44
|
rlm@60
|
45 ;;;;;;;;;;;; eve-style bodies ;;;;;;;;
|
rlm@58
|
46
|
rlm@58
|
47 (defn worm [segment-length num-segments interstitial-space radius]
|
rlm@58
|
48 (letfn [(nth-segment
|
rlm@58
|
49 [n]
|
rlm@58
|
50 (box segment-length radius radius :mass 0.1
|
rlm@58
|
51 :position
|
rlm@58
|
52 (Vector3f.
|
rlm@58
|
53 (* 2 n (+ interstitial-space segment-length)) 0 0)
|
rlm@58
|
54 :name (str "worm-segment" n)
|
rlm@58
|
55 :color (ColorRGBA/randomColor)))]
|
rlm@58
|
56 (map nth-segment (range num-segments))))
|
rlm@58
|
57
|
rlm@58
|
58
|
rlm@58
|
59 (defn connect-at-midpoint
|
rlm@58
|
60 [segmentA segmentB]
|
rlm@58
|
61 (let [centerA (.getWorldTranslation segmentA)
|
rlm@58
|
62 centerB (.getWorldTranslation segmentB)
|
rlm@58
|
63 midpoint (.mult (.add centerA centerB) (float 0.5))
|
rlm@58
|
64 pivotA (.subtract midpoint centerA)
|
rlm@58
|
65 pivotB (.subtract midpoint centerB)
|
rlm@58
|
66 joint (Point2PointJoint.
|
rlm@58
|
67 (.getControl segmentA RigidBodyControl)
|
rlm@58
|
68 (.getControl segmentB RigidBodyControl)
|
rlm@58
|
69 pivotA
|
rlm@58
|
70 pivotB)]
|
rlm@58
|
71 segmentB))
|
rlm@58
|
72
|
rlm@58
|
73 (defn point-worm []
|
rlm@58
|
74 (let [segments (worm 0.2 5 0.1 0.1)]
|
rlm@58
|
75 (dorun (map (partial apply connect-at-midpoint)
|
rlm@58
|
76 (partition 2 1 segments)))
|
rlm@58
|
77 (nodify "worm" segments)))
|
rlm@58
|
78
|
rlm@58
|
79 (defn test-worm []
|
rlm@58
|
80 (.start
|
rlm@58
|
81 (world
|
rlm@58
|
82 (doto (Node.)
|
rlm@58
|
83 ;;(.attachChild (point-worm))
|
rlm@58
|
84 (.attachChild (load-blender-model
|
rlm@58
|
85 "Models/anim2/joint-worm.blend"))
|
rlm@58
|
86
|
rlm@58
|
87 (.attachChild (box 10 1 10
|
rlm@58
|
88 :position (Vector3f. 0 -2 0) :mass 0
|
rlm@58
|
89 :color (ColorRGBA/Gray))))
|
rlm@58
|
90 {
|
rlm@58
|
91 "key-space" (fire-cannon-ball)
|
rlm@58
|
92 }
|
rlm@58
|
93 (fn [world]
|
rlm@58
|
94 (enable-debug world)
|
rlm@58
|
95 (light-up-everything world)
|
rlm@58
|
96 ;;(.setTimer world (NanoTimer.))
|
rlm@58
|
97 )
|
rlm@58
|
98 no-op)))
|
rlm@49
|
99
|
rlm@56
|
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@56
|
101
|
rlm@56
|
102
|
rlm@60
|
103 ;;;;;;;;; Mortor Control ;;;;;;;;;;;;;
|
rlm@56
|
104
|
rlm@56
|
105
|
rlm@60
|
106 ;; surprisingly ehough, terristerial creatures only move by using
|
rlm@60
|
107 ;; torque applied to their joints. There's not a single straight line
|
rlm@60
|
108 ;; of force in the human body at all! (a straight line of force would
|
rlm@60
|
109 ;; correspond to some sort of jet or rocket propulseion)
|
rlm@60
|
110
|
rlm@60
|
111 (defn torque-controls [control]
|
rlm@60
|
112 (let [torques
|
rlm@60
|
113 (concat
|
rlm@60
|
114 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
|
rlm@60
|
115 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
|
rlm@60
|
116 [Vector3f/UNIT_X])]
|
rlm@60
|
117 (map (fn [torque-axis]
|
rlm@60
|
118 (fn [torque]
|
rlm@60
|
119 (.applyTorque
|
rlm@60
|
120 control
|
rlm@60
|
121 (.mult (.mult (.getPhysicsRotation control)
|
rlm@60
|
122 torque-axis)
|
rlm@60
|
123 (float
|
rlm@60
|
124 (* (.getMass control) torque))))))
|
rlm@60
|
125 torques)))
|
rlm@56
|
126
|
rlm@60
|
127 (defn motor-map
|
rlm@60
|
128 "Take a creature and generate a function that will enable fine
|
rlm@60
|
129 grained control over all the creature's limbs."
|
rlm@60
|
130 [#^Node creature]
|
rlm@60
|
131 (let [controls (keep #(.getControl % RigidBodyControl)
|
rlm@60
|
132 (node-seq creature))
|
rlm@60
|
133 limb-controls (reduce concat (map torque-controls controls))
|
rlm@60
|
134 body-control (partial map #(%1 %2) limb-controls)]
|
rlm@60
|
135 body-control))
|
rlm@56
|
136
|
rlm@60
|
137 (defn test-motor-map
|
rlm@60
|
138 "see how torque works."
|
rlm@60
|
139 []
|
rlm@60
|
140 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
|
rlm@60
|
141 :mass 1 :color ColorRGBA/Green)
|
rlm@60
|
142 motor-map (motor-map finger)]
|
rlm@60
|
143 (world
|
rlm@60
|
144 (nodify [finger
|
rlm@60
|
145 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
|
rlm@60
|
146 :color ColorRGBA/Gray)])
|
rlm@60
|
147 standard-debug-controls
|
rlm@60
|
148 (fn [world]
|
rlm@60
|
149 (set-gravity world Vector3f/ZERO)
|
rlm@60
|
150 (light-up-everything world)
|
rlm@60
|
151 (.setTimer world (NanoTimer.)))
|
rlm@60
|
152 (fn [_ _]
|
rlm@60
|
153 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
|
rlm@60
|
154
|
rlm@60
|
155 (defn test-torque
|
rlm@60
|
156 "see how torque works."
|
rlm@60
|
157 []
|
rlm@60
|
158 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
|
rlm@60
|
159 :mass 1 :color ColorRGBA/Green)
|
rlm@60
|
160 move-left? (atom false)
|
rlm@60
|
161 move-right? (atom false)
|
rlm@60
|
162 control (.getControl finger RigidBodyControl)]
|
rlm@60
|
163 (world
|
rlm@60
|
164 (nodify [finger
|
rlm@60
|
165 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
|
rlm@60
|
166 :color ColorRGBA/Gray)])
|
rlm@60
|
167 (merge standard-debug-controls
|
rlm@60
|
168 {"key-k" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@60
|
169 "key-l" (fn [_ pressed?] (reset! move-right? pressed?))})
|
rlm@60
|
170 (fn [world]
|
rlm@60
|
171 (set-gravity world Vector3f/ZERO)
|
rlm@60
|
172 (light-up-everything world)
|
rlm@60
|
173 (.setTimer world (NanoTimer.)))
|
rlm@60
|
174 (fn [_ _]
|
rlm@60
|
175 (if @move-left?
|
rlm@60
|
176 (.applyTorque control
|
rlm@60
|
177 (.mult (.getPhysicsRotation control)
|
rlm@60
|
178 (Vector3f. -3 20 0))))
|
rlm@60
|
179 (if @move-right?
|
rlm@60
|
180 (.applyTorque control (Vector3f. 0 0 1)))))))
|
rlm@60
|
181
|
rlm@60
|
182 (defn worm-pattern [time]
|
rlm@60
|
183 [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
rlm@60
|
184
|
rlm@60
|
185 0 0 0 0 0 0 0 0 0 0 0
|
rlm@60
|
186
|
rlm@60
|
187 (* 20 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))
|
rlm@60
|
188
|
rlm@60
|
189 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
rlm@60
|
190 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
rlm@60
|
191 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
rlm@60
|
192
|
rlm@60
|
193 ])
|
rlm@60
|
194
|
rlm@60
|
195 ;;;;;;;;;;;;;;;;;; Proprioception ;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@60
|
196
|
rlm@60
|
197 ;; this is not used as just getting the rotation would be simpler.
|
rlm@60
|
198 (defn proprioception-senses
|
rlm@60
|
199 "given a control , create a sequence of thunks that will report the
|
rlm@60
|
200 rotation of the control's object along the same axes as the motor-control map."
|
rlm@60
|
201 [control]
|
rlm@60
|
202 (let [torques
|
rlm@60
|
203 (concat
|
rlm@60
|
204 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
|
rlm@60
|
205 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
|
rlm@60
|
206 [Vector3f/UNIT_X])]
|
rlm@60
|
207 (map (fn [torque-axis]
|
rlm@60
|
208 (fn []
|
rlm@60
|
209 (.getPhysicsRotation control)))
|
rlm@60
|
210 torques)))
|
rlm@60
|
211
|
rlm@60
|
212 (defn orthogonal-vect
|
rlm@60
|
213 "Return a vector orthogonal to the current one"
|
rlm@60
|
214 [vector]
|
rlm@60
|
215 (let [x (.getX vector)
|
rlm@60
|
216 y (.getY vector)
|
rlm@60
|
217 z (.getZ vector)]
|
rlm@60
|
218 (cond
|
rlm@60
|
219 (not= x (float 0)) (Vector3f. (- z) 0 x)
|
rlm@60
|
220 (not= y (float 0)) (Vector3f. 0 (- z) y)
|
rlm@60
|
221 (not= z (float 0)) (Vector3f. 0 (- z) y)
|
rlm@60
|
222 true Vector3f/ZERO)))
|
rlm@60
|
223
|
rlm@60
|
224 ;; from
|
rlm@60
|
225 ;; http://stackoverflow.com/questions/3684269/ \\
|
rlm@60
|
226 ;; component-of-a-quaternion-rotation-around-an-axis
|
rlm@60
|
227 (defn rot-about-axis [#^Quaternion q #^Vector3f axis]
|
rlm@60
|
228 (let [basis-1 (orthogonal-vect axis)
|
rlm@60
|
229 basis-2 (.cross axis basis-1)
|
rlm@60
|
230 rotated (.mult q basis-1)
|
rlm@60
|
231 alpha (.dot basis-1 (.project rotated basis-1))
|
rlm@60
|
232 beta (.dot basis-2 (.project rotated basis-2))]
|
rlm@60
|
233 (Math/atan2 beta alpha)))
|
rlm@60
|
234
|
rlm@60
|
235
|
rlm@60
|
236 (defn check-rot [a]
|
rlm@60
|
237 (rot-about-axis
|
rlm@60
|
238 (doto (Quaternion.)
|
rlm@60
|
239 (.fromAngleAxis
|
rlm@60
|
240 (float a)
|
rlm@60
|
241 (Vector3f. 1 0 0))) (Vector3f. 1 0 0)))
|
rlm@60
|
242
|
rlm@60
|
243 (defn relative-positions [joint]
|
rlm@60
|
244 (let [object-a (.getUserObject (.getBodyA joint))
|
rlm@60
|
245 object-b (.getUserObject (.getBodyB joint))
|
rlm@60
|
246 arm-a
|
rlm@60
|
247 (.normalize
|
rlm@60
|
248 (.subtract
|
rlm@60
|
249 (.localToWorld object-a (.getPivotA joint) nil)
|
rlm@60
|
250 (.getWorldTranslation object-a)))
|
rlm@60
|
251 rotate-a
|
rlm@60
|
252 (doto (Matrix3f.)
|
rlm@60
|
253 (.fromStartEndVectors arm-a Vector3f/UNIT_X))
|
rlm@60
|
254 arm-b
|
rlm@60
|
255 (.mult
|
rlm@60
|
256 rotate-a
|
rlm@60
|
257 (.normalize
|
rlm@60
|
258 (.subtract
|
rlm@60
|
259 (.localToWorld object-b (.getPivotB joint) nil)
|
rlm@60
|
260 (.getWorldTranslation object-b))))
|
rlm@60
|
261 pitch
|
rlm@60
|
262 (.angleBetween
|
rlm@60
|
263 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
|
rlm@60
|
264 (Vector2f. 1 0))
|
rlm@60
|
265 yaw
|
rlm@60
|
266 (.angleBetween
|
rlm@60
|
267 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
|
rlm@60
|
268 (Vector2f. 1 0))
|
rlm@60
|
269
|
rlm@60
|
270 roll
|
rlm@61
|
271 (rot-about-axis
|
rlm@61
|
272 (.mult
|
rlm@61
|
273 (.getLocalRotation object-b)
|
rlm@61
|
274 (doto (Quaternion.)
|
rlm@61
|
275 (.fromRotationMatrix rotate-a)))
|
rlm@61
|
276 arm-b)
|
rlm@60
|
277 ]
|
rlm@60
|
278
|
rlm@60
|
279
|
rlm@60
|
280
|
rlm@60
|
281 ;;(println-repl
|
rlm@60
|
282 ;; "arm-b is " arm-b)
|
rlm@60
|
283 ;;(println-repl
|
rlm@60
|
284 ;; "pivot-b is " (.getPivotB joint))
|
rlm@60
|
285 ;;(println-repl
|
rlm@60
|
286 ;; (format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
|
rlm@60
|
287 ;; pitch yaw roll))
|
rlm@60
|
288 [pitch yaw roll]))
|
rlm@60
|
289
|
rlm@60
|
290
|
rlm@60
|
291
|
rlm@60
|
292
|
rlm@60
|
293
|
rlm@60
|
294
|
rlm@60
|
295
|
rlm@60
|
296 (defn test-worm-control
|
rlm@60
|
297 []
|
rlm@60
|
298 (let [worm (point-worm)
|
rlm@60
|
299 time (atom 0)
|
rlm@60
|
300 worm-motor-map (motor-map worm)
|
rlm@61
|
301 ;;body-map (proprioception worm)
|
rlm@60
|
302 debug-segments
|
rlm@60
|
303 (map
|
rlm@60
|
304 #(doto
|
rlm@60
|
305 (make-shape
|
rlm@60
|
306 (assoc base-shape
|
rlm@60
|
307 :name "debug-line"
|
rlm@60
|
308 :physical? false
|
rlm@60
|
309 :shape
|
rlm@60
|
310 (com.jme3.scene.shape.Line.
|
rlm@60
|
311 (.add (.getWorldTranslation %)
|
rlm@60
|
312 (Vector3f. -0.2 0 0 ))
|
rlm@60
|
313 (.add (.getWorldTranslation %)
|
rlm@60
|
314 (Vector3f. 0.2 0 0)))))
|
rlm@60
|
315 (.setMaterial (green-x-ray)))
|
rlm@60
|
316 (drop 1 (node-seq worm)))]
|
rlm@60
|
317 (world
|
rlm@60
|
318 (nodify [worm
|
rlm@60
|
319 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
|
rlm@60
|
320 :color ColorRGBA/Gray)])
|
rlm@60
|
321 standard-debug-controls
|
rlm@60
|
322 (fn [world]
|
rlm@60
|
323 (.attachChild (.getRootNode world) (nodify debug-segments))
|
rlm@60
|
324 (enable-debug world)
|
rlm@60
|
325 (light-up-everything world)
|
rlm@60
|
326 (com.aurellem.capture.Capture/captureVideo
|
rlm@60
|
327 world
|
rlm@60
|
328 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
|
rlm@60
|
329
|
rlm@60
|
330 (fn [_ _]
|
rlm@60
|
331 (dorun
|
rlm@60
|
332 (map
|
rlm@60
|
333 (fn [worm-segment
|
rlm@60
|
334 debug-segment]
|
rlm@60
|
335 (.rotate
|
rlm@60
|
336 debug-segment
|
rlm@60
|
337 (Quaternion. (float 0) (float 0.05) (float 0) (float 1))))
|
rlm@60
|
338 (drop 1 (node-seq worm))
|
rlm@60
|
339 debug-segments))
|
rlm@60
|
340 (swap! time inc)
|
rlm@61
|
341 ;;(println-repl (with-out-str (clojure.pprint/pprint (doall (body-map)))))
|
rlm@60
|
342 (Thread/sleep 200)
|
rlm@60
|
343 (dorun (worm-motor-map
|
rlm@60
|
344 (worm-pattern @time)))))))
|
rlm@60
|
345
|
rlm@60
|
346
|
rlm@60
|
347
|
rlm@60
|
348
|
rlm@60
|
349
|
rlm@60
|
350 (defn test-prop
|
rlm@60
|
351 "see how torque works."
|
rlm@60
|
352 []
|
rlm@60
|
353 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
|
rlm@60
|
354 :mass 0 :color ColorRGBA/Green)
|
rlm@60
|
355 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
|
rlm@61
|
356 :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
|
rlm@60
|
357 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
|
rlm@60
|
358 :mass 0 :color ColorRGBA/Gray)
|
rlm@60
|
359
|
rlm@60
|
360 move-up? (atom false)
|
rlm@60
|
361 move-down? (atom false)
|
rlm@60
|
362 move-left? (atom false)
|
rlm@60
|
363 move-right? (atom false)
|
rlm@60
|
364 roll-left? (atom false)
|
rlm@60
|
365 roll-right? (atom false)
|
rlm@60
|
366 control (.getControl finger RigidBodyControl)
|
rlm@60
|
367 joint
|
rlm@60
|
368 (doto
|
rlm@60
|
369 (Point2PointJoint.
|
rlm@60
|
370 (.getControl hand RigidBodyControl)
|
rlm@60
|
371 control
|
rlm@60
|
372 (Vector3f. 1.2 0 0)
|
rlm@60
|
373 (Vector3f. -1.2 0 0 ))
|
rlm@60
|
374 (.setCollisionBetweenLinkedBodys false))
|
rlm@60
|
375 time (atom 0)
|
rlm@60
|
376 ]
|
rlm@60
|
377 (world
|
rlm@60
|
378 (nodify [hand finger floor])
|
rlm@60
|
379 (merge standard-debug-controls
|
rlm@60
|
380 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@60
|
381 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@60
|
382 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@60
|
383 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@60
|
384 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@60
|
385 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@60
|
386 (fn [world]
|
rlm@61
|
387 (set-gravity world (Vector3f. 0 0 0))
|
rlm@60
|
388 (.setMoveSpeed (.getFlyByCamera world) 50)
|
rlm@60
|
389 (.setRotationSpeed (.getFlyByCamera world) 50)
|
rlm@60
|
390 (light-up-everything world)
|
rlm@60
|
391 (.setTimer world (NanoTimer.))
|
rlm@60
|
392 )
|
rlm@60
|
393 (fn [_ _]
|
rlm@60
|
394 (if @move-up?
|
rlm@60
|
395 (.applyTorque control
|
rlm@60
|
396 (.mult (.getPhysicsRotation control)
|
rlm@61
|
397 (Vector3f. 0 0 10))))
|
rlm@60
|
398 (if @move-down?
|
rlm@60
|
399 (.applyTorque control
|
rlm@60
|
400 (.mult (.getPhysicsRotation control)
|
rlm@61
|
401 (Vector3f. 0 0 -10))))
|
rlm@60
|
402 (if @move-left?
|
rlm@60
|
403 (.applyTorque control
|
rlm@60
|
404 (.mult (.getPhysicsRotation control)
|
rlm@61
|
405 (Vector3f. 0 10 0))))
|
rlm@60
|
406 (if @move-right?
|
rlm@60
|
407 (.applyTorque control
|
rlm@60
|
408 (.mult (.getPhysicsRotation control)
|
rlm@61
|
409 (Vector3f. 0 -10 0))))
|
rlm@60
|
410 (if @roll-left?
|
rlm@60
|
411 (.applyTorque control
|
rlm@60
|
412 (.mult (.getPhysicsRotation control)
|
rlm@61
|
413 (Vector3f. -1 0 0))))
|
rlm@60
|
414 (if @roll-right?
|
rlm@60
|
415 (.applyTorque control
|
rlm@60
|
416 (.mult (.getPhysicsRotation control)
|
rlm@61
|
417 (Vector3f. 1 0 0))))
|
rlm@60
|
418
|
rlm@60
|
419 (if (= 0 (rem (swap! time inc) 2000))
|
rlm@61
|
420 (do
|
rlm@61
|
421
|
rlm@61
|
422 (apply
|
rlm@61
|
423 (comp
|
rlm@61
|
424 println-repl
|
rlm@61
|
425 #(format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n" %1 %2 %3))
|
rlm@61
|
426 (relative-positions joint))))))))
|
rlm@56
|
427
|
rlm@0
|
428 #+end_src
|
rlm@0
|
429
|
rlm@60
|
430
|
rlm@61
|
431 * COMMENT failed-clojure-code
|
rlm@61
|
432 #+begin_src clojure
|
rlm@61
|
433 ;;(.loadModel
|
rlm@61
|
434 ;; (doto (asset-manager)
|
rlm@61
|
435 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@61
|
436 ;; "Models/person/person.blend")
|
rlm@61
|
437
|
rlm@61
|
438 (defn view-model [^String model]
|
rlm@61
|
439 (view
|
rlm@61
|
440 (.loadModel
|
rlm@61
|
441 (doto (asset-manager)
|
rlm@61
|
442 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@61
|
443 model)))
|
rlm@61
|
444
|
rlm@61
|
445 (defn load-blender-scene [^String model]
|
rlm@61
|
446 (.loadModel
|
rlm@61
|
447 (doto (asset-manager)
|
rlm@61
|
448 (.registerLoader BlenderLoader (into-array String ["blend"])))
|
rlm@61
|
449 model))
|
rlm@61
|
450
|
rlm@61
|
451 (defn worm
|
rlm@61
|
452 []
|
rlm@61
|
453 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
|
rlm@61
|
454
|
rlm@61
|
455 (defn oto
|
rlm@61
|
456 []
|
rlm@61
|
457 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
|
rlm@61
|
458
|
rlm@61
|
459 (defn sinbad
|
rlm@61
|
460 []
|
rlm@61
|
461 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
|
rlm@61
|
462
|
rlm@61
|
463 (defn worm-blender
|
rlm@61
|
464 []
|
rlm@61
|
465 (first (seq (.getChildren (load-blender-model
|
rlm@61
|
466 "Models/anim2/simple-worm.blend")))))
|
rlm@61
|
467
|
rlm@61
|
468 (defn body
|
rlm@61
|
469 "given a node with a SkeletonControl, will produce a body sutiable
|
rlm@61
|
470 for AI control with movement and proprioception."
|
rlm@61
|
471 [node]
|
rlm@61
|
472 (let [skeleton-control (.getControl node SkeletonControl)
|
rlm@61
|
473 krc (KinematicRagdollControl.)]
|
rlm@61
|
474 (comment
|
rlm@61
|
475 (dorun
|
rlm@61
|
476 (map #(.addBoneName krc %)
|
rlm@61
|
477 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
|
rlm@61
|
478 ;;"mid2" "mid3" "tail" "head"]
|
rlm@61
|
479 )))
|
rlm@61
|
480 (.addControl node krc)
|
rlm@61
|
481 (.setRagdollMode krc)
|
rlm@61
|
482 )
|
rlm@61
|
483 node
|
rlm@61
|
484 )
|
rlm@61
|
485 (defn show-skeleton [node]
|
rlm@61
|
486 (let [sd
|
rlm@61
|
487
|
rlm@61
|
488 (doto
|
rlm@61
|
489 (SkeletonDebugger. "aurellem-skel-debug"
|
rlm@61
|
490 (skel node))
|
rlm@61
|
491 (.setMaterial (green-x-ray)))]
|
rlm@61
|
492 (.attachChild node sd)
|
rlm@61
|
493 node))
|
rlm@61
|
494
|
rlm@61
|
495
|
rlm@61
|
496
|
rlm@61
|
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
498
|
rlm@61
|
499 ;; this could be a good way to give objects special properties like
|
rlm@61
|
500 ;; being eyes and the like
|
rlm@61
|
501
|
rlm@61
|
502 (.getUserData
|
rlm@61
|
503 (.getChild
|
rlm@61
|
504 (load-blender-model "Models/property/test.blend") 0)
|
rlm@61
|
505 "properties")
|
rlm@61
|
506
|
rlm@61
|
507 ;; the properties are saved along with the blender file.
|
rlm@61
|
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
509
|
rlm@61
|
510
|
rlm@61
|
511
|
rlm@61
|
512
|
rlm@61
|
513 (defn init-debug-skel-node
|
rlm@61
|
514 [f debug-node skeleton]
|
rlm@61
|
515 (let [bones
|
rlm@61
|
516 (map #(.getBone skeleton %)
|
rlm@61
|
517 (range (.getBoneCount skeleton)))]
|
rlm@61
|
518 (dorun (map #(.setUserControl % true) bones))
|
rlm@61
|
519 (dorun (map (fn [b]
|
rlm@61
|
520 (println (.getName b)
|
rlm@61
|
521 " -- " (f b)))
|
rlm@61
|
522 bones))
|
rlm@61
|
523 (dorun
|
rlm@61
|
524 (map #(.attachChild
|
rlm@61
|
525 debug-node
|
rlm@61
|
526 (doto
|
rlm@61
|
527 (sphere 0.1
|
rlm@61
|
528 :position (f %)
|
rlm@61
|
529 :physical? false)
|
rlm@61
|
530 (.setMaterial (green-x-ray))))
|
rlm@61
|
531 bones)))
|
rlm@61
|
532 debug-node)
|
rlm@61
|
533
|
rlm@61
|
534 (import jme3test.bullet.PhysicsTestHelper)
|
rlm@61
|
535
|
rlm@61
|
536
|
rlm@61
|
537 (defn test-zzz [the-worm world value]
|
rlm@61
|
538 (if (not value)
|
rlm@61
|
539 (let [skeleton (skel the-worm)]
|
rlm@61
|
540 (println-repl "enabling bones")
|
rlm@61
|
541 (dorun
|
rlm@61
|
542 (map
|
rlm@61
|
543 #(.setUserControl (.getBone skeleton %) true)
|
rlm@61
|
544 (range (.getBoneCount skeleton))))
|
rlm@61
|
545
|
rlm@61
|
546
|
rlm@61
|
547 (let [b (.getBone skeleton 2)]
|
rlm@61
|
548 (println-repl "moving " (.getName b))
|
rlm@61
|
549 (println-repl (.getLocalPosition b))
|
rlm@61
|
550 (.setUserTransforms b
|
rlm@61
|
551 Vector3f/UNIT_X
|
rlm@61
|
552 Quaternion/IDENTITY
|
rlm@61
|
553 ;;(doto (Quaternion.)
|
rlm@61
|
554 ;; (.fromAngles (/ Math/PI 2)
|
rlm@61
|
555 ;; 0
|
rlm@61
|
556 ;; 0
|
rlm@61
|
557
|
rlm@61
|
558 (Vector3f. 1 1 1))
|
rlm@61
|
559 )
|
rlm@61
|
560
|
rlm@61
|
561 (println-repl "hi! <3"))))
|
rlm@61
|
562
|
rlm@61
|
563
|
rlm@61
|
564 (defn test-ragdoll []
|
rlm@61
|
565
|
rlm@61
|
566 (let [the-worm
|
rlm@61
|
567
|
rlm@61
|
568 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
|
rlm@61
|
569 (doto (show-skeleton (worm-blender))
|
rlm@61
|
570 (.setLocalTranslation (Vector3f. 0 10 0))
|
rlm@61
|
571 ;;(worm)
|
rlm@61
|
572 ;;(oto)
|
rlm@61
|
573 ;;(sinbad)
|
rlm@61
|
574 )
|
rlm@61
|
575 ]
|
rlm@61
|
576
|
rlm@61
|
577
|
rlm@61
|
578 (.start
|
rlm@61
|
579 (world
|
rlm@61
|
580 (doto (Node.)
|
rlm@61
|
581 (.attachChild the-worm))
|
rlm@61
|
582 {"key-return" (fire-cannon-ball)
|
rlm@61
|
583 "key-space" (partial test-zzz the-worm)
|
rlm@61
|
584 }
|
rlm@61
|
585 (fn [world]
|
rlm@61
|
586 (light-up-everything world)
|
rlm@61
|
587 (PhysicsTestHelper/createPhysicsTestWorld
|
rlm@61
|
588 (.getRootNode world)
|
rlm@61
|
589 (asset-manager)
|
rlm@61
|
590 (.getPhysicsSpace
|
rlm@61
|
591 (.getState (.getStateManager world) BulletAppState)))
|
rlm@61
|
592 (set-gravity world Vector3f/ZERO)
|
rlm@61
|
593 ;;(.setTimer world (NanoTimer.))
|
rlm@61
|
594 ;;(org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@61
|
595 )
|
rlm@61
|
596 no-op
|
rlm@61
|
597 )
|
rlm@61
|
598
|
rlm@61
|
599
|
rlm@61
|
600 )))
|
rlm@61
|
601
|
rlm@61
|
602
|
rlm@61
|
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
604 ;;; here is the ragdoll stuff
|
rlm@61
|
605
|
rlm@61
|
606 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
|
rlm@61
|
607 (def mesh worm-mesh)
|
rlm@61
|
608
|
rlm@61
|
609 (.getFloatBuffer mesh VertexBuffer$Type/Position)
|
rlm@61
|
610 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@61
|
611 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@61
|
612
|
rlm@61
|
613
|
rlm@61
|
614 (defn position [index]
|
rlm@61
|
615 (.get
|
rlm@61
|
616 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
|
rlm@61
|
617 index))
|
rlm@61
|
618
|
rlm@61
|
619 (defn bones [index]
|
rlm@61
|
620 (.get
|
rlm@61
|
621 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
|
rlm@61
|
622 index))
|
rlm@61
|
623
|
rlm@61
|
624 (defn bone-weights [index]
|
rlm@61
|
625 (.get
|
rlm@61
|
626 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
|
rlm@61
|
627 index))
|
rlm@61
|
628
|
rlm@61
|
629
|
rlm@61
|
630
|
rlm@61
|
631 (defn vertex-bones [vertex]
|
rlm@61
|
632 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@61
|
633
|
rlm@61
|
634 (defn vertex-weights [vertex]
|
rlm@61
|
635 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
|
rlm@61
|
636
|
rlm@61
|
637 (defn vertex-position [index]
|
rlm@61
|
638 (let [offset (* index 3)]
|
rlm@61
|
639 (Vector3f. (position offset)
|
rlm@61
|
640 (position (inc offset))
|
rlm@61
|
641 (position (inc(inc offset))))))
|
rlm@61
|
642
|
rlm@61
|
643 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
|
rlm@61
|
644
|
rlm@61
|
645 (defn bone-control-color [index]
|
rlm@61
|
646 (get {[1 0 0 0] ColorRGBA/Red
|
rlm@61
|
647 [1 2 0 0] ColorRGBA/Magenta
|
rlm@61
|
648 [2 0 0 0] ColorRGBA/Blue}
|
rlm@61
|
649 (vertex-bones index)
|
rlm@61
|
650 ColorRGBA/White))
|
rlm@61
|
651
|
rlm@61
|
652 (defn influence-color [index bone-num]
|
rlm@61
|
653 (get
|
rlm@61
|
654 {(float 0) ColorRGBA/Blue
|
rlm@61
|
655 (float 0.5) ColorRGBA/Green
|
rlm@61
|
656 (float 1) ColorRGBA/Red}
|
rlm@61
|
657 ;; find the weight of the desired bone
|
rlm@61
|
658 ((zipmap (vertex-bones index)(vertex-weights index))
|
rlm@61
|
659 bone-num)
|
rlm@61
|
660 ColorRGBA/Blue))
|
rlm@61
|
661
|
rlm@61
|
662 (def worm-vertices (set (map vertex-info (range 60))))
|
rlm@61
|
663
|
rlm@61
|
664
|
rlm@61
|
665 (defn test-info []
|
rlm@61
|
666 (let [points (Node.)]
|
rlm@61
|
667 (dorun
|
rlm@61
|
668 (map #(.attachChild points %)
|
rlm@61
|
669 (map #(sphere 0.01
|
rlm@61
|
670 :position (vertex-position %)
|
rlm@61
|
671 :color (influence-color % 1)
|
rlm@61
|
672 :physical? false)
|
rlm@61
|
673 (range 60))))
|
rlm@61
|
674 (view points)))
|
rlm@61
|
675
|
rlm@61
|
676
|
rlm@61
|
677 (defrecord JointControl [joint physics-space]
|
rlm@61
|
678 PhysicsControl
|
rlm@61
|
679 (setPhysicsSpace [this space]
|
rlm@61
|
680 (dosync
|
rlm@61
|
681 (ref-set (:physics-space this) space))
|
rlm@61
|
682 (.addJoint space (:joint this)))
|
rlm@61
|
683 (update [this tpf])
|
rlm@61
|
684 (setSpatial [this spatial])
|
rlm@61
|
685 (render [this rm vp])
|
rlm@61
|
686 (getPhysicsSpace [this] (deref (:physics-space this)))
|
rlm@61
|
687 (isEnabled [this] true)
|
rlm@61
|
688 (setEnabled [this state]))
|
rlm@61
|
689
|
rlm@61
|
690 (defn add-joint
|
rlm@61
|
691 "Add a joint to a particular object. When the object is added to the
|
rlm@61
|
692 PhysicsSpace of a simulation, the joint will also be added"
|
rlm@61
|
693 [object joint]
|
rlm@61
|
694 (let [control (JointControl. joint (ref nil))]
|
rlm@61
|
695 (.addControl object control))
|
rlm@61
|
696 object)
|
rlm@61
|
697
|
rlm@61
|
698
|
rlm@61
|
699 (defn hinge-world
|
rlm@61
|
700 []
|
rlm@61
|
701 (let [sphere1 (sphere)
|
rlm@61
|
702 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
|
rlm@61
|
703 joint (Point2PointJoint.
|
rlm@61
|
704 (.getControl sphere1 RigidBodyControl)
|
rlm@61
|
705 (.getControl sphere2 RigidBodyControl)
|
rlm@61
|
706 Vector3f/ZERO (Vector3f. 3 3 3))]
|
rlm@61
|
707 (add-joint sphere1 joint)
|
rlm@61
|
708 (doto (Node. "hinge-world")
|
rlm@61
|
709 (.attachChild sphere1)
|
rlm@61
|
710 (.attachChild sphere2))))
|
rlm@61
|
711
|
rlm@61
|
712
|
rlm@61
|
713 (defn test-joint []
|
rlm@61
|
714 (view (hinge-world)))
|
rlm@61
|
715
|
rlm@61
|
716 ;; (defn copier-gen []
|
rlm@61
|
717 ;; (let [count (atom 0)]
|
rlm@61
|
718 ;; (fn [in]
|
rlm@61
|
719 ;; (swap! count inc)
|
rlm@61
|
720 ;; (clojure.contrib.duck-streams/copy
|
rlm@61
|
721 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
|
rlm@61
|
722 ;; ;;/home/r/tmp/mao-test/clojure-images
|
rlm@61
|
723 ;; (format "%08d.png" @count)))))))
|
rlm@61
|
724 ;; (defn decrease-framerate []
|
rlm@61
|
725 ;; (map
|
rlm@61
|
726 ;; (copier-gen)
|
rlm@61
|
727 ;; (sort
|
rlm@61
|
728 ;; (map first
|
rlm@61
|
729 ;; (partition
|
rlm@61
|
730 ;; 4
|
rlm@61
|
731 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
|
rlm@61
|
732 ;; (file-seq
|
rlm@61
|
733 ;; (file-str
|
rlm@61
|
734 ;; "/home/r/media/anime/mao-temp/images"))))))))
|
rlm@61
|
735
|
rlm@61
|
736
|
rlm@61
|
737
|
rlm@61
|
738 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
rlm@61
|
739
|
rlm@61
|
740 (defn proprioception
|
rlm@61
|
741 "Create a proprioception map that reports the rotations of the
|
rlm@61
|
742 various limbs of the creature's body"
|
rlm@61
|
743 [creature]
|
rlm@61
|
744 [#^Node creature]
|
rlm@61
|
745 (let [
|
rlm@61
|
746 nodes (node-seq creature)
|
rlm@61
|
747 joints
|
rlm@61
|
748 (map
|
rlm@61
|
749 :joint
|
rlm@61
|
750 (filter
|
rlm@61
|
751 #(isa? (class %) JointControl)
|
rlm@61
|
752 (reduce
|
rlm@61
|
753 concat
|
rlm@61
|
754 (map (fn [node]
|
rlm@61
|
755 (map (fn [num] (.getControl node num))
|
rlm@61
|
756 (range (.getNumControls node))))
|
rlm@61
|
757 nodes))))]
|
rlm@61
|
758 (fn []
|
rlm@61
|
759 (reduce concat (map relative-positions (list (first joints)))))))
|
rlm@61
|
760
|
rlm@61
|
761
|
rlm@61
|
762 #+end_src
|
rlm@0
|
763
|
rlm@0
|
764
|
rlm@0
|
765
|
rlm@0
|
766
|
rlm@0
|
767
|
rlm@0
|
768
|
rlm@0
|
769
|
rlm@0
|
770 * COMMENT generate Source.
|
rlm@44
|
771 #+begin_src clojure :tangle ../src/cortex/body.clj
|
rlm@0
|
772 <<body-main>>
|
rlm@0
|
773 #+end_src
|
rlm@0
|
774
|
rlm@60
|
775
|