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