Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 16th, 2014  |  syntax: None  |  size: 6.83 KB  |  views: 1  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.voxel.engine.core.Camera;
  2.  
  3. import com.voxel.engine.core.Utils.Frustum;
  4. import org.lwjgl.opengl.GL11;
  5. import org.lwjgl.util.vector.Vector3f;
  6.  
  7. /**
  8.  * Created with IntelliJ IDEA.
  9.  * User: Toby's PC
  10.  * Date: 14/01/14
  11.  * Time: 19:10
  12.  * To change this template use File | Settings | File Templates.
  13.  */
  14. public class FPCameraController {
  15.  
  16.         public Frustum frustum;
  17.  
  18.         //3d vector to store the camera's position in
  19.         private Vector3f position = null;
  20.  
  21.         //the rotation around the Y axis of the camera
  22.         private float yaw = 0.0f;
  23.  
  24.         //the rotation around the X axis of the camera
  25.         private float pitch = 0.0f;
  26.  
  27.         private float walkbias  = 0.0f;
  28.         private float walkbiasangle = 0.0f;
  29.  
  30.         private float piover180   =  0.01745329F;
  31.  
  32.         public float getWalkBias() { return walkbias; }
  33.  
  34.         private static FPCameraController instance = null;
  35.  
  36.         public synchronized static FPCameraController getInstance()
  37.         {
  38.             if(instance == null)
  39.             {
  40.                 instance = new FPCameraController(0,4,0,0,0);
  41.             }
  42.             return instance;
  43.         }
  44.  
  45.         //Constructor that takes the starting x, y, z location of the camera
  46.         public FPCameraController(float x, float y, float z, float pitch, float yaw)
  47.         {
  48.             frustum = new Frustum();
  49.             position = new Vector3f(x, y, z);
  50.             this.yaw(yaw);
  51.             this.pitch(pitch);
  52.         }
  53.  
  54.         public float getCameraYaw() { return yaw; }
  55.         public float getCameraPitch() { return pitch; }
  56.         public Vector3f getCameraPosition() { return position; }
  57.  
  58.         public void setPosition(Vector3f pos) {
  59.             this.position.x = pos.x;
  60.             this.position.y = pos.y;
  61.             this.position.z = pos.z;
  62.         }
  63.  
  64.         //increment the camera's current yaw rotation (Y)
  65.         public void yaw(float amount)
  66.         {
  67.             yaw += amount;
  68.  
  69.             if(yaw > 180f) yaw = -180;
  70.             else if(yaw < -180f) yaw = 180f;
  71.  
  72.         }
  73.  
  74.         //increment the camera's current pitch rotation (X)
  75.         public void pitch(float amount)
  76.         {
  77.             pitch += amount;
  78.  
  79.             if(pitch > 80f) pitch = 80f;
  80.             else if(pitch < -80f) pitch = -80f;
  81.  
  82.  
  83.         }
  84.  
  85.         public void roll(float amount)
  86.         {
  87.             // not used
  88.         }
  89.  
  90.         // X is sin(angle)
  91.         public float getXPos(float angle)
  92.         {
  93.             return (float)Math.sin(Math.toRadians(angle));
  94.         }
  95.         // Z is cos(angle) * r
  96.         public float getZPos(float angle)
  97.         {
  98.             return (float)Math.cos(Math.toRadians(angle));
  99.         }
  100.         //moves the camera forward relative to its current rotation (yaw)
  101.         public void walkForward(Vector3f velocity)
  102.         {
  103.  
  104.             position.x += velocity.x * getXPos(yaw);
  105.             position.z -= velocity.z * getZPos(yaw);
  106.  
  107.             if (walkbiasangle >= 359.0f)
  108.             {
  109.                 walkbiasangle = 0.0f;
  110.             }
  111.             else
  112.             {
  113.                 walkbiasangle+= 10;
  114.             }
  115.             walkbias = (float)Math.sin(walkbiasangle * piover180)/15.0f;
  116.  
  117.         }
  118.  
  119.         //moves the camera backward relative to its current rotation (yaw)
  120.         public void walkBackwards(Vector3f velocity)// float distance)
  121.         {
  122.             position.x -= velocity.x * getXPos(yaw);
  123.             position.z += velocity.z * getZPos(yaw);
  124.  
  125.             if (walkbiasangle <= 1.0f)                 // Is walkbiasangle>=359?
  126.             {
  127.                 walkbiasangle = 359.0f;                   // Make walkbiasangle Equal 0
  128.             }
  129.             else                                // Otherwise
  130.             {
  131.                 walkbiasangle-= 10;                    // If walkbiasangle < 359 Increase It By 10
  132.             }
  133.             walkbias = (float)Math.sin(walkbiasangle * piover180)/15.0f;
  134.         }
  135.  
  136.         //strafes the camera left relative to its current rotation (yaw)
  137.         public void strafeLeft(Vector3f velocity)
  138.         {
  139.             // player position is x=sin(yaw), z=cos(yaw)
  140.             // x is distance * sin(yaw)
  141.             // z is distance * cos(yaw)
  142.             // we subtract 90 to the angle so to strafe
  143.             position.x += velocity.x * getXPos(yaw- 90);  // .5PI
  144.             position.z -= velocity.z * getZPos(yaw- 90);  // .5PI
  145.  
  146.             if (walkbiasangle <= 1.0f)                 // Is walkbiasangle>=359?
  147.             {
  148.                 walkbiasangle = 359.0f;                   // Make walkbiasangle Equal 0
  149.             }
  150.             else                                // Otherwise
  151.             {
  152.                 walkbiasangle-= 10;                    // If walkbiasangle < 359 Increase It By 10
  153.             }
  154.             walkbias = (float)Math.sin(walkbiasangle * piover180)/15.0f;
  155.         }
  156.  
  157.         //strafes the camera right relative to its current rotation (yaw)
  158.         public void strafeRight(Vector3f velocity)
  159.         {
  160.             // x is distance * sin(yaw)
  161.             // z is distance * cos(yaw)
  162.             // we add 90 to the angle so to strafe
  163.             position.x += velocity.x * getXPos(yaw+90);
  164.             position.z -= velocity.z * getZPos(yaw+90);
  165.  
  166.             if (walkbiasangle <= 1.0f)                 // Is walkbiasangle>=359?
  167.             {
  168.                 walkbiasangle = 359.0f;                   // Make walkbiasangle Equal 0
  169.             }
  170.             else                                // Otherwise
  171.             {
  172.                 walkbiasangle+= 10;                    // If walkbiasangle < 359 Increase It By 10
  173.             }
  174.             walkbias = (float)Math.sin(walkbiasangle * piover180)/15.0f;
  175.         }
  176.  
  177.         public void jump(float distance)
  178.         {
  179.             position.y += distance;
  180.         }
  181.         public void down(float distance)
  182.         {
  183.             position.y -= distance;
  184.         }
  185.  
  186.         public void resetPosition(float x, float y, float z, float pitch, float yaw)
  187.         {
  188.             position.x = x; position.y = y; position.z = z;
  189.             this.pitch = pitch;
  190.             this.yaw = yaw;
  191.         }
  192.  
  193.         //translates and rotate the matrix so that it looks through the camera
  194.         //this does basically what gluLookAt() does
  195.         public void lookThrough()
  196.         {
  197.             //rotate the pitch around the X axis
  198.             GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
  199.             //rotate the yaw around the Y axis
  200.             // remove line below for third person
  201.             float ytrans = position.y - (-walkbias-0.25f);
  202.             GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
  203.             //translate to the position vector's location
  204.             GL11.glTranslatef(-position.x, -ytrans, -position.z);
  205.  
  206.             frustum.calculateFrustum();
  207.  
  208.         }
  209. }