rlm@73
|
1 #+title: First attempt at a creature!
|
rlm@73
|
2 #+author: Robert McIntyre
|
rlm@73
|
3 #+email: rlm@mit.edu
|
rlm@73
|
4 #+description:
|
rlm@73
|
5 #+keywords: simulation, jMonkeyEngine3, clojure
|
rlm@73
|
6 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@73
|
7 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@73
|
8
|
rlm@73
|
9 * Intro
|
rlm@73
|
10 So far, I've made the following senses --
|
rlm@73
|
11 - Vision
|
rlm@73
|
12 - Hearing
|
rlm@73
|
13 - Touch
|
rlm@73
|
14 - Proprioception
|
rlm@73
|
15
|
rlm@73
|
16 And one effector:
|
rlm@73
|
17 - Movement
|
rlm@73
|
18
|
rlm@73
|
19 However, the code so far has only enabled these senses, but has not
|
rlm@73
|
20 actually implemented them. For example, there is still a lot of work
|
rlm@73
|
21 to be done for vision. I need to be able to create an /eyeball/ in
|
rlm@73
|
22 simulation that can be moved around and see the world from different
|
rlm@73
|
23 angles. I also need to determine weather to use log-polar or cartesian
|
rlm@73
|
24 for the visual input, and I need to determine how/wether to
|
rlm@73
|
25 disceritise the visual input.
|
rlm@73
|
26
|
rlm@73
|
27 I also want to be able to visualize both the sensors and the
|
rlm@73
|
28 effectors in pretty pictures. This semi-retarted creature will by my
|
rlm@73
|
29 first attempt at bringing everything together.
|
rlm@73
|
30
|
rlm@73
|
31 * The creature's body
|
rlm@73
|
32
|
rlm@73
|
33 Still going to do an eve-like body in blender, but due to problems
|
rlm@73
|
34 importing the joints, etc into jMonkeyEngine3, I',m going to do all
|
rlm@73
|
35 the connecting here in clojure code, using the names of the individual
|
rlm@73
|
36 components and trial and error. Later, I'll maybe make some sort of
|
rlm@73
|
37 creature-building modifications to blender that support whatever
|
rlm@73
|
38 discreitized senses I'm going to make.
|
rlm@73
|
39
|
rlm@73
|
40 #+name: body-1
|
rlm@73
|
41 #+begin_src clojure
|
rlm@73
|
42 (ns cortex.silly
|
rlm@73
|
43 "let's play!"
|
rlm@73
|
44 {:author "Robert McIntyre"})
|
rlm@73
|
45
|
rlm@73
|
46 ;; TODO remove this!
|
rlm@73
|
47 (require 'cortex.import)
|
rlm@73
|
48 (cortex.import/mega-import-jme3)
|
rlm@73
|
49 (use '(cortex world util body hearing touch vision))
|
rlm@73
|
50
|
rlm@73
|
51 (use '[clojure.contrib [seq :only [find-first]]])
|
rlm@73
|
52
|
rlm@73
|
53
|
rlm@73
|
54 (rlm.rlm-commands/help)
|
rlm@73
|
55
|
rlm@73
|
56 (defn load-blender-model
|
rlm@73
|
57 "Load a .blend file using an asset folder relative path."
|
rlm@73
|
58 [^String model]
|
rlm@73
|
59 (.loadModel
|
rlm@73
|
60 (doto (asset-manager)
|
rlm@73
|
61 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@73
|
62 model))
|
rlm@73
|
63
|
rlm@74
|
64 (defn meta-data [blender-node key]
|
rlm@74
|
65 (if-let [data (.getUserData blender-node "properties")]
|
rlm@74
|
66 (.findValue data key)
|
rlm@74
|
67 nil))
|
rlm@73
|
68
|
rlm@74
|
69
|
rlm@74
|
70 (defn hand []
|
rlm@74
|
71 (load-blender-model "Models/creature1/one.blend"))
|
rlm@74
|
72
|
rlm@74
|
73
|
rlm@73
|
74
|
rlm@74
|
75 (def hand-names
|
rlm@74
|
76 #{
|
rlm@74
|
77 "middle-1"
|
rlm@74
|
78 "middle-2"
|
rlm@74
|
79 "middle-3"
|
rlm@74
|
80 "palm"
|
rlm@74
|
81 "pinky-1"
|
rlm@74
|
82 "pinky-2"
|
rlm@74
|
83 "pinky-3"
|
rlm@74
|
84 "pointer-1"
|
rlm@74
|
85 "pointer-2"
|
rlm@74
|
86 "pointer-3"
|
rlm@74
|
87 "ring-1"
|
rlm@74
|
88 "ring-2"
|
rlm@74
|
89 "ring-3"
|
rlm@74
|
90 "thumb-1"
|
rlm@74
|
91 "thumb-2"})
|
rlm@74
|
92
|
rlm@74
|
93 (defn hand-pieces []
|
rlm@74
|
94 (filter
|
rlm@74
|
95 (comp hand-names #(apply str (drop-last (.getName %)))) (node-seq (hand))))
|
rlm@74
|
96
|
rlm@74
|
97 (defn hand-joints []
|
rlm@74
|
98 (map #(.getWorldTranslation %)
|
rlm@74
|
99 (filter #(re-matches #"joint\.\d\d\d" (.getName %))
|
rlm@74
|
100 (node-seq (hand)))))
|
rlm@77
|
101 (defn worm []
|
rlm@77
|
102 (load-blender-model "Models/creature1/try-again.blend"))
|
rlm@77
|
103
|
rlm@74
|
104
|
rlm@74
|
105 (defn worm-pieces []
|
rlm@74
|
106 (filter
|
rlm@74
|
107 (comp #{"worm-2" "worm-1"}
|
rlm@74
|
108 #(apply str (drop-last (.getName %))))
|
rlm@77
|
109 (node-seq (worm))))
|
rlm@74
|
110
|
rlm@74
|
111 (defn worm-joints []
|
rlm@77
|
112 (filter #(re-matches #"joint\.\d\d\d" (.getName %))
|
rlm@77
|
113 (node-seq (worm))))
|
rlm@74
|
114
|
rlm@77
|
115 (defn bullet-trans []
|
rlm@77
|
116 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
117 :position (Vector3f. -10 5 0))
|
rlm@77
|
118 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
119 :position (Vector3f. -10 -5 0)
|
rlm@77
|
120 :mass 0)
|
rlm@77
|
121 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
122 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
123 swivel
|
rlm@77
|
124 (.toRotationMatrix
|
rlm@77
|
125 (doto (Quaternion.)
|
rlm@77
|
126 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
127 Vector3f/UNIT_X)))]
|
rlm@77
|
128 (doto
|
rlm@77
|
129 (ConeJoint.
|
rlm@77
|
130 control-a control-b
|
rlm@77
|
131 (Vector3f. 0 5 0)
|
rlm@77
|
132 (Vector3f. 0 -5 0)
|
rlm@77
|
133 swivel swivel)
|
rlm@77
|
134 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
135 (/ Math/PI 4)
|
rlm@77
|
136 (* Math/PI 0.8)))
|
rlm@77
|
137 (world (nodify
|
rlm@77
|
138 [obj-a obj-b])
|
rlm@77
|
139 standard-debug-controls
|
rlm@77
|
140 enable-debug
|
rlm@77
|
141 no-op)))
|
rlm@74
|
142
|
rlm@74
|
143
|
rlm@77
|
144 (defn bullet-trans* []
|
rlm@77
|
145 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
146 :position (Vector3f. 5 0 0)
|
rlm@77
|
147 :mass 90)
|
rlm@77
|
148 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
149 :position (Vector3f. -5 0 0)
|
rlm@77
|
150 :mass 0)
|
rlm@77
|
151 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
152 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
153 move-up? (atom nil)
|
rlm@77
|
154 move-down? (atom nil)
|
rlm@77
|
155 move-left? (atom nil)
|
rlm@77
|
156 move-right? (atom nil)
|
rlm@77
|
157 roll-left? (atom nil)
|
rlm@77
|
158 roll-right? (atom nil)
|
rlm@77
|
159 force 100
|
rlm@77
|
160 swivel
|
rlm@77
|
161 (.toRotationMatrix
|
rlm@77
|
162 (doto (Quaternion.)
|
rlm@77
|
163 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
164 Vector3f/UNIT_X)))
|
rlm@77
|
165 x-move
|
rlm@77
|
166 (doto (Matrix3f.)
|
rlm@77
|
167 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
168 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
169
|
rlm@77
|
170 timer (atom 0)]
|
rlm@77
|
171 (doto
|
rlm@77
|
172 (ConeJoint.
|
rlm@77
|
173 control-a control-b
|
rlm@77
|
174 (Vector3f. -8 0 0)
|
rlm@77
|
175 (Vector3f. 2 0 0)
|
rlm@77
|
176 ;;swivel swivel
|
rlm@77
|
177 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
178 x-move Matrix3f/IDENTITY
|
rlm@77
|
179 )
|
rlm@77
|
180 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
181 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
182 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
183 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
184 (world (nodify
|
rlm@77
|
185 [obj-a obj-b])
|
rlm@77
|
186 (merge standard-debug-controls
|
rlm@77
|
187 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
188 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
189 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
190 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
191 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
192 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
193
|
rlm@77
|
194 (fn [world]
|
rlm@77
|
195 (enable-debug world)
|
rlm@77
|
196 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
197 )
|
rlm@77
|
198
|
rlm@77
|
199 (fn [world _]
|
rlm@77
|
200
|
rlm@77
|
201 (if @move-up?
|
rlm@77
|
202 (.applyForce control-a
|
rlm@77
|
203 (Vector3f. force 0 0)
|
rlm@77
|
204 (Vector3f. 0 0 0)))
|
rlm@77
|
205 (if @move-down?
|
rlm@77
|
206 (.applyForce control-a
|
rlm@77
|
207 (Vector3f. (- force) 0 0)
|
rlm@77
|
208 (Vector3f. 0 0 0)))
|
rlm@77
|
209 (if @move-left?
|
rlm@77
|
210 (.applyForce control-a
|
rlm@77
|
211 (Vector3f. 0 force 0)
|
rlm@77
|
212 (Vector3f. 0 0 0)))
|
rlm@77
|
213 (if @move-right?
|
rlm@77
|
214 (.applyForce control-a
|
rlm@77
|
215 (Vector3f. 0 (- force) 0)
|
rlm@77
|
216 (Vector3f. 0 0 0)))
|
rlm@77
|
217
|
rlm@77
|
218 (if @roll-left?
|
rlm@77
|
219 (.applyForce control-a
|
rlm@77
|
220 (Vector3f. 0 0 force)
|
rlm@77
|
221 (Vector3f. 0 0 0)))
|
rlm@77
|
222 (if @roll-right?
|
rlm@77
|
223 (.applyForce control-a
|
rlm@77
|
224 (Vector3f. 0 0 (- force))
|
rlm@77
|
225 (Vector3f. 0 0 0)))
|
rlm@77
|
226
|
rlm@77
|
227 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
228 (.attachChild
|
rlm@77
|
229 (.getRootNode world)
|
rlm@77
|
230 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
231 :physical? false :position
|
rlm@77
|
232 (.getWorldTranslation obj-a)))))
|
rlm@77
|
233 )
|
rlm@77
|
234 ))
|
rlm@77
|
235
|
rlm@77
|
236
|
rlm@77
|
237
|
rlm@77
|
238
|
rlm@77
|
239
|
rlm@77
|
240
|
rlm@77
|
241
|
rlm@77
|
242
|
rlm@77
|
243
|
rlm@77
|
244
|
rlm@77
|
245
|
rlm@77
|
246 (defn world-setup [joint]
|
rlm@77
|
247 (let [top (doto
|
rlm@77
|
248 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@77
|
249 :position (Vector3f. 0 7 0))
|
rlm@77
|
250 (.addControl
|
rlm@77
|
251 (RigidBodyControl.
|
rlm@77
|
252 (CapsuleCollisionShape. 0.5 1.5 1) (float 15))))
|
rlm@77
|
253 bottom (doto
|
rlm@77
|
254 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@77
|
255 :position (Vector3f. 0 -1 0))
|
rlm@77
|
256 (.addControl
|
rlm@77
|
257 (RigidBodyControl.
|
rlm@77
|
258 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@77
|
259 table (box 10 2 10 :position (Vector3f. 0 -6 0)
|
rlm@77
|
260 :color ColorRGBA/Gray :mass 0)
|
rlm@77
|
261 a (.getControl top RigidBodyControl)
|
rlm@77
|
262 b (.getControl bottom RigidBodyControl)]
|
rlm@77
|
263 (cond
|
rlm@77
|
264 (= joint :point)
|
rlm@77
|
265 (doto
|
rlm@77
|
266 (Point2PointJoint. a b
|
rlm@77
|
267 (Vector3f. 0 -2 0)
|
rlm@77
|
268 (Vector3f. 0 2 0))
|
rlm@77
|
269 (.setCollisionBetweenLinkedBodys false))
|
rlm@77
|
270 (= joint :hinge)
|
rlm@77
|
271 (doto
|
rlm@77
|
272 (HingeJoint.
|
rlm@77
|
273 a b
|
rlm@77
|
274 (Vector3f. 0 -2 0)
|
rlm@77
|
275 (Vector3f. 0 2 0)
|
rlm@77
|
276 (Vector3f. 0 0 1)
|
rlm@77
|
277 (Vector3f. 0 0 1)
|
rlm@77
|
278
|
rlm@77
|
279 )
|
rlm@77
|
280 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
281 ;;(.setLimit (- Math/PI) Math/PI)
|
rlm@77
|
282 )
|
rlm@77
|
283 (= joint :cone)
|
rlm@77
|
284 ;; note to self -- jbullet does NOT implement cone joints
|
rlm@77
|
285 ;; correctly. You must use plain ol' bullet for this to work.
|
rlm@77
|
286 ;; It's faster anyway, so whatever.
|
rlm@77
|
287
|
rlm@77
|
288 (doto (ConeJoint.
|
rlm@77
|
289 a b
|
rlm@77
|
290 (Vector3f. 0 -5 0)
|
rlm@77
|
291 (Vector3f. 0 2 0)
|
rlm@77
|
292
|
rlm@77
|
293 (doto (Matrix3f.)
|
rlm@77
|
294 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
295 Vector3f/UNIT_Y))
|
rlm@77
|
296 (doto (Matrix3f.)
|
rlm@77
|
297 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
298 Vector3f/UNIT_Y))
|
rlm@77
|
299 )
|
rlm@77
|
300 ;;(.setAngularOnly true)
|
rlm@77
|
301
|
rlm@77
|
302 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
303 (.setLimit (* 1 (/ Math/PI 4))
|
rlm@77
|
304 (* 1 (/ Math/PI 4))
|
rlm@77
|
305 (* 0 (/ Math/PI 4)))
|
rlm@77
|
306
|
rlm@77
|
307 )
|
rlm@77
|
308 (= joint :six)
|
rlm@77
|
309 (doto
|
rlm@77
|
310 (SixDofJoint.
|
rlm@77
|
311 a b
|
rlm@77
|
312 (Vector3f. 0 -2 0)
|
rlm@77
|
313 (Vector3f. 0 2 0)
|
rlm@77
|
314 ;;(doto (Matrix3f.)
|
rlm@77
|
315 ;; (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
316 ;; Vector3f/UNIT_Y))
|
rlm@77
|
317 ;;(doto (Matrix3f.)
|
rlm@77
|
318 ;; (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
319 ;; Vector3f/UNIT_Y))
|
rlm@77
|
320 true)
|
rlm@77
|
321 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
322 (.setAngularLowerLimit (Vector3f. 0
|
rlm@77
|
323 (- (/ Math/PI 2))
|
rlm@77
|
324 0))
|
rlm@77
|
325
|
rlm@77
|
326 (.setAngularUpperLimit (Vector3f. 0
|
rlm@77
|
327 (/ Math/PI 2)
|
rlm@77
|
328 0))
|
rlm@77
|
329 (.setLinearLowerLimit (Vector3f. 0 0 0))
|
rlm@77
|
330 (.setLinearUpperLimit (Vector3f. 0 0 0))
|
rlm@77
|
331
|
rlm@77
|
332 )
|
rlm@77
|
333
|
rlm@77
|
334
|
rlm@77
|
335
|
rlm@77
|
336
|
rlm@77
|
337
|
rlm@77
|
338 )
|
rlm@77
|
339
|
rlm@77
|
340 [top bottom table]))
|
rlm@77
|
341
|
rlm@77
|
342 (defn speed-up [world]
|
rlm@77
|
343 (.setMoveSpeed (.getFlyByCamera world)
|
rlm@77
|
344 (float 100))
|
rlm@77
|
345 (.setRotationSpeed (.getFlyByCamera world)
|
rlm@77
|
346 (float 20))
|
rlm@77
|
347 world)
|
rlm@77
|
348
|
rlm@77
|
349
|
rlm@77
|
350 (defn test-joint [joint]
|
rlm@77
|
351 (let [[top bottom floor] (world-setup joint)
|
rlm@77
|
352 control (.getControl top RigidBodyControl)
|
rlm@77
|
353 move-up? (atom false)
|
rlm@77
|
354 move-down? (atom false)
|
rlm@77
|
355 move-left? (atom false)
|
rlm@77
|
356 move-right? (atom false)
|
rlm@77
|
357 roll-left? (atom false)
|
rlm@77
|
358 roll-right? (atom false)
|
rlm@77
|
359 timer (atom 0)]
|
rlm@77
|
360
|
rlm@77
|
361 (world
|
rlm@77
|
362 (nodify [top bottom floor])
|
rlm@77
|
363 (merge standard-debug-controls
|
rlm@77
|
364 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
365 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
366 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
367 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
368 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
369 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
370
|
rlm@77
|
371 (fn [world]
|
rlm@77
|
372 (light-up-everything world)
|
rlm@77
|
373 (enable-debug world)
|
rlm@77
|
374 (set-gravity world (Vector3f. 0 0 0))
|
rlm@77
|
375 )
|
rlm@77
|
376
|
rlm@77
|
377 (fn [world _]
|
rlm@77
|
378 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
379 (do
|
rlm@77
|
380 ;; (println-repl @timer)
|
rlm@77
|
381 (.attachChild (.getRootNode world)
|
rlm@77
|
382 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
383 :position (.getWorldTranslation top)
|
rlm@77
|
384 :physical? false))))
|
rlm@77
|
385 (if @move-up?
|
rlm@77
|
386 (.applyTorque control
|
rlm@77
|
387 (.mult (.getPhysicsRotation control)
|
rlm@77
|
388 (Vector3f. 0 0 10))))
|
rlm@77
|
389 (if @move-down?
|
rlm@77
|
390 (.applyTorque control
|
rlm@77
|
391 (.mult (.getPhysicsRotation control)
|
rlm@77
|
392 (Vector3f. 0 0 -10))))
|
rlm@77
|
393 (if @move-left?
|
rlm@77
|
394 (.applyTorque control
|
rlm@77
|
395 (.mult (.getPhysicsRotation control)
|
rlm@77
|
396 (Vector3f. 0 10 0))))
|
rlm@77
|
397 (if @move-right?
|
rlm@77
|
398 (.applyTorque control
|
rlm@77
|
399 (.mult (.getPhysicsRotation control)
|
rlm@77
|
400 (Vector3f. 0 -10 0))))
|
rlm@77
|
401 (if @roll-left?
|
rlm@77
|
402 (.applyTorque control
|
rlm@77
|
403 (.mult (.getPhysicsRotation control)
|
rlm@77
|
404 (Vector3f. -1 0 0))))
|
rlm@77
|
405 (if @roll-right?
|
rlm@77
|
406 (.applyTorque control
|
rlm@77
|
407 (.mult (.getPhysicsRotation control)
|
rlm@77
|
408 (Vector3f. 1 0 0))))))))
|
rlm@77
|
409
|
rlm@77
|
410
|
rlm@77
|
411
|
rlm@77
|
412 (defn run [joint] (.start (test-joint joint)))
|
rlm@77
|
413 (defn look [joint] (view (nodify (world-setup joint))))
|
rlm@77
|
414
|
rlm@77
|
415 (defn blender-to-jme
|
rlm@77
|
416 "Convert from Blender coordinates to JME coordinates"
|
rlm@77
|
417 [#^Vector3f in]
|
rlm@77
|
418 (Vector3f. (.getX in)
|
rlm@77
|
419 (.getZ in)
|
rlm@77
|
420 (- (.getY in))))
|
rlm@77
|
421
|
rlm@77
|
422
|
rlm@77
|
423 (defn joint-targets
|
rlm@77
|
424 "Return the two closest two objects to the joint object, ordered
|
rlm@77
|
425 from bottom to top according to the joint's rotation."
|
rlm@77
|
426 [#^Node parts #^Node joint]
|
rlm@77
|
427 ;;(println (meta-data joint "joint"))
|
rlm@77
|
428 (.getWorldRotation joint)
|
rlm@74
|
429 (loop [radius (float 0.01)]
|
rlm@74
|
430 (let [results (CollisionResults.)]
|
rlm@74
|
431 (.collideWith
|
rlm@74
|
432 parts
|
rlm@77
|
433 (BoundingBox. (.getWorldTranslation joint)
|
rlm@77
|
434 radius radius radius)
|
rlm@74
|
435 results)
|
rlm@74
|
436 (let [targets
|
rlm@74
|
437 (distinct
|
rlm@74
|
438 (map #(.getGeometry %) results))]
|
rlm@74
|
439 (if (>= (count targets) 2)
|
rlm@77
|
440 (sort-by
|
rlm@77
|
441 #(.getY
|
rlm@77
|
442 (.mult
|
rlm@77
|
443 (.inverse (.getWorldRotation joint))
|
rlm@77
|
444 (.subtract (.getWorldTranslation %)
|
rlm@77
|
445 (.getWorldTranslation joint))))
|
rlm@77
|
446 (take 2 targets))
|
rlm@74
|
447 (recur (float (* radius 2))))))))
|
rlm@74
|
448
|
rlm@77
|
449 (defn connect
|
rlm@77
|
450 "here are some examples:
|
rlm@77
|
451 {:type :point}
|
rlm@77
|
452 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@77
|
453 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@74
|
454
|
rlm@77
|
455 {:type :cone :limit-xz 0]
|
rlm@77
|
456 :limit-yz 0]
|
rlm@77
|
457 :twist 0]}
|
rlm@77
|
458 "
|
rlm@77
|
459 ([#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@77
|
460 (let [center-a (.getWorldTranslation obj-a)
|
rlm@77
|
461 center-b (.getWorldTranslation obj-b)
|
rlm@77
|
462 joint-center (.getWorldTranslation joint)
|
rlm@77
|
463 pivot-a (.subtract joint-center center-a)
|
rlm@77
|
464 pivot-b (.subtract joint-center center-b)
|
rlm@77
|
465 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
466 control-b (.getControl obj-b RigidBodyControl)]
|
rlm@77
|
467 ;; A side-effect of creating a joint registers
|
rlm@77
|
468 ;; it with both physics objects which in turn
|
rlm@77
|
469 ;; will register the joint with the physics system
|
rlm@77
|
470 ;; when the simulation is started.
|
rlm@77
|
471 (if-let [constraints
|
rlm@77
|
472 (map-vals
|
rlm@77
|
473 eval
|
rlm@77
|
474 (read-string
|
rlm@77
|
475 (meta-data (first (worm-joints)) "joint")))]
|
rlm@74
|
476
|
rlm@77
|
477 (let [joint-type (:type constraints)]
|
rlm@77
|
478 (cond (= :point joint-type)
|
rlm@77
|
479 (do
|
rlm@77
|
480 (println-repl "creating POINT joint")
|
rlm@77
|
481 (Point2PointJoint.
|
rlm@77
|
482 control-a
|
rlm@77
|
483 control-b
|
rlm@77
|
484 pivot-a
|
rlm@77
|
485 pivot-b))
|
rlm@77
|
486 (= :hinge joint-type)
|
rlm@77
|
487 (do
|
rlm@77
|
488 (println-repl "creating HINGE joint")
|
rlm@77
|
489 (let [axis (if-let
|
rlm@77
|
490 [axis (:axis constraints)]
|
rlm@77
|
491 axis
|
rlm@77
|
492 Vector3f/UNIT_X)
|
rlm@77
|
493 [limit-1 limit-2] (:limit constraints)
|
rlm@77
|
494 hinge-axis
|
rlm@77
|
495 (.mult
|
rlm@77
|
496 (.getWorldRotation joint)
|
rlm@77
|
497 (blender-to-jme axis))]
|
rlm@77
|
498 (doto
|
rlm@77
|
499 (HingeJoint.
|
rlm@77
|
500 control-a
|
rlm@77
|
501 control-b
|
rlm@77
|
502 pivot-a
|
rlm@77
|
503 pivot-b
|
rlm@77
|
504 hinge-axis
|
rlm@77
|
505 hinge-axis)
|
rlm@77
|
506 (.setLimit limit-1 limit-2))))
|
rlm@77
|
507 (= :cone joint-type)
|
rlm@77
|
508 (do
|
rlm@77
|
509 (let [limit-xy (:limit-xz constraints)
|
rlm@77
|
510 limit-yz (:limit-yz constraints)
|
rlm@77
|
511 twist (:twist constraints)]
|
rlm@77
|
512
|
rlm@77
|
513 (println-repl "creating CONE joint")
|
rlm@77
|
514 (doto
|
rlm@77
|
515 (ConeJoint.
|
rlm@77
|
516 control-a
|
rlm@77
|
517 control-b
|
rlm@77
|
518 pivot-a
|
rlm@77
|
519 pivot-b
|
rlm@77
|
520 (doto (Matrix3f.)
|
rlm@77
|
521 (.fromStartEndVectors
|
rlm@77
|
522 Vector3f/UNIT_X
|
rlm@77
|
523 (.normalize
|
rlm@77
|
524 (.subtract (.getWorldTranslation joint)
|
rlm@77
|
525 (.getWorldTranslation obj-a)))))
|
rlm@77
|
526 (doto (Matrix3f.)
|
rlm@77
|
527 (.fromStartEndVectors
|
rlm@77
|
528 Vector3f/UNIT_X
|
rlm@77
|
529 (.normalize
|
rlm@77
|
530 (.subtract
|
rlm@77
|
531 (.getWorldTranslation obj-b)
|
rlm@77
|
532 (.getWorldTranslation joint))))))
|
rlm@77
|
533 (.setLimit (float limit-xy)
|
rlm@77
|
534 (float limit-yz)
|
rlm@77
|
535 (float twist)))))
|
rlm@77
|
536 true
|
rlm@77
|
537 (println-repl
|
rlm@77
|
538 "joint-type " joint-type " not recognized")))
|
rlm@77
|
539
|
rlm@77
|
540 (println-repl "could not find joint meta-data!")))))
|
rlm@77
|
541
|
rlm@74
|
542
|
rlm@74
|
543
|
rlm@77
|
544 (defn physical-worm [#^Node pieces joints]
|
rlm@74
|
545 (dorun
|
rlm@74
|
546 (map
|
rlm@74
|
547 (fn [geom]
|
rlm@74
|
548 (let [physics-control
|
rlm@74
|
549 (RigidBodyControl.
|
rlm@74
|
550 (HullCollisionShape.
|
rlm@74
|
551 (.getMesh geom))
|
rlm@77
|
552 (if-let [mass (meta-data geom "mass")]
|
rlm@77
|
553 (do
|
rlm@77
|
554 (println-repl
|
rlm@77
|
555 "setting mass to " (float mass))
|
rlm@77
|
556 (float mass))
|
rlm@77
|
557 (float 1)))]
|
rlm@77
|
558
|
rlm@74
|
559 (.addControl geom physics-control)))
|
rlm@74
|
560 (filter #(isa? (class %) Geometry )
|
rlm@74
|
561 (node-seq pieces))))
|
rlm@77
|
562
|
rlm@74
|
563 (dorun
|
rlm@74
|
564 (map
|
rlm@77
|
565 (fn [joint]
|
rlm@77
|
566 (let [[obj-a obj-b]
|
rlm@77
|
567 (joint-targets pieces joint)]
|
rlm@77
|
568 (connect obj-a obj-b joint)))
|
rlm@77
|
569 joints))
|
rlm@77
|
570 pieces)
|
rlm@74
|
571
|
rlm@77
|
572 (defn the-worm []
|
rlm@77
|
573 (physical-worm (worm) (worm-joints)))
|
rlm@74
|
574
|
rlm@77
|
575 (defn test-worm []
|
rlm@74
|
576 (world
|
rlm@77
|
577 (nodify [(the-worm)
|
rlm@77
|
578 (box 10 2 10 :position (Vector3f. 0 -5 0)
|
rlm@77
|
579 :color ColorRGBA/Gray :mass 0)])
|
rlm@74
|
580 standard-debug-controls
|
rlm@77
|
581 (comp light-up-everything enable-debug
|
rlm@77
|
582 (fn [world]
|
rlm@77
|
583 (.setTimer world (NanoTimer.))
|
rlm@77
|
584 (speed-up world)
|
rlm@77
|
585 ;;(set-gravity world (Vector3f. 0 0 0))
|
rlm@77
|
586 world
|
rlm@77
|
587 ))
|
rlm@77
|
588 no-op))
|
rlm@73
|
589
|
rlm@73
|
590 #+end_src
|
rlm@73
|
591
|
rlm@75
|
592 #+results: body-1
|
rlm@77
|
593 : #'cortex.silly/test-try-again
|
rlm@75
|
594
|
rlm@73
|
595
|
rlm@73
|
596
|
rlm@73
|
597 * COMMENT generate source
|
rlm@73
|
598 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@73
|
599 <<body-1>>
|
rlm@73
|
600 #+end_src
|
rlm@73
|
601
|