dawrehxyz

GameClock

Aug 6th, 2016
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. private GameClock gameClock;    
  2.  
  3.     gameClock.Initialize(); //Runs first but only once.
  4.  
  5.     @Override
  6.     public void onDrawFrame(GL10 gl)
  7.     {
  8.         elapsedTime = gameClock.CalculateElapsedTime(); //This is the elapsedTime that is used in setPlayerTimeBasedVelocity() function
  9.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  10.         GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT);
  11.         drawSkyBox();
  12.         drawPlayer();
  13.         drawMaze();
  14.         updateCamera();
  15.         collisionDetectionAndResponse();
  16.     }
  17.  
  18.     //This method is called from drawPlayer() function.
  19.     private void setPlayerTimeBasedVelocity(float elapsedTime)
  20.     {
  21.         //timeBasedVelocity is the vector that I increase the players position with later on.
  22.         timeBasedVelocity.x = initialVelocity.x * elapsedTime;
  23.         timeBasedVelocity.y = initialVelocity.y * elapsedTime;
  24.         timeBasedVelocity.z = initialVelocity.z * elapsedTime;
  25.     }
  26.  
  27.     private long t0;
  28.     private long t1;
  29.     private long elapsedTime;
  30.     private final long nanoSecond = 1000000000;
  31.     private final long microSecond = 1000000;
  32.  
  33.     public void Initialize()
  34.     {
  35.         t0 = System.nanoTime();
  36.     }    
  37.  
  38.     public float CalculateElapsedTime()
  39.     {
  40.         t1 = System.nanoTime();
  41.         elapsedTime = t1 - t0;
  42.         t0 = t1;
  43.         return (float)((double)elapsedTime/(double)nanoSecond);
  44.     }
  45.  
  46.  
  47.     //This is how I update my camera based upon the players position which is player.getWorldMidPoint().
  48.     //The eye and center variables are Vector3 variables with x, y and z and belongs to the camera which is used to create the
  49.     //view matrix.
  50.     public void updateCamera()
  51.     {
  52.         center = player.getWorldMidPoint();
  53.         center.y += player.getPlayerRadius().y * 2;
  54.         float angle = -player.getIncrementalRotationAngle();
  55.         float c = (float)Math.cos(Math.toRadians(angle));
  56.         float s = (float)Math.sin(Math.toRadians(angle));
  57.         eye.x = center.x + (s*horizontalDistanceFromPlayer);
  58.         eye.y = center.y + verticalDistanceFromPlayer;
  59.         eye.z = center.z + (c*horizontalDistanceFromPlayer);
  60.         if(player.getPlayerRunningStatus())
  61.         {
  62.             eye.addVector(player.getPlayerTimeBasedVelocity());
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment