comparison 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
comparison
equal deleted inserted replaced
5:93ff2b4e7e6a 6:e3c6d1c1cb00
2 #+author: Robert McIntyre 2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu 3 #+email: rlm@mit.edu
4 #+description: Simulating touch in JMonkeyEngine 4 #+description: Simulating touch in JMonkeyEngine
5 #+SETUPFILE: ../../aurellem/org/setup.org 5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org 6 #+INCLUDE: ../../aurellem/org/level-0.org
7 7 #+babel: :mkdirp yes :noweb yes
8 8
9 let's see what checkboxes look like: 9 let's see what checkboxes look like:
10 10
11 * test [1/2] 11 * test [1/2]
12 - [ ] item 1 12 - [ ] item 1
13 - [X] item 2 13 - [X] item 2
14 14
15 15
16 * skin! 16 * skin!
17
18
19
20
21
22
23
24 17
25 #+srcname: skin-main 18 #+srcname: skin-main
26 #+begin_src clojure 19 #+begin_src clojure
27 (ns body.skin) 20 (ns body.skin)
28 (use 'cortex.world) 21 (use 'cortex.world)
83 (.setApplyPhysicsLocal true))] 76 (.setApplyPhysicsLocal true))]
84 77
85 (.addControl geom control) 78 (.addControl geom control)
86 (conj! controls! control))))) 79 (conj! controls! control)))))
87 (persistent! controls!))) 80 (persistent! controls!)))
88 81
82
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)))
95
96
97 (defn new-touch [#^Geometry geom]
98 (dorun (map
99 (comp no-op #(.getCenter %))
100 (triangles geom))))
101
102
103
104
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))
135
136
137 (defn get-ray-origin
138 [geom tri]
139 (.add
140 (.getCenter tri)
141 (.getLocalTranslation geom)))
142
143 (defn get-ray-direction
144 [geom tri]
145 (.mult (.getLocalRotation geom)
146 (.getNormal tri)))
147
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))))))))
160
161
162
163
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))
173
174 (.setLimit limit)))
175 (triangles geom))))
176
177
178 (defn collision-debug [node result]
179
180 (println-repl "contact point: " (.getContactPoint result))
181
182
183 )
184
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))
189
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)
197
198 ;;(println-repl (.size results) "results for " ray)
199 ;;(doall (map (partial collision-debug node) results))
200 (.size results)
201 ))))))
202
203 (defn arrow-view [obj]
204 (view (doto (Node.)
205 (.attachChild (normal-arrows obj))
206 (.attachChild obj))))
207
89 208
90 (defn make-touch [#^Geometry geom] 209 (defn make-touch [#^Geometry geom]
91 (let [tri (Triangle.) 210 (let [tri (Triangle.)
92 mesh (.getMesh geom) 211 mesh (.getMesh geom)
93 controls! (transient [])] 212 controls! (transient [])]
199 (-> (.getMaterial) 318 (-> (.getMaterial)
200 (.getAdditionalRenderState) 319 (.getAdditionalRenderState)
201 (.setBlendMode RenderState$BlendMode/Alpha)) 320 (.setBlendMode RenderState$BlendMode/Alpha))
202 (.setQueueBucket RenderQueue$Bucket/Transparent))) 321 (.setQueueBucket RenderQueue$Bucket/Transparent)))
203 322
204 323
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)))
333
205 334
206 (defn no-logging [] 335 (defn no-logging []
207 (.setLevel (Logger/getLogger "com.jme3") Level/OFF)) 336 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
208 337
209 (defn set-accuracy [world new-accuracy] 338 (defn set-accuracy [world new-accuracy]
210 (let [physics-manager (.getState (.getStateManager world) BulletAppState)] 339 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
211 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy)))) 340 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
212 341
213 (defn test-skin [] 342 (defn test-skin []
214 (let [b 343 (let [b
215 ;;(transparent-box) 344 ;;(transparent-sphere)
216 (transparent-sphere) 345 (transparent-box)
217 346 f (transparent-floor)
218 controls 347 ;;controls
219 ;;(make-touch-sphere b) 348 ;;(make-touch-sphere b)
220 (make-touch b) 349 ;;(make-touch b)
350 debug-node (Node.)
351 node (doto (Node.) (.attachChild b) (.attachChild f)
352 (.attachChild debug-node))
353
221 ] 354 ]
222 355
223 (world 356 (world
224 (doto (Node.) (.attachChild b)
225 (.attachChild
226 (doto
227 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
228 :material "Common/MatDefs/Misc/Unshaded.j3md"
229 :texture "Textures/redWisp.png")
230 (-> (.getMaterial)
231 (.getAdditionalRenderState)
232 (.setBlendMode RenderState$BlendMode/Alpha))
233 (.setQueueBucket RenderQueue$Bucket/Transparent))))
234 357
358 node
235 {"key-return" (fire-cannon-ball) 359 {"key-return" (fire-cannon-ball)
236 "key-space" (fn [game value] 360 "key-space" (fn [game value]
237 (touch-print controls))} 361 (println-repl (touch-percieve 1 b node debug-node)))
362 }
363 ;;no-op
238 (fn [world] 364 (fn [world]
239 (Capture/SimpleCaptureVideo 365 ;; (Capture/SimpleCaptureVideo
240 world 366 ;; world
241 (file-str "/home/r/proj/cortex/tmp/blob.avi")) 367 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
242 (no-logging) 368 ;; (no-logging)
243 (enable-debug world) 369 (enable-debug world)
244 (set-accuracy world (/ 1 60)) 370 ;; (set-accuracy world (/ 1 60))
245 ) 371 )
246 372
247 (fn [& _] 373 (fn [& _]
374 (Thread/sleep 10)
248 ;;(touch-print controls) 375 ;;(touch-print controls)
249 (color-touch controls) 376 ;;(color-touch controls)
250 )))) 377 ))))
251 378
252 #+end_src 379 #+end_src
253 380
254 #+results: skin-main 381 #+results: skin-main
255 : #'body.skin/test-skin 382 : #'body.skin/test-skin
256 383
257 384
258 385
259 386
260 387
261 388
262 * COMMENT code generation 389 * COMMENT code generation
263 #+begin_src clojure :tangle ../src/body/skin.clj 390 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
264 <<skin-main>> 391 <<skin-main>>
265 #+end_src 392 #+end_src
266 393
267 394
268 395