Mercurial > cortex
view org/skin.org @ 6:e3c6d1c1cb00
fixing ray casting problem
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 00:17:54 -0700 |
parents | 93ff2b4e7e6a |
children | 9ccfbcbed90a |
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))))105 (defn normal-arrows106 "returns a node containing arrows which point107 in the normal direction of each face of the108 given Geometry"109 [#^Geometry geom]110 (let [node (Node.)]111 (dorun112 (map #(.attachChild node %)113 (map114 (fn [tri]115 (make-shape116 (assoc base-shape117 :shape (Sphere. 5 5 0.05)118 :name "arrow"119 :color ColorRGBA/Orange120 :texture false121 :asset-manager (asset-manager)122 :physical? false123 ;;:rotation124 ;;(.mult125 ;; (doto (Quaternion.)126 ;; (.lookAt (.getNormal tri)127 ;; Vector3f/UNIT_Y))128 ;; (.getLocalRotation geom))129 :position130 (.add131 (.getCenter tri)132 (.getLocalTranslation geom)))))133 (triangles geom))))134 node))137 (defn get-ray-origin138 [geom tri]139 (.add140 (.getCenter tri)141 (.getLocalTranslation geom)))143 (defn get-ray-direction144 [geom tri]145 (.mult (.getLocalRotation geom)146 (.getNormal tri)))148 (defn ray-debug [ray]149 (make-shape150 (assoc151 base-shape152 :name "debug-ray"153 :physical? false154 :shape (com.jme3.scene.shape.Line.155 (.getOrigin ray)156 (.add157 (.getOrigin ray)158 (.mult (.getDirection ray)159 (float (.getLimit ray))))))))164 (defn normal-rays165 "returns rays"166 [limit #^Geometry geom]167 (vec168 (map169 (fn [tri]170 (doto171 (Ray. (get-ray-origin geom tri)172 (get-ray-direction geom tri))174 (.setLimit limit)))175 (triangles geom))))178 (defn collision-debug [node result]180 (println-repl "contact point: " (.getContactPoint result))183 )185 (defn touch-percieve [limit geom node debug-node]186 (let [normals (normal-rays limit geom)]187 (.detachAllChildren debug-node)188 (.attachChild debug-node (normal-arrows geom))190 (println-repl "---------")191 (doall192 (for [ray normals]193 (do194 (let [results (CollisionResults.)]195 (.attachChild debug-node (ray-debug ray))196 (.collideWith geom ray results)198 ;;(println-repl (.size results) "results for " ray)199 ;;(doall (map (partial collision-debug node) results))200 (.size results)201 ))))))203 (defn arrow-view [obj]204 (view (doto (Node.)205 (.attachChild (normal-arrows obj))206 (.attachChild obj))))209 (defn make-touch [#^Geometry geom]210 (let [tri (Triangle.)211 mesh (.getMesh geom)212 controls! (transient [])]213 (dorun214 (for [n (range (.getTriangleCount mesh))]215 (do216 (.getTriangle mesh n tri)217 (.calculateCenter tri)218 (.calculateNormal tri)219 ;; (println-repl tri)220 ;; (println-repl (.get1 tri))221 ;; (println-repl (.get2 tri))222 ;; (println-repl (.get3 tri))223 ;; (println-repl (.getCenter tri))224 ;; (println-repl (.getNormal tri))225 (let [control226 (doto227 (GhostControl.229 (doto (CompoundCollisionShape.)230 (.addChildShape231 (SimplexCollisionShape. Vector3f/ZERO)232 (.mult (.getCenter tri) (float 1)))233 (.setMargin 0)234 ))235 (.setApplyPhysicsLocal true))]236 (.addControl geom control)237 (conj! controls! control)))))238 (persistent! controls!)))240 (use 'hello.brick-wall)242 (defn touch-reception [controls]243 (let [control244 (first245 (filter246 #(< 2 (.getOverlappingCount %)) controls))]247 (if (not (nil? control))248 (println249 (seq250 (.getOverlappingObjects control))))))252 (defn touch-print [controls]253 (println254 (map #(count (.getNonGhostOverlappingObjects %)) controls)))256 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]257 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))259 (defn html-color [html-str]260 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))261 (map #(float (/ (Integer/parseInt % 16) 255))262 (map (partial apply str) (partition 2 html-str)))))264 (defn color-touch [controls]265 (no-exceptions266 (dorun267 (map268 (fn [control]269 (case (count (.getNonGhostOverlappingObjects control))270 0 (set-debug-color ColorRGBA/Gray control)271 1 (set-debug-color ColorRGBA/Blue control)272 2 (set-debug-color ColorRGBA/Green control)273 3 (set-debug-color ColorRGBA/Yellow control)274 4 (set-debug-color ColorRGBA/Orange control)275 5 (set-debug-color ColorRGBA/Red control)276 6 (set-debug-color ColorRGBA/Magenta control)277 7 (set-debug-color ColorRGBA/Pink control)278 8 (set-debug-color ColorRGBA/White control)))279 controls))))281 (defn enable-debug [world]282 (.enableDebug283 (.getPhysicsSpace284 (.getState285 (.getStateManager world)286 BulletAppState))287 (asset-manager)))289 (defn transparent-sphere []290 (doto291 (make-shape292 (merge base-shape293 {:position (Vector3f. 0 2 0)294 :name "the blob."295 :material "Common/MatDefs/Misc/Unshaded.j3md"296 :texture "Textures/purpleWisp.png"297 :physical? true298 :mass 70299 :color ColorRGBA/Blue300 :shape (Sphere. 10 10 1)}))301 (-> (.getMaterial)302 (.getAdditionalRenderState)303 (.setBlendMode RenderState$BlendMode/Alpha))304 (.setQueueBucket RenderQueue$Bucket/Transparent)))306 (defn transparent-box []307 (doto308 (make-shape309 (merge base-shape310 {:position (Vector3f. 0 2 0)311 :name "the blob."312 :material "Common/MatDefs/Misc/Unshaded.j3md"313 :texture "Textures/purpleWisp.png"314 :physical? true315 :mass 70316 :color ColorRGBA/Blue317 :shape (Box. 1 1 1)}))318 (-> (.getMaterial)319 (.getAdditionalRenderState)320 (.setBlendMode RenderState$BlendMode/Alpha))321 (.setQueueBucket RenderQueue$Bucket/Transparent)))324 (defn transparent-floor []325 (doto326 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)327 :material "Common/MatDefs/Misc/Unshaded.j3md"328 :texture "Textures/redWisp.png")329 (-> (.getMaterial)330 (.getAdditionalRenderState)331 (.setBlendMode RenderState$BlendMode/Alpha))332 (.setQueueBucket RenderQueue$Bucket/Transparent)))335 (defn no-logging []336 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))338 (defn set-accuracy [world new-accuracy]339 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]340 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))342 (defn test-skin []343 (let [b344 ;;(transparent-sphere)345 (transparent-box)346 f (transparent-floor)347 ;;controls348 ;;(make-touch-sphere b)349 ;;(make-touch b)350 debug-node (Node.)351 node (doto (Node.) (.attachChild b) (.attachChild f)352 (.attachChild debug-node))354 ]356 (world358 node359 {"key-return" (fire-cannon-ball)360 "key-space" (fn [game value]361 (println-repl (touch-percieve 1 b node debug-node)))362 }363 ;;no-op364 (fn [world]365 ;; (Capture/SimpleCaptureVideo366 ;; world367 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))368 ;; (no-logging)369 (enable-debug world)370 ;; (set-accuracy world (/ 1 60))371 )373 (fn [& _]374 (Thread/sleep 10)375 ;;(touch-print controls)376 ;;(color-touch controls)377 ))))379 #+end_src381 #+results: skin-main382 : #'body.skin/test-skin389 * COMMENT code generation390 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes391 <<skin-main>>392 #+end_src