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