rlm@0
|
1 #+title: SKIN!
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@0
|
4 #+description: Simulating touch in JMonkeyEngine
|
rlm@4
|
5 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
6 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@6
|
7 #+babel: :mkdirp yes :noweb yes
|
rlm@0
|
8
|
rlm@5
|
9 let's see what checkboxes look like:
|
rlm@5
|
10
|
rlm@5
|
11 * test [1/2]
|
rlm@5
|
12 - [ ] item 1
|
rlm@5
|
13 - [X] item 2
|
rlm@0
|
14
|
rlm@0
|
15
|
rlm@0
|
16 * skin!
|
rlm@0
|
17
|
rlm@0
|
18 #+srcname: skin-main
|
rlm@0
|
19 #+begin_src clojure
|
rlm@0
|
20 (ns body.skin)
|
rlm@0
|
21 (use 'cortex.world)
|
rlm@0
|
22 (use 'cortex.import)
|
rlm@0
|
23 (use 'clojure.contrib.def)
|
rlm@0
|
24 (cortex.import/mega-import-jme3)
|
rlm@0
|
25 (rlm.rlm-commands/help)
|
rlm@0
|
26
|
rlm@0
|
27 (import java.util.logging.Level)
|
rlm@0
|
28 (import java.util.logging.Logger)
|
rlm@11
|
29 (use 'hello.brick-wall)
|
rlm@0
|
30
|
rlm@0
|
31
|
rlm@0
|
32
|
rlm@6
|
33 (defn triangles [#^Geometry geom]
|
rlm@6
|
34 (let
|
rlm@6
|
35 [mesh (.getMesh geom)
|
rlm@6
|
36 triangles (transient [])]
|
rlm@6
|
37 (dorun
|
rlm@6
|
38 (for [n (range (.getTriangleCount mesh))]
|
rlm@6
|
39 (let [tri (Triangle.)]
|
rlm@6
|
40 (.getTriangle mesh n tri)
|
rlm@6
|
41 (.calculateNormal tri)
|
rlm@6
|
42 (.calculateCenter tri)
|
rlm@6
|
43 (conj! triangles tri))))
|
rlm@6
|
44 (persistent! triangles)))
|
rlm@6
|
45
|
rlm@6
|
46
|
rlm@7
|
47 (defn get-ray-origin
|
rlm@7
|
48 [geom tri]
|
rlm@7
|
49 (let [new (Vector3f.)]
|
rlm@7
|
50 (.calculateCenter tri)
|
rlm@8
|
51 (.localToWorld geom (.getCenter tri) new)
|
rlm@7
|
52 new))
|
rlm@6
|
53
|
rlm@7
|
54 (defn get-ray-direction
|
rlm@7
|
55 [geom tri]
|
rlm@9
|
56 (let [n+c (Vector3f.)]
|
rlm@7
|
57 (.calculateNormal tri)
|
rlm@9
|
58 (.calculateCenter tri)
|
rlm@9
|
59 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
|
rlm@9
|
60 (.subtract n+c (get-ray-origin geom tri))
|
rlm@8
|
61 ))
|
rlm@7
|
62
|
rlm@7
|
63 (defn ray-origin-debug
|
rlm@9
|
64 [ray color]
|
rlm@7
|
65 (make-shape
|
rlm@6
|
66 (assoc base-shape
|
rlm@6
|
67 :shape (Sphere. 5 5 0.05)
|
rlm@6
|
68 :name "arrow"
|
rlm@9
|
69 :color color
|
rlm@6
|
70 :texture false
|
rlm@6
|
71 :physical? false
|
rlm@6
|
72 :position
|
rlm@7
|
73 (.getOrigin ray))))
|
rlm@6
|
74
|
rlm@9
|
75 (defn ray-debug [ray color]
|
rlm@6
|
76 (make-shape
|
rlm@6
|
77 (assoc
|
rlm@6
|
78 base-shape
|
rlm@6
|
79 :name "debug-ray"
|
rlm@6
|
80 :physical? false
|
rlm@6
|
81 :shape (com.jme3.scene.shape.Line.
|
rlm@6
|
82 (.getOrigin ray)
|
rlm@6
|
83 (.add
|
rlm@6
|
84 (.getOrigin ray)
|
rlm@6
|
85 (.mult (.getDirection ray)
|
rlm@6
|
86 (float (.getLimit ray))))))))
|
rlm@6
|
87
|
rlm@6
|
88
|
rlm@10
|
89 (defn contact-color [contacts]
|
rlm@10
|
90 (case contacts
|
rlm@10
|
91 0 ColorRGBA/Gray
|
rlm@10
|
92 1 ColorRGBA/Blue
|
rlm@10
|
93 2 ColorRGBA/Green
|
rlm@10
|
94 3 ColorRGBA/Yellow
|
rlm@10
|
95 4 ColorRGBA/Orange
|
rlm@10
|
96 5 ColorRGBA/Red
|
rlm@10
|
97 6 ColorRGBA/Magenta
|
rlm@10
|
98 7 ColorRGBA/Pink
|
rlm@10
|
99 8 ColorRGBA/White))
|
rlm@6
|
100
|
rlm@6
|
101 (defn normal-rays
|
rlm@6
|
102 "returns rays"
|
rlm@6
|
103 [limit #^Geometry geom]
|
rlm@6
|
104 (vec
|
rlm@6
|
105 (map
|
rlm@6
|
106 (fn [tri]
|
rlm@6
|
107 (doto
|
rlm@6
|
108 (Ray. (get-ray-origin geom tri)
|
rlm@6
|
109 (get-ray-direction geom tri))
|
rlm@12
|
110 (.setLimit limit)))
|
rlm@6
|
111 (triangles geom))))
|
rlm@6
|
112
|
rlm@6
|
113
|
rlm@6
|
114 (defn collision-debug [node result]
|
rlm@6
|
115 (println-repl "contact point: " (.getContactPoint result))
|
rlm@14
|
116
|
rlm@14
|
117
|
rlm@6
|
118 )
|
rlm@6
|
119
|
rlm@14
|
120 (defn update-ray-debug [node ray contacts]
|
rlm@14
|
121 (let [origin (.getChild node 0)]
|
rlm@14
|
122 (.setLocalTranslation origin (.getOrigin ray))
|
rlm@14
|
123 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
|
rlm@14
|
124
|
rlm@14
|
125
|
rlm@13
|
126
|
rlm@13
|
127
|
rlm@13
|
128 (defn init-node
|
rlm@13
|
129 [debug-node rays]
|
rlm@13
|
130 (println-repl "Init touch debug node.")
|
rlm@12
|
131 (.detachAllChildren debug-node)
|
rlm@13
|
132 (dorun
|
rlm@13
|
133 (for [ray rays]
|
rlm@13
|
134 (do
|
rlm@13
|
135 (.attachChild
|
rlm@13
|
136 debug-node
|
rlm@13
|
137 (doto (Node.)
|
rlm@14
|
138 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
|
rlm@14
|
139 ;;(.attachChild (ray-debug ray ColorRGBA/Gray))
|
rlm@14
|
140 ))))))
|
rlm@14
|
141
|
rlm@12
|
142
|
rlm@12
|
143
|
rlm@13
|
144 (defn manage-ray-debug-node [debug-node geom touch-data limit]
|
rlm@13
|
145 (let [rays (normal-rays limit geom)]
|
rlm@13
|
146 (if (not= (count (.getChildren debug-node)) (count touch-data))
|
rlm@13
|
147 (init-node debug-node rays))
|
rlm@13
|
148 (dorun
|
rlm@13
|
149 (for [n (range (count touch-data))]
|
rlm@14
|
150 (update-ray-debug
|
rlm@14
|
151 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
|
rlm@12
|
152
|
rlm@12
|
153
|
rlm@13
|
154 (defn touch-percieve [limit geom node]
|
rlm@6
|
155 (let [normals (normal-rays limit geom)]
|
rlm@12
|
156
|
rlm@6
|
157 (doall
|
rlm@6
|
158 (for [ray normals]
|
rlm@6
|
159 (do
|
rlm@6
|
160 (let [results (CollisionResults.)]
|
rlm@10
|
161 (.collideWith node ray results)
|
rlm@10
|
162 (let [answer (count (filter #(not (= geom (.getGeometry %))) results))
|
rlm@10
|
163 ;;color (contact-color answer)
|
rlm@10
|
164 ]
|
rlm@10
|
165 ;;(dorun (map #(println-repl (.getName (.getGeometry %))) results))
|
rlm@12
|
166
|
rlm@10
|
167
|
rlm@10
|
168 ;;(println-repl (.size results) "results for " ray)
|
rlm@10
|
169 ;;(doall (map (partial collision-debug node) results))
|
rlm@10
|
170 answer
|
rlm@10
|
171 )))))))
|
rlm@6
|
172
|
rlm@0
|
173 (defn enable-debug [world]
|
rlm@0
|
174 (.enableDebug
|
rlm@0
|
175 (.getPhysicsSpace
|
rlm@0
|
176 (.getState
|
rlm@0
|
177 (.getStateManager world)
|
rlm@0
|
178 BulletAppState))
|
rlm@0
|
179 (asset-manager)))
|
rlm@0
|
180
|
rlm@11
|
181 (defn no-logging []
|
rlm@11
|
182 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
|
rlm@11
|
183
|
rlm@11
|
184 (defn set-accuracy [world new-accuracy]
|
rlm@11
|
185 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
|
rlm@11
|
186 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
|
rlm@11
|
187
|
rlm@11
|
188
|
rlm@0
|
189 (defn transparent-sphere []
|
rlm@0
|
190 (doto
|
rlm@0
|
191 (make-shape
|
rlm@0
|
192 (merge base-shape
|
rlm@0
|
193 {:position (Vector3f. 0 2 0)
|
rlm@0
|
194 :name "the blob."
|
rlm@0
|
195 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
196 :texture "Textures/purpleWisp.png"
|
rlm@0
|
197 :physical? true
|
rlm@0
|
198 :mass 70
|
rlm@0
|
199 :color ColorRGBA/Blue
|
rlm@0
|
200 :shape (Sphere. 10 10 1)}))
|
rlm@0
|
201 (-> (.getMaterial)
|
rlm@0
|
202 (.getAdditionalRenderState)
|
rlm@0
|
203 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
204 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
205
|
rlm@0
|
206 (defn transparent-box []
|
rlm@0
|
207 (doto
|
rlm@0
|
208 (make-shape
|
rlm@0
|
209 (merge base-shape
|
rlm@0
|
210 {:position (Vector3f. 0 2 0)
|
rlm@10
|
211 :name "box"
|
rlm@0
|
212 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
213 :texture "Textures/purpleWisp.png"
|
rlm@0
|
214 :physical? true
|
rlm@0
|
215 :mass 70
|
rlm@0
|
216 :color ColorRGBA/Blue
|
rlm@0
|
217 :shape (Box. 1 1 1)}))
|
rlm@0
|
218 (-> (.getMaterial)
|
rlm@0
|
219 (.getAdditionalRenderState)
|
rlm@0
|
220 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
221 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
222
|
rlm@6
|
223 (defn transparent-floor []
|
rlm@6
|
224 (doto
|
rlm@6
|
225 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
|
rlm@6
|
226 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@10
|
227 :texture "Textures/redWisp.png"
|
rlm@10
|
228 :name "floor")
|
rlm@6
|
229 (-> (.getMaterial)
|
rlm@6
|
230 (.getAdditionalRenderState)
|
rlm@6
|
231 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@6
|
232 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@6
|
233
|
rlm@0
|
234 (defn test-skin []
|
rlm@0
|
235 (let [b
|
rlm@14
|
236 ;;(transparent-box)
|
rlm@14
|
237 (transparent-sphere)
|
rlm@10
|
238 ;;(sphere)
|
rlm@6
|
239 f (transparent-floor)
|
rlm@6
|
240 ;;controls
|
rlm@0
|
241 ;;(make-touch-sphere b)
|
rlm@6
|
242 ;;(make-touch b)
|
rlm@6
|
243 debug-node (Node.)
|
rlm@12
|
244 node (doto (Node.) (.attachChild b) (.attachChild f))
|
rlm@12
|
245 root-node (doto (Node.) (.attachChild node)
|
rlm@12
|
246 (.attachChild debug-node))
|
rlm@12
|
247 ]
|
rlm@0
|
248
|
rlm@0
|
249 (world
|
rlm@12
|
250 root-node
|
rlm@11
|
251 {"key-return" (fire-cannon-ball)}
|
rlm@6
|
252 ;;no-op
|
rlm@0
|
253 (fn [world]
|
rlm@6
|
254 ;; (Capture/SimpleCaptureVideo
|
rlm@6
|
255 ;; world
|
rlm@6
|
256 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
|
rlm@6
|
257 ;; (no-logging)
|
rlm@6
|
258 (enable-debug world)
|
rlm@6
|
259 ;; (set-accuracy world (/ 1 60))
|
rlm@11
|
260 )
|
rlm@0
|
261
|
rlm@0
|
262 (fn [& _]
|
rlm@13
|
263 (let [touch-data (touch-percieve 0.2 b node)]
|
rlm@13
|
264 (println-repl touch-data)
|
rlm@13
|
265 (manage-ray-debug-node debug-node b touch-data 0.2))
|
rlm@11
|
266 (Thread/sleep 10)
|
rlm@5
|
267 ;;(touch-print controls)
|
rlm@6
|
268 ;;(color-touch controls)
|
rlm@0
|
269 ))))
|
rlm@0
|
270
|
rlm@0
|
271 #+end_src
|
rlm@0
|
272
|
rlm@3
|
273 #+results: skin-main
|
rlm@3
|
274 : #'body.skin/test-skin
|
rlm@3
|
275
|
rlm@0
|
276
|
rlm@0
|
277
|
rlm@0
|
278
|
rlm@10
|
279
|
rlm@10
|
280
|
rlm@6
|
281
|
rlm@0
|
282
|
rlm@0
|
283 * COMMENT code generation
|
rlm@6
|
284 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
|
rlm@0
|
285 <<skin-main>>
|
rlm@0
|
286 #+end_src
|
rlm@0
|
287
|
rlm@0
|
288
|
rlm@0
|
289
|
rlm@0
|
290
|