Mercurial > cortex
view org/skin.org @ 10:0251b7e7609f
finally got raycasting tough to work
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 10:09:02 -0700 |
parents | 7d0c0282c3a7 |
children | a149692d3d1f |
line wrap: on
line source
1 #+title: SKIN!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating touch in JMonkeyEngine5 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org7 #+babel: :mkdirp yes :noweb yes9 let's see what checkboxes look like:11 * test [1/2]12 - [ ] item 113 - [X] item 216 * skin!18 #+srcname: skin-main19 #+begin_src clojure20 (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-sphere36 (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= and49 ;; =.getOverlappingObjects=51 ;; looks like I might be able to place a small "touch-sphere" whther52 ;; on every triangle in the object's mesh, or at the verticies, using53 ;; .getTriangleCount() on the mesh gotten by .getMesh()55 ;; this way, I can create a mesh and just divide up it's faces using56 ;; 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 (dorun64 (for [n (range (.getTriangleCount mesh))]65 (do66 (.getTriangle mesh n tri)67 (.calculateCenter tri)68 (let [control69 (doto70 (GhostControl.71 (doto (CompoundCollisionShape.)72 (.addChildShape73 (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 (let85 [mesh (.getMesh geom)86 triangles (transient [])]87 (dorun88 (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 (map99 (comp no-op #(.getCenter %))100 (triangles geom))))102 (defn get-ray-origin103 [geom tri]104 (let [new (Vector3f.)]105 (.calculateCenter tri)106 (.localToWorld geom (.getCenter tri) new)107 new))109 (defn get-ray-direction110 [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-debug119 [ray color]120 (make-shape121 (assoc base-shape122 :shape (Sphere. 5 5 0.05)123 :name "arrow"124 :color color125 :texture false126 :physical? false127 :position128 (.getOrigin ray))))130 (defn ray-debug [ray color]131 (make-shape132 (assoc133 base-shape134 :name "debug-ray"135 :physical? false136 :shape (com.jme3.scene.shape.Line.137 (.getOrigin ray)138 (.add139 (.getOrigin ray)140 (.mult (.getDirection ray)141 (float (.getLimit ray))))))))144 (defn contact-color [contacts]145 (case contacts146 0 ColorRGBA/Gray147 1 ColorRGBA/Blue148 2 ColorRGBA/Green149 3 ColorRGBA/Yellow150 4 ColorRGBA/Orange151 5 ColorRGBA/Red152 6 ColorRGBA/Magenta153 7 ColorRGBA/Pink154 8 ColorRGBA/White))156 (defn normal-rays157 "returns rays"158 [limit #^Geometry geom]159 (vec160 (map161 (fn [tri]162 (doto163 (Ray. (get-ray-origin geom tri)164 (get-ray-direction geom tri))166 (.setLimit limit)167 )168 )169 (triangles geom))))172 (defn collision-debug [node result]173 (println-repl "contact point: " (.getContactPoint result))174 )176 (defn touch-percieve [limit geom node debug-node]177 (let [normals (normal-rays limit geom)]178 (.detachAllChildren debug-node)179 (doall180 (for [ray normals]181 (do182 (let [results (CollisionResults.)]183 (.collideWith node ray results)184 (let [answer (count (filter #(not (= geom (.getGeometry %))) results))185 ;;color (contact-color answer)186 ]187 ;;(dorun (map #(println-repl (.getName (.getGeometry %))) results))188 ;;(.attachChild debug-node (ray-debug ray color))189 ;;(.attachChild debug-node (ray-origin-debug ray color))192 ;;(println-repl (.size results) "results for " ray)193 ;;(doall (map (partial collision-debug node) results))194 answer195 )))))))197 (defn make-touch [#^Geometry geom]198 (let [tri (Triangle.)199 mesh (.getMesh geom)200 controls! (transient [])]201 (dorun202 (for [n (range (.getTriangleCount mesh))]203 (do204 (.getTriangle mesh n tri)205 (.calculateCenter tri)206 (.calculateNormal tri)207 ;; (println-repl tri)208 ;; (println-repl (.get1 tri))209 ;; (println-repl (.get2 tri))210 ;; (println-repl (.get3 tri))211 ;; (println-repl (.getCenter tri))212 ;; (println-repl (.getNormal tri))213 (let [control214 (doto215 (GhostControl.217 (doto (CompoundCollisionShape.)218 (.addChildShape219 (SimplexCollisionShape. Vector3f/ZERO)220 (.mult (.getCenter tri) (float 1)))221 (.setMargin 0)222 ))223 (.setApplyPhysicsLocal true))]224 (.addControl geom control)225 (conj! controls! control)))))226 (persistent! controls!)))228 (use 'hello.brick-wall)235 (defn touch-reception [controls]236 (let [control237 (first238 (filter239 #(< 2 (.getOverlappingCount %)) controls))]240 (if (not (nil? control))241 (println242 (seq243 (.getOverlappingObjects control))))))245 (defn touch-print [controls]246 (println247 (map #(count (.getNonGhostOverlappingObjects %)) controls)))249 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]250 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))252 (defn html-color [html-str]253 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))254 (map #(float (/ (Integer/parseInt % 16) 255))255 (map (partial apply str) (partition 2 html-str)))))257 (defn color-touch [controls]258 (no-exceptions259 (dorun260 (map261 (fn [control]262 (case (count (.getNonGhostOverlappingObjects control))263 0 (set-debug-color ColorRGBA/Gray control)264 1 (set-debug-color ColorRGBA/Blue control)265 2 (set-debug-color ColorRGBA/Green control)266 3 (set-debug-color ColorRGBA/Yellow control)267 4 (set-debug-color ColorRGBA/Orange control)268 5 (set-debug-color ColorRGBA/Red control)269 6 (set-debug-color ColorRGBA/Magenta control)270 7 (set-debug-color ColorRGBA/Pink control)271 8 (set-debug-color ColorRGBA/White control)))272 controls))))274 (defn enable-debug [world]275 (.enableDebug276 (.getPhysicsSpace277 (.getState278 (.getStateManager world)279 BulletAppState))280 (asset-manager)))282 (defn transparent-sphere []283 (doto284 (make-shape285 (merge base-shape286 {:position (Vector3f. 0 2 0)287 :name "the blob."288 :material "Common/MatDefs/Misc/Unshaded.j3md"289 :texture "Textures/purpleWisp.png"290 :physical? true291 :mass 70292 :color ColorRGBA/Blue293 :shape (Sphere. 10 10 1)}))294 (-> (.getMaterial)295 (.getAdditionalRenderState)296 (.setBlendMode RenderState$BlendMode/Alpha))297 (.setQueueBucket RenderQueue$Bucket/Transparent)))299 (defn transparent-box []300 (doto301 (make-shape302 (merge base-shape303 {:position (Vector3f. 0 2 0)304 :name "box"305 :material "Common/MatDefs/Misc/Unshaded.j3md"306 :texture "Textures/purpleWisp.png"307 :physical? true308 :mass 70309 :color ColorRGBA/Blue310 :shape (Box. 1 1 1)}))311 (-> (.getMaterial)312 (.getAdditionalRenderState)313 (.setBlendMode RenderState$BlendMode/Alpha))314 (.setQueueBucket RenderQueue$Bucket/Transparent)))317 (defn transparent-floor []318 (doto319 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)320 :material "Common/MatDefs/Misc/Unshaded.j3md"321 :texture "Textures/redWisp.png"322 :name "floor")323 (-> (.getMaterial)324 (.getAdditionalRenderState)325 (.setBlendMode RenderState$BlendMode/Alpha))326 (.setQueueBucket RenderQueue$Bucket/Transparent)))329 (defn no-logging []330 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))332 (defn set-accuracy [world new-accuracy]333 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]334 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))336 (defn test-skin []337 (let [b338 (transparent-box)339 ;;(transparent-sphere)340 ;;(sphere)341 f (transparent-floor)342 ;;controls343 ;;(make-touch-sphere b)344 ;;(make-touch b)345 debug-node (Node.)346 node (doto (Node.) (.attachChild b) (.attachChild f)347 (.attachChild debug-node))349 ]351 (world353 node354 {"key-return" (fire-cannon-ball)355 "key-space" (fn [game value]356 (if value357 (println-repl (touch-percieve 0.2 b node debug-node))))358 }359 ;;no-op360 (fn [world]361 ;; (Capture/SimpleCaptureVideo362 ;; world363 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))364 ;; (no-logging)365 (enable-debug world)366 ;; (set-accuracy world (/ 1 60))367 )369 (fn [& _]370 ;;(println-repl371 (touch-percieve 0.2 b node debug-node)372 ;;(Thread/sleep 10)373 ;;(touch-print controls)374 ;;(color-touch controls)375 ))))377 #+end_src379 #+results: skin-main380 : #'body.skin/test-skin389 * COMMENT code generation390 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes391 <<skin-main>>392 #+end_src