Mercurial > cortex
view org/movement.org @ 187:6142e85f5825
extracted common elements of display code
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 09:42:19 -0700 |
parents | 0f1c7921d967 |
children | 66fbab414d45 |
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.org10 Surprisingly enough, terristerial creatures only move by using torque11 applied about their joints. There's not a single straight line of12 force in the human body at all! (A straight line of force would13 correspond to some sort of jet or rocket propulseion.)16 Here's how motor-control/ proprioception will work: Each muscle is17 defined by a 1-D array of numbers (the "motor pool") each of which18 represent muscle fibers. A muscle also has a scalar :strength factor19 which determines how strong the muscle as a whole is. The effector20 function for a muscle takes a number < (count motor-pool) and that21 number is said to "activate" all the muscle fibers whose index is22 lower than the number. Each fiber will apply force in proportion to23 its value in the array. Lower values cause less force. The lower24 values can be put at the "beginning" of the 1-D array to simulate the25 layout of actual human muscles, which are capable of more percise26 movements when exerting less force.28 #+name: movement29 #+begin_src clojure30 (ns cortex.movement31 "Give simulated creatures defined in special blender files the power32 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))41 (defn muscle-profile42 "Return a vector where each entry is the strength of the \"motor43 pool\" at that part in the muscle."44 [#^BufferedImage profile]45 (vec46 (let [width (.getWidth profile)]47 (for [x (range width)]48 (- 25549 (bit-and50 0x0000FF51 (.getRGB profile x 0)))))))53 (defvar54 ^{:arglists '([creature])}55 muscles56 (sense-nodes "muscles")57 "Return the children of the creature's \"muscles\" node.")59 (defn movement-fn60 "Returns a function which when called with a integer value inside a61 running simulation, will cause movement in the creature according62 to the muscle's position and strength profile"63 [#^Node parts #^Node muscle]64 (let [target (closest-node parts muscle)65 axis66 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)67 strength (meta-data muscle "strength")68 image-name (read-string (meta-data muscle "muscle"))69 image (load-image image-name)70 fibers (muscle-profile image)71 fiber-integral (reductions + fibers)72 force-index (vec73 (map74 #(float (* strength (/ % (last75 fiber-integral))))76 fiber-integral))77 control (.getControl target RigidBodyControl)]78 (fn [n]79 (let [pool-index (min n (count fibers))]80 (.applyTorque control (.mult axis (force-index n)))))))82 (defn movement!83 "Endow the creature with the power of movement. Returns a sequence84 of functions, each of which accept an integer value and will85 activate their corresponding muscle."86 [#^Node creature]87 (for [muscle (muscles creature)]88 (movement-fn creature muscle)))90 #+end_src96 * COMMENT code generation97 #+begin_src clojure :tangle ../src/cortex/movement.clj98 <<movement>>99 #+end_src