diff 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
line wrap: on
line diff
     1.1 --- a/org/util.org	Mon Oct 24 01:19:15 2011 -0700
     1.2 +++ b/org/util.org	Mon Oct 24 05:25:01 2011 -0700
     1.3 @@ -61,11 +61,13 @@
     1.4  #+results:
     1.5  : 955
     1.6  
     1.7 -** Simplification
     1.8 +
     1.9  #+srcname: world-view
    1.10  #+begin_src clojure :results silent
    1.11 -(in-ns 'cortex.debug)
    1.12 -
    1.13 +(ns cortex.util)
    1.14 +(require 'cortex.import)
    1.15 +(cortex.import/mega-import-jme3)
    1.16 +(use 'cortex.world)
    1.17  (defprotocol Viewable
    1.18    (view [something]))
    1.19  
    1.20 @@ -94,12 +96,6 @@
    1.21  			      (.setColor ColorRGBA/White))]
    1.22  		    (.addLight (.getRootNode world) sun)))
    1.23  		no-op))))
    1.24 -
    1.25 -(defn position-camera [game]
    1.26 -  (doto (.getCamera game)
    1.27 -    (.setLocation (Vector3f. 0 6 6))
    1.28 -    (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
    1.29 -
    1.30  #+end_src
    1.31  
    1.32  Here I make the =Viewable= protocol and extend it to JME's types.  Now
    1.33 @@ -110,8 +106,131 @@
    1.34  #+end_src
    1.35  
    1.36  
    1.37 +#+srcname: util
    1.38 +#+begin_src clojure 
    1.39 +(in-ns 'cortex.util)
    1.40 +
    1.41 +(def println-repl (bound-fn [& args] (apply println args)))
    1.42 +
    1.43 +(defn position-camera [game]
    1.44 +  (doto (.getCamera game)
    1.45 +    (.setLocation (Vector3f. 0 6 6))
    1.46 +    (.lookAt Vector3f/ZERO (Vector3f. 0 1 0))))
    1.47 +
    1.48 +(defn set-gravity*
    1.49 +  [game gravity]
    1.50 +  (traverse
    1.51 +   (fn [geom]
    1.52 +     (if-let
    1.53 +	 [control (.getControl geom RigidBodyControl)]
    1.54 +       (do
    1.55 +	 (.setGravity control gravity)
    1.56 +	 (.applyImpulse control Vector3f/ZERO Vector3f/ZERO)
    1.57 +	 )))
    1.58 +   (.getRootNode game)))
    1.59 +#+end_src
    1.60 +
    1.61 +
    1.62 +#+srcname: shapes
    1.63 +#+begin_src clojure :results silent
    1.64 +(in-ns 'cortex.util)
    1.65 +(defrecord shape-description
    1.66 +  [name
    1.67 +   color
    1.68 +   mass
    1.69 +   friction
    1.70 +   texture
    1.71 +   material
    1.72 +   position
    1.73 +   rotation
    1.74 +   shape
    1.75 +   physical?])
    1.76 +
    1.77 +(def base-shape
    1.78 +     (shape-description.
    1.79 +      "default-shape"
    1.80 +      false
    1.81 +      ;;ColorRGBA/Blue
    1.82 +      1.0 ;; mass
    1.83 +      1.0 ;; friction
    1.84 +      ;; texture
    1.85 +      "Textures/Terrain/BrickWall/BrickWall.jpg"
    1.86 +      ;; material
    1.87 +      "Common/MatDefs/Misc/Unshaded.j3md"
    1.88 +      Vector3f/ZERO
    1.89 +      Quaternion/IDENTITY
    1.90 +      (Box. Vector3f/ZERO 0.5 0.5 0.5)
    1.91 +      true))
    1.92 +
    1.93 +(defn make-shape
    1.94 +  [#^shape-description d]
    1.95 +  (let [asset-manager (if (:asset-manager d) (:asset-manager d) (asset-manager))
    1.96 +        mat (Material. asset-manager (:material d))
    1.97 +	geom (Geometry. (:name d) (:shape d))]
    1.98 +    (if (:texture d)
    1.99 +      (let [key (TextureKey. (:texture d))]
   1.100 +	(.setGenerateMips key true)
   1.101 +	(.setTexture mat "ColorMap" (.loadTexture asset-manager key))))
   1.102 +    (if (:color d) (.setColor mat "Color" (:color d)))
   1.103 +    (.setMaterial geom mat)
   1.104 +    (if-let [rotation (:rotation d)] (.rotate geom rotation))
   1.105 +    (.setLocalTranslation geom (:position d))
   1.106 +    (if (:physical? d)
   1.107 +      (let [impact-shape (doto (GImpactCollisionShape. 
   1.108 +                                (.getMesh geom)) (.setMargin 0))
   1.109 +	    physics-control (RigidBodyControl.
   1.110 +			     ;;impact-shape ;; comment to disable 
   1.111 +			     (float (:mass d)))]
   1.112 +	(.createJmeMesh impact-shape)
   1.113 +	(.addControl geom physics-control)
   1.114 +	;;(.setSleepingThresholds physics-control (float 0) (float 0))
   1.115 +	(.setFriction physics-control (:friction d))))
   1.116 +    ;;the default is to keep this node in the physics engine forever.
   1.117 +    ;;these commands must come after the control is added to the geometry.
   1.118 +    ;;
   1.119 +    geom))
   1.120 +  
   1.121 +(defn box
   1.122 +  ([l w h & {:as options}]
   1.123 +     (let [options (merge base-shape options)]
   1.124 +       (make-shape (assoc options
   1.125 +		     :shape (Box. l w h)))))
   1.126 +  ([] (box 0.5 0.5 0.5)))
   1.127 +
   1.128 +(defn sphere
   1.129 +  ([r & {:as options}]
   1.130 +     (let [options (merge base-shape options)]
   1.131 +       (make-shape (assoc options
   1.132 +		     :shape (Sphere. 32 32 (float r)))))) 
   1.133 +  ([] (sphere 0.5)))
   1.134 +
   1.135 +(defn add-element
   1.136 +  ([game element node]
   1.137 +  (.addAll
   1.138 +   (.getPhysicsSpace
   1.139 +    (.getState
   1.140 +     (.getStateManager game)
   1.141 +     BulletAppState))
   1.142 +    element)
   1.143 +  (.attachChild node element))
   1.144 +  ([game element]
   1.145 +     (add-element game element (.getRootNode game))))
   1.146 +
   1.147 +
   1.148 +
   1.149 +#+end_src
   1.150 +
   1.151 +
   1.152  
   1.153  * COMMENT code generation
   1.154  #+begin_src clojure :tangle ../src/cortex/import.clj
   1.155  <<import>>
   1.156  #+end_src
   1.157 +
   1.158 +
   1.159 +#+begin_src clojure :tangle ../src/cortex/util.clj
   1.160 +<<world-view>>
   1.161 +<<util>>
   1.162 +<<shapes>>
   1.163 +#+end_src
   1.164 +