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 Utilities
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulating senses for AI research using JMonkeyEngine3
5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org
7 #+babel: :mkdirp yes :noweb yes :exports both
9 ** Imports
10 jMonkeyEngine has a plethora of classes which can be overwhelming at
11 first. So that I one can get right to coding, it's good to take the
12 time right now and make a "import all" function which brings in all of
13 the important jme3 classes. Once I'm happy with the general structure
14 of a namespace I can deal with importing only the classes it actually
15 needs.
17 #+srcname: import
18 #+begin_src clojure :results silent
19 (ns cortex.import
20 (:require swank.util.class-browse))
22 (defn permissive-import
23 [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 (and
31 (.startsWith classname "com.jme3.")
32 ;; Don't import the Lwjgl stuff since it can throw exceptions
33 ;; upon being loaded.
34 (not (re-matches #".*Lwjgl.*" classname))))
36 (defn jme-classes
37 "returns a list of all jme3 classes"
38 []
39 (filter
40 jme-class?
41 (map :name
42 swank.util.class-browse/available-classes)))
44 (defn mega-import-jme3
45 "Import ALL the jme classes. For REPL use."
46 []
47 (doall
48 (map (comp permissive-import symbol) (jme-classes))))
49 #+end_src
51 The =mega-import-jme3= is quite usefull for debugging purposes since
52 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 both
58 (clojure.core/count (cortex.import/jme-classes))
59 #+end_src
61 #+results:
62 : 955
64 ** Simplification
65 #+srcname: world-view
66 #+begin_src clojure :results silent
67 (in-ns 'cortex.world)
69 (defprotocol Viewable
70 (view [something]))
72 (extend-type com.jme3.scene.Geometry
73 Viewable
74 (view [geo]
75 (view (doto (Node.)(.attachChild geo)))))
77 (extend-type com.jme3.scene.Node
78 Viewable
79 (view [node]
80 (.start
81 (world node
82 {}
83 (fn [world]
84 (.enableDebug
85 (.getPhysicsSpace
86 (.getState
87 (.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_src
105 Here I make the =Viewable= protocol and extend it to JME's types. Now
106 hello-world can be written as easily as:
108 #+begin_src clojure :results silent
109 (cortex.world/view (cortex.world/box))
110 #+end_src