view org/skin.org @ 9:7d0c0282c3a7

got positioning right, but there is still a problem
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 01:27:20 -0700
parents 522cf85fdb57
children 0251b7e7609f
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))))))))
146 (defn normal-rays
147 "returns rays"
148 [limit #^Geometry geom]
149 (vec
150 (map
151 (fn [tri]
152 (doto
153 (Ray. (get-ray-origin geom tri)
154 (get-ray-direction geom tri))
156 (.setLimit limit)))
157 (triangles geom))))
160 (defn collision-debug [node result]
161 (println-repl "contact point: " (.getContactPoint result))
162 )
164 (defn touch-percieve [limit geom node debug-node]
165 (let [normals (normal-rays limit geom)]
166 (.detachAllChildren debug-node)
167 (doall
168 (for [ray normals]
169 (do
170 (let [results (CollisionResults.)]
171 (.collideWith geom ray results)
172 (let [color (contact-color (.size results))]
174 (.attachChild debug-node (ray-debug ray color))
175 (.attachChild debug-node (ray-origin-debug ray color))
176 )
178 (println-repl (.size results) "results for " ray)
179 (doall (map (partial collision-debug node) results))
180 (.size results)
181 ))))))
183 (defn make-touch [#^Geometry geom]
184 (let [tri (Triangle.)
185 mesh (.getMesh geom)
186 controls! (transient [])]
187 (dorun
188 (for [n (range (.getTriangleCount mesh))]
189 (do
190 (.getTriangle mesh n tri)
191 (.calculateCenter tri)
192 (.calculateNormal tri)
193 ;; (println-repl tri)
194 ;; (println-repl (.get1 tri))
195 ;; (println-repl (.get2 tri))
196 ;; (println-repl (.get3 tri))
197 ;; (println-repl (.getCenter tri))
198 ;; (println-repl (.getNormal tri))
199 (let [control
200 (doto
201 (GhostControl.
203 (doto (CompoundCollisionShape.)
204 (.addChildShape
205 (SimplexCollisionShape. Vector3f/ZERO)
206 (.mult (.getCenter tri) (float 1)))
207 (.setMargin 0)
208 ))
209 (.setApplyPhysicsLocal true))]
210 (.addControl geom control)
211 (conj! controls! control)))))
212 (persistent! controls!)))
214 (use 'hello.brick-wall)
216 (defn touch-reception [controls]
217 (let [control
218 (first
219 (filter
220 #(< 2 (.getOverlappingCount %)) controls))]
221 (if (not (nil? control))
222 (println
223 (seq
224 (.getOverlappingObjects control))))))
226 (defn touch-print [controls]
227 (println
228 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
230 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
231 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
233 (defn html-color [html-str]
234 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
235 (map #(float (/ (Integer/parseInt % 16) 255))
236 (map (partial apply str) (partition 2 html-str)))))
238 (defn color-touch [controls]
239 (no-exceptions
240 (dorun
241 (map
242 (fn [control]
243 (case (count (.getNonGhostOverlappingObjects control))
244 0 (set-debug-color ColorRGBA/Gray control)
245 1 (set-debug-color ColorRGBA/Blue control)
246 2 (set-debug-color ColorRGBA/Green control)
247 3 (set-debug-color ColorRGBA/Yellow control)
248 4 (set-debug-color ColorRGBA/Orange control)
249 5 (set-debug-color ColorRGBA/Red control)
250 6 (set-debug-color ColorRGBA/Magenta control)
251 7 (set-debug-color ColorRGBA/Pink control)
252 8 (set-debug-color ColorRGBA/White control)))
253 controls))))
255 (defn enable-debug [world]
256 (.enableDebug
257 (.getPhysicsSpace
258 (.getState
259 (.getStateManager world)
260 BulletAppState))
261 (asset-manager)))
263 (defn transparent-sphere []
264 (doto
265 (make-shape
266 (merge base-shape
267 {:position (Vector3f. 0 2 0)
268 :name "the blob."
269 :material "Common/MatDefs/Misc/Unshaded.j3md"
270 :texture "Textures/purpleWisp.png"
271 :physical? true
272 :mass 70
273 :color ColorRGBA/Blue
274 :shape (Sphere. 10 10 1)}))
275 (-> (.getMaterial)
276 (.getAdditionalRenderState)
277 (.setBlendMode RenderState$BlendMode/Alpha))
278 (.setQueueBucket RenderQueue$Bucket/Transparent)))
280 (defn transparent-box []
281 (doto
282 (make-shape
283 (merge base-shape
284 {:position (Vector3f. 0 2 0)
285 :name "the blob."
286 :material "Common/MatDefs/Misc/Unshaded.j3md"
287 :texture "Textures/purpleWisp.png"
288 :physical? true
289 :mass 70
290 :color ColorRGBA/Blue
291 :shape (Box. 1 1 1)}))
292 (-> (.getMaterial)
293 (.getAdditionalRenderState)
294 (.setBlendMode RenderState$BlendMode/Alpha))
295 (.setQueueBucket RenderQueue$Bucket/Transparent)))
298 (defn transparent-floor []
299 (doto
300 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
301 :material "Common/MatDefs/Misc/Unshaded.j3md"
302 :texture "Textures/redWisp.png")
303 (-> (.getMaterial)
304 (.getAdditionalRenderState)
305 (.setBlendMode RenderState$BlendMode/Alpha))
306 (.setQueueBucket RenderQueue$Bucket/Transparent)))
309 (defn no-logging []
310 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
312 (defn set-accuracy [world new-accuracy]
313 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
314 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
316 (defn test-skin []
317 (let [b
318 ;;(transparent-sphere)
319 (transparent-box)
320 f (transparent-floor)
321 ;;controls
322 ;;(make-touch-sphere b)
323 ;;(make-touch b)
324 debug-node (Node.)
325 node (doto (Node.) (.attachChild b) (.attachChild f)
326 (.attachChild debug-node))
328 ]
330 (world
332 node
333 {"key-return" (fire-cannon-ball)
334 "key-space" (fn [game value]
335 (println-repl (touch-percieve 1 b node debug-node)))
336 }
337 ;;no-op
338 (fn [world]
339 ;; (Capture/SimpleCaptureVideo
340 ;; world
341 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
342 ;; (no-logging)
343 (enable-debug world)
344 ;; (set-accuracy world (/ 1 60))
345 )
347 (fn [& _]
348 (Thread/sleep 10)
349 ;;(touch-print controls)
350 ;;(color-touch controls)
351 ))))
353 #+end_src
355 #+results: skin-main
356 : #'body.skin/test-skin
363 * COMMENT code generation
364 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
365 <<skin-main>>
366 #+end_src