view org/skin.org @ 7:9ccfbcbed90a

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