Object following custom path

2019-05-31 10:41发布

问题:

I'm working on 2d game dev project. It's some kind of board game.

As background I have board game with path, that isn't real circle, but some kind of wavy line. Objects are moving across that path.

On touch event...I need to my player's object get at that exact position, but they need to follow that path.

How to translate (draw) objects over some custom path?

回答1:

You could draw your path (i.e. your wavy line) using an actual Path object onto the Canvas, and then use PathMeasure to calculate X,Y positions along that Path.

Therefore if you want to draw an object on the screen that appears to move along the path, you would sequentially move your object to X,Y positions that are provided by calling getPosTan(), each time providing getPosTan() with an incrementing (or decrementing) distance argument. I guess you would need to create a simple algorithm (perhaps using some iteration logic) that determines which distance along the path is nearest to the actual touch X,Y coordinate. You might also retain the current position of your moving object not only in terms of its current X,Y but also in terms of its distance along your Path. Then when you come to move the object, you gradually move it between the current and new target distance, calling getPosTan() with those distances to get the X,Y positions to place your object.

That's off the top of my head and I haven't actually tried it. Hope it makes some sense.



回答2:

I've came up with this simple solution, i've didnt payed attention to performance, 'couse i need to keep it simple.

I've made class with Path and PathMeasure objects, mapPath is instance of that class. I've took x and y coordinates from touchEvent and gave them as a parameters to my function.

I use simple function to calculate distance between two vectors, my pixels on the path, and vector I got from touchEvent.

Iterate until i get minimum of distance between them.

When i get x and y coordinate of my path, where i need to place my object, I simple translate object to that coordinates.

Its that simple, but its little dirty :) Hope someone can use it someday, in hour of need. Cheers ;)

//Find point on path closest to touch coordinates    

public float[] findMinDistanceVector(int x, int y) {

 float[] xy = new float[2];
 float[] ten = new float[2];

 float distanceVectorOld = Float.MAX_VALUE;
 float distanceVectorNew = 0;
 float[] minXY = new float[2];
 for (float distance = 0; distance < mapPath.pathMeasure.getLength();distance++) {

 mapPath.pathMeasure.getPosTan(distance, xy, ten);

 distanceVectorNew = dist(x, y, xy[0], xy[1]);

      if (distanceVectorNew < distanceVectorOld) {

          minXY[0] = xy[0];
          minXY[1] = xy[1];
          distanceVectorOld = distanceVectorNew;

      }
 }

return minXY;
}

public float dist(float x1, float y1, float x2, float y2) {

    float distX = x1 - x2;
    float distY = y1 - y2;

    // Pythagora's theorem
    return FloatMath.sqrt(distX * distX + distY * distY);
}