Advertisement
Guest User

Camera-class

a guest
Apr 16th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package base;
  2.  
  3. import org.lwjgl.input.Keyboard;
  4. import org.lwjgl.util.vector.Matrix4f;
  5. import org.lwjgl.util.vector.Vector3f;
  6.  
  7. import base.*;
  8.  
  9. public class Camera {
  10.     Vector3f xAxis, yAxis, zAxis, newPos, currPos;
  11.     float moveSpeed = 0.1f;
  12.     boolean moveForw, moveBack, moveLeft, moveRight;
  13.    
  14.     public Camera(Vector3f position, Vector3f target, Vector3f up){
  15.         Matrices.viewMatrix = new Matrix4f();
  16.         Matrix4f.setIdentity(Matrices.viewMatrix);
  17.        
  18.         //Initialize all vectors
  19.         this.xAxis = new Vector3f();
  20.         this.yAxis = new Vector3f();
  21.         this.zAxis = new Vector3f();
  22.         this.newPos = new Vector3f();
  23.         this.currPos = new Vector3f();
  24.        
  25.         //Set all values to the Camera-class values
  26.         position.negate(this.currPos);
  27.         target.normalise(this.zAxis);
  28.         up.normalise(this.yAxis);
  29.        
  30.         Vector3f.cross(target, up, xAxis);
  31.         Vector3f.cross(xAxis, target, yAxis); //Recalulate yAxis to make it valid coordinate system.
  32.        
  33.         /*
  34.          * This defines the View Matrix. This is calculated and explained here:
  35.          * http://ogldev.atspace.co.uk/www/tutorial13/tutorial13.html
  36.          *
  37.          * m(Column)(Row)
  38.          */
  39.         Matrices.viewMatrix.m00 = xAxis.x; Matrices.viewMatrix.m10 = xAxis.y; Matrices.viewMatrix.m20 = xAxis.z;
  40.         Matrices.viewMatrix.m01 = yAxis.x; Matrices.viewMatrix.m11 = yAxis.y; Matrices.viewMatrix.m21 = yAxis.z;
  41.         Matrices.viewMatrix.m02 = zAxis.x; Matrices.viewMatrix.m12 = zAxis.y; Matrices.viewMatrix.m22 = zAxis.z;
  42.         Matrices.viewMatrix.m33 = 1;
  43.        
  44.         Matrices.viewMatrix.translate(this.currPos);
  45.     }
  46.    
  47.     public void cameraLogic(){
  48.         Vector3f.add(this.currPos, this.newPos, this.currPos); //Update current position
  49.         System.out.println(this.currPos);
  50.         this.newPos.set(0, 0, 0);
  51.        
  52.         //Move Forward
  53.         moveForw = (Keyboard.isKeyDown(Keyboard.KEY_W) ? true : false);
  54.        
  55.         //Move Left
  56.         moveLeft = (Keyboard.isKeyDown(Keyboard.KEY_A) ? true : false);
  57.        
  58.         //Move Back
  59.         moveBack = (Keyboard.isKeyDown(Keyboard.KEY_S) ? true : false);
  60.        
  61.         //Move Left
  62.         moveRight = (Keyboard.isKeyDown(Keyboard.KEY_D) ? true : false);
  63.        
  64.         if(moveForw){
  65.             zAxis.normalise();
  66.             zAxis.scale(this.moveSpeed);
  67.             Vector3f.sub(this.newPos, this.zAxis, this.newPos);
  68.         }
  69.         if(moveLeft){
  70.             xAxis.normalise();
  71.             xAxis.scale(this.moveSpeed);
  72.             Vector3f.add(this.newPos, this.xAxis, this.newPos);
  73.         }
  74.         if(moveBack){
  75.             zAxis.normalise();
  76.             zAxis.scale(this.moveSpeed);
  77.             Vector3f.add(this.zAxis, this.newPos, this.newPos);
  78.         }
  79.         if(moveRight){
  80.             xAxis.normalise();
  81.             xAxis.scale(this.moveSpeed);
  82.             Vector3f.sub(this.newPos, this.xAxis, this.newPos);
  83.         }
  84.        
  85.         if(moveForw || moveLeft || moveBack || moveRight)
  86.             Matrices.viewMatrix.translate(this.newPos);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement