Guest User

camera Class sphere problem

a guest
Oct 22nd, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package cubeTest;
  2.  
  3. import org.lwjgl.input.Keyboard;
  4. import org.lwjgl.input.Mouse;
  5. import org.lwjgl.util.vector.Vector3f;
  6.  
  7. public class Camera {
  8.  
  9.     public Vector3f pos = new Vector3f(50, 50, -50);
  10.     public float pitch, yaw, roll;
  11.     private float speed = 0.3f;
  12.  
  13.     private final float MAX_UP = 85;
  14.     private final float MAX_DOWN = -85;
  15.     private float RANGE = 10;
  16.  
  17.     private boolean m;
  18.  
  19.     public void tick() {
  20.         if (Keyboard.isKeyDown(Keyboard.KEY_F))
  21.             Mouse.setGrabbed(false);
  22.  
  23.         if (Mouse.isGrabbed()) {
  24.             float dx = Mouse.getDX() * 0.1f * 2;
  25.             float dy = Mouse.getDY() * 0.1f * 2;
  26.  
  27.             pitch -= dy;
  28. //          if(pitch < 0) pitch = 360;
  29. //          if(pitch > 360) pitch = 360;
  30.            
  31.             yaw += dx;
  32.  
  33.             if (pitch >= MAX_UP)
  34.                 pitch = MAX_UP;
  35.             if (pitch <= MAX_DOWN)
  36.                 pitch = MAX_DOWN;
  37.  
  38.             // toggle block spawn
  39.             if (Mouse.isButtonDown(0) && m) {
  40.                 m = false;
  41.                 // x,y,z range
  42.                 float xr = RANGE * (float) Math.sin(Math.toRadians(-yaw));
  43.                 float zr = RANGE * (float) Math.cos(Math.toRadians(-yaw));
  44.                 float yr = RANGE * (float) Math.tan(Math.toRadians(pitch));
  45.                
  46. //              float xr = (float) (RANGE * Math.cos(Math.toRadians(yaw)) * Math.sin(pitch));
  47. //              float yr = (float) (RANGE * Math.sin(Math.toRadians(yaw)) * Math.sin(pitch));
  48. //              float zr = (float) (RANGE * Math.cos(pitch));
  49.                
  50.                 CubeGame.addCube(new Vector3f(pos.x - xr, pos.y - yr, pos.z
  51.                         - zr), 1);
  52.                 System.out.println("");
  53.                  System.out.println("pitch: " + pitch);
  54.                 System.out.println("y-range: " + yr);
  55.                 System.out.println("z-range: " + zr);
  56.                 System.out.println("x-range: " + xr);
  57.             } else if (!Mouse.isButtonDown(0)) {
  58.             }
  59.             m = true;
  60.  
  61.             if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  62.                 pos.x += speed * (float) Math.sin(Math.toRadians(yaw));
  63.                 pos.z -= speed * (float) Math.cos(Math.toRadians(yaw));
  64.             }
  65.             if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  66.                 pos.x -= speed * (float) Math.sin(Math.toRadians(yaw));
  67.                 pos.z += speed * (float) Math.cos(Math.toRadians(yaw));
  68.             }
  69.             if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  70.                 pos.x += speed * (float) Math.sin(Math.toRadians(yaw - 90));
  71.                 pos.z -= speed * (float) Math.cos(Math.toRadians(yaw - 90));
  72.             }
  73.             if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  74.                 pos.x += speed * (float) Math.sin(Math.toRadians(yaw + 90));
  75.                 pos.z -= speed * (float) Math.cos(Math.toRadians(yaw + 90));
  76.             }
  77.             if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
  78.                 pos.y += speed;
  79.             }
  80.             if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  81.                 pos.y -= speed;
  82.             }
  83.         } else if (Mouse.isButtonDown(0)) {
  84.             Mouse.setGrabbed(true);
  85.         }
  86.     }
  87.  
  88.     public Vector3f getPos() {
  89.         return pos;
  90.     }
  91.  
  92.     public float getPitch() {
  93.         return pitch;
  94.     }
  95.  
  96.     public float getYaw() {
  97.         return yaw;
  98.     }
  99.  
  100.     public float getRoll() {
  101.         return roll;
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment