Mercurial > cortex
comparison org/test-creature.org @ 160:33278bf028e7
refactored joints
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 03 Feb 2012 06:47:05 -0700 |
parents | 75b6c2ebbf8e |
children | e401dafa5966 |
comparison
equal
deleted
inserted
replaced
159:75b6c2ebbf8e | 160:33278bf028e7 |
---|---|
69 (.loadModel | 69 (.loadModel |
70 (doto (asset-manager) | 70 (doto (asset-manager) |
71 (.registerLoader BlenderModelLoader (into-array String ["blend"]))) | 71 (.registerLoader BlenderModelLoader (into-array String ["blend"]))) |
72 model)) | 72 model)) |
73 | 73 |
74 (defn blender-to-jme | |
75 "Convert from Blender coordinates to JME coordinates" | |
76 [#^Vector3f in] | |
77 (Vector3f. (.getX in) | |
78 (.getZ in) | |
79 (- (.getY in)))) | |
80 | |
81 | |
82 (defmulti joint-dispatch | |
83 "Translate blender pseudo-joints into real JME joints." | |
84 (fn [constraints & _] | |
85 (:type constraints))) | |
86 | |
87 (defmethod joint-dispatch :point | |
88 [constraints control-a control-b pivot-a pivot-b rotation] | |
89 (println-repl "creating POINT2POINT joint") | |
90 ;; bullet's point2point joints are BROKEN, so we must use the | |
91 ;; generic 6DOF joint instead of an actual Point2Point joint! | |
92 | |
93 ;; should be able to do this: | |
94 (comment | |
95 (Point2PointJoint. | |
96 control-a | |
97 control-b | |
98 pivot-a | |
99 pivot-b)) | |
100 | |
101 ;; but instead we must do this: | |
102 (println-repl "substuting 6DOF joint for POINT2POINT joint!") | |
103 (doto | |
104 (SixDofJoint. | |
105 control-a | |
106 control-b | |
107 pivot-a | |
108 pivot-b | |
109 false) | |
110 (.setLinearLowerLimit Vector3f/ZERO) | |
111 (.setLinearUpperLimit Vector3f/ZERO) | |
112 ;;(.setAngularLowerLimit (Vector3f. 1 1 1)) | |
113 ;;(.setAngularUpperLimit (Vector3f. 0 0 0)) | |
114 | |
115 )) | |
116 | |
117 | |
118 (defmethod joint-dispatch :hinge | |
119 [constraints control-a control-b pivot-a pivot-b rotation] | |
120 (println-repl "creating HINGE joint") | |
121 (let [axis | |
122 (if-let | |
123 [axis (:axis constraints)] | |
124 axis | |
125 Vector3f/UNIT_X) | |
126 [limit-1 limit-2] (:limit constraints) | |
127 hinge-axis | |
128 (.mult | |
129 rotation | |
130 (blender-to-jme axis))] | |
131 (doto | |
132 (HingeJoint. | |
133 control-a | |
134 control-b | |
135 pivot-a | |
136 pivot-b | |
137 hinge-axis | |
138 hinge-axis) | |
139 (.setLimit limit-1 limit-2)))) | |
140 | |
141 (defmethod joint-dispatch :cone | |
142 [constraints control-a control-b pivot-a pivot-b rotation] | |
143 (let [limit-xz (:limit-xz constraints) | |
144 limit-xy (:limit-xy constraints) | |
145 twist (:twist constraints)] | |
146 | |
147 (println-repl "creating CONE joint") | |
148 (println-repl rotation) | |
149 (println-repl | |
150 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0))) | |
151 (println-repl | |
152 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0))) | |
153 (println-repl | |
154 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1))) | |
155 (doto | |
156 (ConeJoint. | |
157 control-a | |
158 control-b | |
159 pivot-a | |
160 pivot-b | |
161 rotation | |
162 rotation) | |
163 (.setLimit (float limit-xz) | |
164 (float limit-xy) | |
165 (float twist))))) | |
166 | |
167 (defn connect | |
168 "here are some examples: | |
169 {:type :point} | |
170 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)} | |
171 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) | |
172 | |
173 {:type :cone :limit-xz 0] | |
174 :limit-xy 0] | |
175 :twist 0]} (use XZY rotation mode in blender!)" | |
176 [#^Node obj-a #^Node obj-b #^Node joint] | |
177 (let [control-a (.getControl obj-a RigidBodyControl) | |
178 control-b (.getControl obj-b RigidBodyControl) | |
179 joint-center (.getWorldTranslation joint) | |
180 joint-rotation (.toRotationMatrix (.getWorldRotation joint)) | |
181 pivot-a (world-to-local obj-a joint-center) | |
182 pivot-b (world-to-local obj-b joint-center)] | |
183 | |
184 (if-let [constraints | |
185 (map-vals | |
186 eval | |
187 (read-string | |
188 (meta-data joint "joint")))] | |
189 ;; A side-effect of creating a joint registers | |
190 ;; it with both physics objects which in turn | |
191 ;; will register the joint with the physics system | |
192 ;; when the simulation is started. | |
193 (do | |
194 (println-repl "creating joint between" | |
195 (.getName obj-a) "and" (.getName obj-b)) | |
196 (joint-dispatch constraints | |
197 control-a control-b | |
198 pivot-a pivot-b | |
199 joint-rotation)) | |
200 (println-repl "could not find joint meta-data!")))) | |
201 | |
202 | |
203 | |
204 | |
205 (defn assemble-creature [#^Node pieces joints] | |
206 (dorun | |
207 (map | |
208 (fn [geom] | |
209 (let [physics-control | |
210 (RigidBodyControl. | |
211 (HullCollisionShape. | |
212 (.getMesh geom)) | |
213 (if-let [mass (meta-data geom "mass")] | |
214 (do | |
215 (println-repl | |
216 "setting" (.getName geom) "mass to" (float mass)) | |
217 (float mass)) | |
218 (float 1)))] | |
219 | |
220 (.addControl geom physics-control))) | |
221 (filter #(isa? (class %) Geometry ) | |
222 (node-seq pieces)))) | |
223 (dorun | |
224 (map | |
225 (fn [joint] | |
226 (let [[obj-a obj-b] (joint-targets pieces joint)] | |
227 (connect obj-a obj-b joint))) | |
228 joints)) | |
229 pieces) | |
230 | |
231 (declare blender-creature) | 74 (declare blender-creature) |
75 | |
76 (defn blender-creature | |
77 "Return a creature with all joints in place." | |
78 [blender-path] | |
79 (let [model (load-blender-model blender-path) | |
80 joints (creature-joints model)] | |
81 (assemble-creature model joints))) | |
232 | 82 |
233 (def hand "Models/creature1/one.blend") | 83 (def hand "Models/creature1/one.blend") |
234 | 84 |
235 (def worm "Models/creature1/try-again.blend") | 85 (def worm "Models/creature1/try-again.blend") |
236 | 86 |
269 ;; these are the functions that provide world i/o, chinese-room style | 119 ;; these are the functions that provide world i/o, chinese-room style |
270 | 120 |
271 | 121 |
272 | 122 |
273 | 123 |
274 (defn blender-creature | |
275 "Return a creature with all joints in place." | |
276 [blender-path] | |
277 (let [model (load-blender-model blender-path) | |
278 joints (creature-joints model)] | |
279 (assemble-creature model joints))) | |
280 | 124 |
281 (defn gray-scale [num] | 125 (defn gray-scale [num] |
282 (+ num | 126 (+ num |
283 (bit-shift-left num 8) | 127 (bit-shift-left num 8) |
284 (bit-shift-left num 16))) | 128 (bit-shift-left num 16))) |