view org/skin.org @ 6:e3c6d1c1cb00

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