view org/skin.org @ 5:93ff2b4e7e6a

trying to resolve sensitivity bugs with tough implementation
author Robert McIntyre <rlm@mit.edu>
date Sat, 22 Oct 2011 01:20:34 -0700
parents 50c92af2018e
children e3c6d1c1cb00
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
9 let's see what checkboxes look like:
11 * test [1/2]
12 - [ ] item 1
13 - [X] item 2
16 * skin!
25 #+srcname: skin-main
26 #+begin_src clojure
27 (ns body.skin)
28 (use 'cortex.world)
29 (use 'cortex.import)
30 (use 'clojure.contrib.def)
31 (cortex.import/mega-import-jme3)
32 (rlm.rlm-commands/help)
34 (import java.util.logging.Level)
35 (import java.util.logging.Logger)
39 ;; looks like we can use GhostControls for implementing touch.
40 ;; for example:
41 (def rc (GhostControl. (BoxCollisionShape. (Vector3f. 2 2 2))))
42 (def shifted-sphere
43 (doto (CompoundCollisionShape.)
44 (.addChildShape (SphereCollisionShape. 3) (Vector3f. 1 0 0))))
46 (def gc (GhostControl. shifted-sphere))
47 (def b (box 1 1 1 :mass 1))
48 (.addControl b rc)
49 (.addControl b gc)
52 (defn looksies! []
53 (view b))
55 ;; overlapping objects can be gotten via =.getOverlappingCount= and
56 ;; =.getOverlappingObjects=
58 ;; looks like I might be able to place a small "touch-sphere" whther
59 ;; on every triangle in the object's mesh, or at the verticies, using
60 ;; .getTriangleCount() on the mesh gotten by .getMesh()
62 ;; this way, I can create a mesh and just divide up it's faces using
63 ;; blender, and this will create the touch sensor distribution.
66 (defn make-touch-sphere [#^Geometry geom]
67 (let [tri (Triangle.)
68 mesh (.getMesh geom)
69 controls! (transient [])]
70 (dorun
71 (for [n (range (.getTriangleCount mesh))]
72 (do
73 (.getTriangle mesh n tri)
74 (.calculateCenter tri)
75 (let [control
76 (doto
77 (GhostControl.
78 (doto (CompoundCollisionShape.)
79 (.addChildShape
80 (SphereCollisionShape. (float 0.1))
81 (.mult (.getCenter tri) (float 1)))
82 (.setMargin -0.1)))
83 (.setApplyPhysicsLocal true))]
85 (.addControl geom control)
86 (conj! controls! control)))))
87 (persistent! controls!)))
90 (defn make-touch [#^Geometry geom]
91 (let [tri (Triangle.)
92 mesh (.getMesh geom)
93 controls! (transient [])]
94 (dorun
95 (for [n (range (.getTriangleCount mesh))]
96 (do
97 (.getTriangle mesh n tri)
98 (.calculateCenter tri)
99 (.calculateNormal tri)
100 ;; (println-repl tri)
101 ;; (println-repl (.get1 tri))
102 ;; (println-repl (.get2 tri))
103 ;; (println-repl (.get3 tri))
104 ;; (println-repl (.getCenter tri))
105 ;; (println-repl (.getNormal tri))
106 (let [control
107 (doto
108 (GhostControl.
110 (doto (CompoundCollisionShape.)
111 (.addChildShape
112 (SimplexCollisionShape. Vector3f/ZERO)
113 (.mult (.getCenter tri) (float 1)))
114 (.setMargin 0)
115 ))
116 (.setApplyPhysicsLocal true))]
117 (.addControl geom control)
118 (conj! controls! control)))))
119 (persistent! controls!)))
121 (use 'hello.brick-wall)
123 (defn touch-reception [controls]
124 (let [control
125 (first
126 (filter
127 #(< 2 (.getOverlappingCount %)) controls))]
128 (if (not (nil? control))
129 (println
130 (seq
131 (.getOverlappingObjects control))))))
133 (defn touch-print [controls]
134 (println
135 (map #(count (.getNonGhostOverlappingObjects %)) controls)))
137 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]
138 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))
140 (defn html-color [html-str]
141 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))
142 (map #(float (/ (Integer/parseInt % 16) 255))
143 (map (partial apply str) (partition 2 html-str)))))
145 (defn color-touch [controls]
146 (no-exceptions
147 (dorun
148 (map
149 (fn [control]
150 (case (count (.getNonGhostOverlappingObjects control))
151 0 (set-debug-color ColorRGBA/Gray control)
152 1 (set-debug-color ColorRGBA/Blue control)
153 2 (set-debug-color ColorRGBA/Green control)
154 3 (set-debug-color ColorRGBA/Yellow control)
155 4 (set-debug-color ColorRGBA/Orange control)
156 5 (set-debug-color ColorRGBA/Red control)
157 6 (set-debug-color ColorRGBA/Magenta control)
158 7 (set-debug-color ColorRGBA/Pink control)
159 8 (set-debug-color ColorRGBA/White control)))
160 controls))))
162 (defn enable-debug [world]
163 (.enableDebug
164 (.getPhysicsSpace
165 (.getState
166 (.getStateManager world)
167 BulletAppState))
168 (asset-manager)))
170 (defn transparent-sphere []
171 (doto
172 (make-shape
173 (merge base-shape
174 {:position (Vector3f. 0 2 0)
175 :name "the blob."
176 :material "Common/MatDefs/Misc/Unshaded.j3md"
177 :texture "Textures/purpleWisp.png"
178 :physical? true
179 :mass 70
180 :color ColorRGBA/Blue
181 :shape (Sphere. 10 10 1)}))
182 (-> (.getMaterial)
183 (.getAdditionalRenderState)
184 (.setBlendMode RenderState$BlendMode/Alpha))
185 (.setQueueBucket RenderQueue$Bucket/Transparent)))
187 (defn transparent-box []
188 (doto
189 (make-shape
190 (merge base-shape
191 {:position (Vector3f. 0 2 0)
192 :name "the blob."
193 :material "Common/MatDefs/Misc/Unshaded.j3md"
194 :texture "Textures/purpleWisp.png"
195 :physical? true
196 :mass 70
197 :color ColorRGBA/Blue
198 :shape (Box. 1 1 1)}))
199 (-> (.getMaterial)
200 (.getAdditionalRenderState)
201 (.setBlendMode RenderState$BlendMode/Alpha))
202 (.setQueueBucket RenderQueue$Bucket/Transparent)))
206 (defn no-logging []
207 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
209 (defn set-accuracy [world new-accuracy]
210 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
211 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
213 (defn test-skin []
214 (let [b
215 ;;(transparent-box)
216 (transparent-sphere)
218 controls
219 ;;(make-touch-sphere b)
220 (make-touch b)
221 ]
223 (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))))
235 {"key-return" (fire-cannon-ball)
236 "key-space" (fn [game value]
237 (touch-print controls))}
238 (fn [world]
239 (Capture/SimpleCaptureVideo
240 world
241 (file-str "/home/r/proj/cortex/tmp/blob.avi"))
242 (no-logging)
243 (enable-debug world)
244 (set-accuracy world (/ 1 60))
245 )
247 (fn [& _]
248 ;;(touch-print controls)
249 (color-touch controls)
250 ))))
252 #+end_src
254 #+results: skin-main
255 : #'body.skin/test-skin
262 * COMMENT code generation
263 #+begin_src clojure :tangle ../src/body/skin.clj
264 <<skin-main>>
265 #+end_src