Mercurial > cortex
comparison org/movement.org @ 191:66fbab414d45
added muscle exertion debug view.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 11:02:19 -0700 |
parents | 0f1c7921d967 |
children | 959127e21f81 |
comparison
equal
deleted
inserted
replaced
190:2902aca33c6e | 191:66fbab414d45 |
---|---|
56 (sense-nodes "muscles") | 56 (sense-nodes "muscles") |
57 "Return the children of the creature's \"muscles\" node.") | 57 "Return the children of the creature's \"muscles\" node.") |
58 | 58 |
59 (defn movement-fn | 59 (defn movement-fn |
60 "Returns a function which when called with a integer value inside a | 60 "Returns a function which when called with a integer value inside a |
61 running simulation, will cause movement in the creature according | 61 running simulation will cause movement in the creature according |
62 to the muscle's position and strength profile" | 62 to the muscle's position and strength profile. Each function |
63 returns the amount of force applied / max force." | |
63 [#^Node parts #^Node muscle] | 64 [#^Node parts #^Node muscle] |
64 (let [target (closest-node parts muscle) | 65 (let [target (closest-node parts muscle) |
65 axis | 66 axis |
66 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y) | 67 (.mult (.getWorldRotation muscle) Vector3f/UNIT_Y) |
67 strength (meta-data muscle "strength") | 68 strength (meta-data muscle "strength") |
68 image-name (read-string (meta-data muscle "muscle")) | 69 image-name (read-string (meta-data muscle "muscle")) |
69 image (load-image image-name) | 70 image (load-image image-name) |
70 fibers (muscle-profile image) | 71 fibers (muscle-profile image) |
71 fiber-integral (reductions + fibers) | 72 fiber-integral (reductions + fibers) |
72 force-index (vec | 73 force-index |
73 (map | 74 (vec (map #(float (* strength (/ % (last fiber-integral)))) |
74 #(float (* strength (/ % (last | 75 fiber-integral)) |
75 fiber-integral)))) | |
76 fiber-integral)) | |
77 control (.getControl target RigidBodyControl)] | 76 control (.getControl target RigidBodyControl)] |
78 (fn [n] | 77 (fn [n] |
79 (let [pool-index (min n (count fibers))] | 78 (let [pool-index (max 0 (min n (dec (count fibers)))) |
80 (.applyTorque control (.mult axis (force-index n))))))) | 79 force (force-index pool-index)] |
80 (.applyTorque control (.mult axis force)) | |
81 (float (/ force strength)))))) | |
82 | |
81 | 83 |
82 (defn movement! | 84 (defn movement! |
83 "Endow the creature with the power of movement. Returns a sequence | 85 "Endow the creature with the power of movement. Returns a sequence |
84 of functions, each of which accept an integer value and will | 86 of functions, each of which accept an integer value and will |
85 activate their corresponding muscle." | 87 activate their corresponding muscle." |
86 [#^Node creature] | 88 [#^Node creature] |
87 (for [muscle (muscles creature)] | 89 (for [muscle (muscles creature)] |
88 (movement-fn creature muscle))) | 90 (movement-fn creature muscle))) |
91 | |
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)) | |
105 | |
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)) | |
89 | 111 |
90 #+end_src | 112 #+end_src |
91 | 113 |
92 | 114 |
93 | 115 |