Moving between coordinates, Java Algorithms

Well, this question will be a bit abstract.

I have an icon that moves along a line that is represented by a series of coordinates stored in a vector, and I repeat them through. The distance between the coordinates is variable. Therefore, sometimes the icon will move slowly and smoothly, while others it will scan several 100 pixels at a time.

I am having problems with the algorithm for separating each set of coordinates, which it should move among themselves in a set of relative coordinates, where the number is based on size, so the transition is smooth, regardless of how much it coordinates on one line.

Any ideas would be highly appreciated. Thank:)

+3
source share
3 answers

Take a look at this discussion of the main game loop .

And here is a quote from this page:

At this stage, all objects in the game world are calculated and executed. Usually, the value of the time step is passed to all update methods, indicating how much time has passed since the last update ...

You need to know 3 things:

  • how much time has passed since the last update of the position of your facility?
  • What is the speed of your object?
  • What direction (usually represented as a ray) moves your object?

From this you can calculate the current position of the object.

+2
source

, , , ( ). (ish), (, catmull-rom).

+1

So, you want to go from the start point (x0 / y0) to the end point (x1 / y1) along the line using a variable number of steps, but with a maximum distance for each step?

This can be done as follows:

int stepdist = 10; // max pixels per step
double xdiff = x1 - x0;
double ydiff = y1 - y0;
double dist = sqrt( xdiff * xdiff + ydiff * ydiff );
int steps = (int) ( ( dist - 1 ) / stepdist );
if( steps > 0 )
{
   xdiff /= steps;
   ydiff /= steps;
   while( --steps >= 0 )
   {
       x0 += xdiff;
       y0 += ydiff;
       moveTo( (int) x0, (int) y0 );
   }
}
moveTo( (int) x1, (int) y1 );
0
source

Source: https://habr.com/ru/post/1732921/


All Articles