Mercurial > cortex
comparison org/movement.org @ 260:959127e21f81
fleshing out text in muscle.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 14 Feb 2012 03:16:50 -0700 |
parents | 66fbab414d45 |
children | 2fdcbe8185b1 |
comparison
equal
deleted
inserted
replaced
259:7cac5ef852e3 | 260:959127e21f81 |
---|---|
1 #+title: Movement! | 1 #+title: Simulated Muscles |
2 #+author: Robert McIntyre | 2 #+author: Robert McIntyre |
3 #+email: rlm@mit.edu | 3 #+email: rlm@mit.edu |
4 #+description: muscles for a simulated creature | 4 #+description: muscles for a simulated creature |
5 #+keywords: simulation, jMonkeyEngine3, clojure | 5 #+keywords: simulation, jMonkeyEngine3, clojure |
6 #+SETUPFILE: ../../aurellem/org/setup.org | 6 #+SETUPFILE: ../../aurellem/org/setup.org |
7 #+INCLUDE: ../../aurellem/org/level-0.org | 7 #+INCLUDE: ../../aurellem/org/level-0.org |
8 | 8 |
9 | 9 |
10 * Muscles | |
11 | |
10 Surprisingly enough, terristerial creatures only move by using torque | 12 Surprisingly enough, terristerial creatures only move by using torque |
11 applied about their joints. There's not a single straight line of | 13 applied about their joints. There's not a single straight line of |
12 force in the human body at all! (A straight line of force would | 14 force in the human body at all! (A straight line of force would |
13 correspond to some sort of jet or rocket propulseion.) | 15 correspond to some sort of jet or rocket propulsion.) |
14 | 16 |
17 *(next paragraph is from memory and needs to be checked!)* | |
15 | 18 |
16 Here's how motor-control/ proprioception will work: Each muscle is | 19 In humans, muscles are composed of millions of sarcomeres, which can |
17 defined by a 1-D array of numbers (the "motor pool") each of which | 20 contract to exert force. A single motor neuron might control 100-1,000 |
18 represent muscle fibers. A muscle also has a scalar :strength factor | 21 sarcomeres. When the motor neuron is engaged by the brain, it |
19 which determines how strong the muscle as a whole is. The effector | 22 activates all of the sarcomeres to which it is attached. Some motor |
20 function for a muscle takes a number < (count motor-pool) and that | 23 neurons command many sarcomeres, and some command only a few. The |
21 number is said to "activate" all the muscle fibers whose index is | 24 spinal cord generally engages the motor neurons which control few |
22 lower than the number. Each fiber will apply force in proportion to | 25 sarcomeres before the motor neurons which control many sarcomeres. |
23 its value in the array. Lower values cause less force. The lower | 26 This recruitment stragety allows for percise movements at low |
24 values can be put at the "beginning" of the 1-D array to simulate the | 27 strength. The collection of all motor neurons that control a muscle is |
25 layout of actual human muscles, which are capable of more percise | 28 called the motor pool. The brain essentially says "activate 30% of the |
26 movements when exerting less force. | 29 motor pool" and the spinal cord recruits motor neurons untill 30% are |
30 activated. Since the distribution of power among motor neurons is | |
31 unequal and recruitment goes from weakest to strongest, 30% of the | |
32 motor pool might be 5% of the strength of the muscle. | |
33 | |
34 My simulated muscles follow a similiar design: Each muscle is defined | |
35 by a 1-D array of numbers (the "motor pool"). Each number represents a | |
36 motor neuron which controlls a number of sarcomeres equal to the | |
37 number. A muscle also has a scalar :strength factor which determines | |
38 the total force the muscle can exert when all motor neurons are | |
39 activated. The effector function for a muscle takes a number to index | |
40 into the motor pool, and that number "activates" all the motor neurons | |
41 whose index is lower or equal to the number. Each motor-neuron will | |
42 apply force in proportion to its value in the array. Lower values | |
43 cause less force. The lower values can be put at the "beginning" of | |
44 the 1-D array to simulate the layout of actual human muscles, which | |
45 are capable of more percise movements when exerting less force. Or, | |
46 the motor pool can simulate more exoitic recruitment strageties which | |
47 do not correspond to human muscles. | |
48 | |
49 This 1D array is defined in an image file for ease of | |
50 creation/visualization. Here is an example muscle profile image. | |
51 | |
52 #+caption: A muscle profile image that describes the strengths of each motor neuron in a muscle. White is weakest and dark red is strongest. This particular pattern has weaker motor neurons at the beginning, just like human muscle. | |
53 [[../images/basic-muscle.png]] | |
54 | |
55 * Blender Meta-data | |
56 | |
57 In blender, each muscle is an empty node whose top level parent is | |
58 named "muscles", just like eyes, ears, and joints. | |
59 | |
60 These functions define the expected meta-data for a muscle node. | |
27 | 61 |
28 #+name: movement | 62 #+name: movement |
29 #+begin_src clojure | 63 #+begin_src clojure |
30 (ns cortex.movement | 64 (in-ns 'cortex.movement) |
31 "Give simulated creatures defined in special blender files the power | |
32 to move around in a simulated environment." | |
33 {:author "Robert McIntyre"} | |
34 (:use (cortex world util sense body)) | |
35 (:use clojure.contrib.def) | |
36 (:import java.awt.image.BufferedImage) | |
37 (:import com.jme3.scene.Node) | |
38 (:import com.jme3.math.Vector3f) | |
39 (:import com.jme3.bullet.control.RigidBodyControl)) | |
40 | |
41 (defn muscle-profile | |
42 "Return a vector where each entry is the strength of the \"motor | |
43 pool\" at that part in the muscle." | |
44 [#^BufferedImage profile] | |
45 (vec | |
46 (let [width (.getWidth profile)] | |
47 (for [x (range width)] | |
48 (- 255 | |
49 (bit-and | |
50 0x0000FF | |
51 (.getRGB profile x 0))))))) | |
52 | 65 |
53 (defvar | 66 (defvar |
54 ^{:arglists '([creature])} | 67 ^{:arglists '([creature])} |
55 muscles | 68 muscles |
56 (sense-nodes "muscles") | 69 (sense-nodes "muscles") |
57 "Return the children of the creature's \"muscles\" node.") | 70 "Return the children of the creature's \"muscles\" node.") |
58 | 71 |
59 (defn movement-fn | 72 (defn muscle-profile-image |
73 "Get the muscle-profile image from the node's blender meta-data." | |
74 [#^Node muscle] | |
75 (if-let [image (meta-data muscle "muscle")] | |
76 (load-image image))) | |
77 | |
78 (defn muscle-strength | |
79 "Return the strength of this muscle, or 1 if it is not defined." | |
80 [#^Node muscle] | |
81 (if-let [strength (meta-data muscle "strength")] | |
82 strength 1)) | |
83 | |
84 (defn motor-pool | |
85 "Return a vector where each entry is the strength of the \"motor | |
86 neuron\" at that part in the muscle." | |
87 [#^Node muscle] | |
88 (let [profile (muscle-profile-image muscle)] | |
89 (vec | |
90 (let [width (.getWidth profile)] | |
91 (for [x (range width)] | |
92 (- 255 | |
93 (bit-and | |
94 0x0000FF | |
95 (.getRGB profile x 0)))))))) | |
96 #+end_src | |
97 | |
98 Of note here is =(motor-pool)= which interprets the muscle-profile | |
99 image in a way that allows me to use gradients between white and red, | |
100 instead of shades of gray as I've been using for all the other | |
101 senses. This is purely an aesthetic touch. | |
102 | |
103 * Creating Muscles | |
104 #+begin_src clojure | |
105 (defn movement-kernel | |
60 "Returns a function which when called with a integer value inside a | 106 "Returns a function which when called with a integer value inside a |
61 running simulation will cause movement in the creature according | 107 running simulation will cause movement in the creature according |
62 to the muscle's position and strength profile. Each function | 108 to the muscle's position and strength profile. Each function |
63 returns the amount of force applied / max force." | 109 returns the amount of force applied / max force." |
64 [#^Node parts #^Node muscle] | 110 [#^Node creature #^Node muscle] |
65 (let [target (closest-node parts muscle) | 111 (let [target (closest-node creature muscle) |
66 axis | 112 axis |
67 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y) | 113 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y) |
68 strength (meta-data muscle "strength") | 114 strength (muscle-strength muscle) |
69 image-name (read-string (meta-data muscle "muscle")) | 115 |
70 image (load-image image-name) | 116 pool (motor-pool muscle) |
71 fibers (muscle-profile image) | 117 pool-integral (reductions + pool) |
72 fiber-integral (reductions + fibers) | |
73 force-index | 118 force-index |
74 (vec (map #(float (* strength (/ % (last fiber-integral)))) | 119 (vec (map #(float (* strength (/ % (last pool-integral)))) |
75 fiber-integral)) | 120 pool-integral)) |
76 control (.getControl target RigidBodyControl)] | 121 control (.getControl target RigidBodyControl)] |
77 (fn [n] | 122 (fn [n] |
78 (let [pool-index (max 0 (min n (dec (count fibers)))) | 123 (let [pool-index (max 0 (min n (dec (count pool)))) |
79 force (force-index pool-index)] | 124 force (force-index pool-index)] |
80 (.applyTorque control (.mult axis force)) | 125 (.applyTorque control (.mult axis force)) |
81 (float (/ force strength)))))) | 126 (float (/ force strength)))))) |
82 | |
83 | 127 |
84 (defn movement! | 128 (defn movement! |
85 "Endow the creature with the power of movement. Returns a sequence | 129 "Endow the creature with the power of movement. Returns a sequence |
86 of functions, each of which accept an integer value and will | 130 of functions, each of which accept an integer value and will |
87 activate their corresponding muscle." | 131 activate their corresponding muscle." |
88 [#^Node creature] | 132 [#^Node creature] |
89 (for [muscle (muscles creature)] | 133 (for [muscle (muscles creature)] |
90 (movement-fn creature muscle))) | 134 (movement-kernel creature muscle))) |
135 #+end_src | |
91 | 136 |
137 =(movement-kernel)= creates a function that will move the nearest | |
138 physical object to the muscle node. The muscle exerts a rotational | |
139 force dependant on it's orientation to the object in the blender | |
140 file. The function returned by =(movement-kernel)= is also a sense | |
141 function: it returns the percent of the total muscle strength that is | |
142 currently being employed. This is analogous to muscle tension in | |
143 humans and completes the sense of proprioception begun in the last | |
144 post. | |
145 | |
146 * Visualizing Muscle Tension | |
147 Muscle exertion is a percent of a total, so the visulazation is just a | |
148 simple percent bar. | |
149 | |
150 #+begin_src clojure | |
92 (defn movement-display-kernel | 151 (defn movement-display-kernel |
93 "Display muscle exertion data as a bar filling up with red." | 152 "Display muscle exertion data as a bar filling up with red." |
94 [exertion] | 153 [exertion] |
95 (let [height 20 | 154 (let [height 20 |
96 width 300 | 155 width 300 |
106 (defn view-movement | 165 (defn view-movement |
107 "Creates a function which accepts a list of muscle-exertion data and | 166 "Creates a function which accepts a list of muscle-exertion data and |
108 displays each element of the list to the screen." | 167 displays each element of the list to the screen." |
109 [] | 168 [] |
110 (view-sense movement-display-kernel)) | 169 (view-sense movement-display-kernel)) |
111 | |
112 #+end_src | 170 #+end_src |
113 | 171 |
172 * Adding Touch to the Worm | |
114 | 173 |
174 | |
175 * Headers | |
176 #+name: muscle-header | |
177 #+begin_src clojure | |
178 (ns cortex.movement | |
179 "Give simulated creatures defined in special blender files the power | |
180 to move around in a simulated environment." | |
181 {:author "Robert McIntyre"} | |
182 (:use (cortex world util sense body)) | |
183 (:use clojure.contrib.def) | |
184 (:import java.awt.image.BufferedImage) | |
185 (:import com.jme3.scene.Node) | |
186 (:import com.jme3.math.Vector3f) | |
187 (:import com.jme3.bullet.control.RigidBodyControl)) | |
188 #+end_src | |
115 | 189 |
116 | 190 |
117 | 191 |
118 * COMMENT code generation | 192 * COMMENT code generation |
119 #+begin_src clojure :tangle ../src/cortex/movement.clj | 193 #+begin_src clojure :tangle ../src/cortex/movement.clj |