rlm@34
|
1 #+title: jMonkeyEngine3 from Clojure
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@34
|
4 #+description: Using jMonkeyEngine3 from clojure
|
rlm@34
|
5 #+keywords: clojure, jMonkeyEngine3, tutorial, examples
|
rlm@4
|
6 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
7 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@21
|
8 #+babel: :mkdirp yes :noweb yes :exports both
|
rlm@0
|
9
|
rlm@34
|
10 [TABLE-OF-CONTENTS]
|
rlm@0
|
11
|
rlm@0
|
12
|
rlm@31
|
13 Here are the jMonkeyEngine "Hello" programs translated to clojure.
|
rlm@34
|
14
|
rlm@34
|
15 * Hello Simple App
|
rlm@23
|
16 Here is the hello world example for jme3 in clojure. It's a more or
|
rlm@23
|
17 less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]].
|
rlm@0
|
18
|
rlm@0
|
19 Of note is the fact that since we don't have access to the
|
rlm@0
|
20 =AssetManager= via extendig =SimpleApplication=, we have to build one
|
rlm@0
|
21 ourselves.
|
rlm@0
|
22
|
rlm@0
|
23 #+srcname: hello-simple-app
|
rlm@0
|
24 #+begin_src clojure :results silent
|
rlm@34
|
25 (ns hello.hello-simple-app
|
rlm@34
|
26 (:use cortex.world)
|
rlm@34
|
27 (:import com.jme3.math.Vector3f)
|
rlm@34
|
28 (:import com.jme3.material.Material)
|
rlm@34
|
29 (:import com.jme3.scene.Geometry)
|
rlm@34
|
30 (:import com.jme3.math.ColorRGBA)
|
rlm@34
|
31 (:import com.jme3.app.SimpleApplication)
|
rlm@34
|
32 (:import com.jme3.scene.shape.Box))
|
rlm@0
|
33
|
rlm@0
|
34 (def cube (Box. Vector3f/ZERO 1 1 1))
|
rlm@0
|
35
|
rlm@0
|
36 (def geom (Geometry. "Box" cube))
|
rlm@0
|
37
|
rlm@0
|
38 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
|
rlm@0
|
39
|
rlm@0
|
40 (.setColor mat "Color" ColorRGBA/Blue)
|
rlm@0
|
41
|
rlm@0
|
42 (.setMaterial geom mat)
|
rlm@0
|
43
|
rlm@0
|
44 (defn simple-app []
|
rlm@0
|
45 (doto
|
rlm@0
|
46 (proxy [SimpleApplication] []
|
rlm@0
|
47 (simpleInitApp
|
rlm@0
|
48 []
|
rlm@0
|
49 ;; Don't take control of the mouse
|
rlm@0
|
50 (org.lwjgl.input.Mouse/setGrabbed false)
|
rlm@0
|
51 (.attachChild (.getRootNode this) geom)))
|
rlm@0
|
52 ;; don't show a menu to change options.
|
rlm@0
|
53 (.setShowSettings false)
|
rlm@0
|
54 (.setPauseOnLostFocus false)
|
rlm@0
|
55 (.setSettings *app-settings*)))
|
rlm@0
|
56 #+end_src
|
rlm@0
|
57
|
rlm@0
|
58 Running this program will begin a new jMonkeyEngine game which
|
rlm@0
|
59 displays a single blue cube.
|
rlm@0
|
60
|
rlm@0
|
61 #+begin_src clojure :exports code :results silent
|
rlm@0
|
62 (.start (hello.hello-simple-app/simple-app))
|
rlm@0
|
63 #+end_src
|
rlm@0
|
64
|
rlm@0
|
65 #+caption: the simplest JME game.
|
rlm@31
|
66 [[../images/simple-app.jpg]]
|
rlm@0
|
67
|
rlm@34
|
68 * Simpler HelloSimpleApp
|
rlm@0
|
69
|
rlm@34
|
70 #+srcname: hello-simpler-app
|
rlm@34
|
71 #+begin_src clojure
|
rlm@34
|
72 (ns hello.hello-simpler-app
|
rlm@34
|
73 (:use cortex.world)
|
rlm@34
|
74 (:use cortex.util)
|
rlm@34
|
75 (:import com.jme3.math.ColorRGBA)
|
rlm@34
|
76 (:import com.jme3.scene.Node))
|
rlm@34
|
77
|
rlm@34
|
78 (defn simpler-world
|
rlm@34
|
79 "The jMonkeyEngine3 Hello World program. Displays a blue 3D cube in
|
rlm@34
|
80 a basic 3D world."
|
rlm@34
|
81 []
|
rlm@34
|
82 (world (doto (Node.)
|
rlm@34
|
83 (.attachChild
|
rlm@34
|
84 (box 1 1 1
|
rlm@34
|
85 :color ColorRGBA/Blue :physical? false)))
|
rlm@34
|
86 {} no-op no-op))
|
rlm@34
|
87 #+end_src
|
rlm@34
|
88
|
rlm@34
|
89 More information about the jMonkeyEngine3 hello world program can be
|
rlm@34
|
90 found [[http://jmonkeyengine.org/wiki/doku.php/starter:hello_world][here]].
|
rlm@34
|
91
|
rlm@34
|
92 * COMMENT Hello Physics
|
rlm@0
|
93 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
|
rlm@0
|
94
|
rlm@0
|
95 #+srcname: brick-wall-header
|
rlm@0
|
96 #+begin_src clojure :results silent
|
rlm@0
|
97 (ns hello.brick-wall)
|
rlm@0
|
98 (require 'cortex.import)
|
rlm@0
|
99 (use 'clojure.contrib.def)
|
rlm@0
|
100 (rlm.rlm-commands/help)
|
rlm@0
|
101 (cortex.import/mega-import-jme3)
|
rlm@0
|
102 (use '[pokemon [lpsolve :only [constant-map]]])
|
rlm@0
|
103 (use 'cortex.world)
|
rlm@25
|
104 (use 'cortex.util)
|
rlm@0
|
105 #+end_src
|
rlm@0
|
106
|
rlm@0
|
107 #+srcname: brick-wall-body
|
rlm@0
|
108 #+begin_src clojure :results silent
|
rlm@0
|
109 (in-ns 'hello.brick-wall)
|
rlm@0
|
110
|
rlm@0
|
111 (defn floor
|
rlm@0
|
112 "make a sturdy, unmovable physical floor"
|
rlm@0
|
113 []
|
rlm@0
|
114 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
|
rlm@0
|
115
|
rlm@0
|
116 (def brick-length 0.48)
|
rlm@0
|
117 (def brick-width 0.24)
|
rlm@0
|
118 (def brick-height 0.12)
|
rlm@34
|
119 (def gravity (Vector3f. 0 -9.81 0))
|
rlm@0
|
120
|
rlm@0
|
121 (defn brick* [position]
|
rlm@0
|
122 (doto (box brick-length brick-height brick-width
|
rlm@0
|
123 :position position :name "brick"
|
rlm@0
|
124 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
125 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
126 :mass 36)
|
rlm@0
|
127 (->
|
rlm@0
|
128 (.getMesh)
|
rlm@0
|
129 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
|
rlm@0
|
130 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
131 )
|
rlm@0
|
132 )
|
rlm@0
|
133
|
rlm@0
|
134 (defn inception-brick-wall
|
rlm@0
|
135 "construct a physical brick wall"
|
rlm@0
|
136 []
|
rlm@0
|
137 (let [node (Node. "brick-wall")]
|
rlm@0
|
138 (dorun
|
rlm@0
|
139 (map (comp #(.attachChild node %) brick*)
|
rlm@0
|
140 (for
|
rlm@0
|
141 [x (range 15)
|
rlm@0
|
142 y (range 10)
|
rlm@0
|
143 z (range 1)]
|
rlm@0
|
144 (Vector3f.
|
rlm@0
|
145 (* brick-length x 1.03)
|
rlm@0
|
146 (* brick-width y y 10)
|
rlm@0
|
147 (* brick-height z)))))
|
rlm@0
|
148 node))
|
rlm@0
|
149
|
rlm@0
|
150 (defn gravity-toggle
|
rlm@0
|
151 [new-value]
|
rlm@0
|
152 (fn [game value]
|
rlm@0
|
153 (println-repl "set gravity to " new-value)
|
rlm@0
|
154 (if value
|
rlm@34
|
155 (set-gravity game new-value)
|
rlm@34
|
156 (set-gravity game gravity))))
|
rlm@0
|
157
|
rlm@15
|
158 (defn fire-cannon-ball
|
rlm@15
|
159 ([node]
|
rlm@15
|
160 (fn [game value]
|
rlm@15
|
161 (if (not value)
|
rlm@15
|
162 (let [camera (.getCamera game)
|
rlm@15
|
163 cannon-ball
|
rlm@15
|
164 (sphere 0.7
|
rlm@15
|
165 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@15
|
166 :texture "Textures/PokeCopper.jpg"
|
rlm@15
|
167 :position
|
rlm@15
|
168 (.add (.getLocation camera)
|
rlm@15
|
169 (.mult (.getDirection camera) (float 1)))
|
rlm@15
|
170 :mass 3)] ;200 0.05
|
rlm@15
|
171 (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
|
rlm@15
|
172 (.setLinearVelocity
|
rlm@15
|
173 (.getControl cannon-ball RigidBodyControl)
|
rlm@15
|
174 (.mult (.getDirection camera) (float 50))) ;50
|
rlm@21
|
175 (add-element game cannon-ball (if node node (.getRootNode game)))))))
|
rlm@15
|
176 ([]
|
rlm@15
|
177 (fire-cannon-ball false)))
|
rlm@15
|
178
|
rlm@0
|
179
|
rlm@0
|
180 (defn floor* []
|
rlm@0
|
181 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
|
rlm@0
|
182 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
183 :texture "Textures/Terrain/Pond/Pond.png"
|
rlm@0
|
184 :position (Vector3f. 0 -0.1 0 )
|
rlm@0
|
185 :mass 0)
|
rlm@0
|
186 (->
|
rlm@0
|
187 (.getMesh)
|
rlm@0
|
188 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
|
rlm@0
|
189 (->
|
rlm@0
|
190 (.getMaterial)
|
rlm@0
|
191 (.getTextureParam "ColorMap")
|
rlm@0
|
192 (.getTextureValue)
|
rlm@0
|
193 (.setWrap Texture$WrapMode/Repeat))
|
rlm@0
|
194 (.setShadowMode RenderQueue$ShadowMode/Receive)
|
rlm@0
|
195 ))
|
rlm@0
|
196
|
rlm@0
|
197 (defn brick-wall* []
|
rlm@0
|
198 (let [node (Node. "brick-wall")]
|
rlm@0
|
199 (dorun
|
rlm@0
|
200 (map
|
rlm@0
|
201 (comp #(.attachChild node %) brick*)
|
rlm@0
|
202 (for [y (range 15)
|
rlm@0
|
203 x (range 4)
|
rlm@0
|
204 z (range 1)]
|
rlm@0
|
205 (Vector3f.
|
rlm@0
|
206 (+ (* 2 x brick-length)
|
rlm@0
|
207 (if (even? (+ y z))
|
rlm@0
|
208 (/ brick-length 4) (/ brick-length -4)))
|
rlm@0
|
209 (+ (* brick-height (inc (* 2 y))))
|
rlm@0
|
210 (* 2 z brick-width) ))))
|
rlm@0
|
211 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
212 node))
|
rlm@0
|
213
|
rlm@0
|
214 (defn brick-wall-game-run []
|
rlm@0
|
215 (doto
|
rlm@0
|
216 (world
|
rlm@0
|
217 (doto (Node.) (.attachChild (floor*))
|
rlm@0
|
218 (.attachChild (brick-wall*))
|
rlm@0
|
219 )
|
rlm@0
|
220 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
|
rlm@0
|
221 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
|
rlm@0
|
222 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
|
rlm@0
|
223 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
|
rlm@0
|
224 "key-k" (gravity-toggle Vector3f/ZERO)
|
rlm@0
|
225 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
|
rlm@0
|
226 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
|
rlm@0
|
227 "key-f" (fn[game value]
|
rlm@0
|
228 (if (not value) (add-element game (brick-wall*))))
|
rlm@0
|
229 "key-return" (fire-cannon-ball)}
|
rlm@0
|
230 position-camera
|
rlm@0
|
231 (fn [& _]))
|
rlm@0
|
232 (.start)))
|
rlm@0
|
233 #+end_src
|
rlm@0
|
234
|
rlm@0
|
235 #+begin_src clojure :results silent
|
rlm@0
|
236 (hello.brick-wall/brick-wall-game-run)
|
rlm@0
|
237 #+end_src
|
rlm@0
|
238
|
rlm@0
|
239 #+caption: the brick wall standing
|
rlm@31
|
240 [[../images/brick-wall-standing.jpg]]
|
rlm@0
|
241
|
rlm@0
|
242 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
|
rlm@31
|
243 [[../images/brick-wall-knocked-down.jpg]]
|
rlm@0
|
244
|
rlm@34
|
245 * COMMENT Other Brick Games
|
rlm@0
|
246 #+srcname: other-games
|
rlm@0
|
247 #+begin_src clojure :results silent
|
rlm@34
|
248 (ns hello.other-games
|
rlm@0
|
249 {:author "Dylan Holmes"})
|
rlm@0
|
250 (use 'cortex.world)
|
rlm@0
|
251 (use 'hello.brick-wall)
|
rlm@0
|
252 (use 'cortex.import)
|
rlm@0
|
253 (cortex.import/mega-import-jme3)
|
rlm@0
|
254
|
rlm@0
|
255 (defn scad [position]
|
rlm@0
|
256 (doto (box 0.1 0.1 0.1
|
rlm@0
|
257 :position position :name "brick"
|
rlm@0
|
258 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
259 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
260 :mass 20)
|
rlm@0
|
261 (->
|
rlm@0
|
262 (.getMesh)
|
rlm@0
|
263 (.scaleTextureCoordinates (Vector2f. 1 0.5))
|
rlm@0
|
264 )
|
rlm@0
|
265 (-> (.getControl RigidBodyControl)
|
rlm@0
|
266 (.setLinearVelocity (Vector3f. 0 100 0))
|
rlm@0
|
267 )
|
rlm@0
|
268
|
rlm@0
|
269 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
|
rlm@0
|
270 ))
|
rlm@0
|
271
|
rlm@0
|
272
|
rlm@0
|
273 (defn shrapnel []
|
rlm@0
|
274 (let [node (Node. "explosion-day")]
|
rlm@0
|
275 (dorun
|
rlm@0
|
276 (map
|
rlm@0
|
277 (comp #(.attachChild node %) scad)
|
rlm@0
|
278 (for [y (range 15)
|
rlm@0
|
279 x (range 4)
|
rlm@0
|
280 z (range 1)]
|
rlm@0
|
281 (Vector3f.
|
rlm@0
|
282 (+ (* 2 x brick-height)
|
rlm@0
|
283 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
|
rlm@0
|
284 (+ (* brick-height (inc (* 2 y))))
|
rlm@0
|
285 (* 2 z brick-height) ))))
|
rlm@0
|
286 node))
|
rlm@0
|
287
|
rlm@0
|
288
|
rlm@0
|
289 (def domino-height 0.48)
|
rlm@0
|
290 (def domino-thickness 0.12)
|
rlm@0
|
291 (def domino-width 0.24)
|
rlm@0
|
292
|
rlm@0
|
293 (def domino-thickness 0.05)
|
rlm@0
|
294 (def domino-width 0.5)
|
rlm@0
|
295 (def domino-height 1)
|
rlm@0
|
296
|
rlm@0
|
297 (defn domino
|
rlm@0
|
298 ([position]
|
rlm@0
|
299 (domino position (Quaternion/IDENTITY)))
|
rlm@0
|
300 ([position rotation]
|
rlm@0
|
301 (doto (box domino-width domino-height domino-thickness
|
rlm@0
|
302 :position position :name "domino"
|
rlm@0
|
303 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
304 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
|
rlm@0
|
305 :mass 1
|
rlm@0
|
306 :rotation rotation)
|
rlm@0
|
307 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
308 )))
|
rlm@0
|
309
|
rlm@0
|
310
|
rlm@0
|
311 (defn domino-row []
|
rlm@0
|
312 (let [node (Node. "domino-row")]
|
rlm@0
|
313 (dorun
|
rlm@0
|
314 (map
|
rlm@0
|
315 (comp #(.attachChild node %) domino)
|
rlm@0
|
316 (for [
|
rlm@0
|
317 z (range 10)
|
rlm@0
|
318 x (range 5)
|
rlm@0
|
319 ]
|
rlm@0
|
320 (Vector3f.
|
rlm@0
|
321 (+ (* z domino-width) (* x 5 domino-width))
|
rlm@0
|
322 (/ domino-height 1)
|
rlm@0
|
323 (* -5.5 domino-thickness z) ))))
|
rlm@0
|
324
|
rlm@0
|
325 node))
|
rlm@0
|
326
|
rlm@0
|
327 (defn domino-cycle []
|
rlm@0
|
328 (let [node (Node. "domino-cycle")]
|
rlm@0
|
329 (dorun
|
rlm@0
|
330 (map
|
rlm@0
|
331 (comp #(.attachChild node %) (partial apply domino) )
|
rlm@0
|
332 (for [n (range 720)]
|
rlm@0
|
333 (let [space (* domino-height 5.5)
|
rlm@0
|
334 r (fn[n] (* (+ n 3) domino-width 0.5))
|
rlm@0
|
335 t (fn[n] (reduce
|
rlm@0
|
336 +
|
rlm@0
|
337 (map
|
rlm@0
|
338 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
|
rlm@0
|
339 (range n))))
|
rlm@0
|
340 t (t n)
|
rlm@0
|
341 r (r n)
|
rlm@0
|
342 ct (Math/cos t)
|
rlm@0
|
343 st (Math/sin t)
|
rlm@0
|
344 ]
|
rlm@0
|
345 (list
|
rlm@0
|
346 (Vector3f.
|
rlm@0
|
347 (* -1 r st)
|
rlm@0
|
348 (/ domino-height 1)
|
rlm@0
|
349 (* r ct))
|
rlm@0
|
350 (.fromAngleAxis (Quaternion.)
|
rlm@0
|
351 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
|
rlm@0
|
352 )))
|
rlm@0
|
353 ))
|
rlm@0
|
354 node))
|
rlm@0
|
355
|
rlm@0
|
356
|
rlm@0
|
357 (defn domino-game-run []
|
rlm@0
|
358 (doto
|
rlm@0
|
359 (world
|
rlm@0
|
360 (doto (Node.) (.attachChild (floor*))
|
rlm@0
|
361 )
|
rlm@0
|
362 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
|
rlm@0
|
363 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
|
rlm@0
|
364 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
|
rlm@0
|
365 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
|
rlm@0
|
366 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
|
rlm@0
|
367 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
|
rlm@0
|
368 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
|
rlm@0
|
369
|
rlm@0
|
370 "key-space"
|
rlm@0
|
371 (fn[game value]
|
rlm@0
|
372
|
rlm@0
|
373 (if (not value)
|
rlm@0
|
374 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
|
rlm@0
|
375 (.fromAngleAxis (Quaternion.)
|
rlm@0
|
376 (/ Math/PI 2) (Vector3f. 0 1 0)))]
|
rlm@0
|
377 (add-element game d))))
|
rlm@0
|
378 "key-f"
|
rlm@0
|
379 (fn[game value](if (not value) (add-element game (domino-cycle))))
|
rlm@0
|
380 "key-return" (fire-cannon-ball)}
|
rlm@0
|
381 position-camera
|
rlm@0
|
382 (fn [& _]))
|
rlm@0
|
383 (.start)))
|
rlm@0
|
384 #+end_src
|
rlm@0
|
385
|
rlm@0
|
386 #+begin_src clojure :results silent
|
rlm@0
|
387 (cortex.other-games/domino-game-run)
|
rlm@0
|
388 #+end_src
|
rlm@0
|
389
|
rlm@0
|
390 #+caption: floating dominos
|
rlm@31
|
391 [[../images/dominos.jpg]]
|
rlm@0
|
392
|
rlm@34
|
393 * Hello Loop
|
rlm@0
|
394 #+srcname: hello-loop
|
rlm@0
|
395 #+begin_src clojure :results silent
|
rlm@34
|
396 (ns hello.loop
|
rlm@34
|
397 (:use cortex.world)
|
rlm@34
|
398 (:use cortex.util)
|
rlm@34
|
399 (:import com.jme3.math.ColorRGBA)
|
rlm@34
|
400 (:import com.jme3.scene.Node))
|
rlm@34
|
401
|
rlm@0
|
402 (defn blue-cube []
|
rlm@0
|
403 (box 1 1 1
|
rlm@0
|
404 :color ColorRGBA/Blue
|
rlm@0
|
405 :texture false
|
rlm@0
|
406 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
407 :name "blue-cube"
|
rlm@0
|
408 :physical? false))
|
rlm@0
|
409
|
rlm@0
|
410 (defn blue-cube-game []
|
rlm@0
|
411 (let [cube (blue-cube)
|
rlm@0
|
412 root (doto (Node.) (.attachChild cube))]
|
rlm@0
|
413 (world root
|
rlm@0
|
414 {}
|
rlm@0
|
415 no-op
|
rlm@0
|
416 (fn [game tpf]
|
rlm@0
|
417 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
|
rlm@0
|
418 #+end_src
|
rlm@31
|
419
|
rlm@34
|
420 * COMMENT Hello Collision
|
rlm@0
|
421
|
rlm@0
|
422 #+srcname: hello-collision
|
rlm@0
|
423 #+begin_src clojure :results silent
|
rlm@0
|
424 (ns hello.collision)
|
rlm@0
|
425 (use 'cortex.world)
|
rlm@0
|
426 (use 'cortex.import)
|
rlm@0
|
427 (use 'clojure.contrib.def)
|
rlm@0
|
428
|
rlm@0
|
429
|
rlm@0
|
430 (cortex.import/mega-import-jme3)
|
rlm@0
|
431 (rlm.rlm-commands/help)
|
rlm@0
|
432 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
|
rlm@0
|
433
|
rlm@0
|
434
|
rlm@0
|
435 (defn environment []
|
rlm@0
|
436 (let
|
rlm@0
|
437 [scene-model
|
rlm@0
|
438 (doto
|
rlm@0
|
439 (.loadModel
|
rlm@0
|
440 (doto (asset-manager)
|
rlm@0
|
441 (.registerLocator
|
rlm@0
|
442 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
|
rlm@0
|
443 "main.scene")
|
rlm@0
|
444 (.setLocalScale (float 2.0)))
|
rlm@0
|
445 collision-shape
|
rlm@0
|
446 (CollisionShapeFactory/createMeshShape #^Node scene-model)
|
rlm@0
|
447 landscape (RigidBodyControl. collision-shape 0)]
|
rlm@0
|
448 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
|
rlm@0
|
449 (.addControl scene-model landscape)
|
rlm@0
|
450 scene-model))
|
rlm@0
|
451
|
rlm@0
|
452 (defn player-fn []
|
rlm@0
|
453 (doto
|
rlm@0
|
454 (CharacterControl.
|
rlm@0
|
455 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
|
rlm@0
|
456 (float 0.05))
|
rlm@0
|
457 (.setJumpSpeed 20)
|
rlm@0
|
458 (.setFallSpeed 30)
|
rlm@0
|
459 (.setGravity 30) ;30
|
rlm@0
|
460 (.setPhysicsLocation (Vector3f. 0 10 0))))
|
rlm@0
|
461
|
rlm@0
|
462 (defn lights []
|
rlm@0
|
463 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
|
rlm@0
|
464 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
|
rlm@0
|
465 (doto (DirectionalLight.)
|
rlm@0
|
466 (.setColor (.mult ColorRGBA/White (float 0.9) ))
|
rlm@0
|
467 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
|
rlm@0
|
468
|
rlm@0
|
469 (defn night-lights []
|
rlm@0
|
470 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
|
rlm@0
|
471 (doto (DirectionalLight.)
|
rlm@0
|
472 (.setColor (.mult ColorRGBA/White (float 0.2) ))
|
rlm@0
|
473 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
|
rlm@0
|
474
|
rlm@0
|
475 (def player (atom (player-fn)))
|
rlm@0
|
476
|
rlm@0
|
477 (defn setup-fn [game]
|
rlm@0
|
478 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
|
rlm@0
|
479 ;; set the color of the sky
|
rlm@0
|
480 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
|
rlm@0
|
481 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
|
rlm@0
|
482 (doto (.getFlyByCamera game)
|
rlm@0
|
483 (.setMoveSpeed (float 100))
|
rlm@0
|
484 (.setRotationSpeed 3))
|
rlm@0
|
485 (.add
|
rlm@0
|
486 (.getPhysicsSpace
|
rlm@0
|
487 (.getState (.getStateManager game) BulletAppState))
|
rlm@0
|
488 @player)
|
rlm@0
|
489
|
rlm@0
|
490 (doto (Node.) (.attachChild (.getRootNode game))
|
rlm@0
|
491 (.attachChild (brick-wall*))
|
rlm@0
|
492 )
|
rlm@0
|
493
|
rlm@0
|
494 )
|
rlm@0
|
495
|
rlm@0
|
496
|
rlm@0
|
497 (def walking-up? (atom false))
|
rlm@0
|
498 (def walking-down? (atom false))
|
rlm@0
|
499 (def walking-left? (atom false))
|
rlm@0
|
500 (def walking-right? (atom false))
|
rlm@0
|
501
|
rlm@0
|
502 (defn set-walk [walk-atom game value]
|
rlm@0
|
503 ;;(println-repl "setting stuff to " value)
|
rlm@0
|
504 (reset! walk-atom value))
|
rlm@0
|
505
|
rlm@0
|
506 (defn responses []
|
rlm@0
|
507 {"key-w" (partial set-walk walking-up?)
|
rlm@0
|
508 "key-d" (partial set-walk walking-right?)
|
rlm@0
|
509 "key-s" (partial set-walk walking-down?)
|
rlm@0
|
510 "key-a" (partial set-walk walking-left?)
|
rlm@0
|
511 "key-return" (fire-cannon-ball)
|
rlm@0
|
512 "key-space" (fn [game value] (.jump @player))
|
rlm@0
|
513 })
|
rlm@0
|
514
|
rlm@0
|
515 (defn update-fn
|
rlm@0
|
516 [game tpf]
|
rlm@0
|
517 (let [camera (.getCamera game)
|
rlm@0
|
518 cam-dir (.multLocal
|
rlm@0
|
519 (.clone
|
rlm@0
|
520 (.getDirection camera)) (float 0.6))
|
rlm@0
|
521 cam-left (.multLocal
|
rlm@0
|
522 (.clone
|
rlm@0
|
523 (.getLeft camera)) (float 0.4))
|
rlm@0
|
524 walk-direction (Vector3f. 0 0 0)]
|
rlm@0
|
525
|
rlm@0
|
526 (cond
|
rlm@0
|
527 @walking-up? (.addLocal walk-direction cam-dir)
|
rlm@0
|
528 @walking-right? (.addLocal walk-direction (.negate cam-left))
|
rlm@0
|
529 @walking-down? (.addLocal walk-direction (.negate cam-dir))
|
rlm@0
|
530 @walking-left? (.addLocal walk-direction cam-left))
|
rlm@0
|
531 (.setWalkDirection @player walk-direction)
|
rlm@0
|
532 (.setLocation camera (.getPhysicsLocation @player))))
|
rlm@0
|
533
|
rlm@0
|
534 (defn run-game []
|
rlm@0
|
535 (.start
|
rlm@0
|
536 (world (environment)
|
rlm@0
|
537 (responses)
|
rlm@0
|
538 setup-fn
|
rlm@0
|
539 update-fn)))
|
rlm@0
|
540 #+end_src
|
rlm@0
|
541
|
rlm@34
|
542 * COMMENT Hello Terrain
|
rlm@0
|
543 #+srcname: hello-terrain
|
rlm@0
|
544 #+begin_src clojure :results silent
|
rlm@0
|
545 (ns hello.terrain)
|
rlm@0
|
546 (use 'cortex.world)
|
rlm@0
|
547 (use 'cortex.import)
|
rlm@0
|
548 (use 'clojure.contrib.def)
|
rlm@0
|
549 (import jme3tools.converters.ImageToAwt)
|
rlm@0
|
550
|
rlm@0
|
551
|
rlm@0
|
552 (cortex.import/mega-import-jme3)
|
rlm@0
|
553 (rlm.rlm-commands/help)
|
rlm@0
|
554 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
|
rlm@0
|
555
|
rlm@0
|
556
|
rlm@0
|
557 (defn setup-fn [type game]
|
rlm@0
|
558 (.setMoveSpeed (.getFlyByCamera game) 50)
|
rlm@0
|
559 (.setFrustumFar (.getCamera game) 10000)
|
rlm@0
|
560 (let [env (environment type)
|
rlm@0
|
561 cameras [(.getCamera game)]
|
rlm@0
|
562 control (TerrainLodControl. env cameras)]
|
rlm@0
|
563 ;;(.addControl env control)
|
rlm@0
|
564 (.attachChild (.getRootNode game) env)))
|
rlm@0
|
565
|
rlm@0
|
566 (defn environment [type]
|
rlm@0
|
567 (let
|
rlm@0
|
568 [mat_terrain
|
rlm@0
|
569 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
|
rlm@0
|
570 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
|
rlm@0
|
571 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
|
rlm@0
|
572 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
|
rlm@0
|
573 heightmap-image (.loadTexture (asset-manager)
|
rlm@0
|
574 ({:mountain "Textures/Terrain/splat/mountains512.png"
|
rlm@0
|
575 :fortress "Textures/Terrain/splat/fortress512.png"
|
rlm@0
|
576 }type))
|
rlm@0
|
577 heightmap (ImageBasedHeightMap.
|
rlm@0
|
578 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
|
rlm@0
|
579 terrain (do (.load heightmap)
|
rlm@0
|
580 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
|
rlm@0
|
581 ]
|
rlm@0
|
582
|
rlm@0
|
583 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
|
rlm@0
|
584 [grass dirt rock]))
|
rlm@0
|
585
|
rlm@0
|
586 (doto mat_terrain
|
rlm@0
|
587 (.setTexture "Tex1" grass)
|
rlm@0
|
588 (.setFloat "Tex1Scale" (float 64))
|
rlm@0
|
589
|
rlm@0
|
590 (.setTexture "Tex2" dirt)
|
rlm@0
|
591 (.setFloat "Tex2Scale" (float 32))
|
rlm@0
|
592
|
rlm@0
|
593 (.setTexture "Tex3" rock)
|
rlm@0
|
594 (.setFloat "Tex3Scale" (float 128))
|
rlm@0
|
595
|
rlm@0
|
596 (.setTexture "Alpha"
|
rlm@0
|
597 (.loadTexture
|
rlm@0
|
598 (asset-manager)
|
rlm@0
|
599 ({:mountain "Textures/Terrain/splat/alphamap.png"
|
rlm@0
|
600 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
|
rlm@0
|
601
|
rlm@0
|
602 (doto terrain
|
rlm@0
|
603 (.setMaterial mat_terrain)
|
rlm@0
|
604 (.setLocalTranslation 0 -100 0)
|
rlm@0
|
605 (.setLocalScale 2 1 2))))
|
rlm@0
|
606
|
rlm@0
|
607
|
rlm@0
|
608
|
rlm@0
|
609 (defn run-terrain-game [type]
|
rlm@0
|
610 (.start
|
rlm@0
|
611 (world
|
rlm@0
|
612 (Node.)
|
rlm@0
|
613 {}
|
rlm@0
|
614 (partial setup-fn type)
|
rlm@0
|
615 no-op)))
|
rlm@0
|
616 #+end_src
|
rlm@0
|
617
|
rlm@0
|
618
|
rlm@0
|
619
|
rlm@0
|
620 #+srcname: hello-animation
|
rlm@0
|
621 #+begin_src clojure :results silent
|
rlm@0
|
622 (ns hello.animation)
|
rlm@0
|
623 (use 'cortex.world)
|
rlm@0
|
624 (use 'cortex.import)
|
rlm@0
|
625 (use 'clojure.contrib.def)
|
rlm@0
|
626 (cortex.import/mega-import-jme3)
|
rlm@0
|
627 (rlm.rlm-commands/help)
|
rlm@0
|
628 (use '[hello [collision :only [lights]]])
|
rlm@0
|
629
|
rlm@0
|
630 (defn stand
|
rlm@0
|
631 [channel]
|
rlm@0
|
632 (doto channel
|
rlm@0
|
633 (.setAnim "stand" (float 0.5))
|
rlm@0
|
634 (.setLoopMode LoopMode/DontLoop)
|
rlm@0
|
635 (.setSpeed (float 1))))
|
rlm@0
|
636
|
rlm@0
|
637 (defn anim-listener []
|
rlm@0
|
638 (proxy [AnimEventListener] []
|
rlm@0
|
639 (onAnimChange
|
rlm@0
|
640 [control channel animation-name]
|
rlm@0
|
641 (println-repl "RLM --- onAnimChange"))
|
rlm@0
|
642 (onAnimCycleDone
|
rlm@0
|
643 [control channel animation-name]
|
rlm@0
|
644 (if (= animation-name "Walk")
|
rlm@0
|
645 (stand channel)
|
rlm@0
|
646 ))))
|
rlm@0
|
647
|
rlm@0
|
648 (defn setup-fn [channel game]
|
rlm@0
|
649 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
|
rlm@0
|
650 ;; set the color of the sky
|
rlm@0
|
651 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
|
rlm@0
|
652 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
|
rlm@0
|
653 (.setAnim channel "stand")
|
rlm@0
|
654 (doto (.getFlyByCamera game)
|
rlm@0
|
655 (.setMoveSpeed (float 10))
|
rlm@0
|
656 (.setRotationSpeed 1)))
|
rlm@0
|
657
|
rlm@0
|
658 (defn walk [channel]
|
rlm@0
|
659 (println-repl "zzz")
|
rlm@0
|
660 (doto channel
|
rlm@0
|
661 (.setAnim "Walk" (float 0.5))
|
rlm@0
|
662 (.setLoopMode LoopMode/Loop)))
|
rlm@0
|
663
|
rlm@0
|
664
|
rlm@0
|
665 (defn key-map [channel]
|
rlm@0
|
666 {"key-space" (fn [game value]
|
rlm@0
|
667 (if (not value)
|
rlm@0
|
668 (walk channel)))})
|
rlm@0
|
669
|
rlm@0
|
670 (defn player []
|
rlm@0
|
671 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
|
rlm@0
|
672 control (.getControl model AnimControl)]
|
rlm@0
|
673 (.setLocalScale model (float 0.5))
|
rlm@0
|
674 (.clearListeners control)
|
rlm@0
|
675 (.addListener control (anim-control))
|
rlm@0
|
676 model))
|
rlm@0
|
677
|
rlm@0
|
678
|
rlm@0
|
679
|
rlm@0
|
680 (defn run-anim-game []
|
rlm@0
|
681 (let [ninja (player)
|
rlm@0
|
682 control (.getControl ninja AnimControl)
|
rlm@0
|
683 channel (.createChannel control)]
|
rlm@0
|
684 (.start
|
rlm@0
|
685 (world
|
rlm@0
|
686 ninja
|
rlm@0
|
687 (key-map channel)
|
rlm@0
|
688 (partial setup-fn channel)
|
rlm@0
|
689 no-op))))
|
rlm@0
|
690 #+end_src
|
rlm@0
|
691
|
rlm@34
|
692 * COMMENT Hello Materials
|
rlm@0
|
693 #+srcname: material
|
rlm@0
|
694 #+begin_src clojure :results silent
|
rlm@0
|
695 (ns hello.material)
|
rlm@0
|
696 (use 'cortex.world)
|
rlm@0
|
697 (use 'cortex.import)
|
rlm@34
|
698 (use 'cortex.util)
|
rlm@0
|
699 (use 'clojure.contrib.def)
|
rlm@0
|
700 (cortex.import/mega-import-jme3)
|
rlm@0
|
701 (rlm.rlm-commands/help)
|
rlm@0
|
702
|
rlm@0
|
703 (defn simple-cube []
|
rlm@0
|
704 (box 1 1 1
|
rlm@0
|
705 :position (Vector3f. -3 1.1 0)
|
rlm@0
|
706 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
707 :texture "Interface/Logo/Monkey.jpg"
|
rlm@0
|
708 :physical? false))
|
rlm@0
|
709
|
rlm@0
|
710 (defn leaky-box []
|
rlm@0
|
711 (box 1 1 1
|
rlm@0
|
712 :position (Vector3f. 3 -1 0)
|
rlm@0
|
713 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
|
rlm@0
|
714 :texture "Textures/ColoredTex/Monkey.png"
|
rlm@0
|
715 :color (ColorRGBA. 1 0 1 1)
|
rlm@0
|
716 :physical? false))
|
rlm@0
|
717
|
rlm@0
|
718 (defn transparent-box []
|
rlm@0
|
719 (doto
|
rlm@0
|
720 (box 1 1 0.1
|
rlm@0
|
721 :position Vector3f/ZERO
|
rlm@0
|
722 :name "window frame"
|
rlm@0
|
723 :material "Common/MatDefs/Misc/Unshaded.j3md"
|
rlm@0
|
724 :texture "Textures/ColoredTex/Monkey.png"
|
rlm@0
|
725 :physical? false)
|
rlm@0
|
726 (-> (.getMaterial)
|
rlm@0
|
727 (.getAdditionalRenderState)
|
rlm@0
|
728 (.setBlendMode RenderState$BlendMode/Alpha))
|
rlm@0
|
729 (.setQueueBucket RenderQueue$Bucket/Transparent)))
|
rlm@0
|
730
|
rlm@0
|
731 (defn bumpy-sphere []
|
rlm@0
|
732 (doto
|
rlm@0
|
733 (sphere 2
|
rlm@0
|
734 :position (Vector3f. 0 2 -2)
|
rlm@0
|
735 :name "Shiny rock"
|
rlm@0
|
736 :material "Common/MatDefs/Light/Lighting.j3md"
|
rlm@0
|
737 :texture false
|
rlm@0
|
738 :physical? false)
|
rlm@0
|
739 (-> (.getMesh)
|
rlm@0
|
740 (doto
|
rlm@0
|
741 (.setTextureMode Sphere$TextureMode/Projected)
|
rlm@0
|
742 (TangentBinormalGenerator/generate)))
|
rlm@0
|
743 (-> (.getMaterial)
|
rlm@0
|
744 (doto
|
rlm@31
|
745 (.setTexture "DiffuseMap"
|
rlm@31
|
746 (.loadTexture (asset-manager)
|
rlm@34
|
747 "Textures/Terrain/Pond/Pond.jpg"))
|
rlm@31
|
748 (.setTexture "NormalMap"
|
rlm@31
|
749 (.loadTexture (asset-manager)
|
rlm@31
|
750 "Textures/Terrain/Pond/Pond_normal.png"))
|
rlm@0
|
751 (.setFloat "Shininess" (float 5))))
|
rlm@0
|
752 (.rotate (float 1.6) 0 0)))
|
rlm@0
|
753
|
rlm@0
|
754
|
rlm@0
|
755 (defn start-game []
|
rlm@0
|
756 (.start
|
rlm@0
|
757 (world
|
rlm@0
|
758 (let [root (Node.)]
|
rlm@0
|
759 (dorun (map #(.attachChild root %)
|
rlm@0
|
760 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
|
rlm@0
|
761 root)
|
rlm@0
|
762 {}
|
rlm@0
|
763 (fn [world]
|
rlm@0
|
764 (let [sun (doto (DirectionalLight.)
|
rlm@0
|
765 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
|
rlm@0
|
766 (.setColor ColorRGBA/White))]
|
rlm@0
|
767 (.addLight (.getRootNode world) sun)))
|
rlm@0
|
768 no-op
|
rlm@0
|
769 )))
|
rlm@0
|
770 #+end_src
|
rlm@0
|
771
|
rlm@0
|
772
|
rlm@0
|
773
|
rlm@0
|
774 * COMMENT code generation
|
rlm@0
|
775
|
rlm@0
|
776 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
|
rlm@0
|
777 <<brick-wall-header>>
|
rlm@0
|
778 <<brick-wall-body>>
|
rlm@0
|
779 #+end_src
|
rlm@0
|
780
|
rlm@0
|
781 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
|
rlm@0
|
782 <<hello-simple-app>>
|
rlm@0
|
783 #+end_src
|
rlm@0
|
784
|
rlm@34
|
785 #+begin_src clojure :tangle ../src/hello/hello_simpler_app.clj
|
rlm@34
|
786 <<hello-simpler-app>>
|
rlm@34
|
787 #+end_src
|
rlm@34
|
788
|
rlm@34
|
789
|
rlm@34
|
790 #+begin_src clojure :tangle ../src/hello/other_games.clj
|
rlm@0
|
791 <<other-games>>
|
rlm@0
|
792 #+end_src
|
rlm@0
|
793
|
rlm@0
|
794 #+begin_src clojure :tangle ../src/hello/loop.clj
|
rlm@0
|
795 <<hello-loop>>
|
rlm@0
|
796 #+end_src
|
rlm@0
|
797
|
rlm@0
|
798 #+begin_src clojure :tangle ../src/hello/collision.clj
|
rlm@0
|
799 <<hello-collision>>
|
rlm@0
|
800 #+end_src
|
rlm@0
|
801
|
rlm@0
|
802 #+begin_src clojure :tangle ../src/hello/terrain.clj
|
rlm@0
|
803 <<hello-terrain>>
|
rlm@0
|
804 #+end_src
|
rlm@0
|
805
|
rlm@0
|
806 #+begin_src clojure :tangle ../src/hello/animation.clj
|
rlm@0
|
807 <<hello-animation>>
|
rlm@0
|
808 #+end_src
|
rlm@0
|
809
|
rlm@0
|
810 #+begin_src clojure :tangle ../src/hello/material.clj
|
rlm@0
|
811 <<material>>
|
rlm@0
|
812 #+end_src
|
rlm@0
|
813
|
rlm@0
|
814
|
rlm@0
|
815
|
rlm@0
|
816
|
rlm@29
|
817
|
rlm@29
|
818
|
rlm@29
|
819
|
rlm@29
|
820
|
rlm@31
|
821
|