• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Untitled

a guest Apr 23rd, 2011 153 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.  * To change this template, choose Tools | Templates
3.  * and open the template in the editor.
4.  */
5.
6. package cubicsplines;
7.
8. import org.newdawn.slick.Color;
9. import org.newdawn.slick.Graphics;
10.
11. /**
12.  *
14. */
15. public class Cubic {
16.
17.     private double A,B,C,D,E,F,G,H;
18.     private float x0,y0,x1,y1,x2,y2,x3,y3;
19.     private float totalDelta;
20.
21.     // Interpolated values along the spline
22.     public float xPos,yPos;
23.     public boolean cordsDebug, lerpDebug;
24.
26.             float fromPosX,
27.             float fromPosY,
28.             float toPosX,
29.             float toPosY,
30.             float fromVelX,
31.             float fromVelY,
32.             float toVelX,
33.             float toVelY)
34.     {
35.         float distanceBetweenPoints = fromPosX - toPosX;
36.         float controlPointLength = distanceBetweenPoints / 3;
37.
38.         // Coordinate 1 = starting position
39.         x0 = fromPosX;
40.         y0 = fromPosY;
41.
42.         // Coordinate 2 - Position after 1 second using starting velocity = Coordinate1 + StartVelocity
43.         x1 = fromPosX + fromVelX * controlPointLength;
44.         y1 = fromPosY + fromVelY * controlPointLength;
45.
46.         // Coordinate 3 - Position after 1 second using reversed ending velocity = Coordinate4 – EndVelocity
47.         x2 = toPosX - toVelX * controlPointLength;
48.         y2 = toPosY - toVelY * controlPointLength;
49.
50.         // Coordinate 4 = Ending position
51.         x3 = toPosX;
52.         y3 = toPosY;
53.
54.         A = x3 - (3*x2) + (3*x1) - x0;
55.         B = (3*x2) - (6*x1) + (3*x0);
56.         C = (3*x1) - (3*x0);
57.         D = x0;
58.
59.         E = y3 - (3*y2) + (3*y1) - y0;
60.         F = (3*y2) - (6*y1) + (3*y0);
61.         G = (3*y1) - (3*y0);
62.         H = y0;
63.
64.         if(cordsDebug)
65.         {
66.             System.out.println("----------------------------");
67.             System.out.println("From Velocity" + fromVelX + " | " + fromVelY);
68.             System.out.println("To Velocity" + toVelX + " | " + toVelY);
69.             System.out.println("Coordinate 1: " + x0 + " | " + y0);
70.             System.out.println("Coordinate 2: " + x1 + " | " + y1);
71.             System.out.println("Coordinate 3: " + x2 + " | " + y2);
72.             System.out.println("Coordinate 4: " + x3 + " | " + y3);
73.         }
74.
75.         totalDelta = 0;
76.     }
77.
78.     public void follow(int delta)
79.     {
80.         totalDelta += delta;
81.
82.         float t = totalDelta / 500; // 500 ms before next recieved packet
83.
84.         if  (t >= 0.0f && t <= 1.0f)
85.         {
86.             xPos = (float) (A*Math.pow(t,3) + B*Math.pow(t,2) + C*t + D);
87.             yPos = (float) (E*Math.pow(t,3) + F*Math.pow(t,2) + G*t + H);
88.
89.             if(lerpDebug)
90.             {
91.                System.out.println("Pos: " + xPos + " | " + yPos);
92.             }
93.         }
94.         else
95.             totalDelta = 0;
96.     }
97.
98.
99.     public void draw(Graphics g)
100.     {
101.         g.setColor(Color.red);
102.         g.drawRoundRect(x0,y0, 15, 15, 10);
103.         g.setColor(Color.green);
104.         g.drawRoundRect(x1,y1, 15, 15, 10);
105.         g.drawRoundRect(x2,y2, 15, 15, 10);
106.         g.setColor(Color.white);
107.         g.drawRoundRect(x3,y3, 15, 15, 10);
108.
109.
110.         g.drawString("p0" + "x: " + x0 + " y: " + y0, 10, 50);
111.         g.drawString("p1" + "x: " + x1 + " y: " + y1, 10, 70);
112.         g.drawString("p2" + "x: " + x2 + " y: " + y2, 10, 90);
113.         g.drawString("p3" + "x: " + x3+ " y: " + y3, 10, 110);
114.
115.     }
116. }
RAW Paste Data
Top