comparison org/test-creature.org @ 158:811127d79d24

refactored muscles
author Robert McIntyre <rlm@mit.edu>
date Fri, 03 Feb 2012 06:33:37 -0700
parents 84c67be00abe
children 75b6c2ebbf8e
comparison
equal deleted inserted replaced
157:84c67be00abe 158:811127d79d24
36 Still going to do an eve-like body in blender, but due to problems 36 Still going to do an eve-like body in blender, but due to problems
37 importing the joints, etc into jMonkeyEngine3, I'm going to do all 37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
38 the connecting here in clojure code, using the names of the individual 38 the connecting here in clojure code, using the names of the individual
39 components and trial and error. Later, I'll maybe make some sort of 39 components and trial and error. Later, I'll maybe make some sort of
40 creature-building modifications to blender that support whatever 40 creature-building modifications to blender that support whatever
41 discreitized senses I'm going to make. 41 discritized senses I'm going to make.
42 42
43 #+name: body-1 43 #+name: body-1
44 #+begin_src clojure 44 #+begin_src clojure
45 (ns cortex.silly 45 (ns cortex.silly
46 "let's play!" 46 "let's play!"
47 {:author "Robert McIntyre"}) 47 {:author "Robert McIntyre"})
48 48
49 ;; TODO remove this! 49 ;; TODO remove this!
50 (require 'cortex.import) 50 (require 'cortex.import)
51 (cortex.import/mega-import-jme3) 51 (cortex.import/mega-import-jme3)
52 (use '(cortex world util body hearing touch vision sense proprioception)) 52 (use '(cortex world util body hearing touch vision sense
53 proprioception movement))
53 54
54 (rlm.rlm-commands/help) 55 (rlm.rlm-commands/help)
55 (import java.awt.image.BufferedImage) 56 (import java.awt.image.BufferedImage)
56 (import javax.swing.JPanel) 57 (import javax.swing.JPanel)
57 (import javax.swing.SwingUtilities) 58 (import javax.swing.SwingUtilities)
419 ;;(defn test-touch [world creature] 420 ;;(defn test-touch [world creature]
420 421
421 422
422 423
423 424
424 ;; here's how motor-control/ proprioception will work: Each muscle is
425 ;; defined by a 1-D array of numbers (the "motor pool") each of which
426 ;; represent muscle fibers. A muscle also has a scalar :strength
427 ;; factor which determines how strong the muscle as a whole is.
428 ;; The effector function for a muscle takes a number < (count
429 ;; motor-pool) and that number is said to "activate" all the muscle
430 ;; fibers whose index is lower than the number. Each fiber will apply
431 ;; force in proportion to its value in the array. Lower values cause
432 ;; less force. The lower values can be put at the "beginning" of the
433 ;; 1-D array to simulate the layout of actual human muscles, which are
434 ;; capable of more percise movements when exerting less force.
435
436 ;; I don't know how to encode proprioception, so for now, just return
437 ;; a function for each joint that returns a triplet of floats which
438 ;; represent relative roll, pitch, and yaw. Write display code for
439 ;; this though.
440
441 (defn muscle-fiber-values
442 "get motor pool strengths"
443 [#^BufferedImage image]
444 (vec
445 (let [width (.getWidth image)]
446 (for [x (range width)]
447 (- 255
448 (bit-and
449 0x0000FF
450 (.getRGB image x 0)))))))
451
452
453 (defn creature-muscles
454 "Return the children of the creature's \"muscles\" node."
455 [#^Node creature]
456 (if-let [muscle-node (.getChild creature "muscles")]
457 (seq (.getChildren muscle-node))
458 (do (println-repl "could not find muscles node") [])))
459
460 (defn single-muscle [#^Node parts #^Node muscle]
461 (let [target (closest-node parts muscle)
462 axis
463 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
464 strength (meta-data muscle "strength")
465 image-name (read-string (meta-data muscle "muscle"))
466 image
467 (ImageToAwt/convert
468 (.getImage (.loadTexture (asset-manager) image-name))
469 false false 0)
470 fibers (muscle-fiber-values image)
471 fiber-integral (reductions + fibers)
472 force-index (vec
473 (map
474 #(float (* strength (/ % (last
475 fiber-integral))))
476 fiber-integral))
477 control (.getControl target RigidBodyControl)]
478 (fn [n]
479 (let [pool-index (min n (count fibers))]
480 (.applyTorque control (.mult axis (force-index n)))))))
481
482
483 (defn enable-muscles
484 "Must be called on a creature after RigidBodyControls have been
485 created."
486 [#^Node creature]
487 (let [muscles (creature-muscles creature)]
488 (for [muscle muscles]
489 (single-muscle creature muscle))))
490 425
491 (defn test-creature [thing] 426 (defn test-creature [thing]
492 (let [x-axis 427 (let [x-axis
493 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red) 428 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
494 y-axis 429 y-axis