Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.39 KB | None | 0 0
  1. package mygame;
  2. import com.jme3.export.InputCapsule;
  3. import com.jme3.export.JmeExporter;
  4. import com.jme3.export.JmeImporter;
  5. import com.jme3.export.OutputCapsule;
  6. import com.jme3.input.InputManager;
  7. import com.jme3.math.FastMath;
  8. import com.jme3.math.Quaternion;
  9. import com.jme3.math.Vector3f;
  10. import com.jme3.renderer.Camera;
  11. import com.jme3.renderer.RenderManager;
  12. import com.jme3.renderer.ViewPort;
  13. import com.jme3.scene.Node;
  14. import com.jme3.scene.Spatial;
  15. import com.jme3.scene.control.Control;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * A camera that follows a spatial and can turn around it by dragging the mouse
  20.  * @author nehon (Modified by Taibhse to fit needs)
  21.  *
  22.  */
  23. public class OrbitCamera implements Control {
  24.  
  25.     public static boolean printCamLocation = false;
  26.     public static Node target = null;  //was spatial
  27.    
  28.     private static float minHeight = -1.57f; //max height rotation 90deg
  29.     private static float maxHeight = 1.57f;  //min height rotation 90deg
  30.     private static float minDistance = 1f; //closest zoom can get
  31.     private static float maxDistance = 500.0f; //furthest distance camera will zoom out
  32.     //public to allow control later, ie for controls
  33.     //allowing user to adjust sensitivity
  34.     public static float zoomSpeed = 2.0f; //speed of zoom
  35.     public static float rotationSpeed = 5f; //speed of rotation
  36.     public static float distance = 20; //starting distance of camera
  37.     public static float maxOrbitDistance = 20;
  38.    
  39.     /**
  40.      * the camera.
  41.      */
  42.     public static Camera cam = null;
  43.     private InputManager inputManager;
  44.     private static Vector3f initialUpVec;
  45.     public static boolean canRotate;
  46.     public static float rotation = FastMath.PI / 2;
  47.     public static float vRotation = 0;
  48.     public static boolean enabled = false;  //should be false by default
  49.  
  50.     /**
  51.      * Constructs the chase camera
  52.      * @param cam the application camera
  53.      * @param target the spatial to follow
  54.      */
  55.     public OrbitCamera(Camera cam, Vector3f focus) {
  56.         this.target = new Node();
  57.         this.target.setLocalTranslation(focus.x, focus.y, focus.z);
  58.        
  59.        // target.addControl(this);
  60.         this.cam = cam;
  61.        
  62.         initialUpVec = cam.getUp().clone();
  63.     }
  64.  
  65.     /**
  66.      * Constructs the chase camera, and registers inputs
  67.      * @param cam the application camera
  68.      * @param target the spatial to follow
  69.      * @param inputManager the inputManager of the application to register inputs
  70.      */
  71.     public OrbitCamera(Vector3f focus, Camera cam, final Spatial target, InputManager inputManager) {
  72.         this.target = new Node();
  73.         target.setLocalTranslation(focus.x, focus.y, focus.z);
  74.        
  75.         target.addControl(this);
  76.         this.cam = cam;
  77.         initialUpVec = cam.getUp().clone();
  78.        // registerWithInput(inputManager);
  79.     }
  80.  
  81.  
  82.  
  83.     public void updateFocus(Vector3f focus)
  84.     {
  85.         this.target.setLocalTranslation(focus.x, focus.y, focus.z);
  86.          updateCamera();
  87.     }
  88.     //rotate the camera around the target on the horizontal plane
  89.     public static void rotateCamera(float value) {
  90.         if (!canRotate || !enabled) {
  91.             return;
  92.         }
  93.         rotation += value * rotationSpeed;
  94.         updateCamera();
  95.     }
  96.  
  97.     //move the camera toward or away the target
  98.     public static void zoomCamera(float value) {
  99.  
  100.         //commented out as I want to allow zooming even if rotation is disabled
  101. //        if (!enabled) {
  102. //            return;
  103. //        }
  104.  
  105.         distance += value * zoomSpeed;
  106.         if (distance > maxDistance) {
  107.             distance = maxDistance;
  108.         }
  109.         if (distance < minDistance) {
  110.             distance = minDistance;
  111.         }
  112.         if ((vRotation < minHeight) && (distance > (minDistance + 1.0f))) {
  113.             vRotation = minHeight;
  114.         }
  115.  
  116.         updateCamera();
  117.     }
  118.  
  119.     //rotate the camera around the target on the vertical plane
  120.     public static void vRotateCamera(float value) {
  121.         if (!canRotate || !enabled) {
  122.             return;
  123.         }
  124.         vRotation += value * rotationSpeed;
  125.         if (vRotation > maxHeight) {
  126.             vRotation = maxHeight;
  127.         }
  128.         if ((vRotation < minHeight) && (distance > (minDistance + 1.0f))) {
  129.             vRotation = minHeight;
  130.         }
  131.         updateCamera();
  132.     }
  133.  
  134.     /**
  135.      * Update the camera, should only be called internally
  136.      */
  137.     public static void updateCamera() {
  138.        
  139.        
  140.         float hDistance = (distance) * FastMath.sin((FastMath.PI / 2) - vRotation);
  141.         Vector3f pos = new Vector3f(hDistance * FastMath.cos(rotation), distance * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));
  142.         pos = pos.add(target.getLocalTranslation());
  143.         cam.setLocation(pos);
  144.         cam.lookAt(target.getWorldTranslation(), initialUpVec);
  145.        
  146.         Canvas.n.setLocalRotation(new Quaternion().set(-cam.getRotation().getX(), -cam.getRotation().getY(), -cam.getRotation().getZ(), cam.getRotation().getW()));
  147.    
  148.     }
  149.  
  150.      public void moveCamera(float vertical, float horizontal) {
  151.       distance = 20;
  152.       vRotation = vertical;
  153.       rotation = horizontal;
  154.      
  155.     }
  156.      
  157.      public static Vector3f getCameraLocation()
  158.      {
  159.        Vector3f loc = new Vector3f(cam.getLocation().x, cam.getLocation().y, cam.getLocation().z);
  160.        return loc;
  161.      }
  162.      
  163.      
  164.     /**
  165.      * Return the enabled/disabled state of the camera
  166.      * @return true if the camera is enabled
  167.      */
  168.     public boolean isEnabled() {
  169.         return enabled;
  170.     }
  171.  
  172.     /**
  173.      * Enable or disable the camera
  174.      * @param enabled true to enable
  175.      */
  176.     public void setEnabled(boolean enabled) {
  177.         this.enabled = enabled;
  178.         if (!enabled) {
  179.             canRotate = false; // reset this flag in-case it was on before
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Returns the max zoom distance of the camera (default is 40)
  185.      * @return maxDistance
  186.      */
  187.     public float getMaxDistance() {
  188.         return maxDistance;
  189.     }
  190.    
  191.     /**
  192.      * Sets the max zoom distance of the camera (default is 40)
  193.      * @param maxDistance
  194.      */
  195.     public void setMaxDistance(float maxDistance) {
  196.         this.maxDistance = maxDistance;
  197.     }
  198.  
  199.     /**
  200.      * Returns the min zoom distance of the camera (default is 1)
  201.      * @return minDistance
  202.      */
  203.     public float getMinDistance() {
  204.         return minDistance;
  205.     }
  206.  
  207.     /**
  208.      * Sets the min zoom distance of the camera (default is 1)
  209.      * @return minDistance
  210.      */
  211.     public void setMinDistance(float minDistance) {
  212.         this.minDistance = minDistance;
  213.     }
  214.  
  215.     /**
  216.      * clone this camera for a spatial
  217.      * @param spatial
  218.      * @return
  219.      */
  220.     public Control cloneForSpatial(Spatial spatial) {
  221.      //   OrbitCamera cc = new OrbitCamera(cam, spatial, inputManager);
  222.      //   cc.setMaxDistance(getMaxDistance());
  223.       //  cc.setMinDistance(getMinDistance());
  224.         return null;
  225.     }
  226.  
  227.     /**
  228.      * Sets the spacial for the camera control, should only be used internally
  229.      * @param spatial
  230.      */
  231.     public void setSpatial(Spatial spatial) {
  232.      //   target = spatial;
  233.     }
  234.  
  235.     /**
  236.      * update the camera control, should only be used internally
  237.      * @param tpf
  238.      */
  239.     public void update(float tpf) {
  240.         updateCamera();
  241.        
  242.         if(printCamLocation == true)
  243.         {
  244.         System.out.println("V-ROT: " + vRotation + "   H-ROT: " + rotation + "   D: " + distance);
  245.         }
  246.    
  247.         }
  248.  
  249.     /**
  250.      * renders the camera control, should on ly be used internally
  251.      * @param rm
  252.      * @param vp
  253.      */
  254.     public void render(RenderManager rm, ViewPort vp) {
  255.         //nothing to render
  256.     }
  257.  
  258.     /**
  259.      * Write the camera
  260.      * @param ex the exporter
  261.      * @throws IOException
  262.      */
  263.     public void write(JmeExporter ex) throws IOException {
  264.         OutputCapsule capsule = ex.getCapsule(this);
  265.         capsule.write(maxDistance, "maxDistance", 40);
  266.         capsule.write(minDistance, "minDistance", 1);
  267.     }
  268.  
  269.     /**
  270.      * Read the camera
  271.      * @param im
  272.      * @throws IOException
  273.      */
  274.     public void read(JmeImporter im) throws IOException {
  275.         InputCapsule ic = im.getCapsule(this);
  276.         maxDistance = ic.readFloat("maxDistance", 40);
  277.         minDistance = ic.readFloat("minDistance", 1);
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement