view org/skin.org @ 18:221052dac374

touch has been GREATLY improved
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 12:07:34 -0700
parents 014b14d48092
children 5a371057078f
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)
29 (use 'hello.brick-wall)
33 (defn triangles [#^Geometry geom]
34 (let
35 [mesh (.getMesh geom)
36 triangles (transient [])]
37 (dorun
38 (for [n (range (.getTriangleCount mesh))]
39 (let [tri (Triangle.)]
40 (.getTriangle mesh n tri)
41 (.calculateNormal tri)
42 (.calculateCenter tri)
43 (conj! triangles tri))))
44 (persistent! triangles)))
47 (defn get-ray-origin
48 [geom tri]
49 (let [new (Vector3f.)]
50 (.calculateCenter tri)
51 (.localToWorld geom (.getCenter tri) new)
52 new))
54 (defn get-ray-direction
55 [geom tri]
56 (let [n+c (Vector3f.)]
57 (.calculateNormal tri)
58 (.calculateCenter tri)
59 (.localToWorld geom (.add (.getCenter tri) (.getNormal tri)) n+c)
60 (.subtract n+c (get-ray-origin geom tri))
61 ))
63 (defn ray-origin-debug
64 [ray color]
65 (make-shape
66 (assoc base-shape
67 :shape (Sphere. 5 5 0.05)
68 :name "arrow"
69 :color color
70 :texture false
71 :physical? false
72 :position
73 (.getOrigin ray))))
75 (defn ray-debug [ray color]
76 (make-shape
77 (assoc
78 base-shape
79 :name "debug-ray"
80 :physical? false
81 :shape (com.jme3.scene.shape.Line.
82 (.getOrigin ray)
83 (.add
84 (.getOrigin ray)
85 (.mult (.getDirection ray)
86 (float (.getLimit ray))))))))
89 (defn contact-color [contacts]
90 (case contacts
91 0 ColorRGBA/Gray
92 1 ColorRGBA/Blue
93 2 ColorRGBA/Green
94 3 ColorRGBA/Yellow
95 4 ColorRGBA/Orange
96 5 ColorRGBA/Red
97 6 ColorRGBA/Magenta
98 7 ColorRGBA/Pink
99 8 ColorRGBA/White))
101 (defn normal-rays
102 "returns rays"
103 [limit #^Geometry geom]
104 (vec
105 (map
106 (fn [tri]
107 (doto
108 (Ray. (get-ray-origin geom tri)
109 (get-ray-direction geom tri))
110 (.setLimit limit)))
111 (triangles geom))))
113 (defn update-ray-debug [node ray contacts]
114 (let [origin (.getChild node 0)]
115 (.setLocalTranslation origin (.getOrigin ray))
116 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
118 (defn init-node
119 [debug-node rays]
120 (println-repl "Init touch debug node.")
121 (.detachAllChildren debug-node)
122 (dorun
123 (for [ray rays]
124 (do
125 (.attachChild
126 debug-node
127 (doto (Node.)
128 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
129 ;;(.attachChild (ray-debug ray ColorRGBA/Gray))
130 ))))))
132 (defn manage-ray-debug-node [debug-node geom touch-data limit]
133 (let [rays (normal-rays limit geom)]
134 (if (not= (count (.getChildren debug-node)) (count touch-data))
135 (init-node debug-node rays))
136 (dorun
137 (for [n (range (count touch-data))]
138 (update-ray-debug
139 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
141 (defn touch-percieve [limit geom node]
142 (let [normals (normal-rays limit geom)]
144 (doall
145 (for [ray normals]
146 (do
147 (let [results (CollisionResults.)]
148 (.collideWith node ray results)
149 (let [answer (set (filter #(not (= geom %))
150 (map #(.getGeometry %) results)))
151 ;;color (contact-color answer)
152 ]
154 ;;(dorun (map #(println-repl (.getName %)) answer))
155 (count answer) )))))))
157 (defn enable-debug [world]
158 (.enableDebug
159 (.getPhysicsSpace
160 (.getState
161 (.getStateManager world)
162 BulletAppState))
163 (asset-manager)))
165 (defn no-logging []
166 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
168 (defn set-accuracy [world new-accuracy]
169 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
170 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
173 (defn transparent-sphere []
174 (doto
175 (make-shape
176 (merge base-shape
177 {:position (Vector3f. 0 2 0)
178 :name "the blob."
179 :material "Common/MatDefs/Misc/Unshaded.j3md"
180 :texture "Textures/purpleWisp.png"
181 :physical? true
182 :mass 70
183 :color ColorRGBA/Blue
184 :shape (Sphere. 10 10 1)}))
185 (-> (.getMaterial)
186 (.getAdditionalRenderState)
187 (.setBlendMode RenderState$BlendMode/Alpha))
188 (.setQueueBucket RenderQueue$Bucket/Transparent)))
190 (defn transparent-box []
191 (doto
192 (make-shape
193 (merge base-shape
194 {:position (Vector3f. 0 2 0)
195 :name "box"
196 :material "Common/MatDefs/Misc/Unshaded.j3md"
197 :texture "Textures/purpleWisp.png"
198 :physical? true
199 :mass 70
200 :color ColorRGBA/Blue
201 :shape (Box. 1 1 1)}))
202 (-> (.getMaterial)
203 (.getAdditionalRenderState)
204 (.setBlendMode RenderState$BlendMode/Alpha))
205 (.setQueueBucket RenderQueue$Bucket/Transparent)))
207 (defn transparent-floor []
208 (doto
209 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
210 :material "Common/MatDefs/Misc/Unshaded.j3md"
211 :texture "Textures/redWisp.png"
212 :name "floor")
213 (-> (.getMaterial)
214 (.getAdditionalRenderState)
215 (.setBlendMode RenderState$BlendMode/Alpha))
216 (.setQueueBucket RenderQueue$Bucket/Transparent)))
218 (defn test-skin []
219 (let [b
220 ;;(transparent-box)
221 (transparent-sphere)
222 ;;(sphere)
223 f (transparent-floor)
224 ;;controls
225 ;;(make-touch-sphere b)
226 ;;(make-touch b)
227 debug-node (Node.)
228 node (doto (Node.) (.attachChild b) (.attachChild f))
229 root-node (doto (Node.) (.attachChild node)
230 (.attachChild debug-node))
231 ]
233 (world
234 root-node
235 {"key-return" (fire-cannon-ball node)}
236 ;;no-op
237 (fn [world]
238 ;; (Capture/SimpleCaptureVideo
239 ;; world
240 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
241 ;; (no-logging)
242 ;;(enable-debug world)
243 ;; (set-accuracy world (/ 1 60))
244 )
246 (fn [& _]
247 (let [sensitivity 5
248 touch-data (touch-percieve sensitivity b node)]
249 ;;(println-repl touch-data)
250 (manage-ray-debug-node debug-node b touch-data sensitivity)
251 )
252 (Thread/sleep 10)
253 ;;(touch-print controls)
254 ;;(color-touch controls)
255 ))))
257 #+end_src
259 #+results: skin-main
260 : #'body.skin/test-skin
269 * COMMENT code generation
270 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes
271 <<skin-main>>
272 #+end_src