comparison org/world.org @ 34:183744c179e6

MASSIVE cleanup, especially in the vision code
author Robert McIntyre <rlm@mit.edu>
date Thu, 03 Nov 2011 08:28:26 -0700
parents 6372c108c5c6
children 00d0e1639d4b
comparison
equal deleted inserted replaced
33:c377222528e6 34:183744c179e6
1 #+title: A World for the Creatures 1 #+title: A Virtual World for Sensate Creatures
2 #+author: Robert McIntyre 2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu 3 #+email: rlm@mit.edu
4 #+description: Creating a Virtual World for AI constructs using clojure and JME3 4 #+description: Creating a Virtual World for AI constructs using clojure and JME3
5 #+keywords: JME3, clojure, virtual world, exception handling 5 #+keywords: JME3, clojure, virtual world, exception handling
6 #+SETUPFILE: ../../aurellem/org/setup.org 6 #+SETUPFILE: ../../aurellem/org/setup.org
29 29
30 - A more Clojureish way: 30 - A more Clojureish way:
31 - Use a map from keys->functions to specify key-bindings. 31 - Use a map from keys->functions to specify key-bindings.
32 - Use functions to create objects separately from any particular 32 - Use functions to create objects separately from any particular
33 application. 33 application.
34 - Use an REPL -- this means that there's only ever one JVM, and 34 - Use a REPL -- this means that there's only ever one JVM, and
35 Applications come and go. 35 Applications come and go.
36 36
37 Since most development work using jMonkeyEngine is done in Java, jme3 37 Since most development work using jMonkeyEngine is done in Java, jme3
38 supports "the Java way" quite well out of the box. To work "the 38 supports "the Java way" quite well out of the box. To work "the
39 clojure way", it necessary to wrap the jme3 elements that deal with 39 clojure way", it necessary to wrap the JME3 elements that deal with
40 the Application life-cycle with a REPL driven interface. 40 the Application life-cycle with a REPL driven interface.
41 41
42 The most important modifications are: 42 The most important modifications are:
43 43
44 - Separation of Object life-cycles with the Application life-cycle. 44 - Separation of Object life-cycles with the Application life-cycle.
55 55
56 (:use (clojure.contrib (def :only (defvar)))) 56 (:use (clojure.contrib (def :only (defvar))))
57 (:use [pokemon [lpsolve :only [constant-map]]]) 57 (:use [pokemon [lpsolve :only [constant-map]]])
58 (:use [clojure.contrib [str-utils :only [re-gsub]]]) 58 (:use [clojure.contrib [str-utils :only [re-gsub]]])
59 59
60 (:import com.aurellem.capture.IsoTimer)
61
60 (:import com.jme3.math.Vector3f) 62 (:import com.jme3.math.Vector3f)
61 (:import com.jme3.scene.Node) 63 (:import com.jme3.scene.Node)
62 (:import com.jme3.system.AppSettings) 64 (:import com.jme3.system.AppSettings)
63 (:import com.jme3.system.JmeSystem) 65 (:import com.jme3.system.JmeSystem)
64 (:import com.jme3.system.IsoTimer)
65 (:import com.jme3.input.KeyInput) 66 (:import com.jme3.input.KeyInput)
66 (:import com.jme3.input.controls.KeyTrigger) 67 (:import com.jme3.input.controls.KeyTrigger)
67 (:import com.jme3.input.controls.MouseButtonTrigger) 68 (:import com.jme3.input.controls.MouseButtonTrigger)
68 (:import com.jme3.input.InputManager) 69 (:import com.jme3.input.InputManager)
69 (:import com.jme3.bullet.BulletAppState) 70 (:import com.jme3.bullet.BulletAppState)
81 82
82 (defvar *app-settings* 83 (defvar *app-settings*
83 (doto (AppSettings. true) 84 (doto (AppSettings. true)
84 (.setFullscreen false) 85 (.setFullscreen false)
85 (.setTitle "Aurellem.") 86 (.setTitle "Aurellem.")
86 ;; disable 32 bit stuff for now 87 ;; The "Send" AudioRenderer supports sumulated hearing.
87 ;;(.setAudioRenderer "Send") 88 (.setAudioRenderer "Send"))
88 )
89 "These settings control how the game is displayed on the screen for 89 "These settings control how the game is displayed on the screen for
90 debugging purposes. Use binding forms to change this if desired. 90 debugging purposes. Use binding forms to change this if desired.
91 Full-screen mode does not work on some computers.") 91 Full-screen mode does not work on some computers.")
92 92
93 (defn asset-manager 93 (defn asset-manager
100 100
101 Normally, people just use the =AssetManager= inherited from 101 Normally, people just use the =AssetManager= inherited from
102 =Application= whenever they extend that class. However, 102 =Application= whenever they extend that class. However,
103 =AssetManagers= are useful on their own to create objects/ materials, 103 =AssetManagers= are useful on their own to create objects/ materials,
104 independent from any particular application. =(asset-manager)= makes 104 independent from any particular application. =(asset-manager)= makes
105 object creation less tightly bound to Application initialization. 105 object creation less tightly bound to a particular Application
106 Instance.
106 107
107 108
108 ** Exception Protection 109 ** Exception Protection
109 #+srcname: exceptions 110 #+srcname: exceptions
110 #+begin_src clojure 111 #+begin_src clojure
168 [trigger]))) key-map)) 169 [trigger]))) key-map))
169 (doall 170 (doall
170 (map (fn [name] 171 (map (fn [name]
171 (.addListener 172 (.addListener
172 ^InputManager input-manager game 173 ^InputManager input-manager game
173 (into-array String [name]))) (keys key-map)))) 174 (into-array String [name]))) (keys key-map))))
174 175
175 #+end_src 176 #+end_src
176 177
177 These functions are for controlling the world through the keyboard and 178 These functions are for controlling the world through the keyboard and
178 mouse. 179 mouse.
179 180
180 I reuse =constant-map= from [[../../pokemon-types/html/lpsolve.html#sec-3-3-4][=pokemon.lpsolve=]] to get the numerical 181 I reuse =constant-map= from [[../../pokemon-types/html/lpsolve.html#sec-3-2-4][=pokemon.lpsolve=]] to get the numerical
181 values for all the keys defined in the =KeyInput= class. The 182 values for all the keys defined in the =KeyInput= class. The
182 documentation for =constant-map= is: 183 documentation for =constant-map= is:
183 184
184 #+begin_src clojure :results output 185 #+begin_src clojure :results output :exports both
185 (doc pokemon.lpsolve/constant-map) 186 (doc pokemon.lpsolve/constant-map)
186 #+end_src 187 #+end_src
187 188
188 #+results: 189 #+results:
189 : ------------------------- 190 : -------------------------
217 : ["key-apps" #<KeyTrigger com.jme3.input.controls.KeyTrigger@28edbe7f>] 218 : ["key-apps" #<KeyTrigger com.jme3.input.controls.KeyTrigger@28edbe7f>]
218 : ["key-pgup" #<KeyTrigger com.jme3.input.controls.KeyTrigger@647fd33a>] 219 : ["key-pgup" #<KeyTrigger com.jme3.input.controls.KeyTrigger@647fd33a>]
219 : ["key-f8" #<KeyTrigger com.jme3.input.controls.KeyTrigger@24f97188>] 220 : ["key-f8" #<KeyTrigger com.jme3.input.controls.KeyTrigger@24f97188>]
220 : ["key-o" #<KeyTrigger com.jme3.input.controls.KeyTrigger@685c53ff>] 221 : ["key-o" #<KeyTrigger com.jme3.input.controls.KeyTrigger@685c53ff>]
221 : ["key-at" #<KeyTrigger com.jme3.input.controls.KeyTrigger@4c3e2e5f>]) 222 : ["key-at" #<KeyTrigger com.jme3.input.controls.KeyTrigger@4c3e2e5f>])
222
223 223
224 ** World Creation 224 ** World Creation
225 #+srcname: world 225 #+srcname: world
226 #+begin_src clojure :results silent 226 #+begin_src clojure :results silent
227 (in-ns 'cortex.world) 227 (in-ns 'cortex.world)
269 rendered, according to whatever clock jme is currently 269 rendered, according to whatever clock jme is currently
270 using. The default is to use IsoTimer which will result in this 270 using. The default is to use IsoTimer which will result in this
271 value always being the same. 271 value always being the same.
272 " 272 "
273 [root-node key-map setup-fn update-fn] 273 [root-node key-map setup-fn update-fn]
274 (let [physics-manager (BulletAppState.) 274 (let [physics-manager (BulletAppState.)]
275 shadow-renderer (BasicShadowRenderer.
276 (asset-manager) (int 256))]
277 (doto 275 (doto
278 (proxy [SimpleApplication ActionListener] [] 276 (proxy [SimpleApplication ActionListener] []
279 (simpleInitApp 277 (simpleInitApp
280 [] 278 []
281 (no-exceptions 279 (no-exceptions
300 (for [n (range (.getNumControls geom))] 298 (for [n (range (.getNumControls geom))]
301 (do 299 (do
302 (.add (.getPhysicsSpace physics-manager) 300 (.add (.getPhysicsSpace physics-manager)
303 (.getControl geom n)))))) 301 (.getControl geom n))))))
304 (.getRootNode this)) 302 (.getRootNode this))
305 ;;(.addAll (.getPhysicsSpace physics-manager) (.getRootNode this))
306
307 ;; set some basic defaults for the shadow renderer.
308 ;; these can be undone in the setup function
309 (.setDirection shadow-renderer
310 (.normalizeLocal (Vector3f. -1 -1 -1)))
311 (.addProcessor (.getViewPort this) shadow-renderer)
312 (.setShadowMode (.getRootNode this)
313 RenderQueue$ShadowMode/Off)
314 ;; call the supplied setup-fn 303 ;; call the supplied setup-fn
315 (if setup-fn 304 (if setup-fn
316 (setup-fn this)))) 305 (setup-fn this))))
317 (simpleUpdate 306 (simpleUpdate
318 [tpf] 307 [tpf]