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@87
|
53 (declare joint-create)
|
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@87
|
120 (defn world-to-local
|
rlm@87
|
121 "Convert the world coordinates into coordinates relative to the
|
rlm@87
|
122 object (i.e. local coordinates), taking into account the rotation
|
rlm@87
|
123 of object."
|
rlm@87
|
124 [#^Spatial object world-coordinate]
|
rlm@87
|
125 (let [out (Vector3f.)]
|
rlm@88
|
126 (.worldToLocal object world-coordinate out) out))
|
rlm@87
|
127
|
rlm@96
|
128 (defn local-to-world
|
rlm@96
|
129 "Convert the local coordinates into coordinates into world relative
|
rlm@96
|
130 coordinates"
|
rlm@96
|
131 [#^Spatial object local-coordinate]
|
rlm@96
|
132 (let [world-coordinate (Vector3f.)]
|
rlm@96
|
133 (.localToWorld object local-coordinate world-coordinate)
|
rlm@96
|
134 world-coordinate))
|
rlm@96
|
135
|
rlm@96
|
136
|
rlm@87
|
137 (defmulti joint-dispatch
|
rlm@87
|
138 "Translate blender pseudo-joints into real JME joints."
|
rlm@88
|
139 (fn [constraints & _]
|
rlm@87
|
140 (:type constraints)))
|
rlm@87
|
141
|
rlm@87
|
142 (defmethod joint-dispatch :point
|
rlm@87
|
143 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
144 (println-repl "creating POINT2POINT joint")
|
rlm@87
|
145 (Point2PointJoint.
|
rlm@87
|
146 control-a
|
rlm@87
|
147 control-b
|
rlm@87
|
148 pivot-a
|
rlm@87
|
149 pivot-b))
|
rlm@87
|
150
|
rlm@87
|
151 (defmethod joint-dispatch :hinge
|
rlm@87
|
152 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
153 (println-repl "creating HINGE joint")
|
rlm@87
|
154 (let [axis
|
rlm@87
|
155 (if-let
|
rlm@87
|
156 [axis (:axis constraints)]
|
rlm@87
|
157 axis
|
rlm@87
|
158 Vector3f/UNIT_X)
|
rlm@87
|
159 [limit-1 limit-2] (:limit constraints)
|
rlm@87
|
160 hinge-axis
|
rlm@87
|
161 (.mult
|
rlm@87
|
162 rotation
|
rlm@87
|
163 (blender-to-jme axis))]
|
rlm@87
|
164 (doto
|
rlm@87
|
165 (HingeJoint.
|
rlm@87
|
166 control-a
|
rlm@87
|
167 control-b
|
rlm@87
|
168 pivot-a
|
rlm@87
|
169 pivot-b
|
rlm@87
|
170 hinge-axis
|
rlm@87
|
171 hinge-axis)
|
rlm@87
|
172 (.setLimit limit-1 limit-2))))
|
rlm@87
|
173
|
rlm@87
|
174 (defmethod joint-dispatch :cone
|
rlm@87
|
175 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
176 (let [limit-xz (:limit-xz constraints)
|
rlm@87
|
177 limit-xy (:limit-xy constraints)
|
rlm@87
|
178 twist (:twist constraints)]
|
rlm@87
|
179
|
rlm@87
|
180 (println-repl "creating CONE joint")
|
rlm@87
|
181 (println-repl rotation)
|
rlm@87
|
182 (println-repl
|
rlm@87
|
183 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
|
rlm@87
|
184 (println-repl
|
rlm@87
|
185 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
|
rlm@87
|
186 (println-repl
|
rlm@87
|
187 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
|
rlm@87
|
188 (doto
|
rlm@87
|
189 (ConeJoint.
|
rlm@87
|
190 control-a
|
rlm@87
|
191 control-b
|
rlm@87
|
192 pivot-a
|
rlm@87
|
193 pivot-b
|
rlm@87
|
194 rotation
|
rlm@87
|
195 rotation)
|
rlm@87
|
196 (.setLimit (float limit-xz)
|
rlm@87
|
197 (float limit-xy)
|
rlm@87
|
198 (float twist)))))
|
rlm@87
|
199
|
rlm@88
|
200 (defn connect
|
rlm@87
|
201 "here are some examples:
|
rlm@87
|
202 {:type :point}
|
rlm@87
|
203 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@87
|
204 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@87
|
205
|
rlm@89
|
206 {:type :cone :limit-xz 0]
|
rlm@89
|
207 :limit-xy 0]
|
rlm@89
|
208 :twist 0]} (use XZY rotation mode in blender!)"
|
rlm@87
|
209 [#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@87
|
210 (let [control-a (.getControl obj-a RigidBodyControl)
|
rlm@87
|
211 control-b (.getControl obj-b RigidBodyControl)
|
rlm@87
|
212 joint-center (.getWorldTranslation joint)
|
rlm@87
|
213 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
|
rlm@87
|
214 pivot-a (world-to-local obj-a joint-center)
|
rlm@87
|
215 pivot-b (world-to-local obj-b joint-center)]
|
rlm@89
|
216
|
rlm@87
|
217 (if-let [constraints
|
rlm@87
|
218 (map-vals
|
rlm@87
|
219 eval
|
rlm@87
|
220 (read-string
|
rlm@87
|
221 (meta-data joint "joint")))]
|
rlm@89
|
222 ;; A side-effect of creating a joint registers
|
rlm@89
|
223 ;; it with both physics objects which in turn
|
rlm@89
|
224 ;; will register the joint with the physics system
|
rlm@89
|
225 ;; when the simulation is started.
|
rlm@87
|
226 (do
|
rlm@87
|
227 (println-repl "creating joint between"
|
rlm@87
|
228 (.getName obj-a) "and" (.getName obj-b))
|
rlm@87
|
229 (joint-dispatch constraints
|
rlm@87
|
230 control-a control-b
|
rlm@87
|
231 pivot-a pivot-b
|
rlm@87
|
232 joint-rotation))
|
rlm@87
|
233 (println-repl "could not find joint meta-data!"))))
|
rlm@87
|
234
|
rlm@78
|
235 (defn assemble-creature [#^Node pieces joints]
|
rlm@78
|
236 (dorun
|
rlm@78
|
237 (map
|
rlm@78
|
238 (fn [geom]
|
rlm@78
|
239 (let [physics-control
|
rlm@78
|
240 (RigidBodyControl.
|
rlm@78
|
241 (HullCollisionShape.
|
rlm@78
|
242 (.getMesh geom))
|
rlm@78
|
243 (if-let [mass (meta-data geom "mass")]
|
rlm@78
|
244 (do
|
rlm@78
|
245 (println-repl
|
rlm@78
|
246 "setting" (.getName geom) "mass to" (float mass))
|
rlm@78
|
247 (float mass))
|
rlm@78
|
248 (float 1)))]
|
rlm@78
|
249
|
rlm@78
|
250 (.addControl geom physics-control)))
|
rlm@78
|
251 (filter #(isa? (class %) Geometry )
|
rlm@78
|
252 (node-seq pieces))))
|
rlm@77
|
253
|
rlm@78
|
254 (dorun
|
rlm@78
|
255 (map
|
rlm@78
|
256 (fn [joint]
|
rlm@78
|
257 (let [[obj-a obj-b]
|
rlm@78
|
258 (joint-targets pieces joint)]
|
rlm@88
|
259 (connect obj-a obj-b joint)))
|
rlm@78
|
260 joints))
|
rlm@78
|
261 pieces)
|
rlm@74
|
262
|
rlm@78
|
263 (defn blender-creature [blender-path]
|
rlm@78
|
264 (let [model (load-blender-model blender-path)
|
rlm@78
|
265 joints
|
rlm@78
|
266 (if-let [joint-node (.getChild model "joints")]
|
rlm@78
|
267 (seq (.getChildren joint-node))
|
rlm@78
|
268 (do (println-repl "could not find joints node")
|
rlm@78
|
269 []))]
|
rlm@78
|
270 (assemble-creature model joints)))
|
rlm@74
|
271
|
rlm@78
|
272 (def hand "Models/creature1/one.blend")
|
rlm@74
|
273
|
rlm@78
|
274 (def worm "Models/creature1/try-again.blend")
|
rlm@78
|
275
|
rlm@90
|
276 (def touch "Models/creature1/touch.blend")
|
rlm@90
|
277
|
rlm@90
|
278 (defn worm-model [] (load-blender-model worm))
|
rlm@90
|
279
|
rlm@80
|
280 (defn x-ray [#^ColorRGBA color]
|
rlm@80
|
281 (doto (Material. (asset-manager)
|
rlm@80
|
282 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@80
|
283 (.setColor "Color" color)
|
rlm@80
|
284 (-> (.getAdditionalRenderState)
|
rlm@80
|
285 (.setDepthTest false))))
|
rlm@80
|
286
|
rlm@78
|
287 (defn test-creature [thing]
|
rlm@80
|
288 (let [x-axis
|
rlm@80
|
289 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
|
rlm@80
|
290 y-axis
|
rlm@80
|
291 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
|
rlm@80
|
292 z-axis
|
rlm@80
|
293 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
|
rlm@78
|
294 (world
|
rlm@78
|
295 (nodify [(blender-creature thing)
|
rlm@81
|
296 (box 10 2 10 :position (Vector3f. 0 -9 0)
|
rlm@80
|
297 :color ColorRGBA/Gray :mass 0)
|
rlm@80
|
298 x-axis y-axis z-axis
|
rlm@80
|
299 ])
|
rlm@78
|
300 standard-debug-controls
|
rlm@90
|
301 (fn [world]
|
rlm@90
|
302 (light-up-everything world)
|
rlm@90
|
303 (enable-debug world)
|
rlm@90
|
304 ;;(com.aurellem.capture.Capture/captureVideo
|
rlm@90
|
305 ;; world (file-str "/home/r/proj/ai-videos/hand"))
|
rlm@90
|
306 (.setTimer world (NanoTimer.))
|
rlm@93
|
307 (set-gravity world (Vector3f. 0 0 0))
|
rlm@90
|
308 (speed-up world)
|
rlm@90
|
309 )
|
rlm@90
|
310 no-op
|
rlm@90
|
311 ;;(let [timer (atom 0)]
|
rlm@90
|
312 ;; (fn [_ _]
|
rlm@90
|
313 ;; (swap! timer inc)
|
rlm@90
|
314 ;; (if (= (rem @timer 60) 0)
|
rlm@90
|
315 ;; (println-repl (float (/ @timer 60))))))
|
rlm@90
|
316 )))
|
rlm@90
|
317
|
rlm@90
|
318
|
rlm@91
|
319 (defn colorful []
|
rlm@91
|
320 (.getChild (worm-model) "worm-21"))
|
rlm@90
|
321
|
rlm@90
|
322 (import jme3tools.converters.ImageToAwt)
|
rlm@90
|
323
|
rlm@90
|
324 (import ij.ImagePlus)
|
rlm@90
|
325
|
rlm@90
|
326 (defn triangle-indices
|
rlm@90
|
327 "Get the triangle vertex indices of a given triangle from a given
|
rlm@90
|
328 mesh."
|
rlm@90
|
329 [#^Mesh mesh triangle-index]
|
rlm@90
|
330 (let [indices (int-array 3)]
|
rlm@90
|
331 (.getTriangle mesh triangle-index indices)
|
rlm@90
|
332 (vec indices)))
|
rlm@90
|
333
|
rlm@90
|
334 (defn uv-coord
|
rlm@90
|
335 "Get the uv-coordinates of the vertex named by vertex-index"
|
rlm@90
|
336 [#^Mesh mesh vertex-index]
|
rlm@90
|
337 (let [UV-buffer
|
rlm@90
|
338 (.getData
|
rlm@90
|
339 (.getBuffer
|
rlm@90
|
340 mesh
|
rlm@90
|
341 VertexBuffer$Type/TexCoord))]
|
rlm@91
|
342 (Vector2f.
|
rlm@91
|
343 (.get UV-buffer (* vertex-index 2))
|
rlm@91
|
344 (.get UV-buffer (+ 1 (* vertex-index 2))))))
|
rlm@90
|
345
|
rlm@93
|
346 (defn tri-uv-coord
|
rlm@93
|
347 "Get the uv-cooridnates of the triangle's verticies."
|
rlm@93
|
348 [#^Mesh mesh #^Triangle triangle]
|
rlm@93
|
349 (map (partial uv-coord mesh)
|
rlm@93
|
350 (triangle-indices mesh (.getIndex triangle))))
|
rlm@93
|
351
|
rlm@91
|
352 (defn touch-receptor-image
|
rlm@91
|
353 "Return the touch-sensor distribution image in ImagePlus format."
|
rlm@91
|
354 [#^Geometry obj]
|
rlm@90
|
355 (let
|
rlm@91
|
356 [mat (.getMaterial obj)
|
rlm@90
|
357 texture
|
rlm@90
|
358 (.getTextureValue
|
rlm@90
|
359 (.getTextureParam
|
rlm@90
|
360 mat
|
rlm@90
|
361 MaterialHelper/TEXTURE_TYPE_DIFFUSE))
|
rlm@90
|
362 im (.getImage texture)]
|
rlm@90
|
363 (ImagePlus.
|
rlm@90
|
364 "UV-map"
|
rlm@90
|
365 (ImageToAwt/convert im false false 0))))
|
rlm@90
|
366
|
rlm@91
|
367
|
rlm@91
|
368 (import ij.process.ImageProcessor)
|
rlm@91
|
369 (import java.awt.image.BufferedImage)
|
rlm@91
|
370
|
rlm@91
|
371 (defprotocol Frame
|
rlm@91
|
372 (frame [this]))
|
rlm@91
|
373
|
rlm@91
|
374 (extend-type BufferedImage
|
rlm@91
|
375 Frame
|
rlm@91
|
376 (frame [image]
|
rlm@91
|
377 (merge
|
rlm@91
|
378 (apply
|
rlm@91
|
379 hash-map
|
rlm@91
|
380 (interleave
|
rlm@91
|
381 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@91
|
382 (vector x y)))
|
rlm@91
|
383 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@91
|
384 (let [data (.getRGB image x y)]
|
rlm@91
|
385 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
|
rlm@91
|
386 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
|
rlm@91
|
387 :b (bit-and 0x0000ff data)))))))
|
rlm@91
|
388 {:width (.getWidth image) :height (.getHeight image)})))
|
rlm@91
|
389
|
rlm@91
|
390
|
rlm@91
|
391 (extend-type ImagePlus
|
rlm@91
|
392 Frame
|
rlm@91
|
393 (frame [image+]
|
rlm@91
|
394 (frame (.getBufferedImage image+))))
|
rlm@91
|
395
|
rlm@94
|
396
|
rlm@92
|
397 (def white -1)
|
rlm@94
|
398
|
rlm@91
|
399 (defn filter-pixels
|
rlm@91
|
400 "List the coordinates of all pixels matching pred."
|
rlm@92
|
401 {:author "Dylan Holmes"}
|
rlm@91
|
402 [pred #^ImageProcessor ip]
|
rlm@91
|
403 (let
|
rlm@91
|
404 [width (.getWidth ip)
|
rlm@91
|
405 height (.getHeight ip)]
|
rlm@91
|
406 ((fn accumulate [x y matches]
|
rlm@91
|
407 (cond
|
rlm@91
|
408 (>= y height) matches
|
rlm@91
|
409 (>= x width) (recur 0 (inc y) matches)
|
rlm@91
|
410 (pred (.getPixel ip x y))
|
rlm@91
|
411 (recur (inc x) y (conj matches (Vector2f. x y)))
|
rlm@91
|
412 :else (recur (inc x) y matches)))
|
rlm@91
|
413 0 0 [])))
|
rlm@91
|
414
|
rlm@91
|
415 (defn white-coordinates
|
rlm@91
|
416 "List the coordinates of all the white pixels in an image."
|
rlm@91
|
417 [#^ImageProcessor ip]
|
rlm@92
|
418 (filter-pixels #(= % white) ip))
|
rlm@91
|
419
|
rlm@91
|
420 (defn same-side? [p1 p2 ref p]
|
rlm@91
|
421 (<=
|
rlm@91
|
422 0
|
rlm@91
|
423 (.dot
|
rlm@91
|
424 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@91
|
425 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@91
|
426
|
rlm@94
|
427
|
rlm@94
|
428 (defn triangle->matrix4f
|
rlm@94
|
429 "Converts the triangle into a 4x4 matrix of vertices: The first
|
rlm@94
|
430 three columns contain the vertices of the triangle; the last
|
rlm@94
|
431 contains the unit normal of the triangle. The bottom row is filled
|
rlm@94
|
432 with 1s."
|
rlm@94
|
433 [#^Triangle t]
|
rlm@94
|
434 (let [mat (Matrix4f.)
|
rlm@94
|
435 [vert-1 vert-2 vert-3]
|
rlm@94
|
436 ((comp vec map) #(.get t %) (range 3))
|
rlm@94
|
437 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@94
|
438 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@94
|
439
|
rlm@94
|
440 (dorun
|
rlm@94
|
441 (for [row (range 4) col (range 3)]
|
rlm@94
|
442 (do
|
rlm@94
|
443 (.set mat col row (.get (vertices row)col))
|
rlm@94
|
444 (.set mat 3 row 1))))
|
rlm@94
|
445 mat))
|
rlm@94
|
446
|
rlm@94
|
447 (defn triangle-transformation
|
rlm@94
|
448 "Returns the affine transformation that converts each vertex in the
|
rlm@94
|
449 first triangle into the corresponding vertex in the second
|
rlm@94
|
450 triangle."
|
rlm@94
|
451 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
452 (.mult
|
rlm@94
|
453 (triangle->matrix4f tri-2)
|
rlm@94
|
454 (.invert (triangle->matrix4f tri-1))))
|
rlm@94
|
455
|
rlm@94
|
456 (def death (Triangle.
|
rlm@94
|
457 (Vector3f. 1 1 1)
|
rlm@94
|
458 (Vector3f. 1 2 3)
|
rlm@94
|
459 (Vector3f. 5 6 7)))
|
rlm@94
|
460
|
rlm@94
|
461 (def death-2 (Triangle.
|
rlm@94
|
462 (Vector3f. 2 2 2)
|
rlm@94
|
463 (Vector3f. 1 1 1)
|
rlm@94
|
464 (Vector3f. 0 1 0)))
|
rlm@94
|
465
|
rlm@94
|
466 (defn vector2f->vector3f [v]
|
rlm@94
|
467 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@94
|
468
|
rlm@94
|
469
|
rlm@94
|
470 (extend-type Triangle
|
rlm@94
|
471 Textual
|
rlm@94
|
472 (text [t]
|
rlm@94
|
473 (println "Triangle: " \newline (.get1 t) \newline
|
rlm@94
|
474 (.get2 t) \newline (.get3 t))))
|
rlm@94
|
475
|
rlm@94
|
476
|
rlm@94
|
477 (defn map-triangle [f #^Triangle tri]
|
rlm@94
|
478 (Triangle.
|
rlm@94
|
479 (f 0 (.get1 tri))
|
rlm@94
|
480 (f 1 (.get2 tri))
|
rlm@94
|
481 (f 2 (.get3 tri))))
|
rlm@94
|
482
|
rlm@94
|
483 (defn triangle-seq [#^Triangle tri]
|
rlm@94
|
484 [(.get1 tri) (.get2 tri) (.get3 tri)])
|
rlm@94
|
485
|
rlm@94
|
486 (defn vector3f-seq [#^Vector3f v]
|
rlm@94
|
487 [(.getX v) (.getY v) (.getZ v)])
|
rlm@94
|
488
|
rlm@91
|
489 (defn inside-triangle?
|
rlm@94
|
490 "Is the point inside the triangle? Now what do we do?
|
rlm@94
|
491 You might want to hold on there"
|
rlm@95
|
492 {:author "Dylan Holmes"}
|
rlm@94
|
493 [tri p]
|
rlm@94
|
494 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
|
rlm@94
|
495 (and
|
rlm@94
|
496 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@94
|
497 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@94
|
498 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@91
|
499
|
rlm@94
|
500 (defn uv-triangle
|
rlm@94
|
501 "Convert the mesh triangle into the cooresponding triangle in
|
rlm@94
|
502 UV-space. Z-component of these triangles is always zero."
|
rlm@94
|
503 [#^Mesh mesh #^Triangle tri]
|
rlm@94
|
504 (apply #(Triangle. %1 %2 %3)
|
rlm@94
|
505 (map vector2f->vector3f
|
rlm@94
|
506 (tri-uv-coord mesh tri))))
|
rlm@91
|
507
|
rlm@94
|
508 (defn pixel-triangle
|
rlm@94
|
509 "Convert the mesh triange into the corresponding triangle in
|
rlm@94
|
510 UV-pixel-space. Z compenent will be zero."
|
rlm@94
|
511 [#^Mesh mesh #^Triangle tri width height]
|
rlm@94
|
512 (map-triangle (fn [_ v]
|
rlm@94
|
513 (Vector3f. (* width (.getX v))
|
rlm@94
|
514 (* height (.getY v))
|
rlm@94
|
515 0))
|
rlm@94
|
516 (uv-triangle mesh tri)))
|
rlm@93
|
517
|
rlm@96
|
518 (def rasterize pixel-triangle)
|
rlm@96
|
519
|
rlm@96
|
520
|
rlm@94
|
521 (defn triangle-bounds
|
rlm@94
|
522 "Dimensions of the bounding square of the triangle in the form
|
rlm@94
|
523 [x y width height].
|
rlm@94
|
524 Assumes that the triangle lies in the XY plane."
|
rlm@94
|
525 [#^Triangle tri]
|
rlm@94
|
526 (let [verts (map vector3f-seq (triangle-seq tri))
|
rlm@94
|
527 x (apply min (map first verts))
|
rlm@94
|
528 y (apply min (map second verts))]
|
rlm@93
|
529
|
rlm@94
|
530 [x y
|
rlm@94
|
531 (- (apply max (map first verts)) x)
|
rlm@94
|
532 (- (apply max (map second verts)) y)
|
rlm@94
|
533 ]))
|
rlm@93
|
534
|
rlm@93
|
535
|
rlm@94
|
536 (defn locate-tactile-sensors
|
rlm@94
|
537 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@94
|
538 their positions in geometry-relative coordinates."
|
rlm@94
|
539 [#^Geometry geo]
|
rlm@94
|
540 (let [mesh (.getMesh geo)
|
rlm@96
|
541 tris (triangles geo)
|
rlm@96
|
542
|
rlm@94
|
543 image (touch-receptor-image geo)
|
rlm@94
|
544 width (.getWidth image)
|
rlm@94
|
545 height (.getHeight image)
|
rlm@96
|
546
|
rlm@94
|
547
|
rlm@94
|
548 ;; for each triangle
|
rlm@94
|
549 sensor-coords
|
rlm@94
|
550 (fn [tri]
|
rlm@94
|
551 ;; translate triangle to uv-pixel-space
|
rlm@94
|
552 (let [uv-tri
|
rlm@94
|
553 (pixel-triangle mesh tri width height)
|
rlm@94
|
554 bounds (vec (triangle-bounds uv-tri))]
|
rlm@94
|
555
|
rlm@94
|
556 ;; get that part of the picture
|
rlm@94
|
557
|
rlm@94
|
558 (apply #(.setRoi image %1 %2 %3 %4) bounds)
|
rlm@94
|
559 (let [cutout (.crop (.getProcessor image))
|
rlm@94
|
560 ;; extract white pixels inside triangle
|
rlm@94
|
561 cutout-tri
|
rlm@94
|
562 (map-triangle
|
rlm@94
|
563 (fn [_ v]
|
rlm@94
|
564 (.subtract
|
rlm@94
|
565 v
|
rlm@94
|
566 (Vector3f. (bounds 0) (bounds 1) (float 0))))
|
rlm@94
|
567 uv-tri)
|
rlm@94
|
568 whites (filter (partial inside-triangle? cutout-tri)
|
rlm@94
|
569 (map vector2f->vector3f
|
rlm@94
|
570 (white-coordinates cutout)))
|
rlm@94
|
571 ;; translate pixel coordinates to world-space
|
rlm@94
|
572 transform (triangle-transformation cutout-tri tri)]
|
rlm@94
|
573 (map #(.mult transform %) whites))))]
|
rlm@94
|
574
|
rlm@94
|
575
|
rlm@96
|
576
|
rlm@96
|
577 (vec (map sensor-coords tris))))
|
rlm@96
|
578
|
rlm@96
|
579 (defn locate-tactile-sensors*
|
rlm@96
|
580 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@96
|
581 their positions in geometry-relative coordinates."
|
rlm@96
|
582 [#^Geometry geo]
|
rlm@96
|
583 (let [uv-image (touch-receptor-image geo)
|
rlm@96
|
584 width (.getWidth uv-image)
|
rlm@96
|
585 height (.getHeight uv-image)
|
rlm@96
|
586
|
rlm@96
|
587 mesh (.getMesh geo)
|
rlm@96
|
588 mesh-tris (triangles geo)
|
rlm@96
|
589
|
rlm@96
|
590 ;; for each triangle
|
rlm@96
|
591 sensor-coords
|
rlm@96
|
592 (fn [tri]
|
rlm@96
|
593 ;; translate triangle to uv-pixel-space
|
rlm@96
|
594 (let [uv-tri
|
rlm@96
|
595 (rasterize mesh tri width height)
|
rlm@96
|
596 bounds (vec (triangle-bounds uv-tri))]
|
rlm@96
|
597
|
rlm@96
|
598 ;; get that part of the picture
|
rlm@96
|
599
|
rlm@96
|
600 (apply (partial (memfn setRoi) uv-image) bounds)
|
rlm@96
|
601 (let [cutout (.crop (.getProcessor uv-image))
|
rlm@96
|
602 ;; extract white pixels inside triangle
|
rlm@96
|
603 cutout-tri
|
rlm@96
|
604 (map-triangle
|
rlm@96
|
605 (fn [_ v]
|
rlm@96
|
606 (.subtract
|
rlm@96
|
607 v
|
rlm@96
|
608 (Vector3f. (bounds 0) (bounds 1) (float 0))))
|
rlm@96
|
609 uv-tri)
|
rlm@96
|
610 whites (filter (partial inside-triangle? cutout-tri)
|
rlm@96
|
611 (map vector2f->vector3f
|
rlm@96
|
612 (white-coordinates cutout)))
|
rlm@96
|
613 ;; translate pixel coordinates to world-space
|
rlm@96
|
614 transform (triangle-transformation cutout-tri tri)]
|
rlm@96
|
615 (map #(.mult transform %) whites))))]
|
rlm@96
|
616
|
rlm@96
|
617
|
rlm@96
|
618
|
rlm@96
|
619 (for [mesh-tri mesh-tris]
|
rlm@96
|
620
|
rlm@96
|
621 (let [uv-tri (rasterize mesh mesh-tri width height)
|
rlm@96
|
622 bounding-box (vec (triangle-bounds uv-tri))]
|
rlm@96
|
623 (apply (partial (memfn setRoi) uv-image) bounding-box)
|
rlm@96
|
624
|
rlm@96
|
625
|
rlm@96
|
626
|
rlm@96
|
627
|
rlm@96
|
628 ))
|
rlm@96
|
629 (vec (map sensor-coords mesh-tris))))
|
rlm@96
|
630
|
rlm@96
|
631
|
rlm@96
|
632
|
rlm@96
|
633 (defn measure-touchies [#^Geometry geo]
|
rlm@96
|
634 (let [tactile-sensor-coords (locate-tactile-sensors geo)
|
rlm@96
|
635 tris (triangles geo)]
|
rlm@96
|
636 (fn [world]
|
rlm@96
|
637 (let [sensor-origins (vec
|
rlm@96
|
638 (map
|
rlm@96
|
639 #(map (partial local-to-world geo) %)
|
rlm@96
|
640 tactile-sensor-coords))
|
rlm@96
|
641 triangle-normals (vec
|
rlm@96
|
642 (map (partial get-ray-direction geo)
|
rlm@96
|
643 tris))
|
rlm@96
|
644 rays (flatten
|
rlm@96
|
645 (map
|
rlm@96
|
646 (fn [origins normals]
|
rlm@96
|
647 (map
|
rlm@96
|
648 #(Ray. %1 %2)
|
rlm@96
|
649 origins
|
rlm@96
|
650 normals))
|
rlm@96
|
651 sensor-origins
|
rlm@96
|
652 (map repeat triangle-normals)))
|
rlm@96
|
653
|
rlm@94
|
654
|
rlm@96
|
655 ]
|
rlm@96
|
656 rays))))
|
rlm@96
|
657
|
rlm@96
|
658
|
rlm@96
|
659
|
rlm@96
|
660
|
rlm@96
|
661
|
rlm@96
|
662
|
rlm@96
|
663
|
rlm@96
|
664
|
rlm@96
|
665
|
rlm@96
|
666
|
rlm@94
|
667
|
rlm@94
|
668
|
rlm@94
|
669
|
rlm@94
|
670
|
rlm@94
|
671
|
rlm@94
|
672
|
rlm@93
|
673
|
rlm@93
|
674
|
rlm@91
|
675
|
rlm@90
|
676 ;; for each triangle in the mesh,
|
rlm@90
|
677 ;; get the normal to the triangle,
|
rlm@90
|
678 ;; look at the UV touch map, restricted to that triangle,
|
rlm@90
|
679 ;; get the positions of those touch sensors in geometry-relative
|
rlm@90
|
680 ;; coordinates.
|
rlm@91
|
681 (defn tactile-coords [#^Geometry obj]
|
rlm@91
|
682 (let [mesh (.getMesh obj)
|
rlm@91
|
683 num-triangles (.getTriangleCount mesh)
|
rlm@91
|
684 num-verticies (.getVertexCount mesh)
|
rlm@91
|
685 uv-coord (partial uv-coord mesh)
|
rlm@91
|
686 triangle-indices (partial triangle-indices mesh)
|
rlm@91
|
687 receptors (touch-receptor-image obj)
|
rlm@93
|
688 tris (triangles obj)
|
rlm@91
|
689 ]
|
rlm@91
|
690 (map
|
rlm@91
|
691 (fn [[tri-1 tri-2 tri-3]]
|
rlm@91
|
692 (let [width (.getWidth receptors)
|
rlm@91
|
693 height (.getHeight receptors)
|
rlm@91
|
694 uv-1 (uv-coord tri-1)
|
rlm@91
|
695 uv-2 (uv-coord tri-2)
|
rlm@91
|
696 uv-3 (uv-coord tri-3)
|
rlm@91
|
697 x-coords (map #(.getX %) [uv-1 uv-2 uv-3])
|
rlm@91
|
698 y-coords (map #(.getY %) [uv-1 uv-2 uv-3])
|
rlm@91
|
699 max-x (Math/ceil (* width (apply max x-coords)))
|
rlm@91
|
700 min-x (Math/floor (* width (apply min x-coords)))
|
rlm@91
|
701 max-y (Math/ceil (* height (apply max y-coords)))
|
rlm@91
|
702 min-y (Math/floor (* height (apply min y-coords)))
|
rlm@91
|
703
|
rlm@91
|
704 image-1 (Vector2f. (* width (.getX uv-1))
|
rlm@91
|
705 (* height (.getY uv-1)))
|
rlm@91
|
706 image-2 (Vector2f. (* width (.getX uv-2))
|
rlm@91
|
707 (* height (.getY uv-2)))
|
rlm@91
|
708 image-3 (Vector2f. (* width (.getX uv-3))
|
rlm@91
|
709 (* height (.getY uv-3)))
|
rlm@91
|
710 left-corner
|
rlm@91
|
711 (Vector2f. min-x min-y)
|
rlm@91
|
712 ]
|
rlm@91
|
713
|
rlm@91
|
714 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
|
rlm@93
|
715 (let [processor (.crop (.getProcessor receptors))]
|
rlm@93
|
716 (map
|
rlm@93
|
717 #(.add left-corner %)
|
rlm@93
|
718
|
rlm@93
|
719 (filter
|
rlm@93
|
720 (partial
|
rlm@93
|
721 inside-triangle?
|
rlm@93
|
722 (.subtract image-1 left-corner)
|
rlm@93
|
723 (.subtract image-2 left-corner)
|
rlm@93
|
724 (.subtract image-3 left-corner))
|
rlm@93
|
725 (white-coordinates processor))))
|
rlm@91
|
726 )) (map triangle-indices (range num-triangles)))))
|
rlm@93
|
727
|
rlm@91
|
728
|
rlm@91
|
729
|
rlm@91
|
730
|
rlm@91
|
731
|
rlm@91
|
732
|
rlm@91
|
733
|
rlm@91
|
734 (defn all-names []
|
rlm@91
|
735 (concat
|
rlm@91
|
736 (re-split #"\n" (slurp (file-str
|
rlm@91
|
737 "/home/r/proj/names/dist.female.first")))
|
rlm@91
|
738 (re-split #"\n" (slurp (file-str
|
rlm@91
|
739 "/home/r/proj/names/dist.male.first")))
|
rlm@91
|
740 (re-split #"\n" (slurp (file-str
|
rlm@91
|
741 "/home/r/proj/names/dist.all.last")))))
|
rlm@90
|
742
|
rlm@90
|
743
|
rlm@90
|
744
|
rlm@90
|
745
|
rlm@90
|
746
|
rlm@90
|
747
|
rlm@90
|
748
|
rlm@90
|
749
|
rlm@90
|
750
|
rlm@90
|
751 (defrecord LulzLoader [])
|
rlm@90
|
752 (defprotocol Lulzable (load-lulz [this]))
|
rlm@90
|
753 (extend-type LulzLoader
|
rlm@90
|
754 Lulzable
|
rlm@90
|
755 (load-lulz [this] (println "the lulz have arrived!")))
|
rlm@90
|
756
|
rlm@78
|
757
|
rlm@78
|
758 (defn world-setup [joint]
|
rlm@90
|
759 (let [joint-position (Vector3f. 0 0 0)
|
rlm@83
|
760 joint-rotation
|
rlm@83
|
761 (.toRotationMatrix
|
rlm@83
|
762 (.mult
|
rlm@83
|
763 (doto (Quaternion.)
|
rlm@83
|
764 (.fromAngleAxis
|
rlm@83
|
765 (* 1 (/ Math/PI 4))
|
rlm@85
|
766 (Vector3f. -1 0 0)))
|
rlm@85
|
767 (doto (Quaternion.)
|
rlm@85
|
768 (.fromAngleAxis
|
rlm@85
|
769 (* 1 (/ Math/PI 2))
|
rlm@85
|
770 (Vector3f. 0 0 1)))))
|
rlm@84
|
771 top-position (.mult joint-rotation (Vector3f. 8 0 0))
|
rlm@83
|
772
|
rlm@83
|
773 origin (doto
|
rlm@83
|
774 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
|
rlm@84
|
775 :position top-position))
|
rlm@83
|
776 top (doto
|
rlm@78
|
777 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@84
|
778 :position top-position)
|
rlm@83
|
779
|
rlm@78
|
780 (.addControl
|
rlm@78
|
781 (RigidBodyControl.
|
rlm@83
|
782 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
|
rlm@78
|
783 bottom (doto
|
rlm@78
|
784 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@83
|
785 :position (Vector3f. 0 0 0))
|
rlm@83
|
786 (.addControl
|
rlm@78
|
787 (RigidBodyControl.
|
rlm@78
|
788 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@83
|
789 table (box 10 2 10 :position (Vector3f. 0 -20 0)
|
rlm@78
|
790 :color ColorRGBA/Gray :mass 0)
|
rlm@78
|
791 a (.getControl top RigidBodyControl)
|
rlm@78
|
792 b (.getControl bottom RigidBodyControl)]
|
rlm@83
|
793
|
rlm@78
|
794 (cond
|
rlm@83
|
795 (= joint :cone)
|
rlm@83
|
796
|
rlm@83
|
797 (doto (ConeJoint.
|
rlm@83
|
798 a b
|
rlm@87
|
799 (world-to-local top joint-position)
|
rlm@87
|
800 (world-to-local bottom joint-position)
|
rlm@83
|
801 joint-rotation
|
rlm@83
|
802 joint-rotation
|
rlm@83
|
803 )
|
rlm@78
|
804
|
rlm@83
|
805
|
rlm@83
|
806 (.setLimit (* (/ 10) Math/PI)
|
rlm@83
|
807 (* (/ 4) Math/PI)
|
rlm@83
|
808 0)))
|
rlm@83
|
809 [origin top bottom table]))
|
rlm@78
|
810
|
rlm@78
|
811 (defn test-joint [joint]
|
rlm@83
|
812 (let [[origin top bottom floor] (world-setup joint)
|
rlm@78
|
813 control (.getControl top RigidBodyControl)
|
rlm@78
|
814 move-up? (atom false)
|
rlm@78
|
815 move-down? (atom false)
|
rlm@78
|
816 move-left? (atom false)
|
rlm@78
|
817 move-right? (atom false)
|
rlm@78
|
818 roll-left? (atom false)
|
rlm@78
|
819 roll-right? (atom false)
|
rlm@78
|
820 timer (atom 0)]
|
rlm@78
|
821
|
rlm@78
|
822 (world
|
rlm@83
|
823 (nodify [top bottom floor origin])
|
rlm@78
|
824 (merge standard-debug-controls
|
rlm@78
|
825 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@78
|
826 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@78
|
827 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@78
|
828 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@78
|
829 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@78
|
830 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@78
|
831
|
rlm@78
|
832 (fn [world]
|
rlm@78
|
833 (light-up-everything world)
|
rlm@78
|
834 (enable-debug world)
|
rlm@78
|
835 (set-gravity world (Vector3f. 0 0 0))
|
rlm@78
|
836 )
|
rlm@78
|
837
|
rlm@78
|
838 (fn [world _]
|
rlm@78
|
839 (if (zero? (rem (swap! timer inc) 100))
|
rlm@78
|
840 (do
|
rlm@78
|
841 ;; (println-repl @timer)
|
rlm@78
|
842 (.attachChild (.getRootNode world)
|
rlm@78
|
843 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@78
|
844 :position (.getWorldTranslation top)
|
rlm@83
|
845 :physical? false))
|
rlm@83
|
846 (.attachChild (.getRootNode world)
|
rlm@83
|
847 (sphere 0.05 :color ColorRGBA/LightGray
|
rlm@83
|
848 :position (.getWorldTranslation bottom)
|
rlm@83
|
849 :physical? false))))
|
rlm@83
|
850
|
rlm@78
|
851 (if @move-up?
|
rlm@78
|
852 (.applyTorque control
|
rlm@78
|
853 (.mult (.getPhysicsRotation control)
|
rlm@78
|
854 (Vector3f. 0 0 10))))
|
rlm@78
|
855 (if @move-down?
|
rlm@78
|
856 (.applyTorque control
|
rlm@78
|
857 (.mult (.getPhysicsRotation control)
|
rlm@78
|
858 (Vector3f. 0 0 -10))))
|
rlm@78
|
859 (if @move-left?
|
rlm@78
|
860 (.applyTorque control
|
rlm@78
|
861 (.mult (.getPhysicsRotation control)
|
rlm@78
|
862 (Vector3f. 0 10 0))))
|
rlm@78
|
863 (if @move-right?
|
rlm@78
|
864 (.applyTorque control
|
rlm@78
|
865 (.mult (.getPhysicsRotation control)
|
rlm@78
|
866 (Vector3f. 0 -10 0))))
|
rlm@78
|
867 (if @roll-left?
|
rlm@78
|
868 (.applyTorque control
|
rlm@78
|
869 (.mult (.getPhysicsRotation control)
|
rlm@78
|
870 (Vector3f. -1 0 0))))
|
rlm@78
|
871 (if @roll-right?
|
rlm@78
|
872 (.applyTorque control
|
rlm@78
|
873 (.mult (.getPhysicsRotation control)
|
rlm@78
|
874 (Vector3f. 1 0 0))))))))
|
rlm@83
|
875
|
rlm@83
|
876
|
rlm@83
|
877
|
rlm@87
|
878 #+end_src
|
rlm@83
|
879
|
rlm@87
|
880 #+results: body-1
|
rlm@87
|
881 : #'cortex.silly/test-joint
|
rlm@78
|
882
|
rlm@78
|
883
|
rlm@78
|
884 * COMMENT purgatory
|
rlm@78
|
885 #+begin_src clojure
|
rlm@77
|
886 (defn bullet-trans []
|
rlm@77
|
887 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
888 :position (Vector3f. -10 5 0))
|
rlm@77
|
889 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
890 :position (Vector3f. -10 -5 0)
|
rlm@77
|
891 :mass 0)
|
rlm@77
|
892 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
893 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
894 swivel
|
rlm@77
|
895 (.toRotationMatrix
|
rlm@77
|
896 (doto (Quaternion.)
|
rlm@77
|
897 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
898 Vector3f/UNIT_X)))]
|
rlm@77
|
899 (doto
|
rlm@77
|
900 (ConeJoint.
|
rlm@77
|
901 control-a control-b
|
rlm@77
|
902 (Vector3f. 0 5 0)
|
rlm@77
|
903 (Vector3f. 0 -5 0)
|
rlm@77
|
904 swivel swivel)
|
rlm@77
|
905 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
906 (/ Math/PI 4)
|
rlm@77
|
907 (* Math/PI 0.8)))
|
rlm@77
|
908 (world (nodify
|
rlm@77
|
909 [obj-a obj-b])
|
rlm@77
|
910 standard-debug-controls
|
rlm@77
|
911 enable-debug
|
rlm@77
|
912 no-op)))
|
rlm@74
|
913
|
rlm@74
|
914
|
rlm@77
|
915 (defn bullet-trans* []
|
rlm@77
|
916 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
917 :position (Vector3f. 5 0 0)
|
rlm@77
|
918 :mass 90)
|
rlm@77
|
919 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
920 :position (Vector3f. -5 0 0)
|
rlm@77
|
921 :mass 0)
|
rlm@77
|
922 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
923 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
924 move-up? (atom nil)
|
rlm@77
|
925 move-down? (atom nil)
|
rlm@77
|
926 move-left? (atom nil)
|
rlm@77
|
927 move-right? (atom nil)
|
rlm@77
|
928 roll-left? (atom nil)
|
rlm@77
|
929 roll-right? (atom nil)
|
rlm@77
|
930 force 100
|
rlm@77
|
931 swivel
|
rlm@77
|
932 (.toRotationMatrix
|
rlm@77
|
933 (doto (Quaternion.)
|
rlm@77
|
934 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
935 Vector3f/UNIT_X)))
|
rlm@77
|
936 x-move
|
rlm@77
|
937 (doto (Matrix3f.)
|
rlm@77
|
938 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
939 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
940
|
rlm@77
|
941 timer (atom 0)]
|
rlm@77
|
942 (doto
|
rlm@77
|
943 (ConeJoint.
|
rlm@77
|
944 control-a control-b
|
rlm@77
|
945 (Vector3f. -8 0 0)
|
rlm@77
|
946 (Vector3f. 2 0 0)
|
rlm@77
|
947 ;;swivel swivel
|
rlm@77
|
948 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
949 x-move Matrix3f/IDENTITY
|
rlm@77
|
950 )
|
rlm@77
|
951 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
952 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
953 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
954 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
955 (world (nodify
|
rlm@77
|
956 [obj-a obj-b])
|
rlm@77
|
957 (merge standard-debug-controls
|
rlm@77
|
958 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
959 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
960 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
961 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
962 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
963 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
964
|
rlm@77
|
965 (fn [world]
|
rlm@77
|
966 (enable-debug world)
|
rlm@77
|
967 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
968 )
|
rlm@77
|
969
|
rlm@77
|
970 (fn [world _]
|
rlm@77
|
971
|
rlm@77
|
972 (if @move-up?
|
rlm@77
|
973 (.applyForce control-a
|
rlm@77
|
974 (Vector3f. force 0 0)
|
rlm@77
|
975 (Vector3f. 0 0 0)))
|
rlm@77
|
976 (if @move-down?
|
rlm@77
|
977 (.applyForce control-a
|
rlm@77
|
978 (Vector3f. (- force) 0 0)
|
rlm@77
|
979 (Vector3f. 0 0 0)))
|
rlm@77
|
980 (if @move-left?
|
rlm@77
|
981 (.applyForce control-a
|
rlm@77
|
982 (Vector3f. 0 force 0)
|
rlm@77
|
983 (Vector3f. 0 0 0)))
|
rlm@77
|
984 (if @move-right?
|
rlm@77
|
985 (.applyForce control-a
|
rlm@77
|
986 (Vector3f. 0 (- force) 0)
|
rlm@77
|
987 (Vector3f. 0 0 0)))
|
rlm@77
|
988
|
rlm@77
|
989 (if @roll-left?
|
rlm@77
|
990 (.applyForce control-a
|
rlm@77
|
991 (Vector3f. 0 0 force)
|
rlm@77
|
992 (Vector3f. 0 0 0)))
|
rlm@77
|
993 (if @roll-right?
|
rlm@77
|
994 (.applyForce control-a
|
rlm@77
|
995 (Vector3f. 0 0 (- force))
|
rlm@77
|
996 (Vector3f. 0 0 0)))
|
rlm@77
|
997
|
rlm@77
|
998 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
999 (.attachChild
|
rlm@77
|
1000 (.getRootNode world)
|
rlm@77
|
1001 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
1002 :physical? false :position
|
rlm@77
|
1003 (.getWorldTranslation obj-a)))))
|
rlm@77
|
1004 )
|
rlm@77
|
1005 ))
|
rlm@77
|
1006
|
rlm@77
|
1007
|
rlm@77
|
1008
|
rlm@73
|
1009 #+end_src
|
rlm@73
|
1010
|
rlm@73
|
1011
|
rlm@73
|
1012 * COMMENT generate source
|
rlm@73
|
1013 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@73
|
1014 <<body-1>>
|
rlm@73
|
1015 #+end_src
|
rlm@73
|
1016
|
rlm@94
|
1017
|
rlm@94
|
1018
|
rlm@94
|
1019
|
rlm@94
|
1020
|
rlm@94
|
1021 (defn transform-trianglesdsd
|
rlm@94
|
1022 "Transform that converts each vertex in the first triangle
|
rlm@94
|
1023 into the corresponding vertex in the second triangle."
|
rlm@94
|
1024 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
1025 (let [in [(.get1 tri-1)
|
rlm@94
|
1026 (.get2 tri-1)
|
rlm@94
|
1027 (.get3 tri-1)]
|
rlm@94
|
1028 out [(.get1 tri-2)
|
rlm@94
|
1029 (.get2 tri-2)
|
rlm@94
|
1030 (.get3 tri-2)]]
|
rlm@94
|
1031 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
|
rlm@94
|
1032 in* [(.mult translate (in 0))
|
rlm@94
|
1033 (.mult translate (in 1))
|
rlm@94
|
1034 (.mult translate (in 2))]
|
rlm@94
|
1035 final-translation
|
rlm@94
|
1036 (doto (Matrix4f.)
|
rlm@94
|
1037 (.setTranslation (out 1)))
|
rlm@94
|
1038
|
rlm@94
|
1039 rotate-1
|
rlm@94
|
1040 (doto (Matrix3f.)
|
rlm@94
|
1041 (.fromStartEndVectors
|
rlm@94
|
1042 (.normalize
|
rlm@94
|
1043 (.subtract
|
rlm@94
|
1044 (in* 1) (in* 0)))
|
rlm@94
|
1045 (.normalize
|
rlm@94
|
1046 (.subtract
|
rlm@94
|
1047 (out 1) (out 0)))))
|
rlm@94
|
1048 in** [(.mult rotate-1 (in* 0))
|
rlm@94
|
1049 (.mult rotate-1 (in* 1))
|
rlm@94
|
1050 (.mult rotate-1 (in* 2))]
|
rlm@94
|
1051 scale-factor-1
|
rlm@94
|
1052 (.mult
|
rlm@94
|
1053 (.normalize
|
rlm@94
|
1054 (.subtract
|
rlm@94
|
1055 (out 1)
|
rlm@94
|
1056 (out 0)))
|
rlm@94
|
1057 (/ (.length
|
rlm@94
|
1058 (.subtract (out 1)
|
rlm@94
|
1059 (out 0)))
|
rlm@94
|
1060 (.length
|
rlm@94
|
1061 (.subtract (in** 1)
|
rlm@94
|
1062 (in** 0)))))
|
rlm@94
|
1063 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
|
rlm@94
|
1064 in*** [(.mult scale-1 (in** 0))
|
rlm@94
|
1065 (.mult scale-1 (in** 1))
|
rlm@94
|
1066 (.mult scale-1 (in** 2))]
|
rlm@94
|
1067
|
rlm@94
|
1068
|
rlm@94
|
1069
|
rlm@94
|
1070
|
rlm@94
|
1071
|
rlm@94
|
1072 ]
|
rlm@94
|
1073
|
rlm@94
|
1074 (dorun (map println in))
|
rlm@94
|
1075 (println)
|
rlm@94
|
1076 (dorun (map println in*))
|
rlm@94
|
1077 (println)
|
rlm@94
|
1078 (dorun (map println in**))
|
rlm@94
|
1079 (println)
|
rlm@94
|
1080 (dorun (map println in***))
|
rlm@94
|
1081 (println)
|
rlm@94
|
1082
|
rlm@94
|
1083 )))
|
rlm@94
|
1084
|
rlm@94
|
1085
|
rlm@94
|
1086
|
rlm@94
|
1087
|
rlm@94
|
1088
|
rlm@94
|
1089
|
rlm@94
|
1090
|
rlm@94
|
1091
|
rlm@94
|
1092
|
rlm@94
|
1093
|
rlm@94
|
1094
|
rlm@94
|
1095
|
rlm@94
|
1096
|
rlm@94
|
1097
|
rlm@94
|
1098
|
rlm@94
|
1099
|
rlm@94
|
1100
|
rlm@94
|
1101
|
rlm@94
|
1102 )
|
rlm@94
|
1103
|
rlm@94
|
1104
|