Mercurial > cortex
comparison org/body.org @ 58:25142dad240a
created test suite
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 19 Nov 2011 23:42:21 -0700 |
parents | 37a3256e1ed3 |
children | e5e627f50a3a |
comparison
equal
deleted
inserted
replaced
57:37a3256e1ed3 | 58:25142dad240a |
---|---|
3 #+email: rlm@mit.edu | 3 #+email: rlm@mit.edu |
4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3. | 4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3. |
5 #+SETUPFILE: ../../aurellem/org/setup.org | 5 #+SETUPFILE: ../../aurellem/org/setup.org |
6 #+INCLUDE: ../../aurellem/org/level-0.org | 6 #+INCLUDE: ../../aurellem/org/level-0.org |
7 | 7 |
8 * COMMENT Body | 8 * Body |
9 | 9 |
10 #+srcname: body-main | 10 #+srcname: body-main |
11 #+begin_src clojure | 11 #+begin_src clojure |
12 (ns cortex.body | 12 (ns cortex.body |
13 (use (cortex world util import))) | 13 (use (cortex world util import))) |
231 ) | 231 ) |
232 | 232 |
233 | 233 |
234 ))) | 234 ))) |
235 | 235 |
236 | |
237 | |
238 | |
239 | |
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
241 ;;; here is the ragdoll stuff | |
242 | |
243 (def worm-mesh (.getMesh (.getChild (worm-blender) 0))) | |
244 (def mesh worm-mesh) | |
245 | |
246 (.getFloatBuffer mesh VertexBuffer$Type/Position) | |
247 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) | |
248 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) | |
249 | |
250 | |
251 (defn position [index] | |
252 (.get | |
253 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position) | |
254 index)) | |
255 | |
256 (defn bones [index] | |
257 (.get | |
258 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) | |
259 index)) | |
260 | |
261 (defn bone-weights [index] | |
262 (.get | |
263 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) | |
264 index)) | |
265 | |
266 | |
267 | |
268 (defn vertex-bones [vertex] | |
269 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4))))) | |
270 | |
271 (defn vertex-weights [vertex] | |
272 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4))))) | |
273 | |
274 (defn vertex-position [index] | |
275 (let [offset (* index 3)] | |
276 (Vector3f. (position offset) | |
277 (position (inc offset)) | |
278 (position (inc(inc offset)))))) | |
279 | |
280 (def vertex-info (juxt vertex-position vertex-bones vertex-weights)) | |
281 | |
282 (defn bone-control-color [index] | |
283 (get {[1 0 0 0] ColorRGBA/Red | |
284 [1 2 0 0] ColorRGBA/Magenta | |
285 [2 0 0 0] ColorRGBA/Blue} | |
286 (vertex-bones index) | |
287 ColorRGBA/White)) | |
288 | |
289 (defn influence-color [index bone-num] | |
290 (get | |
291 {(float 0) ColorRGBA/Blue | |
292 (float 0.5) ColorRGBA/Green | |
293 (float 1) ColorRGBA/Red} | |
294 ;; find the weight of the desired bone | |
295 ((zipmap (vertex-bones index)(vertex-weights index)) | |
296 bone-num) | |
297 ColorRGBA/Blue)) | |
298 | |
299 (def worm-vertices (set (map vertex-info (range 60)))) | |
300 | |
301 | |
302 (defn test-info [] | |
303 (let [points (Node.)] | |
304 (dorun | |
305 (map #(.attachChild points %) | |
306 (map #(sphere 0.01 | |
307 :position (vertex-position %) | |
308 :color (influence-color % 1) | |
309 :physical? false) | |
310 (range 60)))) | |
311 (view points))) | |
312 | |
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
314 | |
315 | |
316 | |
317 | |
318 | |
319 | |
320 ;;;;;;;;;;;; eve-style bodies ;;;;;;;; | |
236 (defn joint-control | 321 (defn joint-control |
237 [joint] | 322 [joint] |
238 (let [physics-space (ref nil)] | 323 (let [physics-space (ref nil)] |
239 (reify PhysicsControl | 324 (reify PhysicsControl |
240 (setPhysicsSpace [this space] | 325 (setPhysicsSpace [this space] |
269 (.attachChild sphere1) | 354 (.attachChild sphere1) |
270 (.attachChild sphere2)))) | 355 (.attachChild sphere2)))) |
271 | 356 |
272 (defn test-joint [] | 357 (defn test-joint [] |
273 (view (hinge-world))) | 358 (view (hinge-world))) |
274 | 359 |
275 | 360 |
276 | 361 (defn worm [segment-length num-segments interstitial-space radius] |
362 (letfn [(nth-segment | |
363 [n] | |
364 (box segment-length radius radius :mass 0.1 | |
365 :position | |
366 (Vector3f. | |
367 (* 2 n (+ interstitial-space segment-length)) 0 0) | |
368 :name (str "worm-segment" n) | |
369 :color (ColorRGBA/randomColor)))] | |
370 (map nth-segment (range num-segments)))) | |
371 | |
372 (defn nodify | |
373 "take a sequence of things that can be attached to a node and return | |
374 a node with all of the attached" | |
375 ([name children] | |
376 (let [node (Node. name)] | |
377 (dorun (map #(.attachChild node %) children)) | |
378 node)) | |
379 ([children] (nodify "" children))) | |
380 | |
381 | |
382 (defn connect-at-midpoint | |
383 [segmentA segmentB] | |
384 (let [centerA (.getWorldTranslation segmentA) | |
385 centerB (.getWorldTranslation segmentB) | |
386 midpoint (.mult (.add centerA centerB) (float 0.5)) | |
387 pivotA (.subtract midpoint centerA) | |
388 pivotB (.subtract midpoint centerB) | |
389 | |
390 joint (Point2PointJoint. | |
391 (.getControl segmentA RigidBodyControl) | |
392 (.getControl segmentB RigidBodyControl) | |
393 pivotA | |
394 pivotB)] | |
395 (add-joint segmentA joint) | |
396 segmentB)) | |
397 | |
398 | |
399 (defn point-worm [] | |
400 (let [segments (worm 0.2 5 0.1 0.1)] | |
401 (dorun (map (partial apply connect-at-midpoint) | |
402 (partition 2 1 segments))) | |
403 (nodify "worm" segments))) | |
404 | |
405 | |
406 (defn test-worm [] | |
407 (.start | |
408 (world | |
409 (doto (Node.) | |
410 ;;(.attachChild (point-worm)) | |
411 (.attachChild (load-blender-model | |
412 "Models/anim2/joint-worm.blend")) | |
413 | |
414 (.attachChild (box 10 1 10 | |
415 :position (Vector3f. 0 -2 0) :mass 0 | |
416 :color (ColorRGBA/Gray)))) | |
417 { | |
418 "key-space" (fire-cannon-ball) | |
419 } | |
420 (fn [world] | |
421 (enable-debug world) | |
422 (light-up-everything world) | |
423 ;;(.setTimer world (NanoTimer.)) | |
424 ) | |
425 no-op))) | |
277 | 426 |
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
279 ;;; here is the ragdoll stuff | 428 |
280 | 429 |
281 (def worm-mesh (.getMesh (.getChild (worm-blender) 0))) | 430 |
282 (def mesh worm-mesh) | 431 |
283 | 432 |
284 (.getFloatBuffer mesh VertexBuffer$Type/Position) | |
285 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) | |
286 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) | |
287 | |
288 | |
289 (defn position [index] | |
290 (.get | |
291 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position) | |
292 index)) | |
293 | |
294 (defn bones [index] | |
295 (.get | |
296 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) | |
297 index)) | |
298 | |
299 (defn bone-weights [index] | |
300 (.get | |
301 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) | |
302 index)) | |
303 | |
304 | |
305 | |
306 (defn vertex-bones [vertex] | |
307 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4))))) | |
308 | |
309 (defn vertex-weights [vertex] | |
310 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4))))) | |
311 | |
312 (defn vertex-position [index] | |
313 (let [offset (* index 3)] | |
314 (Vector3f. (position offset) | |
315 (position (inc offset)) | |
316 (position (inc(inc offset)))))) | |
317 | |
318 (def vertex-info (juxt vertex-position vertex-bones vertex-weights)) | |
319 | |
320 (defn bone-control-color [index] | |
321 (get {[1 0 0 0] ColorRGBA/Red | |
322 [1 2 0 0] ColorRGBA/Magenta | |
323 [2 0 0 0] ColorRGBA/Blue} | |
324 (vertex-bones index) | |
325 ColorRGBA/White)) | |
326 | |
327 (defn influence-color [index bone-num] | |
328 (get | |
329 {(float 0) ColorRGBA/Blue | |
330 (float 0.5) ColorRGBA/Green | |
331 (float 1) ColorRGBA/Red} | |
332 ;; find the weight of the desired bone | |
333 ((zipmap (vertex-bones index)(vertex-weights index)) | |
334 bone-num) | |
335 ColorRGBA/Blue)) | |
336 | |
337 | |
338 | |
339 | |
340 (def worm-vertices (set (map vertex-info (range 60)))) | |
341 | |
342 | |
343 (defn test-info [] | |
344 (let [points (Node.)] | |
345 (dorun | |
346 (map #(.attachChild points %) | |
347 (map #(sphere 0.01 | |
348 :position (vertex-position %) | |
349 :color (influence-color % 1) | |
350 :physical? false) | |
351 (range 60)))) | |
352 (view points))) | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | |
360 | |
361 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
362 | 433 |
363 | 434 |
364 | 435 |
365 | 436 |
366 | 437 |