comparison org/util.org @ 25:775d97247dd0

cleaning up world.org
author Robert McIntyre <rlm@mit.edu>
date Mon, 24 Oct 2011 05:25:01 -0700
parents e965675ec4d0
children bbffa41a12a9
comparison
equal deleted inserted replaced
24:e965675ec4d0 25:775d97247dd0
59 #+end_src 59 #+end_src
60 60
61 #+results: 61 #+results:
62 : 955 62 : 955
63 63
64 ** Simplification 64
65 #+srcname: world-view 65 #+srcname: world-view
66 #+begin_src clojure :results silent 66 #+begin_src clojure :results silent
67 (in-ns 'cortex.debug) 67 (ns cortex.util)
68 68 (require 'cortex.import)
69 (cortex.import/mega-import-jme3)
70 (use 'cortex.world)
69 (defprotocol Viewable 71 (defprotocol Viewable
70 (view [something])) 72 (view [something]))
71 73
72 (extend-type com.jme3.scene.Geometry 74 (extend-type com.jme3.scene.Geometry
73 Viewable 75 Viewable
92 (let [sun (doto (DirectionalLight.) 94 (let [sun (doto (DirectionalLight.)
93 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2))) 95 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
94 (.setColor ColorRGBA/White))] 96 (.setColor ColorRGBA/White))]
95 (.addLight (.getRootNode world) sun))) 97 (.addLight (.getRootNode world) sun)))
96 no-op)))) 98 no-op))))
99 #+end_src
100
101 Here I make the =Viewable= protocol and extend it to JME's types. Now
102 hello-world can be written as easily as:
103
104 #+begin_src clojure :results silent
105 (cortex.world/view (cortex.world/box))
106 #+end_src
107
108
109 #+srcname: util
110 #+begin_src clojure
111 (in-ns 'cortex.util)
112
113 (def println-repl (bound-fn [& args] (apply println args)))
97 114
98 (defn position-camera [game] 115 (defn position-camera [game]
99 (doto (.getCamera game) 116 (doto (.getCamera game)
100 (.setLocation (Vector3f. 0 6 6)) 117 (.setLocation (Vector3f. 0 6 6))
101 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0)))) 118 (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
102 119
103 #+end_src 120 (defn set-gravity*
104 121 [game gravity]
105 Here I make the =Viewable= protocol and extend it to JME's types. Now 122 (traverse
106 hello-world can be written as easily as: 123 (fn [geom]
107 124 (if-let
108 #+begin_src clojure :results silent 125 [control (.getControl geom RigidBodyControl)]
109 (cortex.world/view (cortex.world/box)) 126 (do
127 (.setGravity control gravity)
128 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
129 )))
130 (.getRootNode game)))
131 #+end_src
132
133
134 #+srcname: shapes
135 #+begin_src clojure :results silent
136 (in-ns 'cortex.util)
137 (defrecord shape-description
138 [name
139 color
140 mass
141 friction
142 texture
143 material
144 position
145 rotation
146 shape
147 physical?])
148
149 (def base-shape
150 (shape-description.
151 "default-shape"
152 false
153 ;;ColorRGBA/Blue
154 1.0 ;; mass
155 1.0 ;; friction
156 ;; texture
157 "Textures/Terrain/BrickWall/BrickWall.jpg"
158 ;; material
159 "Common/MatDefs/Misc/Unshaded.j3md"
160 Vector3f/ZERO
161 Quaternion/IDENTITY
162 (Box. Vector3f/ZERO 0.5 0.5 0.5)
163 true))
164
165 (defn make-shape
166 [#^shape-description d]
167 (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
168 mat (Material. asset-manager (:material d))
169 geom (Geometry. (:name d) (:shape d))]
170 (if (:texture d)
171 (let [key (TextureKey. (:texture d))]
172 (.setGenerateMips key true)
173 (.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
174 (if (:color d) (.setColor mat "Color" (:color d)))
175 (.setMaterial geom mat)
176 (if-let [rotation (:rotation d)] (.rotate geom rotation))
177 (.setLocalTranslation geom (:position d))
178 (if (:physical? d)
179 (let [impact-shape (doto (GImpactCollisionShape.
180 (.getMesh geom)) (.setMargin 0))
181 physics-control (RigidBodyControl.
182 ;;impact-shape ;; comment to disable
183 (float (:mass d)))]
184 (.createJmeMesh impact-shape)
185 (.addControl geom physics-control)
186 ;;(.setSleepingThresholds physics-control (float 0) (float 0))
187 (.setFriction physics-control (:friction d))))
188 ;;the default is to keep this node in the physics engine forever.
189 ;;these commands must come after the control is added to the geometry.
190 ;;
191 geom))
192
193 (defn box
194 ([l w h & {:as options}]
195 (let [options (merge base-shape options)]
196 (make-shape (assoc options
197 :shape (Box. l w h)))))
198 ([] (box 0.5 0.5 0.5)))
199
200 (defn sphere
201 ([r & {:as options}]
202 (let [options (merge base-shape options)]
203 (make-shape (assoc options
204 :shape (Sphere. 32 32 (float r))))))
205 ([] (sphere 0.5)))
206
207 (defn add-element
208 ([game element node]
209 (.addAll
210 (.getPhysicsSpace
211 (.getState
212 (.getStateManager game)
213 BulletAppState))
214 element)
215 (.attachChild node element))
216 ([game element]
217 (add-element game element (.getRootNode game))))
218
219
220
110 #+end_src 221 #+end_src
111 222
112 223
113 224
114 * COMMENT code generation 225 * COMMENT code generation
115 #+begin_src clojure :tangle ../src/cortex/import.clj 226 #+begin_src clojure :tangle ../src/cortex/import.clj
116 <<import>> 227 <<import>>
117 #+end_src 228 #+end_src
229
230
231 #+begin_src clojure :tangle ../src/cortex/util.clj
232 <<world-view>>
233 <<util>>
234 <<shapes>>
235 #+end_src
236