Mercurial > cortex
view 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 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 ** Imports10 jMonkeyEngine has a plethora of classes which can be overwhelming at11 first. So that I one can get right to coding, it's good to take the12 time right now and make a "import all" function which brings in all of13 the important jme3 classes. Once I'm happy with the general structure14 of a namespace I can deal with importing only the classes it actually15 needs.17 #+srcname: import18 #+begin_src clojure :results silent19 (ns cortex.import20 (:require swank.util.class-browse))22 (defn permissive-import23 [classname]24 (eval `(try (import '~classname)25 (catch java.lang.Exception e#26 (println "couldn't import " '~classname))))27 classname)29 (defn jme-class? [classname]30 (and31 (.startsWith classname "com.jme3.")32 ;; Don't import the Lwjgl stuff since it can throw exceptions33 ;; upon being loaded.34 (not (re-matches #".*Lwjgl.*" classname))))36 (defn jme-classes37 "returns a list of all jme3 classes"38 []39 (filter40 jme-class?41 (map :name42 swank.util.class-browse/available-classes)))44 (defn mega-import-jme345 "Import ALL the jme classes. For REPL use."46 []47 (doall48 (map (comp permissive-import symbol) (jme-classes))))49 #+end_src51 The =mega-import-jme3= is quite usefull for debugging purposes since52 it allows completion for almost all of JME's classes.54 Out of curiousity, let's see just how many classes =mega-import-jme3=55 imports:57 #+begin_src clojure :exports both58 (clojure.core/count (cortex.import/jme-classes))59 #+end_src61 #+results:62 : 95564 ** Simplification65 #+srcname: world-view66 #+begin_src clojure :results silent67 (in-ns 'cortex.world)69 (defprotocol Viewable70 (view [something]))72 (extend-type com.jme3.scene.Geometry73 Viewable74 (view [geo]75 (view (doto (Node.)(.attachChild geo)))))77 (extend-type com.jme3.scene.Node78 Viewable79 (view [node]80 (.start81 (world node82 {}83 (fn [world]84 (.enableDebug85 (.getPhysicsSpace86 (.getState87 (.getStateManager world)88 BulletAppState))89 (asset-manager))90 (set-gravity* world Vector3f/ZERO)91 ;; (set-gravity* world (Vector3f. 0 (float -0.4) 0))92 (let [sun (doto (DirectionalLight.)93 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))94 (.setColor ColorRGBA/White))]95 (.addLight (.getRootNode world) sun)))96 no-op))))98 (defn position-camera [game]99 (doto (.getCamera game)100 (.setLocation (Vector3f. 0 6 6))101 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))103 #+end_src105 Here I make the =Viewable= protocol and extend it to JME's types. Now106 hello-world can be written as easily as:108 #+begin_src clojure :results silent109 (cortex.world/view (cortex.world/box))110 #+end_src