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