view org/skin.org @ 8:522cf85fdb57

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