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