Mercurial > cortex
view org/skin.org @ 7:9ccfbcbed90a
refactored to help fix ray casting problem
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 00:49:33 -0700 |
parents | e3c6d1c1cb00 |
children | 522cf85fdb57 |
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 (.get tri 1) new)107 new))109 (defn get-ray-direction110 [geom tri]111 (let [new (Vector3f.)]112 (.calculateNormal tri)113 (.localToWorld geom (.getNormal tri) new)114 new115 Vector3f/UNIT_Y))117 (defn ray-origin-debug118 [ray]119 (make-shape120 (assoc base-shape121 :shape (Sphere. 5 5 0.05)122 :name "arrow"123 :color ColorRGBA/Orange124 :texture false125 :physical? false126 :position127 (.getOrigin ray))))129 (defn ray-debug [ray]130 (make-shape131 (assoc132 base-shape133 :name "debug-ray"134 :physical? false135 :shape (com.jme3.scene.shape.Line.136 (.getOrigin ray)137 (.add138 (.getOrigin ray)139 (.mult (.getDirection ray)140 (float (.getLimit ray))))))))145 (defn normal-rays146 "returns rays"147 [limit #^Geometry geom]148 (vec149 (map150 (fn [tri]151 (doto152 (Ray. (get-ray-origin geom tri)153 (get-ray-direction geom tri))155 (.setLimit limit)))156 (triangles geom))))159 (defn collision-debug [node result]161 (println-repl "contact point: " (.getContactPoint result))164 )166 (defn touch-percieve [limit geom node debug-node]167 (let [normals (normal-rays limit geom)]168 (.detachAllChildren debug-node)171 (println-repl "---------")172 (doall173 (for [ray normals]174 (do175 (let [results (CollisionResults.)]176 (.attachChild debug-node (ray-debug ray))177 (.attachChild debug-node (ray-origin-debug ray))178 (.collideWith geom ray results)180 ;;(println-repl (.size results) "results for " ray)181 ;;(doall (map (partial collision-debug node) results))182 (.size results)183 ))))))185 (defn make-touch [#^Geometry geom]186 (let [tri (Triangle.)187 mesh (.getMesh geom)188 controls! (transient [])]189 (dorun190 (for [n (range (.getTriangleCount mesh))]191 (do192 (.getTriangle mesh n tri)193 (.calculateCenter tri)194 (.calculateNormal tri)195 ;; (println-repl tri)196 ;; (println-repl (.get1 tri))197 ;; (println-repl (.get2 tri))198 ;; (println-repl (.get3 tri))199 ;; (println-repl (.getCenter tri))200 ;; (println-repl (.getNormal tri))201 (let [control202 (doto203 (GhostControl.205 (doto (CompoundCollisionShape.)206 (.addChildShape207 (SimplexCollisionShape. Vector3f/ZERO)208 (.mult (.getCenter tri) (float 1)))209 (.setMargin 0)210 ))211 (.setApplyPhysicsLocal true))]212 (.addControl geom control)213 (conj! controls! control)))))214 (persistent! controls!)))216 (use 'hello.brick-wall)218 (defn touch-reception [controls]219 (let [control220 (first221 (filter222 #(< 2 (.getOverlappingCount %)) controls))]223 (if (not (nil? control))224 (println225 (seq226 (.getOverlappingObjects control))))))228 (defn touch-print [controls]229 (println230 (map #(count (.getNonGhostOverlappingObjects %)) controls)))232 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]233 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))235 (defn html-color [html-str]236 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))237 (map #(float (/ (Integer/parseInt % 16) 255))238 (map (partial apply str) (partition 2 html-str)))))240 (defn color-touch [controls]241 (no-exceptions242 (dorun243 (map244 (fn [control]245 (case (count (.getNonGhostOverlappingObjects control))246 0 (set-debug-color ColorRGBA/Gray control)247 1 (set-debug-color ColorRGBA/Blue control)248 2 (set-debug-color ColorRGBA/Green control)249 3 (set-debug-color ColorRGBA/Yellow control)250 4 (set-debug-color ColorRGBA/Orange control)251 5 (set-debug-color ColorRGBA/Red control)252 6 (set-debug-color ColorRGBA/Magenta control)253 7 (set-debug-color ColorRGBA/Pink control)254 8 (set-debug-color ColorRGBA/White control)))255 controls))))257 (defn enable-debug [world]258 (.enableDebug259 (.getPhysicsSpace260 (.getState261 (.getStateManager world)262 BulletAppState))263 (asset-manager)))265 (defn transparent-sphere []266 (doto267 (make-shape268 (merge base-shape269 {:position (Vector3f. 0 2 0)270 :name "the blob."271 :material "Common/MatDefs/Misc/Unshaded.j3md"272 :texture "Textures/purpleWisp.png"273 :physical? true274 :mass 70275 :color ColorRGBA/Blue276 :shape (Sphere. 10 10 1)}))277 (-> (.getMaterial)278 (.getAdditionalRenderState)279 (.setBlendMode RenderState$BlendMode/Alpha))280 (.setQueueBucket RenderQueue$Bucket/Transparent)))282 (defn transparent-box []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 (Box. 1 1 1)}))294 (-> (.getMaterial)295 (.getAdditionalRenderState)296 (.setBlendMode RenderState$BlendMode/Alpha))297 (.setQueueBucket RenderQueue$Bucket/Transparent)))300 (defn transparent-floor []301 (doto302 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)303 :material "Common/MatDefs/Misc/Unshaded.j3md"304 :texture "Textures/redWisp.png")305 (-> (.getMaterial)306 (.getAdditionalRenderState)307 (.setBlendMode RenderState$BlendMode/Alpha))308 (.setQueueBucket RenderQueue$Bucket/Transparent)))311 (defn no-logging []312 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))314 (defn set-accuracy [world new-accuracy]315 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]316 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))318 (defn test-skin []319 (let [b320 ;;(transparent-sphere)321 (transparent-box)322 f (transparent-floor)323 ;;controls324 ;;(make-touch-sphere b)325 ;;(make-touch b)326 debug-node (Node.)327 node (doto (Node.) (.attachChild b) (.attachChild f)328 (.attachChild debug-node))330 ]332 (world334 node335 {"key-return" (fire-cannon-ball)336 "key-space" (fn [game value]337 (println-repl (touch-percieve 1 b node debug-node)))338 }339 ;;no-op340 (fn [world]341 ;; (Capture/SimpleCaptureVideo342 ;; world343 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))344 ;; (no-logging)345 (enable-debug world)346 ;; (set-accuracy world (/ 1 60))347 )349 (fn [& _]350 (Thread/sleep 10)351 ;;(touch-print controls)352 ;;(color-touch controls)353 ))))355 #+end_src357 #+results: skin-main358 : #'body.skin/test-skin365 * COMMENT code generation366 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes367 <<skin-main>>368 #+end_src