Mercurial > cortex
diff org/body.org @ 58:25142dad240a
created test suite
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 19 Nov 2011 23:42:21 -0700 |
parents | 37a3256e1ed3 |
children | e5e627f50a3a |
line wrap: on
line diff
1.1 --- a/org/body.org Wed Nov 16 02:42:50 2011 -0700 1.2 +++ b/org/body.org Sat Nov 19 23:42:21 2011 -0700 1.3 @@ -5,7 +5,7 @@ 1.4 #+SETUPFILE: ../../aurellem/org/setup.org 1.5 #+INCLUDE: ../../aurellem/org/level-0.org 1.6 1.7 -* COMMENT Body 1.8 +* Body 1.9 1.10 #+srcname: body-main 1.11 #+begin_src clojure 1.12 @@ -233,6 +233,91 @@ 1.13 1.14 ))) 1.15 1.16 + 1.17 + 1.18 + 1.19 + 1.20 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.21 +;;; here is the ragdoll stuff 1.22 + 1.23 +(def worm-mesh (.getMesh (.getChild (worm-blender) 0))) 1.24 +(def mesh worm-mesh) 1.25 + 1.26 +(.getFloatBuffer mesh VertexBuffer$Type/Position) 1.27 +(.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) 1.28 +(.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) 1.29 + 1.30 + 1.31 +(defn position [index] 1.32 + (.get 1.33 + (.getFloatBuffer worm-mesh VertexBuffer$Type/Position) 1.34 + index)) 1.35 + 1.36 +(defn bones [index] 1.37 + (.get 1.38 + (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) 1.39 + index)) 1.40 + 1.41 +(defn bone-weights [index] 1.42 + (.get 1.43 + (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) 1.44 + index)) 1.45 + 1.46 + 1.47 + 1.48 +(defn vertex-bones [vertex] 1.49 + (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4))))) 1.50 + 1.51 +(defn vertex-weights [vertex] 1.52 + (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4))))) 1.53 + 1.54 +(defn vertex-position [index] 1.55 + (let [offset (* index 3)] 1.56 + (Vector3f. (position offset) 1.57 + (position (inc offset)) 1.58 + (position (inc(inc offset)))))) 1.59 + 1.60 +(def vertex-info (juxt vertex-position vertex-bones vertex-weights)) 1.61 + 1.62 +(defn bone-control-color [index] 1.63 + (get {[1 0 0 0] ColorRGBA/Red 1.64 + [1 2 0 0] ColorRGBA/Magenta 1.65 + [2 0 0 0] ColorRGBA/Blue} 1.66 + (vertex-bones index) 1.67 + ColorRGBA/White)) 1.68 + 1.69 +(defn influence-color [index bone-num] 1.70 + (get 1.71 + {(float 0) ColorRGBA/Blue 1.72 + (float 0.5) ColorRGBA/Green 1.73 + (float 1) ColorRGBA/Red} 1.74 + ;; find the weight of the desired bone 1.75 + ((zipmap (vertex-bones index)(vertex-weights index)) 1.76 + bone-num) 1.77 + ColorRGBA/Blue)) 1.78 + 1.79 +(def worm-vertices (set (map vertex-info (range 60)))) 1.80 + 1.81 + 1.82 +(defn test-info [] 1.83 + (let [points (Node.)] 1.84 + (dorun 1.85 + (map #(.attachChild points %) 1.86 + (map #(sphere 0.01 1.87 + :position (vertex-position %) 1.88 + :color (influence-color % 1) 1.89 + :physical? false) 1.90 + (range 60)))) 1.91 + (view points))) 1.92 + 1.93 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.94 + 1.95 + 1.96 + 1.97 + 1.98 + 1.99 + 1.100 +;;;;;;;;;;;; eve-style bodies ;;;;;;;; 1.101 (defn joint-control 1.102 [joint] 1.103 (let [physics-space (ref nil)] 1.104 @@ -271,94 +356,80 @@ 1.105 1.106 (defn test-joint [] 1.107 (view (hinge-world))) 1.108 - 1.109 1.110 1.111 +(defn worm [segment-length num-segments interstitial-space radius] 1.112 + (letfn [(nth-segment 1.113 + [n] 1.114 + (box segment-length radius radius :mass 0.1 1.115 + :position 1.116 + (Vector3f. 1.117 + (* 2 n (+ interstitial-space segment-length)) 0 0) 1.118 + :name (str "worm-segment" n) 1.119 + :color (ColorRGBA/randomColor)))] 1.120 + (map nth-segment (range num-segments)))) 1.121 + 1.122 +(defn nodify 1.123 + "take a sequence of things that can be attached to a node and return 1.124 + a node with all of the attached" 1.125 + ([name children] 1.126 + (let [node (Node. name)] 1.127 + (dorun (map #(.attachChild node %) children)) 1.128 + node)) 1.129 + ([children] (nodify "" children))) 1.130 + 1.131 + 1.132 +(defn connect-at-midpoint 1.133 + [segmentA segmentB] 1.134 + (let [centerA (.getWorldTranslation segmentA) 1.135 + centerB (.getWorldTranslation segmentB) 1.136 + midpoint (.mult (.add centerA centerB) (float 0.5)) 1.137 + pivotA (.subtract midpoint centerA) 1.138 + pivotB (.subtract midpoint centerB) 1.139 + 1.140 + joint (Point2PointJoint. 1.141 + (.getControl segmentA RigidBodyControl) 1.142 + (.getControl segmentB RigidBodyControl) 1.143 + pivotA 1.144 + pivotB)] 1.145 + (add-joint segmentA joint) 1.146 + segmentB)) 1.147 + 1.148 + 1.149 +(defn point-worm [] 1.150 + (let [segments (worm 0.2 5 0.1 0.1)] 1.151 + (dorun (map (partial apply connect-at-midpoint) 1.152 + (partition 2 1 segments))) 1.153 + (nodify "worm" segments))) 1.154 + 1.155 + 1.156 +(defn test-worm [] 1.157 + (.start 1.158 + (world 1.159 + (doto (Node.) 1.160 + ;;(.attachChild (point-worm)) 1.161 + (.attachChild (load-blender-model 1.162 + "Models/anim2/joint-worm.blend")) 1.163 + 1.164 + (.attachChild (box 10 1 10 1.165 + :position (Vector3f. 0 -2 0) :mass 0 1.166 + :color (ColorRGBA/Gray)))) 1.167 + { 1.168 + "key-space" (fire-cannon-ball) 1.169 + } 1.170 + (fn [world] 1.171 + (enable-debug world) 1.172 + (light-up-everything world) 1.173 + ;;(.setTimer world (NanoTimer.)) 1.174 + ) 1.175 + no-op))) 1.176 1.177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.178 -;;; here is the ragdoll stuff 1.179 1.180 -(def worm-mesh (.getMesh (.getChild (worm-blender) 0))) 1.181 -(def mesh worm-mesh) 1.182 1.183 -(.getFloatBuffer mesh VertexBuffer$Type/Position) 1.184 -(.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) 1.185 -(.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) 1.186 1.187 1.188 -(defn position [index] 1.189 - (.get 1.190 - (.getFloatBuffer worm-mesh VertexBuffer$Type/Position) 1.191 - index)) 1.192 1.193 -(defn bones [index] 1.194 - (.get 1.195 - (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex)) 1.196 - index)) 1.197 - 1.198 -(defn bone-weights [index] 1.199 - (.get 1.200 - (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight) 1.201 - index)) 1.202 - 1.203 - 1.204 - 1.205 -(defn vertex-bones [vertex] 1.206 - (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4))))) 1.207 - 1.208 -(defn vertex-weights [vertex] 1.209 - (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4))))) 1.210 - 1.211 -(defn vertex-position [index] 1.212 - (let [offset (* index 3)] 1.213 - (Vector3f. (position offset) 1.214 - (position (inc offset)) 1.215 - (position (inc(inc offset)))))) 1.216 - 1.217 -(def vertex-info (juxt vertex-position vertex-bones vertex-weights)) 1.218 - 1.219 -(defn bone-control-color [index] 1.220 - (get {[1 0 0 0] ColorRGBA/Red 1.221 - [1 2 0 0] ColorRGBA/Magenta 1.222 - [2 0 0 0] ColorRGBA/Blue} 1.223 - (vertex-bones index) 1.224 - ColorRGBA/White)) 1.225 - 1.226 -(defn influence-color [index bone-num] 1.227 - (get 1.228 - {(float 0) ColorRGBA/Blue 1.229 - (float 0.5) ColorRGBA/Green 1.230 - (float 1) ColorRGBA/Red} 1.231 - ;; find the weight of the desired bone 1.232 - ((zipmap (vertex-bones index)(vertex-weights index)) 1.233 - bone-num) 1.234 - ColorRGBA/Blue)) 1.235 - 1.236 - 1.237 - 1.238 - 1.239 -(def worm-vertices (set (map vertex-info (range 60)))) 1.240 - 1.241 - 1.242 -(defn test-info [] 1.243 - (let [points (Node.)] 1.244 - (dorun 1.245 - (map #(.attachChild points %) 1.246 - (map #(sphere 0.01 1.247 - :position (vertex-position %) 1.248 - :color (influence-color % 1) 1.249 - :physical? false) 1.250 - (range 60)))) 1.251 - (view points))) 1.252 - 1.253 - 1.254 - 1.255 - 1.256 - 1.257 - 1.258 - 1.259 - 1.260 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.261 1.262 1.263