rlm@0
|
1 #+title: SKIN!
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@0
|
4 #+description: Simulating touch in JMonkeyEngine
|
rlm@4
|
5 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
6 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@6
|
7 #+babel: :mkdirp yes :noweb yes
|
rlm@0
|
8
|
rlm@5
|
9 let's see what checkboxes look like:
|
rlm@5
|
10
|
rlm@5
|
11 * test [1/2]
|
rlm@5
|
12 - [ ] item 1
|
rlm@5
|
13 - [X] item 2
|
rlm@0
|
14
|
rlm@0
|
15
|
rlm@0
|
16 * skin!
|
rlm@0
|
17
|
rlm@0
|
18 #+srcname: skin-main
|
rlm@0
|
19 #+begin_src clojure
|
rlm@0
|
20 (ns body.skin)
|
rlm@0
|
21 (use 'cortex.world)
|
rlm@0
|
22 (use 'cortex.import)
|
rlm@0
|
23 (use 'clojure.contrib.def)
|
rlm@0
|
24 (cortex.import/mega-import-jme3)
|
rlm@0
|
25 (rlm.rlm-commands/help)
|
rlm@0
|
26
|
rlm@0
|
27 (import java.util.logging.Level)
|
rlm@0
|
28 (import java.util.logging.Logger)
|
rlm@0
|
29
|
rlm@0
|
30
|
rlm@0
|
31
|
rlm@0
|
32 ;; looks like we can use GhostControls for implementing touch.
|
rlm@0
|
33 ;; for example:
|
rlm@0
|
34 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
|
rlm@0
|
35 (def shifted-sphere
|
rlm@0
|
36 (doto (CompoundCollisionShape.)
|
rlm@0
|
37 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
|
rlm@0
|
38
|
rlm@0
|
39 (def gc (GhostControl. shifted-sphere))
|
rlm@0
|
40 (def b (box 1 1 1 :mass 1))
|
rlm@0
|
41 (.addControl b rc)
|
rlm@0
|
42 (.addControl b gc)
|
rlm@0
|
43
|
rlm@0
|
44
|
rlm@0
|
45 (defn looksies! []
|
rlm@0
|
46 (view b))
|
rlm@0
|
47
|
rlm@0
|
48 ;; overlapping objects can be gotten via =.getOverlappingCount= and
|
rlm@0
|
49 ;; =.getOverlappingObjects=
|
rlm@0
|
50
|
rlm@0
|
51 ;; looks like I might be able to place a small "touch-sphere" whther
|
rlm@0
|
52 ;; on every triangle in the object's mesh, or at the verticies, using
|
rlm@0
|
53 ;; .getTriangleCount() on the mesh gotten by .getMesh()
|
rlm@0
|
54
|
rlm@0
|
55 ;; this way, I can create a mesh and just divide up it's faces using
|
rlm@0
|
56 ;; blender, and this will create the touch sensor distribution.
|
rlm@0
|
57
|
rlm@0
|
58
|
rlm@0
|
59 (defn make-touch-sphere [#^Geometry geom]
|
rlm@0
|
60 (let [tri (Triangle.)
|
rlm@0
|
61 mesh (.getMesh geom)
|
rlm@0
|
62 controls! (transient [])]
|
rlm@0
|
63 (dorun
|
rlm@0
|
64 (for [n (range (.getTriangleCount mesh))]
|
rlm@0
|
65 (do
|
rlm@0
|
66 (.getTriangle mesh n tri)
|
rlm@0
|
67 (.calculateCenter tri)
|
rlm@0
|
68 (let [control
|
rlm@0
|
69 (doto
|
rlm@0
|
70 (GhostControl.
|
rlm@0
|
71 (doto (CompoundCollisionShape.)
|
rlm@0
|
72 (.addChildShape
|
rlm@0
|
73 (SphereCollisionShape. (float 0.1))
|
rlm@0
|
74 (.mult (.getCenter tri) (float 1)))
|
rlm@0
|
75 (.setMargin -0.1)))
|
rlm@0
|
76 (.setApplyPhysicsLocal true))]
|
rlm@0
|
77
|
rlm@0
|
78 (.addControl geom control)
|
rlm@0
|
79 (conj! controls! control)))))
|
rlm@0
|
80 (persistent! controls!)))
|
rlm@6
|
81
|
rlm@6
|
82
|
rlm@6
|
83 (defn triangles [#^Geometry geom]
|
rlm@6
|
84 (let
|
rlm@6
|
85 [mesh (.getMesh geom)
|
rlm@6
|
86 triangles (transient [])]
|
rlm@6
|
87 (dorun
|
rlm@6
|
88 (for [n (range (.getTriangleCount mesh))]
|
rlm@6
|
89 (let [tri (Triangle.)]
|
rlm@6
|
90 (.getTriangle mesh n tri)
|
rlm@6
|
91 (.calculateNormal tri)
|
rlm@6
|
92 (.calculateCenter tri)
|
rlm@6
|
93 (conj! triangles tri))))
|
rlm@6
|
94 (persistent! triangles)))
|
rlm@6
|
95
|
rlm@6
|
96
|
rlm@6
|
97 (defn new-touch [#^Geometry geom]
|
rlm@6
|
98 (dorun (map
|
rlm@6
|
99 (comp no-op #(.getCenter %))
|
rlm@6
|
100 (triangles geom))))
|
rlm@6
|
101
|
rlm@7
|
102 (defn get-ray-origin
|
rlm@7
|
103 [geom tri]
|
rlm@7
|
104 (let [new (Vector3f.)]
|
rlm@7
|
105 (.calculateCenter tri)
|
rlm@8
|
106 (.localToWorld geom (.getCenter tri) new)
|
rlm@7
|
107 new))
|
rlm@6
|
108
|
rlm@7
|
109 (defn get-ray-direction
|
rlm@7
|
110 [geom tri]
|
rlm@9
|
111 (let [n+c (Vector3f.)]
|
rlm@7
|
112 (.calculateNormal tri)
|
rlm@9
|
113 (.calculateCenter tri)
|
rlm@9
|
114 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
|
rlm@9
|
115 (.subtract n+c (get-ray-origin geom tri))
|
rlm@8
|
116 ))
|
rlm@7
|
117
|
rlm@7
|
118 (defn ray-origin-debug
|
rlm@9
|
119 [ray color]
|
rlm@7
|
120 (make-shape
|
rlm@6
|
121 (assoc base-shape
|
rlm@6
|
122 :shape (Sphere. 5 5 0.05)
|
rlm@6
|
123 :name "arrow"
|
rlm@9
|
124 :color color
|
rlm@6
|
125 :texture false
|
rlm@6
|
126 :physical? false
|
rlm@6
|
127 :position
|
rlm@7
|
128 (.getOrigin ray))))
|
rlm@6
|
129
|
rlm@9
|
130 (defn ray-debug [ray color]
|
rlm@6
|
131 (make-shape
|
rlm@6
|
132 (assoc
|
rlm@6
|
133 base-shape
|
rlm@6
|
134 :name "debug-ray"
|
rlm@6
|
135 :physical? false
|
rlm@6
|
136 :shape (com.jme3.scene.shape.Line.
|
rlm@6
|
137 (.getOrigin ray)
|
rlm@6
|
138 (.add
|
rlm@6
|
139 (.getOrigin ray)
|
rlm@6
|
140 (.mult (.getDirection ray)
|
rlm@6
|
141 (float (.getLimit ray))))))))
|
rlm@6
|
142
|
rlm@6
|
143
|
rlm@10
|
144 (defn contact-color [contacts]
|
rlm@10
|
145 (case contacts
|
rlm@10
|
146 0 ColorRGBA/Gray
|
rlm@10
|
147 1 ColorRGBA/Blue
|
rlm@10
|
148 2 ColorRGBA/Green
|
rlm@10
|
149 3 ColorRGBA/Yellow
|
rlm@10
|
150 4 ColorRGBA/Orange
|
rlm@10
|
151 5 ColorRGBA/Red
|
rlm@10
|
152 6 ColorRGBA/Magenta
|
rlm@10
|
153 7 ColorRGBA/Pink
|
rlm@10
|
154 8 ColorRGBA/White))
|
rlm@6
|
155
|
rlm@6
|
156 (defn normal-rays
|
rlm@6
|
157 "returns rays"
|
rlm@6
|
158 [limit #^Geometry geom]
|
rlm@6
|
159 (vec
|
rlm@6
|
160 (map
|
rlm@6
|
161 (fn [tri]
|
rlm@6
|
162 (doto
|
rlm@6
|
163 (Ray. (get-ray-origin geom tri)
|
rlm@6
|
164 (get-ray-direction geom tri))
|
rlm@6
|
165
|
rlm@10
|
166 (.setLimit limit)
|
rlm@10
|
167 )
|
rlm@10
|
168 )
|
rlm@6
|
169 (triangles geom))))
|
rlm@6
|
170
|
rlm@6
|
171
|
rlm@6
|
172 (defn collision-debug [node result]
|
rlm@6
|
173 (println-repl "contact point: " (.getContactPoint result))
|
rlm@6
|
174 )
|
rlm@6
|
175
|
rlm@6
|
176 (defn touch-percieve [limit geom node debug-node]
|
rlm@6
|
177 (let [normals (normal-rays limit geom)]
|
rlm@6
|
178 (.detachAllChildren debug-node)
|
rlm@6
|
179 (doall
|
rlm@6
|
180 (for [ray normals]
|
rlm@6
|
181 (do
|
rlm@6
|
182 (let [results (CollisionResults.)]
|
rlm@10
|
183 (.collideWith node ray results)
|
rlm@10
|
184 (let [answer (count (filter #(not (= geom (.getGeometry %))) results))
|
rlm@10
|
185 ;;color (contact-color answer)
|
rlm@10
|
186 ]
|
rlm@10
|
187 ;;(dorun (map #(println-repl (.getName (.getGeometry %))) results))
|
rlm@10
|
188 ;;(.attachChild debug-node (ray-debug ray color))
|
rlm@10
|
189 ;;(.attachChild debug-node (ray-origin-debug ray color))
|
rlm@6
|
190
|
rlm@10
|
191
|
rlm@10
|
192 ;;(println-repl (.size results) "results for " ray)
|
rlm@10
|
193 ;;(doall (map (partial collision-debug node) results))
|
rlm@10
|
194 answer
|
rlm@10
|
195 )))))))
|
rlm@6
|
196
|
rlm@0
|
197 (defn make-touch [#^Geometry geom]
|
rlm@0
|
198 (let [tri (Triangle.)
|
rlm@0
|
199 mesh (.getMesh geom)
|
rlm@0
|
200 controls! (transient [])]
|
rlm@0
|
201 (dorun
|
rlm@0
|
202 (for [n (range (.getTriangleCount mesh))]
|
rlm@0
|
203 (do
|
rlm@0
|
204 (.getTriangle mesh n tri)
|
rlm@0
|
205 (.calculateCenter tri)
|
rlm@0
|
206 (.calculateNormal tri)
|
rlm@5
|
207 ;; (println-repl tri)
|
rlm@5
|
208 ;; (println-repl (.get1 tri))
|
rlm@5
|
209 ;; (println-repl (.get2 tri))
|
rlm@5
|
210 ;; (println-repl (.get3 tri))
|
rlm@5
|
211 ;; (println-repl (.getCenter tri))
|
rlm@5
|
212 ;; (println-repl (.getNormal tri))
|
rlm@0
|
213 (let [control
|
rlm@0
|
214 (doto
|
rlm@0
|
215 (GhostControl.
|
rlm@0
|
216
|
rlm@0
|
217 (doto (CompoundCollisionShape.)
|
rlm@0
|
218 (.addChildShape
|
rlm@0
|
219 (SimplexCollisionShape. Vector3f/ZERO)
|
rlm@0
|
220 (.mult (.getCenter tri) (float 1)))
|
rlm@0
|
221 (.setMargin 0)
|
rlm@0
|
222 ))
|
rlm@0
|
223 (.setApplyPhysicsLocal true))]
|
rlm@0
|
224 (.addControl geom control)
|
rlm@0
|
225 (conj! controls! control)))))
|
rlm@0
|
226 (persistent! controls!)))
|
rlm@0
|
227
|
rlm@0
|
228 (use 'hello.brick-wall)
|
rlm@0
|
229
|
rlm@10
|
230
|
rlm@10
|
231
|
rlm@10
|
232
|
rlm@10
|
233
|
rlm@10
|
234
|
rlm@0
|
235 (defn touch-reception [controls]
|
rlm@0
|
236 (let [control
|
rlm@0
|
237 (first
|
rlm@0
|
238 (filter
|
rlm@0
|
239 #(< 2 (.getOverlappingCount %)) controls))]
|
rlm@0
|
240 (if (not (nil? control))
|
rlm@0
|
241 (println
|
rlm@0
|
242 (seq
|
rlm@0
|
243 (.getOverlappingObjects control))))))
|
rlm@0
|
244
|
rlm@0
|
245 (defn touch-print [controls]
|
rlm@0
|
246 (println
|
rlm@0
|
247 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
|
rlm@0
|
248
|
rlm@0
|
249 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
|
rlm@0
|
250 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
|
rlm@0
|
251
|
rlm@0
|
252 (defn html-color [html-str]
|
rlm@0
|
253 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
|
rlm@0
|
254 (map #(float (/ (Integer/parseInt % 16) 255))
|
rlm@0
|
255 (map (partial apply str) (partition 2 html-str)))))
|
rlm@0
|
256
|
rlm@0
|
257 (defn color-touch [controls]
|
rlm@0
|
258 (no-exceptions
|
rlm@0
|
259 (dorun
|
rlm@0
|
260 (map
|
rlm@0
|
261 (fn [control]
|
rlm@0
|
262 (case (count (.getNonGhostOverlappingObjects control))
|
rlm@0
|
263 0 (set-debug-color ColorRGBA/Gray control)
|
rlm@0
|
264 1 (set-debug-color ColorRGBA/Blue control)
|
rlm@0
|
265 2 (set-debug-color ColorRGBA/Green control)
|
rlm@0
|
266 3 (set-debug-color ColorRGBA/Yellow control)
|
rlm@0
|
267 4 (set-debug-color ColorRGBA/Orange control)
|
rlm@0
|
268 5 (set-debug-color ColorRGBA/Red control)
|
rlm@0
|
269 6 (set-debug-color ColorRGBA/Magenta control)
|
rlm@0
|
270 7 (set-debug-color ColorRGBA/Pink control)
|
rlm@0
|
271 8 (set-debug-color ColorRGBA/White control)))
|
rlm@0
|
272 controls))))
|
rlm@0
|
273
|
rlm@0
|
274 (defn enable-debug [world]
|
rlm@0
|
275 (.enableDebug
|
rlm@0
|
276 (.getPhysicsSpace
|
rlm@0
|
277 (.getState
|
rlm@0
|
278 (.getStateManager world)
|
rlm@0
|
279 BulletAppState))
|
rlm@0
|
280 (asset-manager)))
|
rlm@0
|
281
|
rlm@0
|
282 (defn transparent-sphere []
|
rlm@0
|
283 (doto
|
rlm@0
|
284 (make-shape
|
rlm@0
|
285 (merge base-shape
|
rlm@0
|
286 {:position (Vector3f. 0 2 0)
|
rlm@0
|
287 :name "the blob."
|
rlm@0
|
288 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
289 :texture "Textures/purpleWisp.png"
|
rlm@0
|
290 :physical? true
|
rlm@0
|
291 :mass 70
|
rlm@0
|
292 :color ColorRGBA/Blue
|
rlm@0
|
293 :shape (Sphere. 10 10 1)}))
|
rlm@0
|
294 (-> (.getMaterial)
|
rlm@0
|
295 (.getAdditionalRenderState)
|
rlm@0
|
296 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
297 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
298
|
rlm@0
|
299 (defn transparent-box []
|
rlm@0
|
300 (doto
|
rlm@0
|
301 (make-shape
|
rlm@0
|
302 (merge base-shape
|
rlm@0
|
303 {:position (Vector3f. 0 2 0)
|
rlm@10
|
304 :name "box"
|
rlm@0
|
305 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
306 :texture "Textures/purpleWisp.png"
|
rlm@0
|
307 :physical? true
|
rlm@0
|
308 :mass 70
|
rlm@0
|
309 :color ColorRGBA/Blue
|
rlm@0
|
310 :shape (Box. 1 1 1)}))
|
rlm@0
|
311 (-> (.getMaterial)
|
rlm@0
|
312 (.getAdditionalRenderState)
|
rlm@0
|
313 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
314 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
315
|
rlm@6
|
316
|
rlm@6
|
317 (defn transparent-floor []
|
rlm@6
|
318 (doto
|
rlm@6
|
319 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
|
rlm@6
|
320 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@10
|
321 :texture "Textures/redWisp.png"
|
rlm@10
|
322 :name "floor")
|
rlm@6
|
323 (-> (.getMaterial)
|
rlm@6
|
324 (.getAdditionalRenderState)
|
rlm@6
|
325 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@6
|
326 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@6
|
327
|
rlm@0
|
328
|
rlm@0
|
329 (defn no-logging []
|
rlm@0
|
330 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
|
rlm@0
|
331
|
rlm@0
|
332 (defn set-accuracy [world new-accuracy]
|
rlm@0
|
333 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
|
rlm@0
|
334 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
|
rlm@0
|
335
|
rlm@0
|
336 (defn test-skin []
|
rlm@0
|
337 (let [b
|
rlm@10
|
338 (transparent-box)
|
rlm@6
|
339 ;;(transparent-sphere)
|
rlm@10
|
340 ;;(sphere)
|
rlm@6
|
341 f (transparent-floor)
|
rlm@6
|
342 ;;controls
|
rlm@0
|
343 ;;(make-touch-sphere b)
|
rlm@6
|
344 ;;(make-touch b)
|
rlm@6
|
345 debug-node (Node.)
|
rlm@6
|
346 node (doto (Node.) (.attachChild b) (.attachChild f)
|
rlm@6
|
347 (.attachChild debug-node))
|
rlm@6
|
348
|
rlm@0
|
349 ]
|
rlm@0
|
350
|
rlm@0
|
351 (world
|
rlm@0
|
352
|
rlm@6
|
353 node
|
rlm@0
|
354 {"key-return" (fire-cannon-ball)
|
rlm@0
|
355 "key-space" (fn [game value]
|
rlm@10
|
356 (if value
|
rlm@10
|
357 (println-repl (touch-percieve 0.2 b node debug-node))))
|
rlm@6
|
358 }
|
rlm@6
|
359 ;;no-op
|
rlm@0
|
360 (fn [world]
|
rlm@6
|
361 ;; (Capture/SimpleCaptureVideo
|
rlm@6
|
362 ;; world
|
rlm@6
|
363 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
|
rlm@6
|
364 ;; (no-logging)
|
rlm@6
|
365 (enable-debug world)
|
rlm@6
|
366 ;; (set-accuracy world (/ 1 60))
|
rlm@0
|
367 )
|
rlm@0
|
368
|
rlm@0
|
369 (fn [& _]
|
rlm@10
|
370 ;;(println-repl
|
rlm@10
|
371 (touch-percieve 0.2 b node debug-node)
|
rlm@10
|
372 ;;(Thread/sleep 10)
|
rlm@5
|
373 ;;(touch-print controls)
|
rlm@6
|
374 ;;(color-touch controls)
|
rlm@0
|
375 ))))
|
rlm@0
|
376
|
rlm@0
|
377 #+end_src
|
rlm@0
|
378
|
rlm@3
|
379 #+results: skin-main
|
rlm@3
|
380 : #'body.skin/test-skin
|
rlm@3
|
381
|
rlm@0
|
382
|
rlm@0
|
383
|
rlm@0
|
384
|
rlm@10
|
385
|
rlm@10
|
386
|
rlm@6
|
387
|
rlm@0
|
388
|
rlm@0
|
389 * COMMENT code generation
|
rlm@6
|
390 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
|
rlm@0
|
391 <<skin-main>>
|
rlm@0
|
392 #+end_src
|
rlm@0
|
393
|
rlm@0
|
394
|
rlm@0
|
395
|
rlm@0
|
396
|