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 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
11 jMonkeyEngine has a plethora of classes which can be overwhelming at
12 first. So that I one can get right to coding, it's good to take the
13 time right now and make a "import all" function which brings in all of
14 the important jme3 classes. Once I'm happy with the general structure
15 of a namespace I can deal with importing only the classes it actually
16 needs.
18 #+srcname: import
19 #+begin_src clojure :results silent
20 (ns cortex.import
21 (:require swank.util.class-browse))
23 (defn permissive-import
24 [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 (and
32 (.startsWith classname "com.jme3.")
33 ;; Don't import the Lwjgl stuff since it can throw exceptions
34 ;; upon being loaded.
35 (not (re-matches #".*Lwjgl.*" classname))))
37 (defn jme-classes
38 "returns a list of all jme3 classes"
39 []
40 (filter
41 jme-class?
42 (map :name
43 swank.util.class-browse/available-classes)))
45 (defn mega-import-jme3
46 "Import ALL the jme classes. For REPL use."
47 []
48 (doall
49 (map (comp permissive-import symbol) (jme-classes))))
50 #+end_src
52 The =mega-import-jme3= is quite usefull for debugging purposes since
53 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 both
59 (clojure.core/count (cortex.import/jme-classes))
60 #+end_src
62 #+results:
63 : 955
66 #+srcname: world-view
67 #+begin_src clojure :results silent
68 (ns cortex.util)
69 (require 'cortex.import)
70 (cortex.import/mega-import-jme3)
71 (use 'cortex.world)
72 (defprotocol Viewable
73 (view [something]))
75 (extend-type com.jme3.scene.Geometry
76 Viewable
77 (view [geo]
78 (view (doto (Node.)(.attachChild geo)))))
80 (extend-type com.jme3.scene.Node
81 Viewable
82 (view [node]
83 (.start
84 (world node
85 {}
86 (fn [world]
87 (.enableDebug
88 (.getPhysicsSpace
89 (.getState
90 (.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_src
102 Here I make the =Viewable= protocol and extend it to JME's types. Now
103 hello-world can be written as easily as:
105 #+begin_src clojure :results silent
106 (cortex.world/view (cortex.world/box))
107 #+end_src
110 #+srcname: util
111 #+begin_src clojure
112 (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 (traverse
124 (fn [geom]
125 (if-let
126 [control (.getControl geom RigidBodyControl)]
127 (do
128 (.setGravity control gravity)
129 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
130 )))
131 (.getRootNode game)))
132 #+end_src
135 #+srcname: shapes
136 #+begin_src clojure :results silent
137 (in-ns 'cortex.util)
138 (defrecord shape-description
139 [name
140 color
141 mass
142 friction
143 texture
144 material
145 position
146 rotation
147 shape
148 physical?])
150 (def base-shape
151 (shape-description.
152 "default-shape"
153 false
154 ;;ColorRGBA/Blue
155 1.0 ;; mass
156 1.0 ;; friction
157 ;; texture
158 "Textures/Terrain/BrickWall/BrickWall.jpg"
159 ;; material
160 "Common/MatDefs/Misc/Unshaded.j3md"
161 Vector3f/ZERO
162 Quaternion/IDENTITY
163 (Box. Vector3f/ZERO 0.5 0.5 0.5)
164 true))
166 (defn make-shape
167 [#^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 disable
184 (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 box
195 ([l w h & {:as options}]
196 (let [options (merge base-shape options)]
197 (make-shape (assoc options
198 :shape (Box. l w h)))))
199 ([] (box 0.5 0.5 0.5)))
201 (defn sphere
202 ([r & {:as options}]
203 (let [options (merge base-shape options)]
204 (make-shape (assoc options
205 :shape (Sphere. 32 32 (float r))))))
206 ([] (sphere 0.5)))
208 (defn add-element
209 ([game element node]
210 (.addAll
211 (.getPhysicsSpace
212 (.getState
213 (.getStateManager game)
214 BulletAppState))
215 element)
216 (.attachChild node element))
217 ([game element]
218 (add-element game element (.getRootNode game))))
221 (defn apply-map
222 "Like apply, but works for maps and functions that expect an
223 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_src
236 * COMMENT code generation
237 #+begin_src clojure :tangle ../src/cortex/import.clj
238 <<import>>
239 #+end_src
242 #+begin_src clojure :tangle ../src/cortex/util.clj
243 <<world-view>>
244 <<util>>
245 <<shapes>>
246 #+end_src