view org/movement.org @ 213:319963720179

fleshing out vision
author Robert McIntyre <rlm@mit.edu>
date Thu, 09 Feb 2012 08:11:10 -0700
parents 66fbab414d45
children 959127e21f81
line wrap: on
line source
1 #+title: Movement!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: muscles for a simulated creature
5 #+keywords: simulation, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
10 Surprisingly enough, terristerial creatures only move by using torque
11 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
13 correspond to some sort of jet or rocket propulseion.)
16 Here's how motor-control/ proprioception will work: Each muscle is
17 defined by a 1-D array of numbers (the "motor pool") each of which
18 represent muscle fibers. A muscle also has a scalar :strength factor
19 which determines how strong the muscle as a whole is. The effector
20 function for a muscle takes a number < (count motor-pool) and that
21 number is said to "activate" all the muscle fibers whose index is
22 lower than the number. Each fiber will apply force in proportion to
23 its value in the array. Lower values cause less force. The lower
24 values can be put at the "beginning" of the 1-D array to simulate the
25 layout of actual human muscles, which are capable of more percise
26 movements when exerting less force.
28 #+name: movement
29 #+begin_src clojure
30 (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))
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)))))))
53 (defvar
54 ^{:arglists '([creature])}
55 muscles
56 (sense-nodes "muscles")
57 "Return the children of the creature's \"muscles\" node.")
59 (defn movement-fn
60 "Returns a function which when called with a integer value inside a
61 running simulation will cause movement in the creature according
62 to the muscle's position and strength profile. Each function
63 returns the amount of force applied / max force."
64 [#^Node parts #^Node muscle]
65 (let [target (closest-node parts muscle)
66 axis
67 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y)
68 strength (meta-data muscle "strength")
69 image-name (read-string (meta-data muscle "muscle"))
70 image (load-image image-name)
71 fibers (muscle-profile image)
72 fiber-integral (reductions + fibers)
73 force-index
74 (vec (map #(float (* strength (/ % (last fiber-integral))))
75 fiber-integral))
76 control (.getControl target RigidBodyControl)]
77 (fn [n]
78 (let [pool-index (max 0 (min n (dec (count fibers))))
79 force (force-index pool-index)]
80 (.applyTorque control (.mult axis force))
81 (float (/ force strength))))))
84 (defn movement!
85 "Endow the creature with the power of movement. Returns a sequence
86 of functions, each of which accept an integer value and will
87 activate their corresponding muscle."
88 [#^Node creature]
89 (for [muscle (muscles creature)]
90 (movement-fn creature muscle)))
92 (defn movement-display-kernel
93 "Display muscle exertion data as a bar filling up with red."
94 [exertion]
95 (let [height 20
96 width 300
97 image (BufferedImage. width height
98 BufferedImage/TYPE_INT_RGB)
99 fill (min (int (* width exertion)) width)]
100 (dorun
101 (for [x (range fill)
102 y (range height)]
103 (.setRGB image x y 0xFF0000)))
104 image))
106 (defn view-movement
107 "Creates a function which accepts a list of muscle-exertion data and
108 displays each element of the list to the screen."
109 []
110 (view-sense movement-display-kernel))
112 #+end_src
118 * COMMENT code generation
119 #+begin_src clojure :tangle ../src/cortex/movement.clj
120 <<movement>>
121 #+end_src