comparison org/skin.org @ 11:a149692d3d1f

remove old GhostControl touch code
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 10:48:41 -0700
parents 0251b7e7609f
children 22c6878a1bc0
comparison
equal deleted inserted replaced
10:0251b7e7609f 11:a149692d3d1f
24 (cortex.import/mega-import-jme3) 24 (cortex.import/mega-import-jme3)
25 (rlm.rlm-commands/help) 25 (rlm.rlm-commands/help)
26 26
27 (import java.util.logging.Level) 27 (import java.util.logging.Level)
28 (import java.util.logging.Logger) 28 (import java.util.logging.Logger)
29 29 (use 'hello.brick-wall)
30 30
31
32 ;; looks like we can use GhostControls for implementing touch.
33 ;; for example:
34 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
35 (def shifted-sphere
36 (doto (CompoundCollisionShape.)
37 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
38
39 (def gc (GhostControl. shifted-sphere))
40 (def b (box 1 1 1 :mass 1))
41 (.addControl b rc)
42 (.addControl b gc)
43
44
45 (defn looksies! []
46 (view b))
47
48 ;; overlapping objects can be gotten via =.getOverlappingCount= and
49 ;; =.getOverlappingObjects=
50
51 ;; looks like I might be able to place a small "touch-sphere" whther
52 ;; on every triangle in the object's mesh, or at the verticies, using
53 ;; .getTriangleCount() on the mesh gotten by .getMesh()
54
55 ;; this way, I can create a mesh and just divide up it's faces using
56 ;; blender, and this will create the touch sensor distribution.
57
58
59 (defn make-touch-sphere [#^Geometry geom]
60 (let [tri (Triangle.)
61 mesh (.getMesh geom)
62 controls! (transient [])]
63 (dorun
64 (for [n (range (.getTriangleCount mesh))]
65 (do
66 (.getTriangle mesh n tri)
67 (.calculateCenter tri)
68 (let [control
69 (doto
70 (GhostControl.
71 (doto (CompoundCollisionShape.)
72 (.addChildShape
73 (SphereCollisionShape. (float 0.1))
74 (.mult (.getCenter tri) (float 1)))
75 (.setMargin -0.1)))
76 (.setApplyPhysicsLocal true))]
77
78 (.addControl geom control)
79 (conj! controls! control)))))
80 (persistent! controls!)))
81 31
82 32
83 (defn triangles [#^Geometry geom] 33 (defn triangles [#^Geometry geom]
84 (let 34 (let
85 [mesh (.getMesh geom) 35 [mesh (.getMesh geom)
91 (.calculateNormal tri) 41 (.calculateNormal tri)
92 (.calculateCenter tri) 42 (.calculateCenter tri)
93 (conj! triangles tri)))) 43 (conj! triangles tri))))
94 (persistent! triangles))) 44 (persistent! triangles)))
95 45
96
97 (defn new-touch [#^Geometry geom]
98 (dorun (map
99 (comp no-op #(.getCenter %))
100 (triangles geom))))
101 46
102 (defn get-ray-origin 47 (defn get-ray-origin
103 [geom tri] 48 [geom tri]
104 (let [new (Vector3f.)] 49 (let [new (Vector3f.)]
105 (.calculateCenter tri) 50 (.calculateCenter tri)
192 ;;(println-repl (.size results) "results for " ray) 137 ;;(println-repl (.size results) "results for " ray)
193 ;;(doall (map (partial collision-debug node) results)) 138 ;;(doall (map (partial collision-debug node) results))
194 answer 139 answer
195 ))))))) 140 )))))))
196 141
197 (defn make-touch [#^Geometry geom]
198 (let [tri (Triangle.)
199 mesh (.getMesh geom)
200 controls! (transient [])]
201 (dorun
202 (for [n (range (.getTriangleCount mesh))]
203 (do
204 (.getTriangle mesh n tri)
205 (.calculateCenter tri)
206 (.calculateNormal tri)
207 ;; (println-repl tri)
208 ;; (println-repl (.get1 tri))
209 ;; (println-repl (.get2 tri))
210 ;; (println-repl (.get3 tri))
211 ;; (println-repl (.getCenter tri))
212 ;; (println-repl (.getNormal tri))
213 (let [control
214 (doto
215 (GhostControl.
216
217 (doto (CompoundCollisionShape.)
218 (.addChildShape
219 (SimplexCollisionShape. Vector3f/ZERO)
220 (.mult (.getCenter tri) (float 1)))
221 (.setMargin 0)
222 ))
223 (.setApplyPhysicsLocal true))]
224 (.addControl geom control)
225 (conj! controls! control)))))
226 (persistent! controls!)))
227
228 (use 'hello.brick-wall)
229
230
231
232
233
234
235 (defn touch-reception [controls]
236 (let [control
237 (first
238 (filter
239 #(< 2 (.getOverlappingCount %)) controls))]
240 (if (not (nil? control))
241 (println
242 (seq
243 (.getOverlappingObjects control))))))
244
245 (defn touch-print [controls]
246 (println
247 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
248
249 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
250 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
251
252 (defn html-color [html-str]
253 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
254 (map #(float (/ (Integer/parseInt % 16) 255))
255 (map (partial apply str) (partition 2 html-str)))))
256
257 (defn color-touch [controls]
258 (no-exceptions
259 (dorun
260 (map
261 (fn [control]
262 (case (count (.getNonGhostOverlappingObjects control))
263 0 (set-debug-color ColorRGBA/Gray control)
264 1 (set-debug-color ColorRGBA/Blue control)
265 2 (set-debug-color ColorRGBA/Green control)
266 3 (set-debug-color ColorRGBA/Yellow control)
267 4 (set-debug-color ColorRGBA/Orange control)
268 5 (set-debug-color ColorRGBA/Red control)
269 6 (set-debug-color ColorRGBA/Magenta control)
270 7 (set-debug-color ColorRGBA/Pink control)
271 8 (set-debug-color ColorRGBA/White control)))
272 controls))))
273
274 (defn enable-debug [world] 142 (defn enable-debug [world]
275 (.enableDebug 143 (.enableDebug
276 (.getPhysicsSpace 144 (.getPhysicsSpace
277 (.getState 145 (.getState
278 (.getStateManager world) 146 (.getStateManager world)
279 BulletAppState)) 147 BulletAppState))
280 (asset-manager))) 148 (asset-manager)))
149
150 (defn no-logging []
151 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
152
153 (defn set-accuracy [world new-accuracy]
154 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
155 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
156
281 157
282 (defn transparent-sphere [] 158 (defn transparent-sphere []
283 (doto 159 (doto
284 (make-shape 160 (make-shape
285 (merge base-shape 161 (merge base-shape
311 (-> (.getMaterial) 187 (-> (.getMaterial)
312 (.getAdditionalRenderState) 188 (.getAdditionalRenderState)
313 (.setBlendMode RenderState$BlendMode/Alpha)) 189 (.setBlendMode RenderState$BlendMode/Alpha))
314 (.setQueueBucket RenderQueue$Bucket/Transparent))) 190 (.setQueueBucket RenderQueue$Bucket/Transparent)))
315 191
316
317 (defn transparent-floor [] 192 (defn transparent-floor []
318 (doto 193 (doto
319 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0) 194 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
320 :material "Common/MatDefs/Misc/Unshaded.j3md" 195 :material "Common/MatDefs/Misc/Unshaded.j3md"
321 :texture "Textures/redWisp.png" 196 :texture "Textures/redWisp.png"
322 :name "floor") 197 :name "floor")
323 (-> (.getMaterial) 198 (-> (.getMaterial)
324 (.getAdditionalRenderState) 199 (.getAdditionalRenderState)
325 (.setBlendMode RenderState$BlendMode/Alpha)) 200 (.setBlendMode RenderState$BlendMode/Alpha))
326 (.setQueueBucket RenderQueue$Bucket/Transparent))) 201 (.setQueueBucket RenderQueue$Bucket/Transparent)))
327
328
329 (defn no-logging []
330 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
331
332 (defn set-accuracy [world new-accuracy]
333 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
334 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
335 202
336 (defn test-skin [] 203 (defn test-skin []
337 (let [b 204 (let [b
338 (transparent-box) 205 (transparent-box)
339 ;;(transparent-sphere) 206 ;;(transparent-sphere)
342 ;;controls 209 ;;controls
343 ;;(make-touch-sphere b) 210 ;;(make-touch-sphere b)
344 ;;(make-touch b) 211 ;;(make-touch b)
345 debug-node (Node.) 212 debug-node (Node.)
346 node (doto (Node.) (.attachChild b) (.attachChild f) 213 node (doto (Node.) (.attachChild b) (.attachChild f)
347 (.attachChild debug-node)) 214 (.attachChild debug-node))]
348
349 ]
350 215
351 (world 216 (world
352
353 node 217 node
354 {"key-return" (fire-cannon-ball) 218 {"key-return" (fire-cannon-ball)}
355 "key-space" (fn [game value]
356 (if value
357 (println-repl (touch-percieve 0.2 b node debug-node))))
358 }
359 ;;no-op 219 ;;no-op
360 (fn [world] 220 (fn [world]
361 ;; (Capture/SimpleCaptureVideo 221 ;; (Capture/SimpleCaptureVideo
362 ;; world 222 ;; world
363 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi")) 223 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
364 ;; (no-logging) 224 ;; (no-logging)
365 (enable-debug world) 225 (enable-debug world)
366 ;; (set-accuracy world (/ 1 60)) 226 ;; (set-accuracy world (/ 1 60))
367 ) 227 )
368 228
369 (fn [& _] 229 (fn [& _]
370 ;;(println-repl 230 (println-repl
371 (touch-percieve 0.2 b node debug-node) 231 (touch-percieve 0.2 b node debug-node)
372 ;;(Thread/sleep 10) 232 )
233 (Thread/sleep 10)
373 ;;(touch-print controls) 234 ;;(touch-print controls)
374 ;;(color-touch controls) 235 ;;(color-touch controls)
375 )))) 236 ))))
376 237
377 #+end_src 238 #+end_src