Mercurial > cortex
view org/util.org @ 28:122f12f81dc1
going to work on util.org now
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 24 Oct 2011 07:36:04 -0700 |
parents | bbffa41a12a9 |
children | 6372c108c5c6 |
line wrap: on
line source
1 #+title: Helper Utilities2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating senses for AI research using JMonkeyEngine35 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org7 #+babel: :mkdirp yes :noweb yes :exports both9 ** Imports11 jMonkeyEngine has a plethora of classes which can be overwhelming at12 first. So that I one can get right to coding, it's good to take the13 time right now and make a "import all" function which brings in all of14 the important jme3 classes. Once I'm happy with the general structure15 of a namespace I can deal with importing only the classes it actually16 needs.18 #+srcname: import19 #+begin_src clojure :results silent20 (ns cortex.import21 (:require swank.util.class-browse))23 (defn permissive-import24 [classname]25 (eval `(try (import '~classname)26 (catch java.lang.Exception e#27 (println "couldn't import " '~classname))))28 classname)30 (defn jme-class? [classname]31 (and32 (.startsWith classname "com.jme3.")33 ;; Don't import the Lwjgl stuff since it can throw exceptions34 ;; upon being loaded.35 (not (re-matches #".*Lwjgl.*" classname))))37 (defn jme-classes38 "returns a list of all jme3 classes"39 []40 (filter41 jme-class?42 (map :name43 swank.util.class-browse/available-classes)))45 (defn mega-import-jme346 "Import ALL the jme classes. For REPL use."47 []48 (doall49 (map (comp permissive-import symbol) (jme-classes))))50 #+end_src52 The =mega-import-jme3= is quite usefull for debugging purposes since53 it allows completion for almost all of JME's classes.55 Out of curiousity, let's see just how many classes =mega-import-jme3=56 imports:58 #+begin_src clojure :exports both59 (clojure.core/count (cortex.import/jme-classes))60 #+end_src62 #+results:63 : 95566 #+srcname: world-view67 #+begin_src clojure :results silent68 (ns cortex.util)69 (require 'cortex.import)70 (cortex.import/mega-import-jme3)71 (use 'cortex.world)72 (defprotocol Viewable73 (view [something]))75 (extend-type com.jme3.scene.Geometry76 Viewable77 (view [geo]78 (view (doto (Node.)(.attachChild geo)))))80 (extend-type com.jme3.scene.Node81 Viewable82 (view [node]83 (.start84 (world node85 {}86 (fn [world]87 (.enableDebug88 (.getPhysicsSpace89 (.getState90 (.getStateManager world)91 BulletAppState))92 (asset-manager))93 (set-gravity* world Vector3f/ZERO)94 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))95 (let [sun (doto (DirectionalLight.)96 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))97 (.setColor ColorRGBA/White))]98 (.addLight (.getRootNode world) sun)))99 no-op))))100 #+end_src102 Here I make the =Viewable= protocol and extend it to JME's types. Now103 hello-world can be written as easily as:105 #+begin_src clojure :results silent106 (cortex.world/view (cortex.world/box))107 #+end_src110 #+srcname: util111 #+begin_src clojure112 (in-ns 'cortex.util)114 (def println-repl (bound-fn [& args] (apply println args)))116 (defn position-camera [game]117 (doto (.getCamera game)118 (.setLocation (Vector3f. 0 6 6))119 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))121 (defn set-gravity*122 [game gravity]123 (traverse124 (fn [geom]125 (if-let126 [control (.getControl geom RigidBodyControl)]127 (do128 (.setGravity control gravity)129 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)130 )))131 (.getRootNode game)))132 #+end_src135 #+srcname: shapes136 #+begin_src clojure :results silent137 (in-ns 'cortex.util)138 (defrecord shape-description139 [name140 color141 mass142 friction143 texture144 material145 position146 rotation147 shape148 physical?])150 (def base-shape151 (shape-description.152 "default-shape"153 false154 ;;ColorRGBA/Blue155 1.0 ;; mass156 1.0 ;; friction157 ;; texture158 "Textures/Terrain/BrickWall/BrickWall.jpg"159 ;; material160 "Common/MatDefs/Misc/Unshaded.j3md"161 Vector3f/ZERO162 Quaternion/IDENTITY163 (Box. Vector3f/ZERO 0.5 0.5 0.5)164 true))166 (defn make-shape167 [#^shape-description d]168 (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))169 mat (Material. asset-manager (:material d))170 geom (Geometry. (:name d) (:shape d))]171 (if (:texture d)172 (let [key (TextureKey. (:texture d))]173 (.setGenerateMips key true)174 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))))175 (if (:color d) (.setColor mat "Color" (:color d)))176 (.setMaterial geom mat)177 (if-let [rotation (:rotation d)] (.rotate geom rotation))178 (.setLocalTranslation geom (:position d))179 (if (:physical? d)180 (let [impact-shape (doto (GImpactCollisionShape.181 (.getMesh geom)) (.setMargin 0))182 physics-control (RigidBodyControl.183 ;;impact-shape ;; comment to disable184 (float (:mass d)))]185 (.createJmeMesh impact-shape)186 (.addControl geom physics-control)187 ;;(.setSleepingThresholds physics-control (float 0) (float 0))188 (.setFriction physics-control (:friction d))))189 ;;the default is to keep this node in the physics engine forever.190 ;;these commands must come after the control is added to the geometry.191 ;;192 geom))194 (defn box195 ([l w h & {:as options}]196 (let [options (merge base-shape options)]197 (make-shape (assoc options198 :shape (Box. l w h)))))199 ([] (box 0.5 0.5 0.5)))201 (defn sphere202 ([r & {:as options}]203 (let [options (merge base-shape options)]204 (make-shape (assoc options205 :shape (Sphere. 32 32 (float r))))))206 ([] (sphere 0.5)))208 (defn add-element209 ([game element node]210 (.addAll211 (.getPhysicsSpace212 (.getState213 (.getStateManager game)214 BulletAppState))215 element)216 (.attachChild node element))217 ([game element]218 (add-element game element (.getRootNode game))))221 (defn apply-map222 "Like apply, but works for maps and functions that expect an223 implicit map and nothing else as in (fn [& {}]).224 ------- Example -------225 (defn demo [& {:keys [www] :or {www \"oh yeah\"} :as env}]226 (println www))227 (apply-map demo {:www \"hello!\"})228 -->\"hello\""229 [fn m]230 (apply fn (reduce #(into %1 %2) [] m)))232 #+end_src236 * COMMENT code generation237 #+begin_src clojure :tangle ../src/cortex/import.clj238 <<import>>239 #+end_src242 #+begin_src clojure :tangle ../src/cortex/util.clj243 <<world-view>>244 <<util>>245 <<shapes>>246 #+end_src