java - Linear interpolation list with points -


i have method named movecar has timer calls car.update() method every 40 ms. counter gets incremented every 40 ms in current situation, counter should incremented when car @ end point. counter should incremented (which means endpoint next point in list) , should moved lineair interpolation next point, should happen untill last point reached. tried make check in update method if car position equal end position counter gets incremented did not solve problem, how this?

the movecar method:

       public void movecar() {             timer timer = new timer(40, new actionlistener() {             @override             public void actionperformed(actionevent e) {                 if (starttime == null) {                     starttime = system.currenttimemillis();                 }                 long = system.currenttimemillis();                 long diff = - starttime;                  = (double) diff / (double) playtime;                  car.update(i);                                                  repaint();              }         });         timer.start();         } 

car update + lerp method:

 public void update(double i){          repaint();          //counter 0 default          if (counter < lane.firstlane.size()) {              startpoint = new point(carposition.x, carposition.y);             endpoint = new point(lane.firstlane.get(counter).x, lane.firstlane.get(counter).y);              carposition.x=(int)lerp(startpoint.x,endpoint.x,i);                               carposition.y=(int)lerp(startpoint.y,endpoint.y,i);                                                     system.out.println("car position: x" + carposition.x + ": y" + carposition.y);             repaint();              counter++;         } }      double lerp(double a, double b, double t) {             return + (b - a) * t;         } 

lane.cs

         public static list<point> firstlane = new arraylist<>(arrays.aslist(new point(10,375),new point(215,385),new point(230,452)/*,new point(531,200)*/)); 

i assume update method wrong

lane currentlane = ...; // store current lane somewhere lane nextlane = ...; // store next lane somewhere  public void update(double progress){     startpoint = new point(currentlane.x, currentlane.y);     endpoint = new point(nextlane.x, nextlane.y);      carposition.x=(int)lerp(startpoint.x, endpoint.x, progress);                       carposition.y=(int)lerp(startpoint.y, endpoint.y, progress);                                             if (progress >= 1.0) { /// assuming 0 <= progress <= 1         currentlane = nextlane;         nextlane = ...; // switch next lane     } } 

i removed repaint() calls... guess need include them in appropriate locations. code not work first lane (or last, depending on implementation). still don't quite problem, hard fix. :)


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -