I'm trying to let an image follow a path. The points of this path are stored in an ArrayList. Right now the image jumps to the next point every two seconds, so I have to use linear interpolation to make the movement smooth. But how can I use linear interpolation in my update() method? I have a searched for this question on the net but couldn't find much information on linear interpolation in the update method in combination with an ArrayList with points.
Update method
public void update(){
repaint();
if(counter < Lane.firstLane.size()){
carPosition.x = Lane.firstLane.get(counter).x;
carPosition.y= Lane.firstLane.get(counter).y;
//
// double x0=carPosition.x;
// double x1=Lane.firstLane.get(counter).x;
// double y0=carPosition.x;
// double y1=Lane.firstLane.get(counter).y;
//
//
// carPosition.x=(int) lerp(x0,x1,5);
// carPosition.y=(int) lerp(y0,y1,5);
//
// System.out.println("Pos: "+getCarPosition());
counter++;
}
else{
//System.out.println("Destination reached");
}
// carPosition.x+=1;
repaint();
}
double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
Thread to move the car
public void moveCar() {
Runnable helloRunnable = new Runnable() {
public void run() {
car.update();
repaint();
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 2000, TimeUnit.MILLISECONDS);
}
Lane.cs
public class Lane {
public static List<Point> firstLane = new ArrayList<>(Arrays.asList(new Point(10,135),new Point(124,190),new Point(363,190),new Point(469,210)));
}
Aucun commentaire:
Enregistrer un commentaire