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@97
|
353 "Return the touch-sensor distribution image in ImagePlus format, or
|
rlm@97
|
354 nil if it does not exist."
|
rlm@91
|
355 [#^Geometry obj]
|
rlm@97
|
356 (let [mat (.getMaterial obj)]
|
rlm@97
|
357 (if-let [texture-param
|
rlm@97
|
358 (.getTextureParam
|
rlm@97
|
359 mat
|
rlm@97
|
360 MaterialHelper/TEXTURE_TYPE_DIFFUSE)]
|
rlm@97
|
361 (let
|
rlm@97
|
362 [texture
|
rlm@97
|
363 (.getTextureValue texture-param)
|
rlm@97
|
364 im (.getImage texture)]
|
rlm@97
|
365 (ImagePlus.
|
rlm@97
|
366 "UV-map"
|
rlm@97
|
367 (ImageToAwt/convert im false false 0))))))
|
rlm@97
|
368
|
rlm@90
|
369
|
rlm@91
|
370
|
rlm@91
|
371 (import ij.process.ImageProcessor)
|
rlm@91
|
372 (import java.awt.image.BufferedImage)
|
rlm@91
|
373
|
rlm@91
|
374 (defprotocol Frame
|
rlm@91
|
375 (frame [this]))
|
rlm@91
|
376
|
rlm@91
|
377 (extend-type BufferedImage
|
rlm@91
|
378 Frame
|
rlm@91
|
379 (frame [image]
|
rlm@91
|
380 (merge
|
rlm@91
|
381 (apply
|
rlm@91
|
382 hash-map
|
rlm@91
|
383 (interleave
|
rlm@91
|
384 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@91
|
385 (vector x y)))
|
rlm@91
|
386 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@91
|
387 (let [data (.getRGB image x y)]
|
rlm@91
|
388 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
|
rlm@91
|
389 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
|
rlm@91
|
390 :b (bit-and 0x0000ff data)))))))
|
rlm@91
|
391 {:width (.getWidth image) :height (.getHeight image)})))
|
rlm@91
|
392
|
rlm@91
|
393
|
rlm@91
|
394 (extend-type ImagePlus
|
rlm@91
|
395 Frame
|
rlm@91
|
396 (frame [image+]
|
rlm@91
|
397 (frame (.getBufferedImage image+))))
|
rlm@91
|
398
|
rlm@94
|
399
|
rlm@92
|
400 (def white -1)
|
rlm@94
|
401
|
rlm@91
|
402 (defn filter-pixels
|
rlm@91
|
403 "List the coordinates of all pixels matching pred."
|
rlm@92
|
404 {:author "Dylan Holmes"}
|
rlm@91
|
405 [pred #^ImageProcessor ip]
|
rlm@91
|
406 (let
|
rlm@91
|
407 [width (.getWidth ip)
|
rlm@91
|
408 height (.getHeight ip)]
|
rlm@91
|
409 ((fn accumulate [x y matches]
|
rlm@91
|
410 (cond
|
rlm@91
|
411 (>= y height) matches
|
rlm@91
|
412 (>= x width) (recur 0 (inc y) matches)
|
rlm@91
|
413 (pred (.getPixel ip x y))
|
rlm@91
|
414 (recur (inc x) y (conj matches (Vector2f. x y)))
|
rlm@91
|
415 :else (recur (inc x) y matches)))
|
rlm@91
|
416 0 0 [])))
|
rlm@91
|
417
|
rlm@91
|
418 (defn white-coordinates
|
rlm@91
|
419 "List the coordinates of all the white pixels in an image."
|
rlm@91
|
420 [#^ImageProcessor ip]
|
rlm@92
|
421 (filter-pixels #(= % white) ip))
|
rlm@91
|
422
|
rlm@91
|
423 (defn same-side? [p1 p2 ref p]
|
rlm@91
|
424 (<=
|
rlm@91
|
425 0
|
rlm@91
|
426 (.dot
|
rlm@91
|
427 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@91
|
428 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@91
|
429
|
rlm@94
|
430
|
rlm@94
|
431 (defn triangle->matrix4f
|
rlm@94
|
432 "Converts the triangle into a 4x4 matrix of vertices: The first
|
rlm@94
|
433 three columns contain the vertices of the triangle; the last
|
rlm@94
|
434 contains the unit normal of the triangle. The bottom row is filled
|
rlm@94
|
435 with 1s."
|
rlm@94
|
436 [#^Triangle t]
|
rlm@94
|
437 (let [mat (Matrix4f.)
|
rlm@94
|
438 [vert-1 vert-2 vert-3]
|
rlm@94
|
439 ((comp vec map) #(.get t %) (range 3))
|
rlm@94
|
440 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@94
|
441 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@94
|
442
|
rlm@94
|
443 (dorun
|
rlm@94
|
444 (for [row (range 4) col (range 3)]
|
rlm@94
|
445 (do
|
rlm@94
|
446 (.set mat col row (.get (vertices row)col))
|
rlm@94
|
447 (.set mat 3 row 1))))
|
rlm@94
|
448 mat))
|
rlm@94
|
449
|
rlm@94
|
450 (defn triangle-transformation
|
rlm@94
|
451 "Returns the affine transformation that converts each vertex in the
|
rlm@94
|
452 first triangle into the corresponding vertex in the second
|
rlm@94
|
453 triangle."
|
rlm@94
|
454 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
455 (.mult
|
rlm@94
|
456 (triangle->matrix4f tri-2)
|
rlm@94
|
457 (.invert (triangle->matrix4f tri-1))))
|
rlm@94
|
458
|
rlm@94
|
459 (def death (Triangle.
|
rlm@94
|
460 (Vector3f. 1 1 1)
|
rlm@94
|
461 (Vector3f. 1 2 3)
|
rlm@94
|
462 (Vector3f. 5 6 7)))
|
rlm@94
|
463
|
rlm@94
|
464 (def death-2 (Triangle.
|
rlm@94
|
465 (Vector3f. 2 2 2)
|
rlm@94
|
466 (Vector3f. 1 1 1)
|
rlm@94
|
467 (Vector3f. 0 1 0)))
|
rlm@94
|
468
|
rlm@94
|
469 (defn vector2f->vector3f [v]
|
rlm@94
|
470 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@94
|
471
|
rlm@94
|
472
|
rlm@94
|
473 (extend-type Triangle
|
rlm@94
|
474 Textual
|
rlm@94
|
475 (text [t]
|
rlm@94
|
476 (println "Triangle: " \newline (.get1 t) \newline
|
rlm@94
|
477 (.get2 t) \newline (.get3 t))))
|
rlm@94
|
478
|
rlm@94
|
479
|
rlm@94
|
480 (defn map-triangle [f #^Triangle tri]
|
rlm@94
|
481 (Triangle.
|
rlm@94
|
482 (f 0 (.get1 tri))
|
rlm@94
|
483 (f 1 (.get2 tri))
|
rlm@94
|
484 (f 2 (.get3 tri))))
|
rlm@94
|
485
|
rlm@94
|
486 (defn triangle-seq [#^Triangle tri]
|
rlm@94
|
487 [(.get1 tri) (.get2 tri) (.get3 tri)])
|
rlm@94
|
488
|
rlm@94
|
489 (defn vector3f-seq [#^Vector3f v]
|
rlm@94
|
490 [(.getX v) (.getY v) (.getZ v)])
|
rlm@94
|
491
|
rlm@91
|
492 (defn inside-triangle?
|
rlm@94
|
493 "Is the point inside the triangle? Now what do we do?
|
rlm@94
|
494 You might want to hold on there"
|
rlm@95
|
495 {:author "Dylan Holmes"}
|
rlm@94
|
496 [tri p]
|
rlm@94
|
497 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
|
rlm@94
|
498 (and
|
rlm@94
|
499 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@94
|
500 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@94
|
501 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@91
|
502
|
rlm@94
|
503 (defn uv-triangle
|
rlm@94
|
504 "Convert the mesh triangle into the cooresponding triangle in
|
rlm@94
|
505 UV-space. Z-component of these triangles is always zero."
|
rlm@94
|
506 [#^Mesh mesh #^Triangle tri]
|
rlm@94
|
507 (apply #(Triangle. %1 %2 %3)
|
rlm@94
|
508 (map vector2f->vector3f
|
rlm@94
|
509 (tri-uv-coord mesh tri))))
|
rlm@91
|
510
|
rlm@94
|
511 (defn pixel-triangle
|
rlm@94
|
512 "Convert the mesh triange into the corresponding triangle in
|
rlm@94
|
513 UV-pixel-space. Z compenent will be zero."
|
rlm@94
|
514 [#^Mesh mesh #^Triangle tri width height]
|
rlm@94
|
515 (map-triangle (fn [_ v]
|
rlm@94
|
516 (Vector3f. (* width (.getX v))
|
rlm@94
|
517 (* height (.getY v))
|
rlm@94
|
518 0))
|
rlm@94
|
519 (uv-triangle mesh tri)))
|
rlm@93
|
520
|
rlm@96
|
521 (def rasterize pixel-triangle)
|
rlm@96
|
522
|
rlm@96
|
523
|
rlm@94
|
524 (defn triangle-bounds
|
rlm@94
|
525 "Dimensions of the bounding square of the triangle in the form
|
rlm@94
|
526 [x y width height].
|
rlm@94
|
527 Assumes that the triangle lies in the XY plane."
|
rlm@94
|
528 [#^Triangle tri]
|
rlm@94
|
529 (let [verts (map vector3f-seq (triangle-seq tri))
|
rlm@94
|
530 x (apply min (map first verts))
|
rlm@94
|
531 y (apply min (map second verts))]
|
rlm@93
|
532
|
rlm@94
|
533 [x y
|
rlm@94
|
534 (- (apply max (map first verts)) x)
|
rlm@94
|
535 (- (apply max (map second verts)) y)
|
rlm@94
|
536 ]))
|
rlm@93
|
537
|
rlm@93
|
538
|
rlm@97
|
539 (defn locate-feelers
|
rlm@94
|
540 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@94
|
541 their positions in geometry-relative coordinates."
|
rlm@94
|
542 [#^Geometry geo]
|
rlm@97
|
543 (if-let [image (touch-receptor-image geo)]
|
rlm@97
|
544 (let [mesh (.getMesh geo)
|
rlm@97
|
545 tris (triangles geo)
|
rlm@97
|
546
|
rlm@97
|
547
|
rlm@97
|
548 width (.getWidth image)
|
rlm@97
|
549 height (.getHeight image)
|
rlm@97
|
550
|
rlm@97
|
551 ;; for each triangle
|
rlm@97
|
552 sensor-coords
|
rlm@97
|
553 (fn [tri]
|
rlm@97
|
554 ;; translate triangle to uv-pixel-space
|
rlm@97
|
555 (let [uv-tri
|
rlm@97
|
556 (pixel-triangle mesh tri width height)
|
rlm@97
|
557 bounds (vec (triangle-bounds uv-tri))]
|
rlm@97
|
558
|
rlm@97
|
559 ;; get that part of the picture
|
rlm@97
|
560
|
rlm@97
|
561 (apply #(.setRoi image %1 %2 %3 %4) bounds)
|
rlm@97
|
562 (let [cutout (.crop (.getProcessor image))
|
rlm@97
|
563 ;; extract white pixels inside triangle
|
rlm@97
|
564 cutout-tri
|
rlm@97
|
565 (map-triangle
|
rlm@97
|
566 (fn [_ v]
|
rlm@97
|
567 (.subtract
|
rlm@97
|
568 v
|
rlm@97
|
569 (Vector3f. (bounds 0) (bounds 1) (float 0))))
|
rlm@97
|
570 uv-tri)
|
rlm@97
|
571 whites (filter (partial inside-triangle? cutout-tri)
|
rlm@97
|
572 (map vector2f->vector3f
|
rlm@97
|
573 (white-coordinates cutout)))
|
rlm@97
|
574 ;; translate pixel coordinates to world-space
|
rlm@97
|
575 transform (triangle-transformation cutout-tri tri)]
|
rlm@97
|
576 (map #(.mult transform %) whites))))]
|
rlm@97
|
577 (vec (map sensor-coords tris)))
|
rlm@97
|
578 (repeat (count (triangles geo)) [])))
|
rlm@97
|
579
|
rlm@97
|
580 (defn enable-touch [#^Geometry geo]
|
rlm@97
|
581 (let [feeler-coords (locate-feelers geo)
|
rlm@96
|
582 tris (triangles geo)
|
rlm@97
|
583 limit 0.1]
|
rlm@97
|
584 (fn [node]
|
rlm@97
|
585 (let [sensor-origins
|
rlm@97
|
586 (map
|
rlm@97
|
587 #(map (partial local-to-world geo) %)
|
rlm@97
|
588 feeler-coords)
|
rlm@97
|
589 triangle-normals
|
rlm@97
|
590 (map (partial get-ray-direction geo)
|
rlm@97
|
591 tris)
|
rlm@97
|
592 rays
|
rlm@97
|
593 (flatten
|
rlm@97
|
594 (map (fn [origins norm]
|
rlm@97
|
595 (map #(doto (Ray. % norm)
|
rlm@97
|
596 (.setLimit limit)) origins))
|
rlm@97
|
597 sensor-origins triangle-normals))]
|
rlm@97
|
598 (for [ray rays]
|
rlm@97
|
599 (do
|
rlm@97
|
600 (let [results (CollisionResults.)]
|
rlm@97
|
601 (.collideWith node ray results)
|
rlm@97
|
602 (let [touch-objects
|
rlm@97
|
603 (set
|
rlm@97
|
604 (filter #(not (= geo %))
|
rlm@97
|
605 (map #(.getGeometry %) results)))]
|
rlm@97
|
606 (count touch-objects)))))))))
|
rlm@94
|
607
|
rlm@97
|
608 (defn touch [#^Node pieces]
|
rlm@97
|
609 (let [touch-components
|
rlm@97
|
610 (map enable-touch
|
rlm@97
|
611 (filter #(isa? (class %) Geometry)
|
rlm@97
|
612 (node-seq pieces)))]
|
rlm@97
|
613 (fn [node]
|
rlm@97
|
614 (reduce into [] (map #(% node) touch-components)))))
|
rlm@96
|
615
|
rlm@97
|
616
|
rlm@91
|
617
|
rlm@91
|
618
|
rlm@91
|
619
|
rlm@91
|
620
|
rlm@91
|
621 (defn all-names []
|
rlm@91
|
622 (concat
|
rlm@91
|
623 (re-split #"\n" (slurp (file-str
|
rlm@91
|
624 "/home/r/proj/names/dist.female.first")))
|
rlm@91
|
625 (re-split #"\n" (slurp (file-str
|
rlm@91
|
626 "/home/r/proj/names/dist.male.first")))
|
rlm@91
|
627 (re-split #"\n" (slurp (file-str
|
rlm@91
|
628 "/home/r/proj/names/dist.all.last")))))
|
rlm@90
|
629
|
rlm@90
|
630
|
rlm@90
|
631
|
rlm@90
|
632
|
rlm@90
|
633
|
rlm@90
|
634
|
rlm@90
|
635
|
rlm@90
|
636
|
rlm@90
|
637
|
rlm@90
|
638 (defrecord LulzLoader [])
|
rlm@90
|
639 (defprotocol Lulzable (load-lulz [this]))
|
rlm@90
|
640 (extend-type LulzLoader
|
rlm@90
|
641 Lulzable
|
rlm@90
|
642 (load-lulz [this] (println "the lulz have arrived!")))
|
rlm@90
|
643
|
rlm@78
|
644
|
rlm@78
|
645 (defn world-setup [joint]
|
rlm@90
|
646 (let [joint-position (Vector3f. 0 0 0)
|
rlm@83
|
647 joint-rotation
|
rlm@83
|
648 (.toRotationMatrix
|
rlm@83
|
649 (.mult
|
rlm@83
|
650 (doto (Quaternion.)
|
rlm@83
|
651 (.fromAngleAxis
|
rlm@83
|
652 (* 1 (/ Math/PI 4))
|
rlm@85
|
653 (Vector3f. -1 0 0)))
|
rlm@85
|
654 (doto (Quaternion.)
|
rlm@85
|
655 (.fromAngleAxis
|
rlm@85
|
656 (* 1 (/ Math/PI 2))
|
rlm@85
|
657 (Vector3f. 0 0 1)))))
|
rlm@84
|
658 top-position (.mult joint-rotation (Vector3f. 8 0 0))
|
rlm@83
|
659
|
rlm@83
|
660 origin (doto
|
rlm@83
|
661 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
|
rlm@84
|
662 :position top-position))
|
rlm@83
|
663 top (doto
|
rlm@78
|
664 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@84
|
665 :position top-position)
|
rlm@83
|
666
|
rlm@78
|
667 (.addControl
|
rlm@78
|
668 (RigidBodyControl.
|
rlm@83
|
669 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
|
rlm@78
|
670 bottom (doto
|
rlm@78
|
671 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@83
|
672 :position (Vector3f. 0 0 0))
|
rlm@83
|
673 (.addControl
|
rlm@78
|
674 (RigidBodyControl.
|
rlm@78
|
675 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@83
|
676 table (box 10 2 10 :position (Vector3f. 0 -20 0)
|
rlm@78
|
677 :color ColorRGBA/Gray :mass 0)
|
rlm@78
|
678 a (.getControl top RigidBodyControl)
|
rlm@78
|
679 b (.getControl bottom RigidBodyControl)]
|
rlm@83
|
680
|
rlm@78
|
681 (cond
|
rlm@83
|
682 (= joint :cone)
|
rlm@83
|
683
|
rlm@83
|
684 (doto (ConeJoint.
|
rlm@83
|
685 a b
|
rlm@87
|
686 (world-to-local top joint-position)
|
rlm@87
|
687 (world-to-local bottom joint-position)
|
rlm@83
|
688 joint-rotation
|
rlm@83
|
689 joint-rotation
|
rlm@83
|
690 )
|
rlm@78
|
691
|
rlm@83
|
692
|
rlm@83
|
693 (.setLimit (* (/ 10) Math/PI)
|
rlm@83
|
694 (* (/ 4) Math/PI)
|
rlm@83
|
695 0)))
|
rlm@83
|
696 [origin top bottom table]))
|
rlm@78
|
697
|
rlm@78
|
698 (defn test-joint [joint]
|
rlm@83
|
699 (let [[origin top bottom floor] (world-setup joint)
|
rlm@78
|
700 control (.getControl top RigidBodyControl)
|
rlm@78
|
701 move-up? (atom false)
|
rlm@78
|
702 move-down? (atom false)
|
rlm@78
|
703 move-left? (atom false)
|
rlm@78
|
704 move-right? (atom false)
|
rlm@78
|
705 roll-left? (atom false)
|
rlm@78
|
706 roll-right? (atom false)
|
rlm@78
|
707 timer (atom 0)]
|
rlm@78
|
708
|
rlm@78
|
709 (world
|
rlm@83
|
710 (nodify [top bottom floor origin])
|
rlm@78
|
711 (merge standard-debug-controls
|
rlm@78
|
712 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@78
|
713 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@78
|
714 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@78
|
715 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@78
|
716 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@78
|
717 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@78
|
718
|
rlm@78
|
719 (fn [world]
|
rlm@78
|
720 (light-up-everything world)
|
rlm@78
|
721 (enable-debug world)
|
rlm@78
|
722 (set-gravity world (Vector3f. 0 0 0))
|
rlm@78
|
723 )
|
rlm@78
|
724
|
rlm@78
|
725 (fn [world _]
|
rlm@78
|
726 (if (zero? (rem (swap! timer inc) 100))
|
rlm@78
|
727 (do
|
rlm@78
|
728 ;; (println-repl @timer)
|
rlm@78
|
729 (.attachChild (.getRootNode world)
|
rlm@78
|
730 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@78
|
731 :position (.getWorldTranslation top)
|
rlm@83
|
732 :physical? false))
|
rlm@83
|
733 (.attachChild (.getRootNode world)
|
rlm@83
|
734 (sphere 0.05 :color ColorRGBA/LightGray
|
rlm@83
|
735 :position (.getWorldTranslation bottom)
|
rlm@83
|
736 :physical? false))))
|
rlm@83
|
737
|
rlm@78
|
738 (if @move-up?
|
rlm@78
|
739 (.applyTorque control
|
rlm@78
|
740 (.mult (.getPhysicsRotation control)
|
rlm@78
|
741 (Vector3f. 0 0 10))))
|
rlm@78
|
742 (if @move-down?
|
rlm@78
|
743 (.applyTorque control
|
rlm@78
|
744 (.mult (.getPhysicsRotation control)
|
rlm@78
|
745 (Vector3f. 0 0 -10))))
|
rlm@78
|
746 (if @move-left?
|
rlm@78
|
747 (.applyTorque control
|
rlm@78
|
748 (.mult (.getPhysicsRotation control)
|
rlm@78
|
749 (Vector3f. 0 10 0))))
|
rlm@78
|
750 (if @move-right?
|
rlm@78
|
751 (.applyTorque control
|
rlm@78
|
752 (.mult (.getPhysicsRotation control)
|
rlm@78
|
753 (Vector3f. 0 -10 0))))
|
rlm@78
|
754 (if @roll-left?
|
rlm@78
|
755 (.applyTorque control
|
rlm@78
|
756 (.mult (.getPhysicsRotation control)
|
rlm@78
|
757 (Vector3f. -1 0 0))))
|
rlm@78
|
758 (if @roll-right?
|
rlm@78
|
759 (.applyTorque control
|
rlm@78
|
760 (.mult (.getPhysicsRotation control)
|
rlm@78
|
761 (Vector3f. 1 0 0))))))))
|
rlm@83
|
762
|
rlm@83
|
763
|
rlm@97
|
764 (defn locate-feelers*
|
rlm@97
|
765 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@97
|
766 their positions in geometry-relative coordinates."
|
rlm@97
|
767 [#^Geometry geo]
|
rlm@97
|
768 (let [uv-image (touch-receptor-image geo)
|
rlm@97
|
769 width (.getWidth uv-image)
|
rlm@97
|
770 height (.getHeight uv-image)
|
rlm@97
|
771
|
rlm@97
|
772 mesh (.getMesh geo)
|
rlm@97
|
773 mesh-tris (triangles geo)
|
rlm@97
|
774
|
rlm@97
|
775 ;; for each triangle
|
rlm@97
|
776 sensor-coords
|
rlm@97
|
777 (fn [tri]
|
rlm@97
|
778 ;; translate triangle to uv-pixel-space
|
rlm@97
|
779 (let [uv-tri
|
rlm@97
|
780 (rasterize mesh tri width height)
|
rlm@97
|
781 bounds (vec (triangle-bounds uv-tri))]
|
rlm@97
|
782
|
rlm@97
|
783 ;; get that part of the picture
|
rlm@97
|
784
|
rlm@97
|
785 (apply (partial (memfn setRoi) uv-image) bounds)
|
rlm@97
|
786 (let [cutout (.crop (.getProcessor uv-image))
|
rlm@97
|
787 ;; extract white pixels inside triangle
|
rlm@97
|
788 cutout-tri
|
rlm@97
|
789 (map-triangle
|
rlm@97
|
790 (fn [_ v]
|
rlm@97
|
791 (.subtract
|
rlm@97
|
792 v
|
rlm@97
|
793 (Vector3f. (bounds 0) (bounds 1) (float 0))))
|
rlm@97
|
794 uv-tri)
|
rlm@97
|
795 whites (filter (partial inside-triangle? cutout-tri)
|
rlm@97
|
796 (map vector2f->vector3f
|
rlm@97
|
797 (white-coordinates cutout)))
|
rlm@97
|
798 ;; translate pixel coordinates to world-space
|
rlm@97
|
799 transform (triangle-transformation cutout-tri tri)]
|
rlm@97
|
800 (map #(.mult transform %) whites))))]
|
rlm@97
|
801
|
rlm@97
|
802
|
rlm@97
|
803
|
rlm@97
|
804 (for [mesh-tri mesh-tris]
|
rlm@97
|
805
|
rlm@97
|
806 (let [uv-tri (rasterize mesh mesh-tri width height)
|
rlm@97
|
807 bounding-box (vec (triangle-bounds uv-tri))]
|
rlm@97
|
808 (apply (partial (memfn setRoi) uv-image) bounding-box)
|
rlm@97
|
809 ))
|
rlm@97
|
810 (vec (map sensor-coords mesh-tris))))
|
rlm@97
|
811
|
rlm@97
|
812
|
rlm@97
|
813 (defn tactile-coords [#^Geometry obj]
|
rlm@97
|
814 (let [mesh (.getMesh obj)
|
rlm@97
|
815 num-triangles (.getTriangleCount mesh)
|
rlm@97
|
816 num-verticies (.getVertexCount mesh)
|
rlm@97
|
817 uv-coord (partial uv-coord mesh)
|
rlm@97
|
818 triangle-indices (partial triangle-indices mesh)
|
rlm@97
|
819 receptors (touch-receptor-image obj)
|
rlm@97
|
820 tris (triangles obj)
|
rlm@97
|
821 ]
|
rlm@97
|
822 (map
|
rlm@97
|
823 (fn [[tri-1 tri-2 tri-3]]
|
rlm@97
|
824 (let [width (.getWidth receptors)
|
rlm@97
|
825 height (.getHeight receptors)
|
rlm@97
|
826 uv-1 (uv-coord tri-1)
|
rlm@97
|
827 uv-2 (uv-coord tri-2)
|
rlm@97
|
828 uv-3 (uv-coord tri-3)
|
rlm@97
|
829 x-coords (map #(.getX %) [uv-1 uv-2 uv-3])
|
rlm@97
|
830 y-coords (map #(.getY %) [uv-1 uv-2 uv-3])
|
rlm@97
|
831 max-x (Math/ceil (* width (apply max x-coords)))
|
rlm@97
|
832 min-x (Math/floor (* width (apply min x-coords)))
|
rlm@97
|
833 max-y (Math/ceil (* height (apply max y-coords)))
|
rlm@97
|
834 min-y (Math/floor (* height (apply min y-coords)))
|
rlm@97
|
835
|
rlm@97
|
836 image-1 (Vector2f. (* width (.getX uv-1))
|
rlm@97
|
837 (* height (.getY uv-1)))
|
rlm@97
|
838 image-2 (Vector2f. (* width (.getX uv-2))
|
rlm@97
|
839 (* height (.getY uv-2)))
|
rlm@97
|
840 image-3 (Vector2f. (* width (.getX uv-3))
|
rlm@97
|
841 (* height (.getY uv-3)))
|
rlm@97
|
842 left-corner
|
rlm@97
|
843 (Vector2f. min-x min-y)
|
rlm@97
|
844 ]
|
rlm@97
|
845
|
rlm@97
|
846 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
|
rlm@97
|
847 (let [processor (.crop (.getProcessor receptors))]
|
rlm@97
|
848 (map
|
rlm@97
|
849 #(.add left-corner %)
|
rlm@97
|
850
|
rlm@97
|
851 (filter
|
rlm@97
|
852 (partial
|
rlm@97
|
853 inside-triangle?
|
rlm@97
|
854 (.subtract image-1 left-corner)
|
rlm@97
|
855 (.subtract image-2 left-corner)
|
rlm@97
|
856 (.subtract image-3 left-corner))
|
rlm@97
|
857 (white-coordinates processor))))
|
rlm@97
|
858 )) (map triangle-indices (range num-triangles)))))
|
rlm@83
|
859
|
rlm@87
|
860 #+end_src
|
rlm@83
|
861
|
rlm@87
|
862 #+results: body-1
|
rlm@87
|
863 : #'cortex.silly/test-joint
|
rlm@78
|
864
|
rlm@78
|
865
|
rlm@78
|
866 * COMMENT purgatory
|
rlm@78
|
867 #+begin_src clojure
|
rlm@77
|
868 (defn bullet-trans []
|
rlm@77
|
869 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
870 :position (Vector3f. -10 5 0))
|
rlm@77
|
871 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
872 :position (Vector3f. -10 -5 0)
|
rlm@77
|
873 :mass 0)
|
rlm@77
|
874 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
875 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
876 swivel
|
rlm@77
|
877 (.toRotationMatrix
|
rlm@77
|
878 (doto (Quaternion.)
|
rlm@77
|
879 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
880 Vector3f/UNIT_X)))]
|
rlm@77
|
881 (doto
|
rlm@77
|
882 (ConeJoint.
|
rlm@77
|
883 control-a control-b
|
rlm@77
|
884 (Vector3f. 0 5 0)
|
rlm@77
|
885 (Vector3f. 0 -5 0)
|
rlm@77
|
886 swivel swivel)
|
rlm@77
|
887 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
888 (/ Math/PI 4)
|
rlm@77
|
889 (* Math/PI 0.8)))
|
rlm@77
|
890 (world (nodify
|
rlm@77
|
891 [obj-a obj-b])
|
rlm@77
|
892 standard-debug-controls
|
rlm@77
|
893 enable-debug
|
rlm@77
|
894 no-op)))
|
rlm@74
|
895
|
rlm@74
|
896
|
rlm@77
|
897 (defn bullet-trans* []
|
rlm@77
|
898 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
899 :position (Vector3f. 5 0 0)
|
rlm@77
|
900 :mass 90)
|
rlm@77
|
901 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
902 :position (Vector3f. -5 0 0)
|
rlm@77
|
903 :mass 0)
|
rlm@77
|
904 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
905 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
906 move-up? (atom nil)
|
rlm@77
|
907 move-down? (atom nil)
|
rlm@77
|
908 move-left? (atom nil)
|
rlm@77
|
909 move-right? (atom nil)
|
rlm@77
|
910 roll-left? (atom nil)
|
rlm@77
|
911 roll-right? (atom nil)
|
rlm@77
|
912 force 100
|
rlm@77
|
913 swivel
|
rlm@77
|
914 (.toRotationMatrix
|
rlm@77
|
915 (doto (Quaternion.)
|
rlm@77
|
916 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
917 Vector3f/UNIT_X)))
|
rlm@77
|
918 x-move
|
rlm@77
|
919 (doto (Matrix3f.)
|
rlm@77
|
920 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
921 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
922
|
rlm@77
|
923 timer (atom 0)]
|
rlm@77
|
924 (doto
|
rlm@77
|
925 (ConeJoint.
|
rlm@77
|
926 control-a control-b
|
rlm@77
|
927 (Vector3f. -8 0 0)
|
rlm@77
|
928 (Vector3f. 2 0 0)
|
rlm@77
|
929 ;;swivel swivel
|
rlm@77
|
930 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
931 x-move Matrix3f/IDENTITY
|
rlm@77
|
932 )
|
rlm@77
|
933 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
934 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
935 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
936 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
937 (world (nodify
|
rlm@77
|
938 [obj-a obj-b])
|
rlm@77
|
939 (merge standard-debug-controls
|
rlm@77
|
940 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
941 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
942 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
943 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
944 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
945 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
946
|
rlm@77
|
947 (fn [world]
|
rlm@77
|
948 (enable-debug world)
|
rlm@77
|
949 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
950 )
|
rlm@77
|
951
|
rlm@77
|
952 (fn [world _]
|
rlm@77
|
953
|
rlm@77
|
954 (if @move-up?
|
rlm@77
|
955 (.applyForce control-a
|
rlm@77
|
956 (Vector3f. force 0 0)
|
rlm@77
|
957 (Vector3f. 0 0 0)))
|
rlm@77
|
958 (if @move-down?
|
rlm@77
|
959 (.applyForce control-a
|
rlm@77
|
960 (Vector3f. (- force) 0 0)
|
rlm@77
|
961 (Vector3f. 0 0 0)))
|
rlm@77
|
962 (if @move-left?
|
rlm@77
|
963 (.applyForce control-a
|
rlm@77
|
964 (Vector3f. 0 force 0)
|
rlm@77
|
965 (Vector3f. 0 0 0)))
|
rlm@77
|
966 (if @move-right?
|
rlm@77
|
967 (.applyForce control-a
|
rlm@77
|
968 (Vector3f. 0 (- force) 0)
|
rlm@77
|
969 (Vector3f. 0 0 0)))
|
rlm@77
|
970
|
rlm@77
|
971 (if @roll-left?
|
rlm@77
|
972 (.applyForce control-a
|
rlm@77
|
973 (Vector3f. 0 0 force)
|
rlm@77
|
974 (Vector3f. 0 0 0)))
|
rlm@77
|
975 (if @roll-right?
|
rlm@77
|
976 (.applyForce control-a
|
rlm@77
|
977 (Vector3f. 0 0 (- force))
|
rlm@77
|
978 (Vector3f. 0 0 0)))
|
rlm@77
|
979
|
rlm@77
|
980 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
981 (.attachChild
|
rlm@77
|
982 (.getRootNode world)
|
rlm@77
|
983 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
984 :physical? false :position
|
rlm@77
|
985 (.getWorldTranslation obj-a)))))
|
rlm@77
|
986 )
|
rlm@77
|
987 ))
|
rlm@77
|
988
|
rlm@77
|
989
|
rlm@77
|
990
|
rlm@73
|
991 #+end_src
|
rlm@73
|
992
|
rlm@73
|
993
|
rlm@73
|
994 * COMMENT generate source
|
rlm@73
|
995 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@73
|
996 <<body-1>>
|
rlm@73
|
997 #+end_src
|
rlm@73
|
998
|
rlm@94
|
999
|
rlm@94
|
1000
|
rlm@94
|
1001
|
rlm@94
|
1002
|
rlm@94
|
1003 (defn transform-trianglesdsd
|
rlm@94
|
1004 "Transform that converts each vertex in the first triangle
|
rlm@94
|
1005 into the corresponding vertex in the second triangle."
|
rlm@94
|
1006 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
1007 (let [in [(.get1 tri-1)
|
rlm@94
|
1008 (.get2 tri-1)
|
rlm@94
|
1009 (.get3 tri-1)]
|
rlm@94
|
1010 out [(.get1 tri-2)
|
rlm@94
|
1011 (.get2 tri-2)
|
rlm@94
|
1012 (.get3 tri-2)]]
|
rlm@94
|
1013 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
|
rlm@94
|
1014 in* [(.mult translate (in 0))
|
rlm@94
|
1015 (.mult translate (in 1))
|
rlm@94
|
1016 (.mult translate (in 2))]
|
rlm@94
|
1017 final-translation
|
rlm@94
|
1018 (doto (Matrix4f.)
|
rlm@94
|
1019 (.setTranslation (out 1)))
|
rlm@94
|
1020
|
rlm@94
|
1021 rotate-1
|
rlm@94
|
1022 (doto (Matrix3f.)
|
rlm@94
|
1023 (.fromStartEndVectors
|
rlm@94
|
1024 (.normalize
|
rlm@94
|
1025 (.subtract
|
rlm@94
|
1026 (in* 1) (in* 0)))
|
rlm@94
|
1027 (.normalize
|
rlm@94
|
1028 (.subtract
|
rlm@94
|
1029 (out 1) (out 0)))))
|
rlm@94
|
1030 in** [(.mult rotate-1 (in* 0))
|
rlm@94
|
1031 (.mult rotate-1 (in* 1))
|
rlm@94
|
1032 (.mult rotate-1 (in* 2))]
|
rlm@94
|
1033 scale-factor-1
|
rlm@94
|
1034 (.mult
|
rlm@94
|
1035 (.normalize
|
rlm@94
|
1036 (.subtract
|
rlm@94
|
1037 (out 1)
|
rlm@94
|
1038 (out 0)))
|
rlm@94
|
1039 (/ (.length
|
rlm@94
|
1040 (.subtract (out 1)
|
rlm@94
|
1041 (out 0)))
|
rlm@94
|
1042 (.length
|
rlm@94
|
1043 (.subtract (in** 1)
|
rlm@94
|
1044 (in** 0)))))
|
rlm@94
|
1045 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
|
rlm@94
|
1046 in*** [(.mult scale-1 (in** 0))
|
rlm@94
|
1047 (.mult scale-1 (in** 1))
|
rlm@94
|
1048 (.mult scale-1 (in** 2))]
|
rlm@94
|
1049
|
rlm@94
|
1050
|
rlm@94
|
1051
|
rlm@94
|
1052
|
rlm@94
|
1053
|
rlm@94
|
1054 ]
|
rlm@94
|
1055
|
rlm@94
|
1056 (dorun (map println in))
|
rlm@94
|
1057 (println)
|
rlm@94
|
1058 (dorun (map println in*))
|
rlm@94
|
1059 (println)
|
rlm@94
|
1060 (dorun (map println in**))
|
rlm@94
|
1061 (println)
|
rlm@94
|
1062 (dorun (map println in***))
|
rlm@94
|
1063 (println)
|
rlm@94
|
1064
|
rlm@94
|
1065 )))
|
rlm@94
|
1066
|
rlm@94
|
1067
|
rlm@94
|
1068
|
rlm@94
|
1069
|
rlm@94
|
1070
|
rlm@94
|
1071
|
rlm@94
|
1072
|
rlm@94
|
1073
|
rlm@94
|
1074
|
rlm@94
|
1075
|
rlm@94
|
1076
|
rlm@94
|
1077
|
rlm@94
|
1078
|
rlm@94
|
1079
|
rlm@94
|
1080
|
rlm@94
|
1081
|
rlm@94
|
1082
|
rlm@94
|
1083
|
rlm@94
|
1084 )
|
rlm@94
|
1085
|
rlm@94
|
1086
|