Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

java - Rotating Vector to Target Point

Basic Explanation: I have a Path and a Target Point, I want the Path to rotate so that the Path's end point connects with the Target Point.

Example:

Target Point: X: 274, Y: 290

Path: Might be Horizontal (Ex: X: 100, Y: 150 TO X: 150, Y: 150) or Might be Vertical (Ex: X: 100, Y: 100 TO X: 100, Y: 150)

The Path is not a straight line at all vertices as it has some noise (It's basically a recorded mouse pattern).

What I have currently (In code):

private List<Movement> movements = new ArrayList(); //Path stored here

private Movement[] generateApplicable(int x, int y) {
    Movement[] updated = null;
    Point target = new Point(x,y);
    startingPoint = new Point(m.getLocation().x,m.getLocation().y);
    Movement lastMovement = movements.get(movements.size()-1);
    double movementRadius = lastMovement.distance(m.getLocation());
    if(movementRadius >= m.getLocation().distance(x,y)) {
        Movement chosen = null;
        double angle = -1337;
        for(int i = movements.size() - 1; i >= 0; i--) {
            Movement current = movements.get(i);
            if(current.distance(startingPoint) >= target.distance(startingPoint)) {
                chosen = current;
                continue;
            }
            double radius = current.distance(startingPoint);
            int nX;
            int nY;
            if(chosen != null) {
                if(angle == -1337) {
                    updated = new Movement[i + 2];
                    angle = angleBetweenTwoPointsWithFixedPoint(target.x, target.y,
                           chosen.getPoint().x, chosen.getPoint().y, chosen.getPoint().x,chosen.getPoint().y);
                    updated[i + 1] = new Movement(x, y, chosen.timeBefore, true);
                } else {
                    nX = (int) (startingPoint.x + (radius + current.x) * Math.cos(angle));
                    nY = (int) (startingPoint.y + (radius + current.y) * Math.sin(angle));
                    updated[i] = new Movement(nX, nY, current.timeBefore, true);
                }
            }
        }
    }
    return updated;
}

How I get the angle:

public double angleBetweenTwoPointsWithFixedPoint(double point1X, double point1Y,
                                                         double point2X, double point2Y,
                                                         double fixedX, double fixedY) {

    double angle1 = Math.atan2(point1Y - fixedY, point1X - fixedX);
    double angle2 = Math.atan2(point2Y - fixedY, point2X - fixedX);

    return angle1 - angle2;
}

The code above makes the path travel close to the target (when horizontal), but when vertical the path goes a wrong direction.

Note 1: the Movement object contains the coordinates and the time it takes to cover the distance<- irrelevant to the question.

Note 2: (m.getLocation()) gets the mouse current location (which is the where I want the path to start).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...