view org/skin.org @ 10:0251b7e7609f

finally got raycasting tough to work
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 10:09:02 -0700
parents 7d0c0282c3a7
children a149692d3d1f
line wrap: on
line source
1 #+title: SKIN!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulating touch in JMonkeyEngine
5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org
7 #+babel: :mkdirp yes :noweb yes
9 let's see what checkboxes look like:
11 * test [1/2]
12 - [ ] item 1
13 - [X] item 2
16 * skin!
18 #+srcname: skin-main
19 #+begin_src clojure
20 (ns body.skin)
21 (use 'cortex.world)
22 (use 'cortex.import)
23 (use 'clojure.contrib.def)
24 (cortex.import/mega-import-jme3)
25 (rlm.rlm-commands/help)
27 (import java.util.logging.Level)
28 (import java.util.logging.Logger)
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))))
39 (def gc (GhostControl. shifted-sphere))
40 (def b (box 1 1 1 :mass 1))
41 (.addControl b rc)
42 (.addControl b gc)
45 (defn looksies! []
46 (view b))
48 ;; overlapping objects can be gotten via =.getOverlappingCount= and
49 ;; =.getOverlappingObjects=
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()
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.
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))]
78 (.addControl geom control)
79 (conj! controls! control)))))
80 (persistent! controls!)))
83 (defn triangles [#^Geometry geom]
84 (let
85 [mesh (.getMesh geom)
86 triangles (transient [])]
87 (dorun
88 (for [n (range (.getTriangleCount mesh))]
89 (let [tri (Triangle.)]
90 (.getTriangle mesh n tri)
91 (.calculateNormal tri)
92 (.calculateCenter tri)
93 (conj! triangles tri))))
94 (persistent! triangles)))
97 (defn new-touch [#^Geometry geom]
98 (dorun (map
99 (comp no-op #(.getCenter %))
100 (triangles geom))))
102 (defn get-ray-origin
103 [geom tri]
104 (let [new (Vector3f.)]
105 (.calculateCenter tri)
106 (.localToWorld geom (.getCenter tri) new)
107 new))
109 (defn get-ray-direction
110 [geom tri]
111 (let [n+c (Vector3f.)]
112 (.calculateNormal tri)
113 (.calculateCenter tri)
114 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
115 (.subtract n+c (get-ray-origin geom tri))
116 ))
118 (defn ray-origin-debug
119 [ray color]
120 (make-shape
121 (assoc base-shape
122 :shape (Sphere. 5 5 0.05)
123 :name "arrow"
124 :color color
125 :texture false
126 :physical? false
127 :position
128 (.getOrigin ray))))
130 (defn ray-debug [ray color]
131 (make-shape
132 (assoc
133 base-shape
134 :name "debug-ray"
135 :physical? false
136 :shape (com.jme3.scene.shape.Line.
137 (.getOrigin ray)
138 (.add
139 (.getOrigin ray)
140 (.mult (.getDirection ray)
141 (float (.getLimit ray))))))))
144 (defn contact-color [contacts]
145 (case contacts
146 0 ColorRGBA/Gray
147 1 ColorRGBA/Blue
148 2 ColorRGBA/Green
149 3 ColorRGBA/Yellow
150 4 ColorRGBA/Orange
151 5 ColorRGBA/Red
152 6 ColorRGBA/Magenta
153 7 ColorRGBA/Pink
154 8 ColorRGBA/White))
156 (defn normal-rays
157 "returns rays"
158 [limit #^Geometry geom]
159 (vec
160 (map
161 (fn [tri]
162 (doto
163 (Ray. (get-ray-origin geom tri)
164 (get-ray-direction geom tri))
166 (.setLimit limit)
167 )
168 )
169 (triangles geom))))
172 (defn collision-debug [node result]
173 (println-repl "contact point: " (.getContactPoint result))
174 )
176 (defn touch-percieve [limit geom node debug-node]
177 (let [normals (normal-rays limit geom)]
178 (.detachAllChildren debug-node)
179 (doall
180 (for [ray normals]
181 (do
182 (let [results (CollisionResults.)]
183 (.collideWith node ray results)
184 (let [answer (count (filter #(not (= geom (.getGeometry %))) results))
185 ;;color (contact-color answer)
186 ]
187 ;;(dorun (map #(println-repl (.getName (.getGeometry %))) results))
188 ;;(.attachChild debug-node (ray-debug ray color))
189 ;;(.attachChild debug-node (ray-origin-debug ray color))
192 ;;(println-repl (.size results) "results for " ray)
193 ;;(doall (map (partial collision-debug node) results))
194 answer
195 )))))))
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.
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!)))
228 (use 'hello.brick-wall)
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))))))
245 (defn touch-print [controls]
246 (println
247 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
249 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
250 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
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)))))
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))))
274 (defn enable-debug [world]
275 (.enableDebug
276 (.getPhysicsSpace
277 (.getState
278 (.getStateManager world)
279 BulletAppState))
280 (asset-manager)))
282 (defn transparent-sphere []
283 (doto
284 (make-shape
285 (merge base-shape
286 {:position (Vector3f. 0 2 0)
287 :name "the blob."
288 :material "Common/MatDefs/Misc/Unshaded.j3md"
289 :texture "Textures/purpleWisp.png"
290 :physical? true
291 :mass 70
292 :color ColorRGBA/Blue
293 :shape (Sphere. 10 10 1)}))
294 (-> (.getMaterial)
295 (.getAdditionalRenderState)
296 (.setBlendMode RenderState$BlendMode/Alpha))
297 (.setQueueBucket RenderQueue$Bucket/Transparent)))
299 (defn transparent-box []
300 (doto
301 (make-shape
302 (merge base-shape
303 {:position (Vector3f. 0 2 0)
304 :name "box"
305 :material "Common/MatDefs/Misc/Unshaded.j3md"
306 :texture "Textures/purpleWisp.png"
307 :physical? true
308 :mass 70
309 :color ColorRGBA/Blue
310 :shape (Box. 1 1 1)}))
311 (-> (.getMaterial)
312 (.getAdditionalRenderState)
313 (.setBlendMode RenderState$BlendMode/Alpha))
314 (.setQueueBucket RenderQueue$Bucket/Transparent)))
317 (defn transparent-floor []
318 (doto
319 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
320 :material "Common/MatDefs/Misc/Unshaded.j3md"
321 :texture "Textures/redWisp.png"
322 :name "floor")
323 (-> (.getMaterial)
324 (.getAdditionalRenderState)
325 (.setBlendMode RenderState$BlendMode/Alpha))
326 (.setQueueBucket RenderQueue$Bucket/Transparent)))
329 (defn no-logging []
330 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
332 (defn set-accuracy [world new-accuracy]
333 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
334 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
336 (defn test-skin []
337 (let [b
338 (transparent-box)
339 ;;(transparent-sphere)
340 ;;(sphere)
341 f (transparent-floor)
342 ;;controls
343 ;;(make-touch-sphere b)
344 ;;(make-touch b)
345 debug-node (Node.)
346 node (doto (Node.) (.attachChild b) (.attachChild f)
347 (.attachChild debug-node))
349 ]
351 (world
353 node
354 {"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
360 (fn [world]
361 ;; (Capture/SimpleCaptureVideo
362 ;; world
363 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
364 ;; (no-logging)
365 (enable-debug world)
366 ;; (set-accuracy world (/ 1 60))
367 )
369 (fn [& _]
370 ;;(println-repl
371 (touch-percieve 0.2 b node debug-node)
372 ;;(Thread/sleep 10)
373 ;;(touch-print controls)
374 ;;(color-touch controls)
375 ))))
377 #+end_src
379 #+results: skin-main
380 : #'body.skin/test-skin
389 * COMMENT code generation
390 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
391 <<skin-main>>
392 #+end_src