view org/skin.org @ 4:50c92af2018e

fixed headers, make compat symlink
author Robert McIntyre <rlm@mit.edu>
date Thu, 20 Oct 2011 15:10:38 -0700
parents 6c93f2018655
children 93ff2b4e7e6a
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
11 * skin!
13 #+srcname: skin-main
14 #+begin_src clojure
15 (ns body.skin)
16 (use 'cortex.world)
17 (use 'cortex.import)
18 (use 'clojure.contrib.def)
19 (cortex.import/mega-import-jme3)
20 (rlm.rlm-commands/help)
22 (import java.util.logging.Level)
23 (import java.util.logging.Logger)
27 ;; looks like we can use GhostControls for implementing touch.
28 ;; for example:
29 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
30 (def shifted-sphere
31 (doto (CompoundCollisionShape.)
32 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
34 (def gc (GhostControl. shifted-sphere))
35 (def b (box 1 1 1 :mass 1))
36 (.addControl b rc)
37 (.addControl b gc)
40 (defn looksies! []
41 (view b))
43 ;; overlapping objects can be gotten via =.getOverlappingCount= and
44 ;; =.getOverlappingObjects=
46 ;; looks like I might be able to place a small "touch-sphere" whther
47 ;; on every triangle in the object's mesh, or at the verticies, using
48 ;; .getTriangleCount() on the mesh gotten by .getMesh()
50 ;; this way, I can create a mesh and just divide up it's faces using
51 ;; blender, and this will create the touch sensor distribution.
54 (defn make-touch-sphere [#^Geometry geom]
55 (let [tri (Triangle.)
56 mesh (.getMesh geom)
57 controls! (transient [])]
58 (dorun
59 (for [n (range (.getTriangleCount mesh))]
60 (do
61 (.getTriangle mesh n tri)
62 (.calculateCenter tri)
63 (let [control
64 (doto
65 (GhostControl.
66 (doto (CompoundCollisionShape.)
67 (.addChildShape
68 (SphereCollisionShape. (float 0.1))
69 (.mult (.getCenter tri) (float 1)))
70 (.setMargin -0.1)))
71 (.setApplyPhysicsLocal true))]
73 (.addControl geom control)
74 (conj! controls! control)))))
75 (persistent! controls!)))
78 (defn make-touch [#^Geometry geom]
79 (let [tri (Triangle.)
80 mesh (.getMesh geom)
81 controls! (transient [])]
82 (dorun
83 (for [n (range (.getTriangleCount mesh))]
84 (do
85 (.getTriangle mesh n tri)
86 (.calculateCenter tri)
87 (.calculateNormal tri)
88 (println-repl tri)
89 (println-repl (.get1 tri))
90 (println-repl (.get2 tri))
91 (println-repl (.get3 tri))
92 (println-repl (.getCenter tri))
93 (println-repl (.getNormal tri))
94 (let [control
95 (doto
96 (GhostControl.
98 (doto (CompoundCollisionShape.)
99 (.addChildShape
100 (SimplexCollisionShape. Vector3f/ZERO)
101 (.mult (.getCenter tri) (float 1)))
102 (.setMargin 0)
103 ))
104 (.setApplyPhysicsLocal true))]
106 (.addControl geom control)
107 (conj! controls! control)))))
108 (persistent! controls!)))
110 (defn make-fucked-touch [#^Geometry geom]
111 (let [tri (Triangle.)
112 mesh (.getMesh geom)
113 controls! (transient [])]
114 (dorun
115 (for [n (range (.getTriangleCount mesh))]
116 (do
117 (.getTriangle mesh n tri)
118 (.calculateCenter tri)
119 (.calculateNormal tri)
120 (println-repl tri)
121 (println-repl (.get1 tri))
122 (println-repl (.get2 tri))
123 (println-repl (.get3 tri))
124 (println-repl (.getCenter tri))
125 (println-repl (.getNormal tri))
126 (let [control1
127 (doto
128 (GhostControl.
129 (doto (CompoundCollisionShape.)
130 (.addChildShape
131 (SimplexCollisionShape. Vector3f/ZERO)
132 (.get1 tri))))
133 (.setApplyPhysicsLocal true))]
135 (.addControl geom control1)
136 (conj! controls! control1)
137 )
139 ;; (let [control1
140 ;; (doto
141 ;; (GhostControl.
142 ;; (doto (CompoundCollisionShape.)
143 ;; (.addChildShape
144 ;; (SimplexCollisionShape. Vector3f/ZERO)
145 ;; (.get2 tri))))
146 ;; (.setApplyPhysicsLocal true))]
148 ;; (.addControl geom control1)
149 ;; (conj! controls! control1)
150 ;; )
152 ;; (let [control1
153 ;; (doto
154 ;; (GhostControl.
155 ;; (doto (CompoundCollisionShape.)
156 ;; (.addChildShape
157 ;; (SimplexCollisionShape. Vector3f/ZERO)
158 ;; (.get3 tri))))
159 ;; (.setApplyPhysicsLocal true))]
161 ;; (.addControl geom control1)
162 ;; (conj! controls! control1)
164 )))
165 (persistent! controls!)))
169 (use 'hello.brick-wall)
171 (defn touch-reception [controls]
172 (let [control
173 (first
174 (filter
175 #(< 2 (.getOverlappingCount %)) controls))]
176 (if (not (nil? control))
177 (println
178 (seq
179 (.getOverlappingObjects control))))))
181 (defn touch-print [controls]
182 (println
183 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
185 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
186 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
188 (defn html-color [html-str]
189 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
190 (map #(float (/ (Integer/parseInt % 16) 255))
191 (map (partial apply str) (partition 2 html-str)))))
194 (defn color-touch [controls]
195 (no-exceptions
196 (dorun
197 (map
198 (fn [control]
199 (case (count (.getNonGhostOverlappingObjects control))
200 0 (set-debug-color ColorRGBA/Gray control)
201 1 (set-debug-color ColorRGBA/Blue control)
202 2 (set-debug-color ColorRGBA/Green control)
203 3 (set-debug-color ColorRGBA/Yellow control)
204 4 (set-debug-color ColorRGBA/Orange control)
205 5 (set-debug-color ColorRGBA/Red control)
206 6 (set-debug-color ColorRGBA/Magenta control)
207 7 (set-debug-color ColorRGBA/Pink control)
208 8 (set-debug-color ColorRGBA/White control)))
209 controls))))
211 (defn enable-debug [world]
212 (.enableDebug
213 (.getPhysicsSpace
214 (.getState
215 (.getStateManager world)
216 BulletAppState))
217 (asset-manager)))
219 (def with-debug
220 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))
221 (def no-debug
222 '(1 1 1 1 0 1 2 0 2 0 1 1 1 1 0 2 0 2 2 1 0 0 0 1 0 0 0 0 1 0))
226 (defn transparent-sphere []
227 (doto
228 (make-shape
229 (merge base-shape
230 {:position (Vector3f. 0 2 0)
231 :name "the blob."
232 :material "Common/MatDefs/Misc/Unshaded.j3md"
233 :texture "Textures/purpleWisp.png"
234 :physical? true
235 :mass 70
236 :color ColorRGBA/Blue
237 :shape (Sphere. 10 10 1)}))
238 (-> (.getMaterial)
239 (.getAdditionalRenderState)
240 (.setBlendMode RenderState$BlendMode/Alpha))
241 (.setQueueBucket RenderQueue$Bucket/Transparent)))
243 (defn transparent-box []
244 (doto
245 (make-shape
246 (merge base-shape
247 {:position (Vector3f. 0 2 0)
248 :name "the blob."
249 :material "Common/MatDefs/Misc/Unshaded.j3md"
250 :texture "Textures/purpleWisp.png"
251 :physical? true
252 :mass 70
253 :color ColorRGBA/Blue
254 :shape (Box. 1 1 1)}))
255 (-> (.getMaterial)
256 (.getAdditionalRenderState)
257 (.setBlendMode RenderState$BlendMode/Alpha))
258 (.setQueueBucket RenderQueue$Bucket/Transparent)))
262 (defn no-logging []
263 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
265 (defn set-accuracy [world new-accuracy]
266 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
267 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
269 (defn test-skin []
270 (let [b
271 ;;(transparent-box)
272 (transparent-sphere)
274 controls
275 ;;(make-touch-sphere b)
276 (make-touch b)
277 ]
279 (world
280 (doto (Node.) (.attachChild b)
281 (.attachChild
282 (doto
283 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
284 :material "Common/MatDefs/Misc/Unshaded.j3md"
285 :texture "Textures/redWisp.png")
286 (-> (.getMaterial)
287 (.getAdditionalRenderState)
288 (.setBlendMode RenderState$BlendMode/Alpha))
289 (.setQueueBucket RenderQueue$Bucket/Transparent))))
291 {"key-return" (fire-cannon-ball)
292 "key-space" (fn [game value]
293 (touch-print controls))}
294 (fn [world]
295 (Capture/SimpleCaptureVideo
296 world
297 (file-str "/home/r/proj/cortex/tmp/blob.avi"))
298 (no-logging)
299 (enable-debug world)
300 (set-accuracy world (/ 1 60))
301 )
303 (fn [& _]
304 (touch-print controls)
305 (color-touch controls)
306 ))))
308 #+end_src
310 #+results: skin-main
311 : #'body.skin/test-skin
318 * COMMENT code generation
319 #+begin_src clojure :tangle ../src/body/skin.clj
320 <<skin-main>>
321 #+end_src