Mercurial > cortex
view org/movement.org @ 158:811127d79d24
refactored muscles
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 03 Feb 2012 06:33:37 -0700 |
parents | |
children | 2cbdd7034c6c |
line wrap: on
line source
1 #+title: Movement!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: muscles for a simulated creature5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 #+name: movement10 #+begin_src clojure11 (ns cortex.movement12 (:use (cortex world util sense body))13 (:import jme3tools.converters.ImageToAwt)14 (:import java.awt.image.BufferedImage))16 (cortex.import/mega-import-jme3)21 ;; here's how motor-control/ proprioception will work: Each muscle is22 ;; defined by a 1-D array of numbers (the "motor pool") each of which23 ;; represent muscle fibers. A muscle also has a scalar :strength24 ;; factor which determines how strong the muscle as a whole is.25 ;; The effector function for a muscle takes a number < (count26 ;; motor-pool) and that number is said to "activate" all the muscle27 ;; fibers whose index is lower than the number. Each fiber will apply28 ;; force in proportion to its value in the array. Lower values cause29 ;; less force. The lower values can be put at the "beginning" of the30 ;; 1-D array to simulate the layout of actual human muscles, which are31 ;; capable of more percise movements when exerting less force.33 ;; I don't know how to encode proprioception, so for now, just return34 ;; a function for each joint that returns a triplet of floats which35 ;; represent relative roll, pitch, and yaw. Write display code for36 ;; this though.38 (defn muscle-fiber-values39 "get motor pool strengths"40 [#^BufferedImage image]41 (vec42 (let [width (.getWidth image)]43 (for [x (range width)]44 (- 25545 (bit-and46 0x0000FF47 (.getRGB image x 0)))))))50 (defn creature-muscles51 "Return the children of the creature's \"muscles\" node."52 [#^Node creature]53 (if-let [muscle-node (.getChild creature "muscles")]54 (seq (.getChildren muscle-node))55 (do (println-repl "could not find muscles node") [])))57 (defn single-muscle [#^Node parts #^Node muscle]58 (let [target (closest-node parts muscle)59 axis60 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)61 strength (meta-data muscle "strength")62 image-name (read-string (meta-data muscle "muscle"))63 image64 (ImageToAwt/convert65 (.getImage (.loadTexture (asset-manager) image-name))66 false false 0)67 fibers (muscle-fiber-values image)68 fiber-integral (reductions + fibers)69 force-index (vec70 (map71 #(float (* strength (/ % (last72 fiber-integral))))73 fiber-integral))74 control (.getControl target RigidBodyControl)]75 (fn [n]76 (let [pool-index (min n (count fibers))]77 (.applyTorque control (.mult axis (force-index n)))))))80 (defn enable-muscles81 "Must be called on a creature after RigidBodyControls have been82 created."83 [#^Node creature]84 (let [muscles (creature-muscles creature)]85 (for [muscle muscles]86 (single-muscle creature muscle))))89 #+end_src95 * COMMENT code generation96 #+begin_src clojure :tangle ../src/cortex/movement.clj97 <<movement>>98 #+end_src