Mercurial > cortex
view org/movement.org @ 291:8bdfd7c2ff76
apply rotation and scale to hand, add ear
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 16 Feb 2012 07:09:19 -0700 |
parents | 23aadf376e9d |
children | e27a91b1f997 |
line wrap: on
line source
1 #+title: Simulated Muscles2 #+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.org10 * Muscles12 Surprisingly enough, terristerial creatures only move by using torque13 applied about their joints. There's not a single straight line of14 force in the human body at all! (A straight line of force would15 correspond to some sort of jet or rocket propulsion.)17 *(next paragraph is from memory and needs to be checked!)*19 In humans, muscles are composed of muscle fibers which can contract to20 exert force. The muscle fibers which compose a muscle are partitioned21 into discrete groups which are each controlled by a single alpha motor22 neuton. A single alpha motor neuron might control as little as three23 or as many as one thousand muscle fibers. When the alpha motor neuron24 is engaged by the spinal cord, it activates all of the muscle fibers25 to which it is attached. The spinal cord generally engages the alpha26 motor neurons which control few muscle fibers before the motor neurons27 which control many muscle fibers. This recruitment stragety allows28 for percise movements at low strength. The collection of all motor29 neurons that control a muscle is called the motor pool. The brain30 essentially says "activate 30% of the motor pool" and the spinal cord31 recruits motor neurons untill 30% are activated. Since the32 distribution of power among motor neurons is unequal and recruitment33 goes from weakest to strongest, the first 30% of the motor pool might34 be 5% of the strength of the muscle.36 My simulated muscles follow a similiar design: Each muscle is defined37 by a 1-D array of numbers (the "motor pool"). Each entry in the array38 represents a motor neuron which controlls a number of muscle fibers39 equal to the value of the entry. Each muscle has a scalar strength40 factor which determines the total force the muscle can exert when all41 motor neurons are activated. The effector function for a muscle takes42 a number to index into the motor pool, and then "activates" all the43 motor neurons whose index is lower or equal to the number. Each44 motor-neuron will apply force in proportion to its value in the array.45 Lower values cause less force. The lower values can be put at the46 "beginning" of the 1-D array to simulate the layout of actual human47 muscles, which are capable of more percise movements when exerting48 less force. Or, the motor pool can simulate more exoitic recruitment49 strageties which do not correspond to human muscles.51 This 1D array is defined in an image file for ease of52 creation/visualization. Here is an example muscle profile image.54 #+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.55 [[../images/basic-muscle.png]]57 * Blender Meta-data59 In blender, each muscle is an empty node whose top level parent is60 named "muscles", just like eyes, ears, and joints.62 These functions define the expected meta-data for a muscle node.64 #+name: muscle-meta-data65 #+begin_src clojure66 (in-ns 'cortex.movement)68 (defvar69 ^{:arglists '([creature])}70 muscles71 (sense-nodes "muscles")72 "Return the children of the creature's \"muscles\" node.")74 (defn muscle-profile-image75 "Get the muscle-profile image from the node's blender meta-data."76 [#^Node muscle]77 (if-let [image (meta-data muscle "muscle")]78 (load-image image)))80 (defn muscle-strength81 "Return the strength of this muscle, or 1 if it is not defined."82 [#^Node muscle]83 (if-let [strength (meta-data muscle "strength")]84 strength 1))86 (defn motor-pool87 "Return a vector where each entry is the strength of the \"motor88 neuron\" at that part in the muscle."89 [#^Node muscle]90 (let [profile (muscle-profile-image muscle)]91 (vec92 (let [width (.getWidth profile)]93 (for [x (range width)]94 (- 25595 (bit-and96 0x0000FF97 (.getRGB profile x 0))))))))98 #+end_src100 Of note here is =motor-pool= which interprets the muscle-profile101 image in a way that allows me to use gradients between white and red,102 instead of shades of gray as I've been using for all the other103 senses. This is purely an aesthetic touch.105 * Creating Muscles106 #+name: muscle-kernel107 #+begin_src clojure108 (in-ns 'cortex.movement)110 (defn movement-kernel111 "Returns a function which when called with a integer value inside a112 running simulation will cause movement in the creature according113 to the muscle's position and strength profile. Each function114 returns the amount of force applied / max force."115 [#^Node creature #^Node muscle]116 (let [target (closest-node creature muscle)117 axis118 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)119 strength (muscle-strength muscle)121 pool (motor-pool muscle)122 pool-integral (reductions + pool)123 force-index124 (vec (map #(float (* strength (/ % (last pool-integral))))125 pool-integral))126 control (.getControl target RigidBodyControl)]127 (fn [n]128 (let [pool-index (max 0 (min n (dec (count pool))))129 force (force-index pool-index)]130 (.applyTorque control (.mult axis force))131 (float (/ force strength))))))133 (defn movement!134 "Endow the creature with the power of movement. Returns a sequence135 of functions, each of which accept an integer value and will136 activate their corresponding muscle."137 [#^Node creature]138 (for [muscle (muscles creature)]139 (movement-kernel creature muscle)))140 #+end_src142 =movement-kernel= creates a function that will move the nearest143 physical object to the muscle node. The muscle exerts a rotational144 force dependant on it's orientation to the object in the blender145 file. The function returned by =movement-kernel= is also a sense146 function: it returns the percent of the total muscle strength that is147 currently being employed. This is analogous to muscle tension in148 humans and completes the sense of proprioception begun in the last149 post.151 * Visualizing Muscle Tension152 Muscle exertion is a percent of a total, so the visulazation is just a153 simple percent bar.155 #+name: visualization156 #+begin_src clojure157 (defn movement-display-kernel158 "Display muscle exertion data as a bar filling up with red."159 [exertion]160 (let [height 20161 width 300162 image (BufferedImage. width height163 BufferedImage/TYPE_INT_RGB)164 fill (min (int (* width exertion)) width)]165 (dorun166 (for [x (range fill)167 y (range height)]168 (.setRGB image x y 0xFF0000)))169 image))171 (defn view-movement172 "Creates a function which accepts a list of muscle-exertion data and173 displays each element of the list to the screen."174 []175 (view-sense movement-display-kernel))176 #+end_src178 * Adding Touch to the Worm180 To the worm, I add two new nodes which describe a single muscle.182 #+attr_html: width=755183 #+caption: The node highlighted in orange is the parent node of all muscles in the worm. The arrow highlighted in yellow represents the creature's single muscle, which moves the top segment. The other nodes which are not highlighted are joints, eyes, and ears.184 [[../images/worm-with-muscle.png]]186 #+name: test-movement187 #+begin_src clojure188 (defn test-worm-movement189 ([] (test-worm-movement false))190 ([record?]191 (let [creature (doto (worm) (body!))193 muscle-exertion (atom 0)194 muscles (movement! creature)195 muscle-display (view-movement)]196 (.setMass197 (.getControl (.getChild creature "worm-11") RigidBodyControl)198 (float 0))199 (world200 (nodify [creature (floor)])201 (merge standard-debug-controls202 {"key-h"203 (fn [_ value]204 (if value205 (swap! muscle-exertion (partial + 20))))206 "key-n"207 (fn [_ value]208 (if value209 (swap! muscle-exertion (fn [v] (- v 20)))))})210 (fn [world]211 (if record?212 (Capture/captureVideo213 world214 (File. "/home/r/proj/cortex/render/worm-muscles/main-view")))215 (light-up-everything world)216 (enable-debug world)217 (.setTimer world (RatchetTimer. 60))218 (set-gravity world (Vector3f. 0 0 0))219 (.setLocation (.getCamera world)220 (Vector3f. -4.912815, 2.004171, 0.15710819))221 (.setRotation (.getCamera world)222 (Quaternion. 0.13828252, 0.65516764,223 -0.12370994, 0.7323449)))224 (fn [world tpf]225 (muscle-display226 (map #(% @muscle-exertion) muscles)227 (if record?228 (File. "/home/r/proj/cortex/render/worm-muscles/muscles"))))))))229 #+end_src231 * Video Demonstration233 #+begin_html234 <div class="figure">235 <center>236 <video controls="controls" width="550">237 <source src="../video/worm-muscles.ogg" type="video/ogg"238 preload="none" poster="../images/aurellem-1280x480.png" />239 </video>240 </center>241 <p>The worm is now able to move. The bar in the lower right displays242 the power output of the muscle . Each jump causes 20 more motor neurons to243 be recruited. Notice that the power output increases non-linearly244 with motror neuron recruitement, similiar to a human muscle.</p>245 </div>246 #+end_html248 ** Making the Worm Muscles Video249 #+name: magick7250 #+begin_src clojure251 (ns cortex.video.magick7252 (:import java.io.File)253 (:use clojure.contrib.shell-out))255 (defn images [path]256 (sort (rest (file-seq (File. path)))))258 (def base "/home/r/proj/cortex/render/worm-muscles/")260 (defn pics [file]261 (images (str base file)))263 (defn combine-images []264 (let [main-view (pics "main-view")265 muscles (pics "muscles/0")266 targets (map267 #(File. (str base "out/" (format "%07d.png" %)))268 (range 0 (count main-view)))]269 (dorun270 (pmap271 (comp272 (fn [[ main-view muscles target]]273 (println target)274 (sh "convert"275 main-view276 muscles "-geometry" "+320+440" "-composite"277 target))278 (fn [& args] (map #(.getCanonicalPath %) args)))279 main-view muscles targets))))280 #+end_src282 #+begin_src sh :results silent283 cd ~/proj/cortex/render/worm-muscles284 ffmpeg -r 60 -i out/%07d.png -b:v 9000k -c:v libtheora worm-muscles.ogg285 #+end_src287 * Headers288 #+name: muscle-header289 #+begin_src clojure290 (ns cortex.movement291 "Give simulated creatures defined in special blender files the power292 to move around in a simulated environment."293 {:author "Robert McIntyre"}294 (:use (cortex world util sense body))295 (:use clojure.contrib.def)296 (:import java.awt.image.BufferedImage)297 (:import com.jme3.scene.Node)298 (:import com.jme3.math.Vector3f)299 (:import com.jme3.bullet.control.RigidBodyControl))300 #+end_src302 #+name: test-header303 #+begin_src clojure304 (ns cortex.test.movement305 (:use (cortex world util sense body movement))306 (:use cortex.test.body)307 (:use clojure.contrib.def)308 (:import java.io.File)309 (:import java.awt.image.BufferedImage)310 (:import com.jme3.scene.Node)311 (:import (com.jme3.math Quaternion Vector3f))312 (:import (com.aurellem.capture Capture RatchetTimer))313 (:import com.jme3.bullet.control.RigidBodyControl))314 #+end_src316 * Source Listing317 - [[../src/cortex/movement.clj][cortex.movement]]318 - [[../src/cortex/test/movement.clj][cortex.test.movement]]319 - [[../src/cortex/video/magick7.clj][cortex.video.magick7]]320 #+html: <ul> <li> <a href="../org/movement.org">This org file</a> </li> </ul>321 - [[http://hg.bortreb.com ][source-repository]]323 * COMMENT code generation324 #+begin_src clojure :tangle ../src/cortex/movement.clj325 <<muscle-header>>326 <<muscle-meta-data>>327 <<muscle-kernel>>328 <<visualization>>329 #+end_src331 #+begin_src clojure :tangle ../src/cortex/test/movement.clj332 <<test-header>>333 <<test-movement>>334 #+end_src336 #+begin_src clojure :tangle ../src/cortex/video/magick7.clj337 <<magick7>>338 #+end_src