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