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@6
|
102
|
rlm@6
|
103
|
rlm@6
|
104
|
rlm@6
|
105 (defn normal-arrows
|
rlm@6
|
106 "returns a node containing arrows which point
|
rlm@6
|
107 in the normal direction of each face of the
|
rlm@6
|
108 given Geometry"
|
rlm@6
|
109 [#^Geometry geom]
|
rlm@6
|
110 (let [node (Node.)]
|
rlm@6
|
111 (dorun
|
rlm@6
|
112 (map #(.attachChild node %)
|
rlm@6
|
113 (map
|
rlm@6
|
114 (fn [tri]
|
rlm@6
|
115 (make-shape
|
rlm@6
|
116 (assoc base-shape
|
rlm@6
|
117 :shape (Sphere. 5 5 0.05)
|
rlm@6
|
118 :name "arrow"
|
rlm@6
|
119 :color ColorRGBA/Orange
|
rlm@6
|
120 :texture false
|
rlm@6
|
121 :asset-manager (asset-manager)
|
rlm@6
|
122 :physical? false
|
rlm@6
|
123 ;;:rotation
|
rlm@6
|
124 ;;(.mult
|
rlm@6
|
125 ;; (doto (Quaternion.)
|
rlm@6
|
126 ;; (.lookAt (.getNormal tri)
|
rlm@6
|
127 ;; Vector3f/UNIT_Y))
|
rlm@6
|
128 ;; (.getLocalRotation geom))
|
rlm@6
|
129 :position
|
rlm@6
|
130 (.add
|
rlm@6
|
131 (.getCenter tri)
|
rlm@6
|
132 (.getLocalTranslation geom)))))
|
rlm@6
|
133 (triangles geom))))
|
rlm@6
|
134 node))
|
rlm@6
|
135
|
rlm@6
|
136
|
rlm@6
|
137 (defn get-ray-origin
|
rlm@6
|
138 [geom tri]
|
rlm@6
|
139 (.add
|
rlm@6
|
140 (.getCenter tri)
|
rlm@6
|
141 (.getLocalTranslation geom)))
|
rlm@6
|
142
|
rlm@6
|
143 (defn get-ray-direction
|
rlm@6
|
144 [geom tri]
|
rlm@6
|
145 (.mult (.getLocalRotation geom)
|
rlm@6
|
146 (.getNormal tri)))
|
rlm@6
|
147
|
rlm@6
|
148 (defn ray-debug [ray]
|
rlm@6
|
149 (make-shape
|
rlm@6
|
150 (assoc
|
rlm@6
|
151 base-shape
|
rlm@6
|
152 :name "debug-ray"
|
rlm@6
|
153 :physical? false
|
rlm@6
|
154 :shape (com.jme3.scene.shape.Line.
|
rlm@6
|
155 (.getOrigin ray)
|
rlm@6
|
156 (.add
|
rlm@6
|
157 (.getOrigin ray)
|
rlm@6
|
158 (.mult (.getDirection ray)
|
rlm@6
|
159 (float (.getLimit ray))))))))
|
rlm@6
|
160
|
rlm@6
|
161
|
rlm@6
|
162
|
rlm@6
|
163
|
rlm@6
|
164 (defn normal-rays
|
rlm@6
|
165 "returns rays"
|
rlm@6
|
166 [limit #^Geometry geom]
|
rlm@6
|
167 (vec
|
rlm@6
|
168 (map
|
rlm@6
|
169 (fn [tri]
|
rlm@6
|
170 (doto
|
rlm@6
|
171 (Ray. (get-ray-origin geom tri)
|
rlm@6
|
172 (get-ray-direction geom tri))
|
rlm@6
|
173
|
rlm@6
|
174 (.setLimit limit)))
|
rlm@6
|
175 (triangles geom))))
|
rlm@6
|
176
|
rlm@6
|
177
|
rlm@6
|
178 (defn collision-debug [node result]
|
rlm@6
|
179
|
rlm@6
|
180 (println-repl "contact point: " (.getContactPoint result))
|
rlm@6
|
181
|
rlm@6
|
182
|
rlm@6
|
183 )
|
rlm@6
|
184
|
rlm@6
|
185 (defn touch-percieve [limit geom node debug-node]
|
rlm@6
|
186 (let [normals (normal-rays limit geom)]
|
rlm@6
|
187 (.detachAllChildren debug-node)
|
rlm@6
|
188 (.attachChild debug-node (normal-arrows geom))
|
rlm@6
|
189
|
rlm@6
|
190 (println-repl "---------")
|
rlm@6
|
191 (doall
|
rlm@6
|
192 (for [ray normals]
|
rlm@6
|
193 (do
|
rlm@6
|
194 (let [results (CollisionResults.)]
|
rlm@6
|
195 (.attachChild debug-node (ray-debug ray))
|
rlm@6
|
196 (.collideWith geom ray results)
|
rlm@6
|
197
|
rlm@6
|
198 ;;(println-repl (.size results) "results for " ray)
|
rlm@6
|
199 ;;(doall (map (partial collision-debug node) results))
|
rlm@6
|
200 (.size results)
|
rlm@6
|
201 ))))))
|
rlm@6
|
202
|
rlm@6
|
203 (defn arrow-view [obj]
|
rlm@6
|
204 (view (doto (Node.)
|
rlm@6
|
205 (.attachChild (normal-arrows obj))
|
rlm@6
|
206 (.attachChild obj))))
|
rlm@6
|
207
|
rlm@0
|
208
|
rlm@0
|
209 (defn make-touch [#^Geometry geom]
|
rlm@0
|
210 (let [tri (Triangle.)
|
rlm@0
|
211 mesh (.getMesh geom)
|
rlm@0
|
212 controls! (transient [])]
|
rlm@0
|
213 (dorun
|
rlm@0
|
214 (for [n (range (.getTriangleCount mesh))]
|
rlm@0
|
215 (do
|
rlm@0
|
216 (.getTriangle mesh n tri)
|
rlm@0
|
217 (.calculateCenter tri)
|
rlm@0
|
218 (.calculateNormal tri)
|
rlm@5
|
219 ;; (println-repl tri)
|
rlm@5
|
220 ;; (println-repl (.get1 tri))
|
rlm@5
|
221 ;; (println-repl (.get2 tri))
|
rlm@5
|
222 ;; (println-repl (.get3 tri))
|
rlm@5
|
223 ;; (println-repl (.getCenter tri))
|
rlm@5
|
224 ;; (println-repl (.getNormal tri))
|
rlm@0
|
225 (let [control
|
rlm@0
|
226 (doto
|
rlm@0
|
227 (GhostControl.
|
rlm@0
|
228
|
rlm@0
|
229 (doto (CompoundCollisionShape.)
|
rlm@0
|
230 (.addChildShape
|
rlm@0
|
231 (SimplexCollisionShape. Vector3f/ZERO)
|
rlm@0
|
232 (.mult (.getCenter tri) (float 1)))
|
rlm@0
|
233 (.setMargin 0)
|
rlm@0
|
234 ))
|
rlm@0
|
235 (.setApplyPhysicsLocal true))]
|
rlm@0
|
236 (.addControl geom control)
|
rlm@0
|
237 (conj! controls! control)))))
|
rlm@0
|
238 (persistent! controls!)))
|
rlm@0
|
239
|
rlm@0
|
240 (use 'hello.brick-wall)
|
rlm@0
|
241
|
rlm@0
|
242 (defn touch-reception [controls]
|
rlm@0
|
243 (let [control
|
rlm@0
|
244 (first
|
rlm@0
|
245 (filter
|
rlm@0
|
246 #(< 2 (.getOverlappingCount %)) controls))]
|
rlm@0
|
247 (if (not (nil? control))
|
rlm@0
|
248 (println
|
rlm@0
|
249 (seq
|
rlm@0
|
250 (.getOverlappingObjects control))))))
|
rlm@0
|
251
|
rlm@0
|
252 (defn touch-print [controls]
|
rlm@0
|
253 (println
|
rlm@0
|
254 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
|
rlm@0
|
255
|
rlm@0
|
256 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
|
rlm@0
|
257 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
|
rlm@0
|
258
|
rlm@0
|
259 (defn html-color [html-str]
|
rlm@0
|
260 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
|
rlm@0
|
261 (map #(float (/ (Integer/parseInt % 16) 255))
|
rlm@0
|
262 (map (partial apply str) (partition 2 html-str)))))
|
rlm@0
|
263
|
rlm@0
|
264 (defn color-touch [controls]
|
rlm@0
|
265 (no-exceptions
|
rlm@0
|
266 (dorun
|
rlm@0
|
267 (map
|
rlm@0
|
268 (fn [control]
|
rlm@0
|
269 (case (count (.getNonGhostOverlappingObjects control))
|
rlm@0
|
270 0 (set-debug-color ColorRGBA/Gray control)
|
rlm@0
|
271 1 (set-debug-color ColorRGBA/Blue control)
|
rlm@0
|
272 2 (set-debug-color ColorRGBA/Green control)
|
rlm@0
|
273 3 (set-debug-color ColorRGBA/Yellow control)
|
rlm@0
|
274 4 (set-debug-color ColorRGBA/Orange control)
|
rlm@0
|
275 5 (set-debug-color ColorRGBA/Red control)
|
rlm@0
|
276 6 (set-debug-color ColorRGBA/Magenta control)
|
rlm@0
|
277 7 (set-debug-color ColorRGBA/Pink control)
|
rlm@0
|
278 8 (set-debug-color ColorRGBA/White control)))
|
rlm@0
|
279 controls))))
|
rlm@0
|
280
|
rlm@0
|
281 (defn enable-debug [world]
|
rlm@0
|
282 (.enableDebug
|
rlm@0
|
283 (.getPhysicsSpace
|
rlm@0
|
284 (.getState
|
rlm@0
|
285 (.getStateManager world)
|
rlm@0
|
286 BulletAppState))
|
rlm@0
|
287 (asset-manager)))
|
rlm@0
|
288
|
rlm@0
|
289 (defn transparent-sphere []
|
rlm@0
|
290 (doto
|
rlm@0
|
291 (make-shape
|
rlm@0
|
292 (merge base-shape
|
rlm@0
|
293 {:position (Vector3f. 0 2 0)
|
rlm@0
|
294 :name "the blob."
|
rlm@0
|
295 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
296 :texture "Textures/purpleWisp.png"
|
rlm@0
|
297 :physical? true
|
rlm@0
|
298 :mass 70
|
rlm@0
|
299 :color ColorRGBA/Blue
|
rlm@0
|
300 :shape (Sphere. 10 10 1)}))
|
rlm@0
|
301 (-> (.getMaterial)
|
rlm@0
|
302 (.getAdditionalRenderState)
|
rlm@0
|
303 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
304 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
305
|
rlm@0
|
306 (defn transparent-box []
|
rlm@0
|
307 (doto
|
rlm@0
|
308 (make-shape
|
rlm@0
|
309 (merge base-shape
|
rlm@0
|
310 {:position (Vector3f. 0 2 0)
|
rlm@0
|
311 :name "the blob."
|
rlm@0
|
312 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
313 :texture "Textures/purpleWisp.png"
|
rlm@0
|
314 :physical? true
|
rlm@0
|
315 :mass 70
|
rlm@0
|
316 :color ColorRGBA/Blue
|
rlm@0
|
317 :shape (Box. 1 1 1)}))
|
rlm@0
|
318 (-> (.getMaterial)
|
rlm@0
|
319 (.getAdditionalRenderState)
|
rlm@0
|
320 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
321 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
322
|
rlm@6
|
323
|
rlm@6
|
324 (defn transparent-floor []
|
rlm@6
|
325 (doto
|
rlm@6
|
326 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
|
rlm@6
|
327 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@6
|
328 :texture "Textures/redWisp.png")
|
rlm@6
|
329 (-> (.getMaterial)
|
rlm@6
|
330 (.getAdditionalRenderState)
|
rlm@6
|
331 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@6
|
332 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@6
|
333
|
rlm@0
|
334
|
rlm@0
|
335 (defn no-logging []
|
rlm@0
|
336 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
|
rlm@0
|
337
|
rlm@0
|
338 (defn set-accuracy [world new-accuracy]
|
rlm@0
|
339 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
|
rlm@0
|
340 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
|
rlm@0
|
341
|
rlm@0
|
342 (defn test-skin []
|
rlm@0
|
343 (let [b
|
rlm@6
|
344 ;;(transparent-sphere)
|
rlm@6
|
345 (transparent-box)
|
rlm@6
|
346 f (transparent-floor)
|
rlm@6
|
347 ;;controls
|
rlm@0
|
348 ;;(make-touch-sphere b)
|
rlm@6
|
349 ;;(make-touch b)
|
rlm@6
|
350 debug-node (Node.)
|
rlm@6
|
351 node (doto (Node.) (.attachChild b) (.attachChild f)
|
rlm@6
|
352 (.attachChild debug-node))
|
rlm@6
|
353
|
rlm@0
|
354 ]
|
rlm@0
|
355
|
rlm@0
|
356 (world
|
rlm@0
|
357
|
rlm@6
|
358 node
|
rlm@0
|
359 {"key-return" (fire-cannon-ball)
|
rlm@0
|
360 "key-space" (fn [game value]
|
rlm@6
|
361 (println-repl (touch-percieve 1 b node debug-node)))
|
rlm@6
|
362 }
|
rlm@6
|
363 ;;no-op
|
rlm@0
|
364 (fn [world]
|
rlm@6
|
365 ;; (Capture/SimpleCaptureVideo
|
rlm@6
|
366 ;; world
|
rlm@6
|
367 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
|
rlm@6
|
368 ;; (no-logging)
|
rlm@6
|
369 (enable-debug world)
|
rlm@6
|
370 ;; (set-accuracy world (/ 1 60))
|
rlm@0
|
371 )
|
rlm@0
|
372
|
rlm@0
|
373 (fn [& _]
|
rlm@6
|
374 (Thread/sleep 10)
|
rlm@5
|
375 ;;(touch-print controls)
|
rlm@6
|
376 ;;(color-touch controls)
|
rlm@0
|
377 ))))
|
rlm@0
|
378
|
rlm@0
|
379 #+end_src
|
rlm@0
|
380
|
rlm@3
|
381 #+results: skin-main
|
rlm@3
|
382 : #'body.skin/test-skin
|
rlm@3
|
383
|
rlm@0
|
384
|
rlm@0
|
385
|
rlm@0
|
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
|