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