comparison org/games.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 f0562b9bde94
children b1b90c4ab0bf
comparison
equal deleted inserted replaced
33:c377222528e6 34:183744c179e6
1 #+title: Games! Games! Games! <3 1 #+title: jMonkeyEngine3 from Clojure
2 #+author: Robert McIntyre 2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu 3 #+email: rlm@mit.edu
4 #+description: Simulating senses for AI research using JMonkeyEngine3 4 #+description: Using jMonkeyEngine3 from clojure
5 #+keywords: clojure, jMonkeyEngine3, tutorial, examples
5 #+SETUPFILE: ../../aurellem/org/setup.org 6 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org 7 #+INCLUDE: ../../aurellem/org/level-0.org
7 #+babel: :mkdirp yes :noweb yes :exports both 8 #+babel: :mkdirp yes :noweb yes :exports both
8 9
9 10 [TABLE-OF-CONTENTS]
10 * Games! 11
11 12
12 Here are the jMonkeyEngine "Hello" programs translated to clojure. 13 Here are the jMonkeyEngine "Hello" programs translated to clojure.
13 ** Hello Simple App 14
15 * Hello Simple App
14 Here is the hello world example for jme3 in clojure. It's a more or 16 Here is the hello world example for jme3 in clojure. It's a more or
15 less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]]. 17 less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]].
16 18
17 Of note is the fact that since we don't have access to the 19 Of note is the fact that since we don't have access to the
18 =AssetManager= via extendig =SimpleApplication=, we have to build one 20 =AssetManager= via extendig =SimpleApplication=, we have to build one
19 ourselves. 21 ourselves.
20 22
21 #+srcname: hello-simple-app 23 #+srcname: hello-simple-app
22 #+begin_src clojure :results silent 24 #+begin_src clojure :results silent
23 (ns hello.hello-simple-app) 25 (ns hello.hello-simple-app
24 (require 'cortex.import) 26 (:use cortex.world)
25 (use 'clojure.contrib.def) 27 (:import com.jme3.math.Vector3f)
26 (rlm.rlm-commands/help) 28 (:import com.jme3.material.Material)
27 (cortex.import/mega-import-jme3) 29 (:import com.jme3.scene.Geometry)
28 (use 'cortex.world) 30 (:import com.jme3.math.ColorRGBA)
29 31 (:import com.jme3.app.SimpleApplication)
32 (:import com.jme3.scene.shape.Box))
30 33
31 (def cube (Box. Vector3f/ZERO 1 1 1)) 34 (def cube (Box. Vector3f/ZERO 1 1 1))
32 35
33 (def geom (Geometry. "Box" cube)) 36 (def geom (Geometry. "Box" cube))
34 37
60 #+end_src 63 #+end_src
61 64
62 #+caption: the simplest JME game. 65 #+caption: the simplest JME game.
63 [[../images/simple-app.jpg]] 66 [[../images/simple-app.jpg]]
64 67
65 68 * Simpler HelloSimpleApp
66 ** Hello Physics 69
70 #+srcname: hello-simpler-app
71 #+begin_src clojure
72 (ns hello.hello-simpler-app
73 (:use cortex.world)
74 (:use cortex.util)
75 (:import com.jme3.math.ColorRGBA)
76 (:import com.jme3.scene.Node))
77
78 (defn simpler-world
79 "The jMonkeyEngine3 Hello World program. Displays a blue 3D cube in
80 a basic 3D world."
81 []
82 (world (doto (Node.)
83 (.attachChild
84 (box 1 1 1
85 :color ColorRGBA/Blue :physical? false)))
86 {} no-op no-op))
87 #+end_src
88
89 More information about the jMonkeyEngine3 hello world program can be
90 found [[http://jmonkeyengine.org/wiki/doku.php/starter:hello_world][here]].
91
92 * COMMENT Hello Physics
67 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics 93 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
68 94
69 #+srcname: brick-wall-header 95 #+srcname: brick-wall-header
70 #+begin_src clojure :results silent 96 #+begin_src clojure :results silent
71 (ns hello.brick-wall) 97 (ns hello.brick-wall)
88 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0))) 114 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
89 115
90 (def brick-length 0.48) 116 (def brick-length 0.48)
91 (def brick-width 0.24) 117 (def brick-width 0.24)
92 (def brick-height 0.12) 118 (def brick-height 0.12)
93 119 (def gravity (Vector3f. 0 -9.81 0))
94 120
95 (defn brick* [position] 121 (defn brick* [position]
96 (doto (box brick-length brick-height brick-width 122 (doto (box brick-length brick-height brick-width
97 :position position :name "brick" 123 :position position :name "brick"
98 :material "Common/MatDefs/Misc/Unshaded.j3md" 124 :material "Common/MatDefs/Misc/Unshaded.j3md"
124 (defn gravity-toggle 150 (defn gravity-toggle
125 [new-value] 151 [new-value]
126 (fn [game value] 152 (fn [game value]
127 (println-repl "set gravity to " new-value) 153 (println-repl "set gravity to " new-value)
128 (if value 154 (if value
129 (set-gravity* game new-value) 155 (set-gravity game new-value)
130 (set-gravity* game gravity)))) 156 (set-gravity game gravity))))
131 157
132 (defn fire-cannon-ball 158 (defn fire-cannon-ball
133 ([node] 159 ([node]
134 (fn [game value] 160 (fn [game value]
135 (if (not value) 161 (if (not value)
214 [[../images/brick-wall-standing.jpg]] 240 [[../images/brick-wall-standing.jpg]]
215 241
216 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball" 242 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
217 [[../images/brick-wall-knocked-down.jpg]] 243 [[../images/brick-wall-knocked-down.jpg]]
218 244
219 ** Other Brick Games 245 * COMMENT Other Brick Games
220 #+srcname: other-games 246 #+srcname: other-games
221 #+begin_src clojure :results silent 247 #+begin_src clojure :results silent
222 (ns cortex.other-games 248 (ns hello.other-games
223 {:author "Dylan Holmes"}) 249 {:author "Dylan Holmes"})
224 (use 'cortex.world) 250 (use 'cortex.world)
225 (use 'hello.brick-wall) 251 (use 'hello.brick-wall)
226 (use 'cortex.import) 252 (use 'cortex.import)
227 (cortex.import/mega-import-jme3) 253 (cortex.import/mega-import-jme3)
362 #+end_src 388 #+end_src
363 389
364 #+caption: floating dominos 390 #+caption: floating dominos
365 [[../images/dominos.jpg]] 391 [[../images/dominos.jpg]]
366 392
367 ** Hello Loop 393 * Hello Loop
368 #+srcname: hello-loop 394 #+srcname: hello-loop
369 #+begin_src clojure :results silent 395 #+begin_src clojure :results silent
370 (ns hello.loop) 396 (ns hello.loop
371 (use 'cortex.world) 397 (:use cortex.world)
372 (use 'cortex.import) 398 (:use cortex.util)
373 (cortex.import/mega-import-jme3) 399 (:import com.jme3.math.ColorRGBA)
374 (rlm.rlm-commands/help) 400 (:import com.jme3.scene.Node))
375 401
376 (defn blue-cube [] 402 (defn blue-cube []
377 (box 1 1 1 403 (box 1 1 1
378 :color ColorRGBA/Blue 404 :color ColorRGBA/Blue
379 :texture false 405 :texture false
380 :material "Common/MatDefs/Misc/Unshaded.j3md" 406 :material "Common/MatDefs/Misc/Unshaded.j3md"
389 no-op 415 no-op
390 (fn [game tpf] 416 (fn [game tpf]
391 (.rotate cube 0.0 (* 2 tpf) 0.0))))) 417 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
392 #+end_src 418 #+end_src
393 419
394 ** Hello Collision 420 * COMMENT Hello Collision
395 421
396 #+srcname: hello-collision 422 #+srcname: hello-collision
397 #+begin_src clojure :results silent 423 #+begin_src clojure :results silent
398 (ns hello.collision) 424 (ns hello.collision)
399 (use 'cortex.world) 425 (use 'cortex.world)
511 (responses) 537 (responses)
512 setup-fn 538 setup-fn
513 update-fn))) 539 update-fn)))
514 #+end_src 540 #+end_src
515 541
516 ** Hello Terrain 542 * COMMENT Hello Terrain
517 #+srcname: hello-terrain 543 #+srcname: hello-terrain
518 #+begin_src clojure :results silent 544 #+begin_src clojure :results silent
519 (ns hello.terrain) 545 (ns hello.terrain)
520 (use 'cortex.world) 546 (use 'cortex.world)
521 (use 'cortex.import) 547 (use 'cortex.import)
661 (key-map channel) 687 (key-map channel)
662 (partial setup-fn channel) 688 (partial setup-fn channel)
663 no-op)))) 689 no-op))))
664 #+end_src 690 #+end_src
665 691
666 ** Hello Materials 692 * COMMENT Hello Materials
667 #+srcname: material 693 #+srcname: material
668 #+begin_src clojure :results silent 694 #+begin_src clojure :results silent
669 (ns hello.material) 695 (ns hello.material)
670 (use 'cortex.world) 696 (use 'cortex.world)
671 (use 'cortex.import) 697 (use 'cortex.import)
698 (use 'cortex.util)
672 (use 'clojure.contrib.def) 699 (use 'clojure.contrib.def)
673 (cortex.import/mega-import-jme3) 700 (cortex.import/mega-import-jme3)
674 (rlm.rlm-commands/help) 701 (rlm.rlm-commands/help)
675 702
676 (defn simple-cube [] 703 (defn simple-cube []
715 (TangentBinormalGenerator/generate))) 742 (TangentBinormalGenerator/generate)))
716 (-> (.getMaterial) 743 (-> (.getMaterial)
717 (doto 744 (doto
718 (.setTexture "DiffuseMap" 745 (.setTexture "DiffuseMap"
719 (.loadTexture (asset-manager) 746 (.loadTexture (asset-manager)
720 "Textures/Terrain/Pond/Pond.png")) 747 "Textures/Terrain/Pond/Pond.jpg"))
721 (.setTexture "NormalMap" 748 (.setTexture "NormalMap"
722 (.loadTexture (asset-manager) 749 (.loadTexture (asset-manager)
723 "Textures/Terrain/Pond/Pond_normal.png")) 750 "Textures/Terrain/Pond/Pond_normal.png"))
724 (.setFloat "Shininess" (float 5)))) 751 (.setFloat "Shininess" (float 5))))
725 (.rotate (float 1.6) 0 0))) 752 (.rotate (float 1.6) 0 0)))
753 780
754 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj 781 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
755 <<hello-simple-app>> 782 <<hello-simple-app>>
756 #+end_src 783 #+end_src
757 784
758 #+begin_src clojure :tangle ../src/cortex/other_games.clj 785 #+begin_src clojure :tangle ../src/hello/hello_simpler_app.clj
786 <<hello-simpler-app>>
787 #+end_src
788
789
790 #+begin_src clojure :tangle ../src/hello/other_games.clj
759 <<other-games>> 791 <<other-games>>
760 #+end_src 792 #+end_src
761 793
762 #+begin_src clojure :tangle ../src/hello/loop.clj 794 #+begin_src clojure :tangle ../src/hello/loop.clj
763 <<hello-loop>> 795 <<hello-loop>>