Mercurial > cortex
view org/skin.org @ 9:7d0c0282c3a7
got positioning right, but there is still a problem
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 23 Oct 2011 01:27:20 -0700 |
parents | 522cf85fdb57 |
children | 0251b7e7609f |
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))))))))146 (defn normal-rays147 "returns rays"148 [limit #^Geometry geom]149 (vec150 (map151 (fn [tri]152 (doto153 (Ray. (get-ray-origin geom tri)154 (get-ray-direction geom tri))156 (.setLimit limit)))157 (triangles geom))))160 (defn collision-debug [node result]161 (println-repl "contact point: " (.getContactPoint result))162 )164 (defn touch-percieve [limit geom node debug-node]165 (let [normals (normal-rays limit geom)]166 (.detachAllChildren debug-node)167 (doall168 (for [ray normals]169 (do170 (let [results (CollisionResults.)]171 (.collideWith geom ray results)172 (let [color (contact-color (.size results))]174 (.attachChild debug-node (ray-debug ray color))175 (.attachChild debug-node (ray-origin-debug ray color))176 )178 (println-repl (.size results) "results for " ray)179 (doall (map (partial collision-debug node) results))180 (.size results)181 ))))))183 (defn make-touch [#^Geometry geom]184 (let [tri (Triangle.)185 mesh (.getMesh geom)186 controls! (transient [])]187 (dorun188 (for [n (range (.getTriangleCount mesh))]189 (do190 (.getTriangle mesh n tri)191 (.calculateCenter tri)192 (.calculateNormal tri)193 ;; (println-repl tri)194 ;; (println-repl (.get1 tri))195 ;; (println-repl (.get2 tri))196 ;; (println-repl (.get3 tri))197 ;; (println-repl (.getCenter tri))198 ;; (println-repl (.getNormal tri))199 (let [control200 (doto201 (GhostControl.203 (doto (CompoundCollisionShape.)204 (.addChildShape205 (SimplexCollisionShape. Vector3f/ZERO)206 (.mult (.getCenter tri) (float 1)))207 (.setMargin 0)208 ))209 (.setApplyPhysicsLocal true))]210 (.addControl geom control)211 (conj! controls! control)))))212 (persistent! controls!)))214 (use 'hello.brick-wall)216 (defn touch-reception [controls]217 (let [control218 (first219 (filter220 #(< 2 (.getOverlappingCount %)) controls))]221 (if (not (nil? control))222 (println223 (seq224 (.getOverlappingObjects control))))))226 (defn touch-print [controls]227 (println228 (map #(count (.getNonGhostOverlappingObjects %)) controls)))230 (defn set-debug-color [#^ColorRGBA color #^GhostControl gc]231 (.setColor (.getMaterial (.getChild (.getDebugShape gc) 0)) "Color" color))233 (defn html-color [html-str]234 ((fn [[r g b]] (ColorRGBA. r g b (float 1)))235 (map #(float (/ (Integer/parseInt % 16) 255))236 (map (partial apply str) (partition 2 html-str)))))238 (defn color-touch [controls]239 (no-exceptions240 (dorun241 (map242 (fn [control]243 (case (count (.getNonGhostOverlappingObjects control))244 0 (set-debug-color ColorRGBA/Gray control)245 1 (set-debug-color ColorRGBA/Blue control)246 2 (set-debug-color ColorRGBA/Green control)247 3 (set-debug-color ColorRGBA/Yellow control)248 4 (set-debug-color ColorRGBA/Orange control)249 5 (set-debug-color ColorRGBA/Red control)250 6 (set-debug-color ColorRGBA/Magenta control)251 7 (set-debug-color ColorRGBA/Pink control)252 8 (set-debug-color ColorRGBA/White control)))253 controls))))255 (defn enable-debug [world]256 (.enableDebug257 (.getPhysicsSpace258 (.getState259 (.getStateManager world)260 BulletAppState))261 (asset-manager)))263 (defn transparent-sphere []264 (doto265 (make-shape266 (merge base-shape267 {:position (Vector3f. 0 2 0)268 :name "the blob."269 :material "Common/MatDefs/Misc/Unshaded.j3md"270 :texture "Textures/purpleWisp.png"271 :physical? true272 :mass 70273 :color ColorRGBA/Blue274 :shape (Sphere. 10 10 1)}))275 (-> (.getMaterial)276 (.getAdditionalRenderState)277 (.setBlendMode RenderState$BlendMode/Alpha))278 (.setQueueBucket RenderQueue$Bucket/Transparent)))280 (defn transparent-box []281 (doto282 (make-shape283 (merge base-shape284 {:position (Vector3f. 0 2 0)285 :name "the blob."286 :material "Common/MatDefs/Misc/Unshaded.j3md"287 :texture "Textures/purpleWisp.png"288 :physical? true289 :mass 70290 :color ColorRGBA/Blue291 :shape (Box. 1 1 1)}))292 (-> (.getMaterial)293 (.getAdditionalRenderState)294 (.setBlendMode RenderState$BlendMode/Alpha))295 (.setQueueBucket RenderQueue$Bucket/Transparent)))298 (defn transparent-floor []299 (doto300 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)301 :material "Common/MatDefs/Misc/Unshaded.j3md"302 :texture "Textures/redWisp.png")303 (-> (.getMaterial)304 (.getAdditionalRenderState)305 (.setBlendMode RenderState$BlendMode/Alpha))306 (.setQueueBucket RenderQueue$Bucket/Transparent)))309 (defn no-logging []310 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))312 (defn set-accuracy [world new-accuracy]313 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]314 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))316 (defn test-skin []317 (let [b318 ;;(transparent-sphere)319 (transparent-box)320 f (transparent-floor)321 ;;controls322 ;;(make-touch-sphere b)323 ;;(make-touch b)324 debug-node (Node.)325 node (doto (Node.) (.attachChild b) (.attachChild f)326 (.attachChild debug-node))328 ]330 (world332 node333 {"key-return" (fire-cannon-ball)334 "key-space" (fn [game value]335 (println-repl (touch-percieve 1 b node debug-node)))336 }337 ;;no-op338 (fn [world]339 ;; (Capture/SimpleCaptureVideo340 ;; world341 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))342 ;; (no-logging)343 (enable-debug world)344 ;; (set-accuracy world (/ 1 60))345 )347 (fn [& _]348 (Thread/sleep 10)349 ;;(touch-print controls)350 ;;(color-touch controls)351 ))))353 #+end_src355 #+results: skin-main356 : #'body.skin/test-skin363 * COMMENT code generation364 #+begin_src clojure :tangle ../src/body/skin.clj :noweb yes365 <<skin-main>>366 #+end_src