diff org/util.org @ 23:cab2da252494

split off the rest of cortex.org
author Robert McIntyre <rlm@mit.edu>
date Sun, 23 Oct 2011 23:54:26 -0700
parents
children e965675ec4d0
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/util.org	Sun Oct 23 23:54:26 2011 -0700
     1.3 @@ -0,0 +1,110 @@
     1.4 +#+title: Helper Utilities
     1.5 +#+author: Robert McIntyre
     1.6 +#+email: rlm@mit.edu
     1.7 +#+description: Simulating senses for AI research using JMonkeyEngine3
     1.8 +#+SETUPFILE: ../../aurellem/org/setup.org
     1.9 +#+INCLUDE: ../../aurellem/org/level-0.org
    1.10 +#+babel: :mkdirp yes :noweb yes :exports both
    1.11 +
    1.12 +** Imports
    1.13 +jMonkeyEngine has a plethora of classes which can be overwhelming at
    1.14 +first. So that I one can get right to coding, it's good to take the
    1.15 +time right now and make a "import all" function which brings in all of
    1.16 +the important jme3 classes. Once I'm happy with the general structure
    1.17 +of a namespace I can deal with importing only the classes it actually
    1.18 +needs.
    1.19 +
    1.20 +#+srcname: import
    1.21 +#+begin_src clojure :results silent
    1.22 +(ns cortex.import
    1.23 +  (:require swank.util.class-browse))
    1.24 +
    1.25 +(defn permissive-import
    1.26 +  [classname]
    1.27 +  (eval `(try (import '~classname)
    1.28 +              (catch java.lang.Exception e#
    1.29 +                (println "couldn't import " '~classname))))
    1.30 +  classname)
    1.31 +
    1.32 +(defn jme-class? [classname]
    1.33 +  (and
    1.34 +   (.startsWith classname "com.jme3.")
    1.35 +   ;; Don't import the Lwjgl stuff since it can throw exceptions
    1.36 +   ;;  upon being loaded.
    1.37 +   (not (re-matches #".*Lwjgl.*" classname))))
    1.38 +
    1.39 +(defn jme-classes
    1.40 +  "returns a list of all jme3 classes"
    1.41 +  []
    1.42 +  (filter
    1.43 +   jme-class?
    1.44 +   (map :name
    1.45 +        swank.util.class-browse/available-classes)))
    1.46 +  
    1.47 +(defn mega-import-jme3
    1.48 +  "Import ALL the jme classes. For REPL use."
    1.49 +  []
    1.50 +  (doall
    1.51 +   (map (comp permissive-import symbol) (jme-classes))))
    1.52 +#+end_src  
    1.53 +
    1.54 +The =mega-import-jme3= is quite usefull for debugging purposes since
    1.55 +it allows completion for almost all of JME's classes.
    1.56 +
    1.57 +Out of curiousity, let's see just how many classes =mega-import-jme3=
    1.58 +imports:
    1.59 +
    1.60 +#+begin_src clojure :exports both
    1.61 +(clojure.core/count (cortex.import/jme-classes))
    1.62 +#+end_src
    1.63 +
    1.64 +#+results:
    1.65 +: 955
    1.66 +
    1.67 +** Simplification
    1.68 +#+srcname: world-view
    1.69 +#+begin_src clojure :results silent
    1.70 +(in-ns 'cortex.world)
    1.71 +
    1.72 +(defprotocol Viewable
    1.73 +  (view [something]))
    1.74 +
    1.75 +(extend-type com.jme3.scene.Geometry
    1.76 +  Viewable
    1.77 +  (view [geo]
    1.78 +	(view (doto (Node.)(.attachChild geo)))))
    1.79 +
    1.80 +(extend-type com.jme3.scene.Node
    1.81 +  Viewable
    1.82 +  (view [node]
    1.83 +	(.start
    1.84 +	 (world node
    1.85 +		{}
    1.86 +		(fn [world]
    1.87 +		  (.enableDebug 
    1.88 +		   (.getPhysicsSpace
    1.89 +		    (.getState
    1.90 +		     (.getStateManager world)
    1.91 +		     BulletAppState))
    1.92 +		   (asset-manager))
    1.93 +		  (set-gravity* world Vector3f/ZERO)
    1.94 +;;		  (set-gravity* world (Vector3f. 0 (float -0.4) 0))
    1.95 +		  (let [sun (doto (DirectionalLight.)
    1.96 +			      (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
    1.97 +			      (.setColor ColorRGBA/White))]
    1.98 +		    (.addLight (.getRootNode world) sun)))
    1.99 +		no-op))))
   1.100 +
   1.101 +(defn position-camera [game]
   1.102 +  (doto (.getCamera game)
   1.103 +    (.setLocation (Vector3f. 0 6 6))
   1.104 +    (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
   1.105 +
   1.106 +#+end_src
   1.107 +
   1.108 +Here I make the =Viewable= protocol and extend it to JME's types.  Now
   1.109 +hello-world can be written as easily as:
   1.110 +
   1.111 +#+begin_src clojure :results silent
   1.112 +(cortex.world/view (cortex.world/box))
   1.113 +#+end_src