annotate org/cortex.org @ 29:6372c108c5c6

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