0
votes

I want to put some Sprites on path. I thought about calculate bezier path and then put sprites on calculated points. But I can't find in java method to do that.


SOLVED

This is my main method to draw line of images

private List<Sprite> drawStarLine(Point start, Point end, Point control1, Point control2, int starsCount,TextureRegion pTexture) {
    ArrayList<Sprite> starsForReturn = new ArrayList<Sprite>();
    ArrayList<Point> points = new ArrayList<Point>(generateBezierPath(start, end, control1, control2, starsCount));
    for (int i = 0; i < points.size(); i++) {
        Point p = points.get(i);
        Sprite s = new Sprite((float) p.getX(), (float) p.getY(), pTexture, activity.getVertexBufferObjectManager());
        s.setScale(mainScaleX / 2);
        starsForReturn.add(s);
    }
    return starsForReturn;
}

This is how I calculate bezier path

private List<Point> generateBezierPath(Point origin, Point destination, Point control1, Point control2, int segments) {
    ArrayList<Point> pointsForReturn = new ArrayList<Point>();

    float t = 0;
    for (int i = 0; i < segments; i++) {
        Point p = new Point();
        p.setX(Math.pow(1 - t, 3) * origin.x + 3.0f * Math.pow(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x);
        p.setY(Math.pow(1 - t, 3) * origin.y + 3.0f * Math.pow(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y);
        t += 1.0f / segments;
        pointsForReturn.add(p);
    }
    pointsForReturn.add(destination);
    return pointsForReturn;
}
1
Don't add your additional information as an answer: put it here, in your question, and then delete the answer you made for it. At 670 reputation, you should know all the information goes in your question =)Mike 'Pomax' Kamermans
@Mike'Pomax'Kamermans I thought "share your knowledge, Q&A-style" is proper way. It isn't?Błażej
not if it's your own question. all information pertaining to your question, unless you're literally answering your own question, goes in the original post, not as follow ups.Mike 'Pomax' Kamermans
oo good to know. I will keep that in mind :)Błażej

1 Answers

1
votes

Simply calculate the quadratic curve coordinates (for position) and tangent (for orientation). The code is simple.

double[] getPoint(double[] x, double[] y, double t) {
  double mt = 1-t;
  return new double[]{
    x[0]*mt*mt + 2*x[1]*mt*t + x[2]*t*t,
    y[0]*mt*mt + 2*y[1]*mt*t + y[2]*t*t
  };
}

double[] getTangent(double[] x, double[] y, double t) {
  double mt = t-1; // note: NOT 1-t
  return new double[]{
    2*(x[0]*mt - x[1]*(2*t-1) + x[2]*t),
    2*(y[0]*mt - y[1]*(2*t-1) + y[2]*t)
  };
}

Place your "thing" at the coordinate you get from getPoint, and then if you need it to follow your path, rotate your thing so that its axis of travel lines up with the tangent you get out of getTangent. Done.