comparison org/test-creature.org @ 81:10f495560c59

correcting problem with joints
author Robert McIntyre <rlm@mit.edu>
date Thu, 05 Jan 2012 21:31:34 -0700
parents 7af5ef686539
children 6b4ca076285e
comparison
equal deleted inserted replaced
80:7af5ef686539 81:10f495560c59
114 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints) 114 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
115 115
116 {:type :cone :limit-xz 0] 116 {:type :cone :limit-xz 0]
117 :limit-xy 0] 117 :limit-xy 0]
118 :twist 0]} (use XZY rotation mode in blender!)" 118 :twist 0]} (use XZY rotation mode in blender!)"
119 ([#^Node obj-a #^Node obj-b #^Node joint] 119 [#^Node obj-a #^Node obj-b #^Node joint]
120 (let [center-a (.getWorldTranslation obj-a) 120 (let [center-a (.getWorldTranslation obj-a)
121 center-b (.getWorldTranslation obj-b) 121 center-b (.getWorldTranslation obj-b)
122 joint-center (.getWorldTranslation joint) 122 joint-center (.getWorldTranslation joint)
123 pivot-a (.subtract joint-center center-a) 123 pivot-a (.subtract joint-center center-a)
124 pivot-b (.subtract joint-center center-b) 124 pivot-b (.subtract joint-center center-b)
125 control-a (.getControl obj-a RigidBodyControl) 125 control-a (.getControl obj-a RigidBodyControl)
126 control-b (.getControl obj-b RigidBodyControl)] 126 control-b (.getControl obj-b RigidBodyControl)]
127 ;; A side-effect of creating a joint registers 127 ;; A side-effect of creating a joint registers
128 ;; it with both physics objects which in turn 128 ;; it with both physics objects which in turn
129 ;; will register the joint with the physics system 129 ;; will register the joint with the physics system
130 ;; when the simulation is started. 130 ;; when the simulation is started.
131 (if-let [constraints 131 (if-let [constraints
132 (map-vals 132 (map-vals
133 eval 133 eval
134 (read-string 134 (read-string
135 (meta-data joint "joint")))] 135 (meta-data joint "joint")))]
136 136
137 (let [joint-type (:type constraints)] 137 (let [joint-type (:type constraints)]
138 (println-repl "creating joint between" 138 (println-repl "creating joint between"
139 (.getName obj-a) "and" (.getName obj-b)) 139 (.getName obj-a) "and" (.getName obj-b))
140 (cond (= :point joint-type) 140 (cond (= :point joint-type)
141 (do 141 (do
142 (println-repl "creating POINT joint") 142 (println-repl "creating POINT joint")
143 (Point2PointJoint. 143 (Point2PointJoint.
144 control-a 144 control-a
145 control-b 145 control-b
146 pivot-a 146 pivot-a
147 pivot-b)) 147 pivot-b))
148 (= :hinge joint-type) 148 (= :hinge joint-type)
149 (do 149 (do
150 (println-repl "creating HINGE joint") 150 (println-repl "creating HINGE joint")
151 (let [axis (if-let 151 (let [axis (if-let
152 [axis (:axis constraints)] 152 [axis (:axis constraints)]
153 axis 153 axis
154 Vector3f/UNIT_X) 154 Vector3f/UNIT_X)
155 [limit-1 limit-2] (:limit constraints) 155 [limit-1 limit-2] (:limit constraints)
156 hinge-axis 156 hinge-axis
157 (.mult 157 (.mult
158 (.getWorldRotation joint) 158 (.getWorldRotation joint)
159 (blender-to-jme axis))] 159 (blender-to-jme axis))]
160 (doto 160 (doto
161 (HingeJoint. 161 (HingeJoint.
162 control-a 162 control-a
163 control-b 163 control-b
164 pivot-a 164 pivot-a
165 pivot-b 165 pivot-b
166 hinge-axis 166 hinge-axis
167 hinge-axis) 167 hinge-axis)
168 (.setLimit limit-1 limit-2)))) 168 (.setLimit limit-1 limit-2))))
169 (= :cone joint-type) 169 (= :cone joint-type)
170 (do 170 (do
171 (let [limit-xy (:limit-xz constraints) 171 (let [limit-xz (:limit-xz constraints)
172 limit-yz (:limit-xy constraints) 172 limit-xy (:limit-xy constraints)
173 twist (:twist constraints)] 173 twist (:twist constraints)]
174 174
175 (println-repl "creating CONE joint") 175 (println-repl "creating CONE joint")
176 (let [vector-1 176 (let [vector-1
177 (.mult (.getWorldRotation joint) 177 (.mult (.getWorldRotation joint)
178 Vector3f/UNIT_X) 178 Vector3f/UNIT_X)
179 vector-2 179 vector-2
180 (.normalize 180 (.normalize
181 (.subtract 181 (.subtract
182 (.getWorldTranslation joint) 182 (.getWorldTranslation joint)
183 (.getWorldTranslation obj-a))) 183 (.getWorldTranslation obj-a)))
184 ] 184 ]
185 (println-repl 185 (println-repl
186 "vector-1 :" vector-1) 186 "vector-1 :" vector-1)
187 (println-repl 187 (println-repl
188 "vector-2 :" vector-2) 188 "vector-2 :" vector-2)
189 189
190 190
191 191
192 (doto 192 (doto
193 (ConeJoint. 193 (ConeJoint.
194 control-a 194 control-a
195 control-b 195 control-b
196 pivot-a 196 pivot-a
197 pivot-b 197 pivot-b
198 198
199 ;;(doto (Matrix3f.) 199 ;;(doto (Matrix3f.)
200 ;; (.fromStartEndVectors 200 ;; (.fromStartEndVectors
201 ;; Vector3f/UNIT_X 201 ;; Vector3f/UNIT_X
202 ;; (.normalize 202 ;; (.normalize
203 ;; (.subtract 203 ;; (.subtract
204 ;; (.getWorldTranslation joint) 204 ;; (.getWorldTranslation joint)
205 ;; (.getWorldTranslation obj-a))))) 205 ;; (.getWorldTranslation obj-a)))))
206 206
207 (.toRotationMatrix (.getWorldRotation joint)) 207
208 208 (.toRotationMatrix
209 (.toRotationMatrix 209 (.getWorldRotation joint))
210 (.fromAngleAxis (Quaternion.) 210
211 (.angleBetween Vector3f/UNIT_X pivot-a) 211
212 (.cross Vector3f/UNIT_X pivot-a))) 212
213 213 (.toRotationMatrix
214 ;; (doto (Matrix3f.) 214 (.fromAngleAxis
215 ;; (.fromStartEndVectors 215 (Quaternion.)
216 ;; Vector3f/UNIT_X 216 (.angleBetween Vector3f/UNIT_X pivot-a)
217 ;; (.normalize 217 (.cross Vector3f/UNIT_X pivot-a)))
218 ;; vector-1))) 218
219 219
220 ;; (doto (Matrix3f.) 220
221 ;; (.fromStartEndVectors 221
222 ;; Vector3f/UNIT_X 222 ;; (doto (Matrix3f.)
223 ;; vector-2)) 223 ;; (.fromStartEndVectors
224 ) 224 ;; Vector3f/UNIT_X
225 (.setLimit (float limit-xy) 225 ;; (.normalize
226 (float limit-yz) 226 ;; vector-1)))
227 (float twist)))))) 227
228 true 228 ;; (doto (Matrix3f.)
229 (println-repl 229 ;; (.fromStartEndVectors
230 "joint-type" joint-type "not recognized"))) 230 ;; Vector3f/UNIT_X
231 231 ;; vector-2))
232 (println-repl "could not find joint meta-data!"))))) 232 )
233 (.setLimit (float limit-xz)
234 (float limit-xy)
235 (float twist))))))
236 true
237 (println-repl
238 "joint-type" joint-type "not recognized")))
239
240 (println-repl "could not find joint meta-data!"))))
233 241
234 (defn assemble-creature [#^Node pieces joints] 242 (defn assemble-creature [#^Node pieces joints]
235 (dorun 243 (dorun
236 (map 244 (map
237 (fn [geom] 245 (fn [geom]
286 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green) 294 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
287 z-axis 295 z-axis
288 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)] 296 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
289 (world 297 (world
290 (nodify [(blender-creature thing) 298 (nodify [(blender-creature thing)
291 (box 10 2 10 :position (Vector3f. 0 -5.5 0) 299 (box 10 2 10 :position (Vector3f. 0 -9 0)
292 :color ColorRGBA/Gray :mass 0) 300 :color ColorRGBA/Gray :mass 0)
293 x-axis y-axis z-axis 301 x-axis y-axis z-axis
294 ]) 302 ])
295 standard-debug-controls 303 standard-debug-controls
296 (comp light-up-everything enable-debug 304 (comp light-up-everything enable-debug