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@99
|
9 * objectives
|
rlm@103
|
10 - [X] get an overall bitmap-like image for touch
|
rlm@103
|
11 - [X] write code to visuliaze this bitmap
|
rlm@99
|
12 - [ ] directly change the UV-pixels to show touch sensor activation
|
rlm@99
|
13 - [ ] write an explination for why b&w bitmaps for senses is appropiate
|
rlm@99
|
14 - [ ] clean up touch code and write visulazation test
|
rlm@99
|
15 - [ ] do the same for eyes
|
rlm@99
|
16
|
rlm@73
|
17 * Intro
|
rlm@73
|
18 So far, I've made the following senses --
|
rlm@73
|
19 - Vision
|
rlm@73
|
20 - Hearing
|
rlm@73
|
21 - Touch
|
rlm@73
|
22 - Proprioception
|
rlm@73
|
23
|
rlm@73
|
24 And one effector:
|
rlm@73
|
25 - Movement
|
rlm@73
|
26
|
rlm@73
|
27 However, the code so far has only enabled these senses, but has not
|
rlm@73
|
28 actually implemented them. For example, there is still a lot of work
|
rlm@73
|
29 to be done for vision. I need to be able to create an /eyeball/ in
|
rlm@73
|
30 simulation that can be moved around and see the world from different
|
rlm@73
|
31 angles. I also need to determine weather to use log-polar or cartesian
|
rlm@73
|
32 for the visual input, and I need to determine how/wether to
|
rlm@73
|
33 disceritise the visual input.
|
rlm@73
|
34
|
rlm@73
|
35 I also want to be able to visualize both the sensors and the
|
rlm@104
|
36 effectors in pretty pictures. This semi-retarted creature will be my
|
rlm@73
|
37 first attempt at bringing everything together.
|
rlm@73
|
38
|
rlm@73
|
39 * The creature's body
|
rlm@73
|
40
|
rlm@73
|
41 Still going to do an eve-like body in blender, but due to problems
|
rlm@104
|
42 importing the joints, etc into jMonkeyEngine3, I'm going to do all
|
rlm@73
|
43 the connecting here in clojure code, using the names of the individual
|
rlm@73
|
44 components and trial and error. Later, I'll maybe make some sort of
|
rlm@73
|
45 creature-building modifications to blender that support whatever
|
rlm@73
|
46 discreitized senses I'm going to make.
|
rlm@73
|
47
|
rlm@73
|
48 #+name: body-1
|
rlm@73
|
49 #+begin_src clojure
|
rlm@73
|
50 (ns cortex.silly
|
rlm@73
|
51 "let's play!"
|
rlm@73
|
52 {:author "Robert McIntyre"})
|
rlm@73
|
53
|
rlm@73
|
54 ;; TODO remove this!
|
rlm@73
|
55 (require 'cortex.import)
|
rlm@73
|
56 (cortex.import/mega-import-jme3)
|
rlm@73
|
57 (use '(cortex world util body hearing touch vision))
|
rlm@73
|
58
|
rlm@73
|
59 (rlm.rlm-commands/help)
|
rlm@99
|
60 (import java.awt.image.BufferedImage)
|
rlm@99
|
61 (import javax.swing.JPanel)
|
rlm@99
|
62 (import javax.swing.SwingUtilities)
|
rlm@99
|
63 (import java.awt.Dimension)
|
rlm@99
|
64 (import javax.swing.JFrame)
|
rlm@99
|
65 (import java.awt.Dimension)
|
rlm@106
|
66 (import com.aurellem.capture.RatchetTimer)
|
rlm@99
|
67 (declare joint-create)
|
rlm@108
|
68 (use 'clojure.contrib.def)
|
rlm@73
|
69
|
rlm@100
|
70 (defn points->image
|
rlm@100
|
71 "Take a sparse collection of points and visuliaze it as a
|
rlm@100
|
72 BufferedImage."
|
rlm@102
|
73
|
rlm@102
|
74 ;; TODO maybe parallelize this since it's easy
|
rlm@102
|
75
|
rlm@100
|
76 [points]
|
rlm@106
|
77 (if (empty? points)
|
rlm@106
|
78 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
|
rlm@106
|
79 (let [xs (vec (map first points))
|
rlm@106
|
80 ys (vec (map second points))
|
rlm@106
|
81 x0 (apply min xs)
|
rlm@106
|
82 y0 (apply min ys)
|
rlm@106
|
83 width (- (apply max xs) x0)
|
rlm@106
|
84 height (- (apply max ys) y0)
|
rlm@106
|
85 image (BufferedImage. (inc width) (inc height)
|
rlm@106
|
86 BufferedImage/TYPE_BYTE_BINARY)]
|
rlm@106
|
87 (dorun
|
rlm@106
|
88 (for [index (range (count points))]
|
rlm@106
|
89 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
|
rlm@106
|
90
|
rlm@106
|
91 image)))
|
rlm@100
|
92
|
rlm@101
|
93 (defn average [coll]
|
rlm@101
|
94 (/ (reduce + coll) (count coll)))
|
rlm@101
|
95
|
rlm@101
|
96 (defn collapse-1d
|
rlm@101
|
97 "One dimensional analogue of collapse"
|
rlm@101
|
98 [center line]
|
rlm@101
|
99 (let [length (count line)
|
rlm@101
|
100 num-above (count (filter (partial < center) line))
|
rlm@101
|
101 num-below (- length num-above)]
|
rlm@101
|
102 (range (- center num-below)
|
rlm@115
|
103 (+ center num-above))))
|
rlm@99
|
104
|
rlm@99
|
105 (defn collapse
|
rlm@99
|
106 "Take a set of pairs of integers and collapse them into a
|
rlm@99
|
107 contigous bitmap."
|
rlm@99
|
108 [points]
|
rlm@108
|
109 (if (empty? points) []
|
rlm@108
|
110 (let
|
rlm@108
|
111 [num-points (count points)
|
rlm@108
|
112 center (vector
|
rlm@108
|
113 (int (average (map first points)))
|
rlm@108
|
114 (int (average (map first points))))
|
rlm@108
|
115 flattened
|
rlm@108
|
116 (reduce
|
rlm@108
|
117 concat
|
rlm@108
|
118 (map
|
rlm@108
|
119 (fn [column]
|
rlm@108
|
120 (map vector
|
rlm@108
|
121 (map first column)
|
rlm@108
|
122 (collapse-1d (second center)
|
rlm@108
|
123 (map second column))))
|
rlm@108
|
124 (partition-by first (sort-by first points))))
|
rlm@108
|
125 squeezed
|
rlm@108
|
126 (reduce
|
rlm@108
|
127 concat
|
rlm@108
|
128 (map
|
rlm@108
|
129 (fn [row]
|
rlm@108
|
130 (map vector
|
rlm@108
|
131 (collapse-1d (first center)
|
rlm@108
|
132 (map first row))
|
rlm@108
|
133 (map second row)))
|
rlm@108
|
134 (partition-by second (sort-by second flattened))))
|
rlm@108
|
135 relocate
|
rlm@108
|
136 (let [min-x (apply min (map first squeezed))
|
rlm@108
|
137 min-y (apply min (map second squeezed))]
|
rlm@108
|
138 (map (fn [[x y]]
|
rlm@108
|
139 [(- x min-x)
|
rlm@108
|
140 (- y min-y)])
|
rlm@108
|
141 squeezed))]
|
rlm@115
|
142 relocate)))
|
rlm@83
|
143
|
rlm@83
|
144 (defn load-bullet []
|
rlm@84
|
145 (let [sim (world (Node.) {} no-op no-op)]
|
rlm@102
|
146 (doto sim
|
rlm@102
|
147 (.enqueue
|
rlm@102
|
148 (fn []
|
rlm@102
|
149 (.stop sim)))
|
rlm@102
|
150 (.start))))
|
rlm@83
|
151
|
rlm@73
|
152 (defn load-blender-model
|
rlm@73
|
153 "Load a .blend file using an asset folder relative path."
|
rlm@73
|
154 [^String model]
|
rlm@73
|
155 (.loadModel
|
rlm@73
|
156 (doto (asset-manager)
|
rlm@73
|
157 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
|
rlm@73
|
158 model))
|
rlm@73
|
159
|
rlm@74
|
160 (defn meta-data [blender-node key]
|
rlm@74
|
161 (if-let [data (.getUserData blender-node "properties")]
|
rlm@74
|
162 (.findValue data key)
|
rlm@74
|
163 nil))
|
rlm@73
|
164
|
rlm@78
|
165 (defn blender-to-jme
|
rlm@78
|
166 "Convert from Blender coordinates to JME coordinates"
|
rlm@78
|
167 [#^Vector3f in]
|
rlm@78
|
168 (Vector3f. (.getX in)
|
rlm@78
|
169 (.getZ in)
|
rlm@78
|
170 (- (.getY in))))
|
rlm@74
|
171
|
rlm@79
|
172 (defn jme-to-blender
|
rlm@79
|
173 "Convert from JME coordinates to Blender coordinates"
|
rlm@79
|
174 [#^Vector3f in]
|
rlm@79
|
175 (Vector3f. (.getX in)
|
rlm@79
|
176 (- (.getZ in))
|
rlm@79
|
177 (.getY in)))
|
rlm@79
|
178
|
rlm@78
|
179 (defn joint-targets
|
rlm@78
|
180 "Return the two closest two objects to the joint object, ordered
|
rlm@78
|
181 from bottom to top according to the joint's rotation."
|
rlm@78
|
182 [#^Node parts #^Node joint]
|
rlm@78
|
183 (loop [radius (float 0.01)]
|
rlm@78
|
184 (let [results (CollisionResults.)]
|
rlm@78
|
185 (.collideWith
|
rlm@78
|
186 parts
|
rlm@78
|
187 (BoundingBox. (.getWorldTranslation joint)
|
rlm@78
|
188 radius radius radius)
|
rlm@78
|
189 results)
|
rlm@78
|
190 (let [targets
|
rlm@78
|
191 (distinct
|
rlm@78
|
192 (map #(.getGeometry %) results))]
|
rlm@78
|
193 (if (>= (count targets) 2)
|
rlm@78
|
194 (sort-by
|
rlm@79
|
195 #(let [v
|
rlm@79
|
196 (jme-to-blender
|
rlm@79
|
197 (.mult
|
rlm@79
|
198 (.inverse (.getWorldRotation joint))
|
rlm@79
|
199 (.subtract (.getWorldTranslation %)
|
rlm@79
|
200 (.getWorldTranslation joint))))]
|
rlm@79
|
201 (println-repl (.getName %) ":" v)
|
rlm@79
|
202 (.dot (Vector3f. 1 1 1)
|
rlm@79
|
203 v))
|
rlm@78
|
204 (take 2 targets))
|
rlm@78
|
205 (recur (float (* radius 2))))))))
|
rlm@74
|
206
|
rlm@87
|
207 (defn world-to-local
|
rlm@87
|
208 "Convert the world coordinates into coordinates relative to the
|
rlm@87
|
209 object (i.e. local coordinates), taking into account the rotation
|
rlm@87
|
210 of object."
|
rlm@87
|
211 [#^Spatial object world-coordinate]
|
rlm@87
|
212 (let [out (Vector3f.)]
|
rlm@88
|
213 (.worldToLocal object world-coordinate out) out))
|
rlm@87
|
214
|
rlm@96
|
215 (defn local-to-world
|
rlm@96
|
216 "Convert the local coordinates into coordinates into world relative
|
rlm@96
|
217 coordinates"
|
rlm@96
|
218 [#^Spatial object local-coordinate]
|
rlm@96
|
219 (let [world-coordinate (Vector3f.)]
|
rlm@96
|
220 (.localToWorld object local-coordinate world-coordinate)
|
rlm@96
|
221 world-coordinate))
|
rlm@96
|
222
|
rlm@87
|
223 (defmulti joint-dispatch
|
rlm@87
|
224 "Translate blender pseudo-joints into real JME joints."
|
rlm@88
|
225 (fn [constraints & _]
|
rlm@87
|
226 (:type constraints)))
|
rlm@87
|
227
|
rlm@87
|
228 (defmethod joint-dispatch :point
|
rlm@87
|
229 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
230 (println-repl "creating POINT2POINT joint")
|
rlm@87
|
231 (Point2PointJoint.
|
rlm@87
|
232 control-a
|
rlm@87
|
233 control-b
|
rlm@87
|
234 pivot-a
|
rlm@87
|
235 pivot-b))
|
rlm@87
|
236
|
rlm@87
|
237 (defmethod joint-dispatch :hinge
|
rlm@87
|
238 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
239 (println-repl "creating HINGE joint")
|
rlm@87
|
240 (let [axis
|
rlm@87
|
241 (if-let
|
rlm@87
|
242 [axis (:axis constraints)]
|
rlm@87
|
243 axis
|
rlm@87
|
244 Vector3f/UNIT_X)
|
rlm@87
|
245 [limit-1 limit-2] (:limit constraints)
|
rlm@87
|
246 hinge-axis
|
rlm@87
|
247 (.mult
|
rlm@87
|
248 rotation
|
rlm@87
|
249 (blender-to-jme axis))]
|
rlm@87
|
250 (doto
|
rlm@87
|
251 (HingeJoint.
|
rlm@87
|
252 control-a
|
rlm@87
|
253 control-b
|
rlm@87
|
254 pivot-a
|
rlm@87
|
255 pivot-b
|
rlm@87
|
256 hinge-axis
|
rlm@87
|
257 hinge-axis)
|
rlm@87
|
258 (.setLimit limit-1 limit-2))))
|
rlm@87
|
259
|
rlm@87
|
260 (defmethod joint-dispatch :cone
|
rlm@87
|
261 [constraints control-a control-b pivot-a pivot-b rotation]
|
rlm@87
|
262 (let [limit-xz (:limit-xz constraints)
|
rlm@87
|
263 limit-xy (:limit-xy constraints)
|
rlm@87
|
264 twist (:twist constraints)]
|
rlm@87
|
265
|
rlm@87
|
266 (println-repl "creating CONE joint")
|
rlm@87
|
267 (println-repl rotation)
|
rlm@87
|
268 (println-repl
|
rlm@87
|
269 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
|
rlm@87
|
270 (println-repl
|
rlm@87
|
271 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
|
rlm@87
|
272 (println-repl
|
rlm@87
|
273 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
|
rlm@87
|
274 (doto
|
rlm@87
|
275 (ConeJoint.
|
rlm@87
|
276 control-a
|
rlm@87
|
277 control-b
|
rlm@87
|
278 pivot-a
|
rlm@87
|
279 pivot-b
|
rlm@87
|
280 rotation
|
rlm@87
|
281 rotation)
|
rlm@87
|
282 (.setLimit (float limit-xz)
|
rlm@87
|
283 (float limit-xy)
|
rlm@87
|
284 (float twist)))))
|
rlm@87
|
285
|
rlm@88
|
286 (defn connect
|
rlm@87
|
287 "here are some examples:
|
rlm@87
|
288 {:type :point}
|
rlm@87
|
289 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
|
rlm@87
|
290 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
|
rlm@87
|
291
|
rlm@89
|
292 {:type :cone :limit-xz 0]
|
rlm@89
|
293 :limit-xy 0]
|
rlm@89
|
294 :twist 0]} (use XZY rotation mode in blender!)"
|
rlm@87
|
295 [#^Node obj-a #^Node obj-b #^Node joint]
|
rlm@87
|
296 (let [control-a (.getControl obj-a RigidBodyControl)
|
rlm@87
|
297 control-b (.getControl obj-b RigidBodyControl)
|
rlm@87
|
298 joint-center (.getWorldTranslation joint)
|
rlm@87
|
299 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
|
rlm@87
|
300 pivot-a (world-to-local obj-a joint-center)
|
rlm@87
|
301 pivot-b (world-to-local obj-b joint-center)]
|
rlm@89
|
302
|
rlm@87
|
303 (if-let [constraints
|
rlm@87
|
304 (map-vals
|
rlm@87
|
305 eval
|
rlm@87
|
306 (read-string
|
rlm@87
|
307 (meta-data joint "joint")))]
|
rlm@89
|
308 ;; A side-effect of creating a joint registers
|
rlm@89
|
309 ;; it with both physics objects which in turn
|
rlm@89
|
310 ;; will register the joint with the physics system
|
rlm@89
|
311 ;; when the simulation is started.
|
rlm@87
|
312 (do
|
rlm@87
|
313 (println-repl "creating joint between"
|
rlm@87
|
314 (.getName obj-a) "and" (.getName obj-b))
|
rlm@87
|
315 (joint-dispatch constraints
|
rlm@87
|
316 control-a control-b
|
rlm@87
|
317 pivot-a pivot-b
|
rlm@87
|
318 joint-rotation))
|
rlm@87
|
319 (println-repl "could not find joint meta-data!"))))
|
rlm@87
|
320
|
rlm@78
|
321 (defn assemble-creature [#^Node pieces joints]
|
rlm@78
|
322 (dorun
|
rlm@78
|
323 (map
|
rlm@78
|
324 (fn [geom]
|
rlm@78
|
325 (let [physics-control
|
rlm@78
|
326 (RigidBodyControl.
|
rlm@78
|
327 (HullCollisionShape.
|
rlm@78
|
328 (.getMesh geom))
|
rlm@78
|
329 (if-let [mass (meta-data geom "mass")]
|
rlm@78
|
330 (do
|
rlm@78
|
331 (println-repl
|
rlm@78
|
332 "setting" (.getName geom) "mass to" (float mass))
|
rlm@78
|
333 (float mass))
|
rlm@78
|
334 (float 1)))]
|
rlm@78
|
335
|
rlm@78
|
336 (.addControl geom physics-control)))
|
rlm@78
|
337 (filter #(isa? (class %) Geometry )
|
rlm@78
|
338 (node-seq pieces))))
|
rlm@78
|
339 (dorun
|
rlm@78
|
340 (map
|
rlm@78
|
341 (fn [joint]
|
rlm@78
|
342 (let [[obj-a obj-b]
|
rlm@78
|
343 (joint-targets pieces joint)]
|
rlm@88
|
344 (connect obj-a obj-b joint)))
|
rlm@78
|
345 joints))
|
rlm@78
|
346 pieces)
|
rlm@74
|
347
|
rlm@116
|
348 (declare blender-creature)
|
rlm@74
|
349
|
rlm@78
|
350 (def hand "Models/creature1/one.blend")
|
rlm@74
|
351
|
rlm@78
|
352 (def worm "Models/creature1/try-again.blend")
|
rlm@78
|
353
|
rlm@90
|
354 (def touch "Models/creature1/touch.blend")
|
rlm@90
|
355
|
rlm@90
|
356 (defn worm-model [] (load-blender-model worm))
|
rlm@90
|
357
|
rlm@80
|
358 (defn x-ray [#^ColorRGBA color]
|
rlm@80
|
359 (doto (Material. (asset-manager)
|
rlm@80
|
360 "Common/MatDefs/Misc/Unshaded.j3md")
|
rlm@80
|
361 (.setColor "Color" color)
|
rlm@80
|
362 (-> (.getAdditionalRenderState)
|
rlm@80
|
363 (.setDepthTest false))))
|
rlm@80
|
364
|
rlm@91
|
365 (defn colorful []
|
rlm@91
|
366 (.getChild (worm-model) "worm-21"))
|
rlm@90
|
367
|
rlm@90
|
368 (import jme3tools.converters.ImageToAwt)
|
rlm@90
|
369
|
rlm@90
|
370 (import ij.ImagePlus)
|
rlm@90
|
371
|
rlm@108
|
372 ;; Every Mesh has many triangles, each with its own index.
|
rlm@108
|
373 ;; Every vertex has its own index as well.
|
rlm@90
|
374
|
rlm@108
|
375 (defn tactile-sensor-image
|
rlm@110
|
376 "Return the touch-sensor distribution image in BufferedImage format,
|
rlm@110
|
377 or nil if it does not exist."
|
rlm@91
|
378 [#^Geometry obj]
|
rlm@110
|
379 (if-let [image-path (meta-data obj "touch")]
|
rlm@110
|
380 (ImageToAwt/convert
|
rlm@110
|
381 (.getImage
|
rlm@110
|
382 (.loadTexture
|
rlm@110
|
383 (asset-manager)
|
rlm@110
|
384 image-path))
|
rlm@110
|
385 false false 0)))
|
rlm@110
|
386
|
rlm@91
|
387 (import ij.process.ImageProcessor)
|
rlm@91
|
388 (import java.awt.image.BufferedImage)
|
rlm@91
|
389
|
rlm@92
|
390 (def white -1)
|
rlm@94
|
391
|
rlm@91
|
392 (defn filter-pixels
|
rlm@108
|
393 "List the coordinates of all pixels matching pred, within the bounds
|
rlm@108
|
394 provided. Bounds -> [x0 y0 width height]"
|
rlm@92
|
395 {:author "Dylan Holmes"}
|
rlm@108
|
396 ([pred #^BufferedImage image]
|
rlm@108
|
397 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
|
rlm@108
|
398 ([pred #^BufferedImage image [x0 y0 width height]]
|
rlm@108
|
399 ((fn accumulate [x y matches]
|
rlm@108
|
400 (cond
|
rlm@108
|
401 (>= y (+ height y0)) matches
|
rlm@108
|
402 (>= x (+ width x0)) (recur 0 (inc y) matches)
|
rlm@108
|
403 (pred (.getRGB image x y))
|
rlm@108
|
404 (recur (inc x) y (conj matches [x y]))
|
rlm@108
|
405 :else (recur (inc x) y matches)))
|
rlm@108
|
406 x0 y0 [])))
|
rlm@91
|
407
|
rlm@91
|
408 (defn white-coordinates
|
rlm@108
|
409 "Coordinates of all the white pixels in a subset of the image."
|
rlm@112
|
410 ([#^BufferedImage image bounds]
|
rlm@112
|
411 (filter-pixels #(= % white) image bounds))
|
rlm@112
|
412 ([#^BufferedImage image]
|
rlm@112
|
413 (filter-pixels #(= % white) image)))
|
rlm@108
|
414
|
rlm@108
|
415 (defn triangle
|
rlm@112
|
416 "Get the triangle specified by triangle-index from the mesh within
|
rlm@112
|
417 bounds."
|
rlm@108
|
418 [#^Mesh mesh triangle-index]
|
rlm@108
|
419 (let [scratch (Triangle.)]
|
rlm@108
|
420 (.getTriangle mesh triangle-index scratch)
|
rlm@108
|
421 scratch))
|
rlm@108
|
422
|
rlm@108
|
423 (defn triangle-vertex-indices
|
rlm@108
|
424 "Get the triangle vertex indices of a given triangle from a given
|
rlm@108
|
425 mesh."
|
rlm@108
|
426 [#^Mesh mesh triangle-index]
|
rlm@108
|
427 (let [indices (int-array 3)]
|
rlm@108
|
428 (.getTriangle mesh triangle-index indices)
|
rlm@108
|
429 (vec indices)))
|
rlm@108
|
430
|
rlm@108
|
431 (defn vertex-UV-coord
|
rlm@108
|
432 "Get the uv-coordinates of the vertex named by vertex-index"
|
rlm@108
|
433 [#^Mesh mesh vertex-index]
|
rlm@108
|
434 (let [UV-buffer
|
rlm@108
|
435 (.getData
|
rlm@108
|
436 (.getBuffer
|
rlm@108
|
437 mesh
|
rlm@108
|
438 VertexBuffer$Type/TexCoord))]
|
rlm@108
|
439 [(.get UV-buffer (* vertex-index 2))
|
rlm@108
|
440 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
|
rlm@108
|
441
|
rlm@108
|
442 (defn triangle-UV-coord
|
rlm@108
|
443 "Get the uv-cooridnates of the triangle's verticies."
|
rlm@108
|
444 [#^Mesh mesh width height triangle-index]
|
rlm@108
|
445 (map (fn [[u v]] (vector (* width u) (* height v)))
|
rlm@108
|
446 (map (partial vertex-UV-coord mesh)
|
rlm@108
|
447 (triangle-vertex-indices mesh triangle-index))))
|
rlm@91
|
448
|
rlm@102
|
449 (defn same-side?
|
rlm@102
|
450 "Given the points p1 and p2 and the reference point ref, is point p
|
rlm@102
|
451 on the same side of the line that goes through p1 and p2 as ref is?"
|
rlm@102
|
452 [p1 p2 ref p]
|
rlm@91
|
453 (<=
|
rlm@91
|
454 0
|
rlm@91
|
455 (.dot
|
rlm@91
|
456 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@91
|
457 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@91
|
458
|
rlm@108
|
459 (defn triangle-seq [#^Triangle tri]
|
rlm@108
|
460 [(.get1 tri) (.get2 tri) (.get3 tri)])
|
rlm@108
|
461
|
rlm@108
|
462 (defn vector3f-seq [#^Vector3f v]
|
rlm@108
|
463 [(.getX v) (.getY v) (.getZ v)])
|
rlm@108
|
464
|
rlm@108
|
465 (defn inside-triangle?
|
rlm@108
|
466 "Is the point inside the triangle?"
|
rlm@108
|
467 {:author "Dylan Holmes"}
|
rlm@108
|
468 [#^Triangle tri #^Vector3f p]
|
rlm@108
|
469 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
|
rlm@108
|
470 (and
|
rlm@108
|
471 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@108
|
472 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@108
|
473 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@108
|
474
|
rlm@94
|
475 (defn triangle->matrix4f
|
rlm@108
|
476 "Converts the triangle into a 4x4 matrix: The first three columns
|
rlm@108
|
477 contain the vertices of the triangle; the last contains the unit
|
rlm@108
|
478 normal of the triangle. The bottom row is filled with 1s."
|
rlm@94
|
479 [#^Triangle t]
|
rlm@94
|
480 (let [mat (Matrix4f.)
|
rlm@94
|
481 [vert-1 vert-2 vert-3]
|
rlm@94
|
482 ((comp vec map) #(.get t %) (range 3))
|
rlm@94
|
483 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@94
|
484 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@94
|
485 (dorun
|
rlm@94
|
486 (for [row (range 4) col (range 3)]
|
rlm@94
|
487 (do
|
rlm@94
|
488 (.set mat col row (.get (vertices row)col))
|
rlm@94
|
489 (.set mat 3 row 1))))
|
rlm@94
|
490 mat))
|
rlm@94
|
491
|
rlm@94
|
492 (defn triangle-transformation
|
rlm@94
|
493 "Returns the affine transformation that converts each vertex in the
|
rlm@94
|
494 first triangle into the corresponding vertex in the second
|
rlm@94
|
495 triangle."
|
rlm@94
|
496 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
497 (.mult
|
rlm@94
|
498 (triangle->matrix4f tri-2)
|
rlm@94
|
499 (.invert (triangle->matrix4f tri-1))))
|
rlm@94
|
500
|
rlm@108
|
501 (defn point->vector2f [[u v]]
|
rlm@108
|
502 (Vector2f. u v))
|
rlm@94
|
503
|
rlm@94
|
504 (defn vector2f->vector3f [v]
|
rlm@94
|
505 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@94
|
506
|
rlm@94
|
507 (defn map-triangle [f #^Triangle tri]
|
rlm@94
|
508 (Triangle.
|
rlm@94
|
509 (f 0 (.get1 tri))
|
rlm@94
|
510 (f 1 (.get2 tri))
|
rlm@94
|
511 (f 2 (.get3 tri))))
|
rlm@94
|
512
|
rlm@108
|
513 (defn points->triangle
|
rlm@108
|
514 "Convert a list of points into a triangle."
|
rlm@108
|
515 [points]
|
rlm@108
|
516 (apply #(Triangle. %1 %2 %3)
|
rlm@108
|
517 (map (fn [point]
|
rlm@108
|
518 (let [point (vec point)]
|
rlm@108
|
519 (Vector3f. (get point 0 0)
|
rlm@108
|
520 (get point 1 0)
|
rlm@108
|
521 (get point 2 0))))
|
rlm@108
|
522 (take 3 points))))
|
rlm@94
|
523
|
rlm@108
|
524 (defn convex-bounds
|
rlm@108
|
525 "Dimensions of the smallest integer bounding square of the list of
|
rlm@108
|
526 2D verticies in the form: [x y width height]."
|
rlm@108
|
527 [uv-verts]
|
rlm@108
|
528 (let [xs (map first uv-verts)
|
rlm@108
|
529 ys (map second uv-verts)
|
rlm@108
|
530 x0 (Math/floor (apply min xs))
|
rlm@108
|
531 y0 (Math/floor (apply min ys))
|
rlm@108
|
532 x1 (Math/ceil (apply max xs))
|
rlm@108
|
533 y1 (Math/ceil (apply max ys))]
|
rlm@108
|
534 [x0 y0 (- x1 x0) (- y1 y0)]))
|
rlm@93
|
535
|
rlm@106
|
536 (defn sensors-in-triangle
|
rlm@107
|
537 "Find the locations of the touch sensors within a triangle in both
|
rlm@107
|
538 UV and gemoetry relative coordinates."
|
rlm@107
|
539 [image mesh tri-index]
|
rlm@107
|
540 (let [width (.getWidth image)
|
rlm@108
|
541 height (.getHeight image)
|
rlm@108
|
542 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
|
rlm@108
|
543 bounds (convex-bounds UV-vertex-coords)
|
rlm@108
|
544
|
rlm@108
|
545 cutout-triangle (points->triangle UV-vertex-coords)
|
rlm@108
|
546 UV-sensor-coords
|
rlm@108
|
547 (filter (comp (partial inside-triangle? cutout-triangle)
|
rlm@108
|
548 (fn [[u v]] (Vector3f. u v 0)))
|
rlm@108
|
549 (white-coordinates image bounds))
|
rlm@108
|
550 UV->geometry (triangle-transformation
|
rlm@108
|
551 cutout-triangle
|
rlm@108
|
552 (triangle mesh tri-index))
|
rlm@108
|
553 geometry-sensor-coords
|
rlm@108
|
554 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
|
rlm@108
|
555 UV-sensor-coords)]
|
rlm@108
|
556 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
|
rlm@107
|
557
|
rlm@108
|
558 (defn-memo locate-feelers
|
rlm@94
|
559 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@94
|
560 their positions in geometry-relative coordinates."
|
rlm@94
|
561 [#^Geometry geo]
|
rlm@108
|
562 (let [mesh (.getMesh geo)
|
rlm@108
|
563 num-triangles (.getTriangleCount mesh)]
|
rlm@108
|
564 (if-let [image (tactile-sensor-image geo)]
|
rlm@108
|
565 (map
|
rlm@108
|
566 (partial sensors-in-triangle image mesh)
|
rlm@108
|
567 (range num-triangles))
|
rlm@108
|
568 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
|
rlm@102
|
569
|
rlm@102
|
570 (use 'clojure.contrib.def)
|
rlm@102
|
571
|
rlm@102
|
572 (defn-memo touch-topology [#^Gemoetry geo]
|
rlm@108
|
573 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
|
rlm@108
|
574
|
rlm@108
|
575 (defn-memo feeler-coordinates [#^Geometry geo]
|
rlm@108
|
576 (vec (map :geometry (locate-feelers geo))))
|
rlm@102
|
577
|
rlm@97
|
578 (defn enable-touch [#^Geometry geo]
|
rlm@108
|
579 (let [feeler-coords (feeler-coordinates geo)
|
rlm@96
|
580 tris (triangles geo)
|
rlm@109
|
581 limit 0.1
|
rlm@109
|
582 ;;results (CollisionResults.)
|
rlm@109
|
583 ]
|
rlm@111
|
584 (if (empty? (touch-topology geo))
|
rlm@111
|
585 nil
|
rlm@111
|
586 (fn [node]
|
rlm@111
|
587 (let [sensor-origins
|
rlm@111
|
588 (map
|
rlm@111
|
589 #(map (partial local-to-world geo) %)
|
rlm@111
|
590 feeler-coords)
|
rlm@111
|
591 triangle-normals
|
rlm@111
|
592 (map (partial get-ray-direction geo)
|
rlm@111
|
593 tris)
|
rlm@111
|
594 rays
|
rlm@111
|
595 (flatten
|
rlm@111
|
596 (map (fn [origins norm]
|
rlm@111
|
597 (map #(doto (Ray. % norm)
|
rlm@97
|
598 (.setLimit limit)) origins))
|
rlm@111
|
599 sensor-origins triangle-normals))]
|
rlm@111
|
600 (vector
|
rlm@111
|
601 (touch-topology geo)
|
rlm@111
|
602 (vec
|
rlm@111
|
603 (for [ray rays]
|
rlm@111
|
604 (do
|
rlm@111
|
605 (let [results (CollisionResults.)]
|
rlm@111
|
606 (.collideWith node ray results)
|
rlm@111
|
607 (let [touch-objects
|
rlm@111
|
608 (set
|
rlm@111
|
609 (filter #(not (= geo %))
|
rlm@111
|
610 (map #(.getGeometry %) results)))]
|
rlm@111
|
611 (if (> (count touch-objects) 0)
|
rlm@111
|
612 1 0))))))))))))
|
rlm@111
|
613
|
rlm@111
|
614 (defn touch [#^Node pieces]
|
rlm@111
|
615 (filter (comp not nil?)
|
rlm@111
|
616 (map enable-touch
|
rlm@111
|
617 (filter #(isa? (class %) Geometry)
|
rlm@111
|
618 (node-seq pieces)))))
|
rlm@94
|
619
|
rlm@109
|
620
|
rlm@111
|
621 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
|
rlm@111
|
622 ;; http://en.wikipedia.org/wiki/Retina
|
rlm@109
|
623
|
rlm@111
|
624 (defn test-eye []
|
rlm@111
|
625 (.getChild (worm-model) "worm-11"))
|
rlm@111
|
626
|
rlm@111
|
627
|
rlm@111
|
628 (defn retina-sensor-image
|
rlm@111
|
629 "Return a map of pixel selection functions to BufferedImages
|
rlm@111
|
630 describing the distribution of light-sensitive components on this
|
rlm@111
|
631 geometry's surface. Each function creates an integer from the rgb
|
rlm@111
|
632 values found in the pixel. :red, :green, :blue, :gray are already
|
rlm@111
|
633 defined as extracting the red green blue and average components
|
rlm@111
|
634 respectively."
|
rlm@111
|
635 [#^Geometry eye]
|
rlm@111
|
636 (if-let [eye-map (meta-data eye "eye")]
|
rlm@111
|
637 (map-vals
|
rlm@111
|
638 #(ImageToAwt/convert
|
rlm@111
|
639 (.getImage (.loadTexture (asset-manager) %))
|
rlm@111
|
640 false false 0)
|
rlm@111
|
641 (read-string
|
rlm@111
|
642 eye-map))))
|
rlm@111
|
643
|
rlm@116
|
644 (defn creature-eyes
|
rlm@116
|
645 "The eye nodes which are children of the \"eyes\" node in the
|
rlm@116
|
646 creature."
|
rlm@116
|
647 [#^Node creature]
|
rlm@116
|
648 (if-let [eye-node (.getChild creature "eyes")]
|
rlm@116
|
649 (seq (.getChildren eye-node))
|
rlm@116
|
650 (do (println-repl "could not find eyes node") [])))
|
rlm@111
|
651
|
rlm@112
|
652
|
rlm@112
|
653 ;; Here's how vision will work.
|
rlm@112
|
654
|
rlm@112
|
655 ;; Make the continuation in scene-processor take FrameBuffer,
|
rlm@112
|
656 ;; byte-buffer, BufferedImage already sized to the correct
|
rlm@112
|
657 ;; dimensions. the continuation will decide wether to "mix" them
|
rlm@112
|
658 ;; into the BufferedImage, lazily ignore them, or mix them halfway
|
rlm@112
|
659 ;; and call c/graphics card routines.
|
rlm@112
|
660
|
rlm@112
|
661 ;; (vision creature) will take an optional :skip argument which will
|
rlm@112
|
662 ;; inform the continuations in scene processor to skip the given
|
rlm@112
|
663 ;; number of cycles; 0 means that no cycles will be skipped.
|
rlm@112
|
664
|
rlm@112
|
665 ;; (vision creature) will return [init-functions sensor-functions].
|
rlm@112
|
666 ;; The init-functions are each single-arg functions that take the
|
rlm@112
|
667 ;; world and register the cameras and must each be called before the
|
rlm@112
|
668 ;; corresponding sensor-functions. Each init-function returns the
|
rlm@112
|
669 ;; viewport for that eye which can be manipulated, saved, etc. Each
|
rlm@112
|
670 ;; sensor-function is a thunk and will return data in the same
|
rlm@112
|
671 ;; format as the tactile-sensor functions; the structure is
|
rlm@112
|
672 ;; [topology, sensor-data]. Internally, these sensor-functions
|
rlm@112
|
673 ;; maintain a reference to sensor-data which is periodically updated
|
rlm@112
|
674 ;; by the continuation function established by its init-function.
|
rlm@112
|
675 ;; They can be queried every cycle, but their information may not
|
rlm@112
|
676 ;; necessairly be different every cycle.
|
rlm@112
|
677
|
rlm@112
|
678 ;; Each eye in the creature in blender will work the same way as
|
rlm@112
|
679 ;; joints -- a one dimensional object with no geometry whose local
|
rlm@112
|
680 ;; coordinate system determines the orientation of the resulting
|
rlm@112
|
681 ;; eye. All eyes will have a parent named "eyes" just as all joints
|
rlm@112
|
682 ;; have a parent named "joints". The resulting camera will be a
|
rlm@112
|
683 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
|
rlm@112
|
684 ;; the eye marker. The eye marker will contain the metadata for the
|
rlm@112
|
685 ;; eye, and will be moved by it's bound geometry. The dimensions of
|
rlm@112
|
686 ;; the eye's camera are equal to the dimensions of the eye's "UV"
|
rlm@112
|
687 ;; map.
|
rlm@116
|
688
|
rlm@116
|
689 (defn eye-target
|
rlm@116
|
690 "The closest object in creature to eye."
|
rlm@116
|
691 [#^Node creature #^Node eye]
|
rlm@116
|
692 (loop [radius (float 0.01)]
|
rlm@116
|
693 (let [results (CollisionResults.)]
|
rlm@116
|
694 (.collideWith
|
rlm@116
|
695 creature
|
rlm@116
|
696 (BoundingBox. (.getWorldTranslation eye)
|
rlm@116
|
697 radius radius radius)
|
rlm@116
|
698 results)
|
rlm@116
|
699 (if-let [target (first results)]
|
rlm@116
|
700 (.getGeometry target)
|
rlm@116
|
701 (recur (float (* 2 radius)))))))
|
rlm@116
|
702
|
rlm@116
|
703 (defn attach-eyes
|
rlm@116
|
704 "For each eye in the creature, attach a CameraNode to the appropiate
|
rlm@116
|
705 area and return the Camera."
|
rlm@116
|
706 [#^Node creature]
|
rlm@116
|
707 (for [eye (creature-eyes creature)]
|
rlm@116
|
708 (let [target (eye-target creature eye)]
|
rlm@116
|
709 (CameraNode
|
rlm@116
|
710 )
|
rlm@116
|
711
|
rlm@116
|
712 (defn vision
|
rlm@116
|
713
|
rlm@116
|
714 ;; need to create a camera based on uv image,
|
rlm@116
|
715 ;; update this camera every frame based on the position of this
|
rlm@116
|
716 ;; geometry. (maybe can get cam to follow the object)
|
rlm@116
|
717
|
rlm@116
|
718 ;; use a stack for the continuation to grab the image.
|
rlm@116
|
719
|
rlm@116
|
720
|
rlm@116
|
721 [#^Geometry eye]
|
rlm@116
|
722
|
rlm@116
|
723
|
rlm@112
|
724
|
rlm@112
|
725
|
rlm@112
|
726 )
|
rlm@102
|
727
|
rlm@116
|
728
|
rlm@116
|
729 (defn blender-creature
|
rlm@116
|
730 "Return a creature with all joints in place."
|
rlm@116
|
731 [blender-path]
|
rlm@116
|
732 (let [model (load-blender-model blender-path)
|
rlm@116
|
733 joints
|
rlm@116
|
734 (if-let [joint-node (.getChild model "joints")]
|
rlm@116
|
735 (seq (.getChildren joint-node))
|
rlm@116
|
736 (do (println-repl "could not find joints node") []))]
|
rlm@116
|
737 (assemble-creature model joints)))
|
rlm@116
|
738
|
rlm@116
|
739
|
rlm@116
|
740
|
rlm@116
|
741
|
rlm@116
|
742
|
rlm@116
|
743
|
rlm@103
|
744 (defn debug-window
|
rlm@103
|
745 "creates function that offers a debug view of sensor data"
|
rlm@103
|
746 []
|
rlm@103
|
747 (let [vi (view-image)]
|
rlm@103
|
748 (fn
|
rlm@103
|
749 [[coords sensor-data]]
|
rlm@103
|
750 (let [image (points->image coords)]
|
rlm@103
|
751 (dorun
|
rlm@103
|
752 (for [i (range (count coords))]
|
rlm@103
|
753 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@103
|
754 ({0 -16777216
|
rlm@103
|
755 1 -1} (sensor-data i)))))
|
rlm@103
|
756 (vi image)))))
|
rlm@103
|
757
|
rlm@83
|
758
|
rlm@106
|
759 ;;(defn test-touch [world creature]
|
rlm@83
|
760
|
rlm@78
|
761
|
rlm@106
|
762 (defn test-creature [thing]
|
rlm@106
|
763 (let [x-axis
|
rlm@106
|
764 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
|
rlm@106
|
765 y-axis
|
rlm@106
|
766 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
|
rlm@106
|
767 z-axis
|
rlm@106
|
768 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
|
rlm@106
|
769 creature (blender-creature thing)
|
rlm@106
|
770 touch-nerves (touch creature)
|
rlm@106
|
771 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
|
rlm@106
|
772 ]
|
rlm@106
|
773 (world
|
rlm@106
|
774 (nodify [creature
|
rlm@106
|
775 (box 10 2 10 :position (Vector3f. 0 -9 0)
|
rlm@106
|
776 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
777 x-axis y-axis z-axis
|
rlm@106
|
778 ])
|
rlm@106
|
779 standard-debug-controls
|
rlm@106
|
780 (fn [world]
|
rlm@106
|
781 (light-up-everything world)
|
rlm@106
|
782 (enable-debug world)
|
rlm@106
|
783 ;;(com.aurellem.capture.Capture/captureVideo
|
rlm@106
|
784 ;; world (file-str "/home/r/proj/ai-videos/hand"))
|
rlm@110
|
785 ;;(.setTimer world (RatchetTimer. 60))
|
rlm@110
|
786 ;;(speed-up world)
|
rlm@106
|
787 ;;(set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
788 )
|
rlm@106
|
789 (fn [world tpf]
|
rlm@109
|
790 ;;(dorun
|
rlm@109
|
791 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
|
rlm@110
|
792
|
rlm@106
|
793 (dorun
|
rlm@109
|
794 (map #(%1 (%2 (.getRootNode world)))
|
rlm@110
|
795 touch-debug-windows touch-nerves)
|
rlm@110
|
796 )
|
rlm@109
|
797
|
rlm@106
|
798 )
|
rlm@106
|
799 ;;(let [timer (atom 0)]
|
rlm@106
|
800 ;; (fn [_ _]
|
rlm@106
|
801 ;; (swap! timer inc)
|
rlm@106
|
802 ;; (if (= (rem @timer 60) 0)
|
rlm@106
|
803 ;; (println-repl (float (/ @timer 60))))))
|
rlm@106
|
804 )))
|
rlm@83
|
805
|
rlm@109
|
806
|
rlm@109
|
807
|
rlm@109
|
808
|
rlm@109
|
809
|
rlm@109
|
810
|
rlm@109
|
811
|
rlm@109
|
812
|
rlm@109
|
813
|
rlm@109
|
814 ;;; experiments in collisions
|
rlm@109
|
815
|
rlm@109
|
816
|
rlm@109
|
817
|
rlm@109
|
818 (defn collision-test []
|
rlm@110
|
819 (let [b-radius 1
|
rlm@110
|
820 b-position (Vector3f. 0 0 0)
|
rlm@109
|
821 obj-b (box 1 1 1 :color ColorRGBA/Blue
|
rlm@109
|
822 :position b-position
|
rlm@110
|
823 :mass 0)
|
rlm@110
|
824 node (nodify [obj-b])
|
rlm@110
|
825 bounds-b
|
rlm@110
|
826 (doto (Picture.)
|
rlm@110
|
827 (.setHeight 50)
|
rlm@110
|
828 (.setWidth 50)
|
rlm@110
|
829 (.setImage (asset-manager)
|
rlm@110
|
830 "Models/creature1/hand.png"
|
rlm@110
|
831 false
|
rlm@110
|
832 ))
|
rlm@110
|
833
|
rlm@110
|
834 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
|
rlm@110
|
835
|
rlm@110
|
836 collisions
|
rlm@110
|
837 (let [cr (CollisionResults.)]
|
rlm@110
|
838 (.collideWith node bounds-b cr)
|
rlm@110
|
839 (println (map #(.getContactPoint %) cr))
|
rlm@110
|
840 cr)
|
rlm@110
|
841
|
rlm@110
|
842 ;;collision-points
|
rlm@110
|
843 ;;(map #(sphere 0.1 :position (.getContactPoint %))
|
rlm@110
|
844 ;; collisions)
|
rlm@110
|
845
|
rlm@110
|
846 ;;node (nodify (conj collision-points obj-b))
|
rlm@110
|
847
|
rlm@109
|
848 sim
|
rlm@109
|
849 (world node
|
rlm@110
|
850 {"key-space"
|
rlm@110
|
851 (fn [_ value]
|
rlm@110
|
852 (if value
|
rlm@110
|
853 (let [cr (CollisionResults.)]
|
rlm@110
|
854 (.collideWith node bounds-b cr)
|
rlm@110
|
855 (println-repl (map #(.getContactPoint %) cr))
|
rlm@110
|
856 cr)))}
|
rlm@109
|
857 no-op
|
rlm@109
|
858 no-op)
|
rlm@109
|
859
|
rlm@109
|
860 ]
|
rlm@110
|
861 sim
|
rlm@109
|
862
|
rlm@109
|
863 ))
|
rlm@109
|
864
|
rlm@116
|
865
|
rlm@116
|
866 ;; the camera will stay in its initial position/rotation with relation
|
rlm@116
|
867 ;; to the spatial.
|
rlm@116
|
868
|
rlm@116
|
869 (defn bind-camera [#^Spatial obj #^Camera cam]
|
rlm@116
|
870 (let [cam-offset (.subtract (.getLocation cam)
|
rlm@116
|
871 (.getWorldTranslation obj))
|
rlm@116
|
872 initial-cam-rotation (.getRotation cam)
|
rlm@116
|
873 base-anti-rotation (.inverse (.getWorldRotation obj))]
|
rlm@116
|
874 (.addControl
|
rlm@116
|
875 obj
|
rlm@116
|
876 (proxy [AbstractControl] []
|
rlm@116
|
877 (controlUpdate [tpf]
|
rlm@116
|
878 (let [total-rotation
|
rlm@116
|
879 (.mult base-anti-rotation (.getWorldRotation obj))]
|
rlm@116
|
880
|
rlm@116
|
881 (.setLocation cam
|
rlm@116
|
882 (.add
|
rlm@116
|
883 (.mult total-rotation cam-offset)
|
rlm@116
|
884 (.getWorldTranslation obj)))
|
rlm@116
|
885 (.setRotation cam
|
rlm@116
|
886 initial-cam-rotation)
|
rlm@116
|
887 ;;(.mult total-rotation initial-cam-rotation)
|
rlm@116
|
888
|
rlm@116
|
889 ))
|
rlm@116
|
890
|
rlm@116
|
891 (controlRender [_ _])))))
|
rlm@109
|
892
|
rlm@116
|
893
|
rlm@116
|
894
|
rlm@116
|
895 (defn follow-test []
|
rlm@116
|
896 (let [camera-pos (Vector3f. 0 30 0)
|
rlm@116
|
897 rock (box 1 1 1 :color ColorRGBA/Blue
|
rlm@116
|
898 :position (Vector3f. 0 10 0)
|
rlm@116
|
899 :mass 30
|
rlm@116
|
900 )
|
rlm@116
|
901
|
rlm@116
|
902 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
|
rlm@116
|
903 :position (Vector3f. 0 -3 0))]
|
rlm@116
|
904
|
rlm@116
|
905 (world
|
rlm@116
|
906 (nodify [rock table])
|
rlm@116
|
907 standard-debug-controls
|
rlm@116
|
908 (fn [world]
|
rlm@116
|
909 (let
|
rlm@116
|
910 [cam (doto (.clone (.getCamera world))
|
rlm@116
|
911 (.setLocation camera-pos)
|
rlm@116
|
912 (.lookAt Vector3f/ZERO
|
rlm@116
|
913 Vector3f/UNIT_X))]
|
rlm@116
|
914 (bind-camera rock cam)
|
rlm@116
|
915
|
rlm@116
|
916 (.setTimer world (RatchetTimer. 60))
|
rlm@116
|
917 (add-eye world cam (comp (view-image) BufferedImage!))
|
rlm@116
|
918 (add-eye world (.getCamera world) no-op))
|
rlm@116
|
919 )
|
rlm@116
|
920 no-op)))
|
rlm@116
|
921
|
rlm@87
|
922 #+end_src
|
rlm@83
|
923
|
rlm@87
|
924 #+results: body-1
|
rlm@109
|
925 : #'cortex.silly/test-creature
|
rlm@78
|
926
|
rlm@78
|
927
|
rlm@78
|
928 * COMMENT purgatory
|
rlm@78
|
929 #+begin_src clojure
|
rlm@77
|
930 (defn bullet-trans []
|
rlm@77
|
931 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
932 :position (Vector3f. -10 5 0))
|
rlm@77
|
933 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
934 :position (Vector3f. -10 -5 0)
|
rlm@77
|
935 :mass 0)
|
rlm@77
|
936 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
937 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
938 swivel
|
rlm@77
|
939 (.toRotationMatrix
|
rlm@77
|
940 (doto (Quaternion.)
|
rlm@77
|
941 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
942 Vector3f/UNIT_X)))]
|
rlm@77
|
943 (doto
|
rlm@77
|
944 (ConeJoint.
|
rlm@77
|
945 control-a control-b
|
rlm@77
|
946 (Vector3f. 0 5 0)
|
rlm@77
|
947 (Vector3f. 0 -5 0)
|
rlm@77
|
948 swivel swivel)
|
rlm@77
|
949 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
950 (/ Math/PI 4)
|
rlm@77
|
951 (* Math/PI 0.8)))
|
rlm@77
|
952 (world (nodify
|
rlm@77
|
953 [obj-a obj-b])
|
rlm@77
|
954 standard-debug-controls
|
rlm@77
|
955 enable-debug
|
rlm@77
|
956 no-op)))
|
rlm@74
|
957
|
rlm@74
|
958
|
rlm@77
|
959 (defn bullet-trans* []
|
rlm@77
|
960 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
961 :position (Vector3f. 5 0 0)
|
rlm@77
|
962 :mass 90)
|
rlm@77
|
963 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
964 :position (Vector3f. -5 0 0)
|
rlm@77
|
965 :mass 0)
|
rlm@77
|
966 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
967 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
968 move-up? (atom nil)
|
rlm@77
|
969 move-down? (atom nil)
|
rlm@77
|
970 move-left? (atom nil)
|
rlm@77
|
971 move-right? (atom nil)
|
rlm@77
|
972 roll-left? (atom nil)
|
rlm@77
|
973 roll-right? (atom nil)
|
rlm@77
|
974 force 100
|
rlm@77
|
975 swivel
|
rlm@77
|
976 (.toRotationMatrix
|
rlm@77
|
977 (doto (Quaternion.)
|
rlm@77
|
978 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
979 Vector3f/UNIT_X)))
|
rlm@77
|
980 x-move
|
rlm@77
|
981 (doto (Matrix3f.)
|
rlm@77
|
982 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
983 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
984
|
rlm@77
|
985 timer (atom 0)]
|
rlm@77
|
986 (doto
|
rlm@77
|
987 (ConeJoint.
|
rlm@77
|
988 control-a control-b
|
rlm@77
|
989 (Vector3f. -8 0 0)
|
rlm@77
|
990 (Vector3f. 2 0 0)
|
rlm@77
|
991 ;;swivel swivel
|
rlm@77
|
992 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
993 x-move Matrix3f/IDENTITY
|
rlm@77
|
994 )
|
rlm@77
|
995 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
996 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
997 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
998 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
999 (world (nodify
|
rlm@77
|
1000 [obj-a obj-b])
|
rlm@77
|
1001 (merge standard-debug-controls
|
rlm@77
|
1002 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
1003 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
1004 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
1005 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
1006 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
1007 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
1008
|
rlm@77
|
1009 (fn [world]
|
rlm@77
|
1010 (enable-debug world)
|
rlm@77
|
1011 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
1012 )
|
rlm@77
|
1013
|
rlm@77
|
1014 (fn [world _]
|
rlm@77
|
1015
|
rlm@77
|
1016 (if @move-up?
|
rlm@77
|
1017 (.applyForce control-a
|
rlm@77
|
1018 (Vector3f. force 0 0)
|
rlm@77
|
1019 (Vector3f. 0 0 0)))
|
rlm@77
|
1020 (if @move-down?
|
rlm@77
|
1021 (.applyForce control-a
|
rlm@77
|
1022 (Vector3f. (- force) 0 0)
|
rlm@77
|
1023 (Vector3f. 0 0 0)))
|
rlm@77
|
1024 (if @move-left?
|
rlm@77
|
1025 (.applyForce control-a
|
rlm@77
|
1026 (Vector3f. 0 force 0)
|
rlm@77
|
1027 (Vector3f. 0 0 0)))
|
rlm@77
|
1028 (if @move-right?
|
rlm@77
|
1029 (.applyForce control-a
|
rlm@77
|
1030 (Vector3f. 0 (- force) 0)
|
rlm@77
|
1031 (Vector3f. 0 0 0)))
|
rlm@77
|
1032
|
rlm@77
|
1033 (if @roll-left?
|
rlm@77
|
1034 (.applyForce control-a
|
rlm@77
|
1035 (Vector3f. 0 0 force)
|
rlm@77
|
1036 (Vector3f. 0 0 0)))
|
rlm@77
|
1037 (if @roll-right?
|
rlm@77
|
1038 (.applyForce control-a
|
rlm@77
|
1039 (Vector3f. 0 0 (- force))
|
rlm@77
|
1040 (Vector3f. 0 0 0)))
|
rlm@77
|
1041
|
rlm@77
|
1042 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
1043 (.attachChild
|
rlm@77
|
1044 (.getRootNode world)
|
rlm@77
|
1045 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
1046 :physical? false :position
|
rlm@77
|
1047 (.getWorldTranslation obj-a)))))
|
rlm@77
|
1048 )
|
rlm@77
|
1049 ))
|
rlm@77
|
1050
|
rlm@94
|
1051 (defn transform-trianglesdsd
|
rlm@94
|
1052 "Transform that converts each vertex in the first triangle
|
rlm@94
|
1053 into the corresponding vertex in the second triangle."
|
rlm@94
|
1054 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
1055 (let [in [(.get1 tri-1)
|
rlm@94
|
1056 (.get2 tri-1)
|
rlm@94
|
1057 (.get3 tri-1)]
|
rlm@94
|
1058 out [(.get1 tri-2)
|
rlm@94
|
1059 (.get2 tri-2)
|
rlm@94
|
1060 (.get3 tri-2)]]
|
rlm@94
|
1061 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
|
rlm@94
|
1062 in* [(.mult translate (in 0))
|
rlm@94
|
1063 (.mult translate (in 1))
|
rlm@94
|
1064 (.mult translate (in 2))]
|
rlm@94
|
1065 final-translation
|
rlm@94
|
1066 (doto (Matrix4f.)
|
rlm@94
|
1067 (.setTranslation (out 1)))
|
rlm@94
|
1068
|
rlm@94
|
1069 rotate-1
|
rlm@94
|
1070 (doto (Matrix3f.)
|
rlm@94
|
1071 (.fromStartEndVectors
|
rlm@94
|
1072 (.normalize
|
rlm@94
|
1073 (.subtract
|
rlm@94
|
1074 (in* 1) (in* 0)))
|
rlm@94
|
1075 (.normalize
|
rlm@94
|
1076 (.subtract
|
rlm@94
|
1077 (out 1) (out 0)))))
|
rlm@94
|
1078 in** [(.mult rotate-1 (in* 0))
|
rlm@94
|
1079 (.mult rotate-1 (in* 1))
|
rlm@94
|
1080 (.mult rotate-1 (in* 2))]
|
rlm@94
|
1081 scale-factor-1
|
rlm@94
|
1082 (.mult
|
rlm@94
|
1083 (.normalize
|
rlm@94
|
1084 (.subtract
|
rlm@94
|
1085 (out 1)
|
rlm@94
|
1086 (out 0)))
|
rlm@94
|
1087 (/ (.length
|
rlm@94
|
1088 (.subtract (out 1)
|
rlm@94
|
1089 (out 0)))
|
rlm@94
|
1090 (.length
|
rlm@94
|
1091 (.subtract (in** 1)
|
rlm@94
|
1092 (in** 0)))))
|
rlm@94
|
1093 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
|
rlm@94
|
1094 in*** [(.mult scale-1 (in** 0))
|
rlm@94
|
1095 (.mult scale-1 (in** 1))
|
rlm@94
|
1096 (.mult scale-1 (in** 2))]
|
rlm@94
|
1097
|
rlm@94
|
1098
|
rlm@94
|
1099
|
rlm@94
|
1100
|
rlm@94
|
1101
|
rlm@94
|
1102 ]
|
rlm@94
|
1103
|
rlm@94
|
1104 (dorun (map println in))
|
rlm@94
|
1105 (println)
|
rlm@94
|
1106 (dorun (map println in*))
|
rlm@94
|
1107 (println)
|
rlm@94
|
1108 (dorun (map println in**))
|
rlm@94
|
1109 (println)
|
rlm@94
|
1110 (dorun (map println in***))
|
rlm@94
|
1111 (println)
|
rlm@94
|
1112
|
rlm@99
|
1113 ))))
|
rlm@94
|
1114
|
rlm@94
|
1115
|
rlm@106
|
1116 (defn world-setup [joint]
|
rlm@106
|
1117 (let [joint-position (Vector3f. 0 0 0)
|
rlm@106
|
1118 joint-rotation
|
rlm@106
|
1119 (.toRotationMatrix
|
rlm@106
|
1120 (.mult
|
rlm@106
|
1121 (doto (Quaternion.)
|
rlm@106
|
1122 (.fromAngleAxis
|
rlm@106
|
1123 (* 1 (/ Math/PI 4))
|
rlm@106
|
1124 (Vector3f. -1 0 0)))
|
rlm@106
|
1125 (doto (Quaternion.)
|
rlm@106
|
1126 (.fromAngleAxis
|
rlm@106
|
1127 (* 1 (/ Math/PI 2))
|
rlm@106
|
1128 (Vector3f. 0 0 1)))))
|
rlm@106
|
1129 top-position (.mult joint-rotation (Vector3f. 8 0 0))
|
rlm@106
|
1130
|
rlm@106
|
1131 origin (doto
|
rlm@106
|
1132 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
|
rlm@106
|
1133 :position top-position))
|
rlm@106
|
1134 top (doto
|
rlm@106
|
1135 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@106
|
1136 :position top-position)
|
rlm@106
|
1137
|
rlm@106
|
1138 (.addControl
|
rlm@106
|
1139 (RigidBodyControl.
|
rlm@106
|
1140 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
|
rlm@106
|
1141 bottom (doto
|
rlm@106
|
1142 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@106
|
1143 :position (Vector3f. 0 0 0))
|
rlm@106
|
1144 (.addControl
|
rlm@106
|
1145 (RigidBodyControl.
|
rlm@106
|
1146 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@106
|
1147 table (box 10 2 10 :position (Vector3f. 0 -20 0)
|
rlm@106
|
1148 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
1149 a (.getControl top RigidBodyControl)
|
rlm@106
|
1150 b (.getControl bottom RigidBodyControl)]
|
rlm@106
|
1151
|
rlm@106
|
1152 (cond
|
rlm@106
|
1153 (= joint :cone)
|
rlm@106
|
1154
|
rlm@106
|
1155 (doto (ConeJoint.
|
rlm@106
|
1156 a b
|
rlm@106
|
1157 (world-to-local top joint-position)
|
rlm@106
|
1158 (world-to-local bottom joint-position)
|
rlm@106
|
1159 joint-rotation
|
rlm@106
|
1160 joint-rotation
|
rlm@106
|
1161 )
|
rlm@106
|
1162
|
rlm@106
|
1163
|
rlm@106
|
1164 (.setLimit (* (/ 10) Math/PI)
|
rlm@106
|
1165 (* (/ 4) Math/PI)
|
rlm@106
|
1166 0)))
|
rlm@106
|
1167 [origin top bottom table]))
|
rlm@106
|
1168
|
rlm@106
|
1169 (defn test-joint [joint]
|
rlm@106
|
1170 (let [[origin top bottom floor] (world-setup joint)
|
rlm@106
|
1171 control (.getControl top RigidBodyControl)
|
rlm@106
|
1172 move-up? (atom false)
|
rlm@106
|
1173 move-down? (atom false)
|
rlm@106
|
1174 move-left? (atom false)
|
rlm@106
|
1175 move-right? (atom false)
|
rlm@106
|
1176 roll-left? (atom false)
|
rlm@106
|
1177 roll-right? (atom false)
|
rlm@106
|
1178 timer (atom 0)]
|
rlm@106
|
1179
|
rlm@106
|
1180 (world
|
rlm@106
|
1181 (nodify [top bottom floor origin])
|
rlm@106
|
1182 (merge standard-debug-controls
|
rlm@106
|
1183 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@106
|
1184 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@106
|
1185 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@106
|
1186 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@106
|
1187 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@106
|
1188 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@106
|
1189
|
rlm@106
|
1190 (fn [world]
|
rlm@106
|
1191 (light-up-everything world)
|
rlm@106
|
1192 (enable-debug world)
|
rlm@106
|
1193 (set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
1194 )
|
rlm@106
|
1195
|
rlm@106
|
1196 (fn [world _]
|
rlm@106
|
1197 (if (zero? (rem (swap! timer inc) 100))
|
rlm@106
|
1198 (do
|
rlm@106
|
1199 ;; (println-repl @timer)
|
rlm@106
|
1200 (.attachChild (.getRootNode world)
|
rlm@106
|
1201 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@106
|
1202 :position (.getWorldTranslation top)
|
rlm@106
|
1203 :physical? false))
|
rlm@106
|
1204 (.attachChild (.getRootNode world)
|
rlm@106
|
1205 (sphere 0.05 :color ColorRGBA/LightGray
|
rlm@106
|
1206 :position (.getWorldTranslation bottom)
|
rlm@106
|
1207 :physical? false))))
|
rlm@106
|
1208
|
rlm@106
|
1209 (if @move-up?
|
rlm@106
|
1210 (.applyTorque control
|
rlm@106
|
1211 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1212 (Vector3f. 0 0 10))))
|
rlm@106
|
1213 (if @move-down?
|
rlm@106
|
1214 (.applyTorque control
|
rlm@106
|
1215 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1216 (Vector3f. 0 0 -10))))
|
rlm@106
|
1217 (if @move-left?
|
rlm@106
|
1218 (.applyTorque control
|
rlm@106
|
1219 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1220 (Vector3f. 0 10 0))))
|
rlm@106
|
1221 (if @move-right?
|
rlm@106
|
1222 (.applyTorque control
|
rlm@106
|
1223 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1224 (Vector3f. 0 -10 0))))
|
rlm@106
|
1225 (if @roll-left?
|
rlm@106
|
1226 (.applyTorque control
|
rlm@106
|
1227 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1228 (Vector3f. -1 0 0))))
|
rlm@106
|
1229 (if @roll-right?
|
rlm@106
|
1230 (.applyTorque control
|
rlm@106
|
1231 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1232 (Vector3f. 1 0 0))))))))
|
rlm@106
|
1233
|
rlm@99
|
1234
|
rlm@99
|
1235
|
rlm@107
|
1236 (defprotocol Frame
|
rlm@107
|
1237 (frame [this]))
|
rlm@107
|
1238
|
rlm@107
|
1239 (extend-type BufferedImage
|
rlm@107
|
1240 Frame
|
rlm@107
|
1241 (frame [image]
|
rlm@107
|
1242 (merge
|
rlm@107
|
1243 (apply
|
rlm@107
|
1244 hash-map
|
rlm@107
|
1245 (interleave
|
rlm@107
|
1246 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1247 (vector x y)))
|
rlm@107
|
1248 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1249 (let [data (.getRGB image x y)]
|
rlm@107
|
1250 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
|
rlm@107
|
1251 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
|
rlm@107
|
1252 :b (bit-and 0x0000ff data)))))))
|
rlm@107
|
1253 {:width (.getWidth image) :height (.getHeight image)})))
|
rlm@107
|
1254
|
rlm@107
|
1255
|
rlm@107
|
1256 (extend-type ImagePlus
|
rlm@107
|
1257 Frame
|
rlm@107
|
1258 (frame [image+]
|
rlm@107
|
1259 (frame (.getBufferedImage image+))))
|
rlm@107
|
1260
|
rlm@107
|
1261
|
rlm@99
|
1262 #+end_src
|
rlm@99
|
1263
|
rlm@99
|
1264
|
rlm@99
|
1265 * COMMENT generate source
|
rlm@99
|
1266 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@99
|
1267 <<body-1>>
|
rlm@99
|
1268 #+end_src
|
rlm@99
|
1269
|
rlm@99
|
1270
|
rlm@94
|
1271
|
rlm@94
|
1272
|