Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 21st, 2011  |  syntax: Java  |  size: 3.19 KB  |  views: 89  |  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. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package cubicsplines;
  7.  
  8. /**
  9.  *
  10.  * @author Adam
  11.  */
  12. public class Cubic {
  13.  
  14.  
  15.     public boolean cordsDebug, lerpDebug;
  16.     double FREQ = 0.05f;
  17.  
  18.  
  19.     private double A,B,C,D,E,F,G,H;
  20.     private float lastPositionTime;
  21.  
  22.  
  23.     // Interpolated values along the spline
  24.     public float xPos,yPos;
  25.  
  26.     public void addNewPosition(
  27.             float fromPosX,
  28.             float fromPosY,
  29.             float toPosX,
  30.             float toPosY,
  31.             float fromVelX,
  32.             float fromVelY,
  33.             float toVelX,
  34.             float toVelY)
  35.     {
  36.         float currTime =  (float) CubicSpline.getTime();
  37.         float timeDiff = currTime - lastPositionTime;
  38.  
  39.         // Calculate change in acceleration
  40.         float accelerationX = toVelX - fromVelX;
  41.         float accelerationY = toVelX - fromVelY;
  42.        
  43.         // Coordinate 1 = starting position
  44.         float x0 = fromPosX;
  45.         float y0 = fromPosY;
  46.  
  47.         // Coordinate 2 - Position after 1 second using starting velocity = Coordinate1 + StartVelocity
  48.         float x1 = fromPosX + fromVelX;
  49.         float y1 = fromPosY + fromVelY;
  50.  
  51.         // Coordinate 3 - Position after 1 second using reversed ending velocity = Coordinate4 – EndVelocity
  52.         float x2 = (float) ((toPosX + toVelX * timeDiff) + (0.9 * accelerationX * (timeDiff * timeDiff)));
  53.         float y2 = (float) ((toPosY + toVelY * timeDiff) + (0.9 * accelerationY * (timeDiff * timeDiff)));
  54.  
  55.  
  56.         // Coordinate 4 = Ending position
  57.         float x3 = x2 - (toVelX + accelerationX * timeDiff);
  58.         float y3 = y2 - (toVelY + accelerationX * timeDiff);
  59.  
  60.  
  61.         A = x3 - (3*x2) + (3*x1) - x0;
  62.         B = (3*x2) - (6*x1) + (3*x0);
  63.         C = (3*x1) - (3*x0);
  64.         D = x0;
  65.  
  66.         E = y3 - (3*y2) + (3*y1) - y0;
  67.         F = (3*y2) - (6*y1) + (3*y0);
  68.         G = (3*y1) - (3*y0);
  69.         H = y0;
  70.  
  71.  
  72.         if(cordsDebug)
  73.         {
  74.             System.out.println("----------------------------");
  75.             System.out.println("currTime: " + currTime);
  76.             System.out.println("From Velocity" + fromVelX + " | " + fromVelY);
  77.             System.out.println("To Velocity" + toVelX + " | " + toVelY);
  78.             System.out.println("Coordinate 1: " + x0 + " | " + y0);
  79.             System.out.println("Coordinate 2: " + x1 + " | " + y1);
  80.             System.out.println("Coordinate 3: " + x2 + " | " + y2);
  81.             System.out.println("Coordinate 4: " + x3 + " | " + y3);
  82.         }
  83.  
  84.         lastPositionTime = currTime;
  85.  
  86.     }
  87.  
  88.     // This function is called outside this class on each game tick
  89.  
  90.  
  91.     public void follow()
  92.     {
  93.         double curTime = CubicSpline.getTime();
  94.                 //Time since last update
  95.         double timeSince = curTime - lastPositionTime;
  96.  
  97.         double t = timeSince / FREQ;
  98.  
  99.         if  (t >= 0.0f && t <= 1.0f)
  100.         {
  101.             xPos = (float) (A*Math.pow(t,3) + B*Math.pow(t,2) + C*t + D);
  102.             yPos = (float) (E*Math.pow(t,3) + F*Math.pow(t,2) + G*t + H);
  103.  
  104.             if(lerpDebug)
  105.             {
  106.                 System.out.println("Pos: " + xPos + " | " + yPos);
  107.             }
  108.         }
  109.     }
  110. }
clone this paste RAW Paste Data