annotate org/movement.org @ 164:c33a8e5fe7bc

removed hearing-init-fns requirement, changed names
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 03:29:59 -0700
parents 2cbdd7034c6c
children f1b078375484
rev   line source
rlm@158 1 #+title: Movement!
rlm@158 2 #+author: Robert McIntyre
rlm@158 3 #+email: rlm@mit.edu
rlm@158 4 #+description: muscles for a simulated creature
rlm@158 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@158 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@158 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@158 8
rlm@158 9 #+name: movement
rlm@158 10 #+begin_src clojure
rlm@158 11 (ns cortex.movement
rlm@158 12 (:use (cortex world util sense body))
rlm@158 13 (:import jme3tools.converters.ImageToAwt)
rlm@158 14 (:import java.awt.image.BufferedImage))
rlm@158 15
rlm@158 16 (cortex.import/mega-import-jme3)
rlm@158 17
rlm@158 18 ;; here's how motor-control/ proprioception will work: Each muscle is
rlm@158 19 ;; defined by a 1-D array of numbers (the "motor pool") each of which
rlm@158 20 ;; represent muscle fibers. A muscle also has a scalar :strength
rlm@158 21 ;; factor which determines how strong the muscle as a whole is.
rlm@158 22 ;; The effector function for a muscle takes a number < (count
rlm@158 23 ;; motor-pool) and that number is said to "activate" all the muscle
rlm@158 24 ;; fibers whose index is lower than the number. Each fiber will apply
rlm@158 25 ;; force in proportion to its value in the array. Lower values cause
rlm@158 26 ;; less force. The lower values can be put at the "beginning" of the
rlm@158 27 ;; 1-D array to simulate the layout of actual human muscles, which are
rlm@158 28 ;; capable of more percise movements when exerting less force.
rlm@158 29
rlm@158 30 (defn muscle-fiber-values
rlm@158 31 "get motor pool strengths"
rlm@158 32 [#^BufferedImage image]
rlm@158 33 (vec
rlm@158 34 (let [width (.getWidth image)]
rlm@158 35 (for [x (range width)]
rlm@158 36 (- 255
rlm@158 37 (bit-and
rlm@158 38 0x0000FF
rlm@158 39 (.getRGB image x 0)))))))
rlm@158 40
rlm@158 41 (defn creature-muscles
rlm@158 42 "Return the children of the creature's \"muscles\" node."
rlm@158 43 [#^Node creature]
rlm@158 44 (if-let [muscle-node (.getChild creature "muscles")]
rlm@158 45 (seq (.getChildren muscle-node))
rlm@158 46 (do (println-repl "could not find muscles node") [])))
rlm@158 47
rlm@158 48 (defn single-muscle [#^Node parts #^Node muscle]
rlm@158 49 (let [target (closest-node parts muscle)
rlm@158 50 axis
rlm@158 51 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
rlm@158 52 strength (meta-data muscle "strength")
rlm@158 53 image-name (read-string (meta-data muscle "muscle"))
rlm@158 54 image
rlm@158 55 (ImageToAwt/convert
rlm@158 56 (.getImage (.loadTexture (asset-manager) image-name))
rlm@158 57 false false 0)
rlm@158 58 fibers (muscle-fiber-values image)
rlm@158 59 fiber-integral (reductions + fibers)
rlm@158 60 force-index (vec
rlm@158 61 (map
rlm@158 62 #(float (* strength (/ % (last
rlm@158 63 fiber-integral))))
rlm@158 64 fiber-integral))
rlm@158 65 control (.getControl target RigidBodyControl)]
rlm@158 66 (fn [n]
rlm@158 67 (let [pool-index (min n (count fibers))]
rlm@158 68 (.applyTorque control (.mult axis (force-index n)))))))
rlm@158 69
rlm@158 70
rlm@158 71 (defn enable-muscles
rlm@158 72 "Must be called on a creature after RigidBodyControls have been
rlm@158 73 created."
rlm@158 74 [#^Node creature]
rlm@158 75 (let [muscles (creature-muscles creature)]
rlm@158 76 (for [muscle muscles]
rlm@158 77 (single-muscle creature muscle))))
rlm@158 78
rlm@158 79
rlm@158 80 #+end_src
rlm@158 81
rlm@158 82
rlm@158 83
rlm@158 84
rlm@158 85
rlm@158 86 * COMMENT code generation
rlm@158 87 #+begin_src clojure :tangle ../src/cortex/movement.clj
rlm@158 88 <<movement>>
rlm@158 89 #+end_src