user75832
user75832

Reputation:

Java 2D: Moving a point P a certain distance closer to another point?

What is the best way to go about moving a Point2D.Double x distance closer to another Point2D.Double?

Edit: Tried to edit, but so went down for maintenance. No this is not homework

I need to move a plane (A) towards the end of a runway (C) and point it in the correct direction (angle a).

alt text http://img246.imageshack.us/img246/9707/planec.png

Here is what I have so far, but it seems messy, what is the usual way to go about doing something like this?

    //coordinate = plane coordinate (Point2D.Double)
    //Distance = max distance the plane can travel in this frame

    Triangle triangle = new Triangle(coordinate, new Coordinate(coordinate.x, landingCoordinate.y),  landingCoordinate);

    double angle = 0;

    //Above to the left
    if (coordinate.x <= landingCoordinate.x && coordinate.y <= landingCoordinate.y)
    {
        angle = triangle.getAngleC();
        coordinate.rotate(angle, distance);
        angle = (Math.PI-angle);
    }
    //Above to the right
    else if (coordinate.x >= landingCoordinate.x && coordinate.y <= landingCoordinate.y)
    {
        angle = triangle.getAngleC();
        coordinate.rotate(Math.PI-angle, distance);
        angle = (Math.PI*1.5-angle);
    }

    plane.setAngle(angle);

The triangle class can be found at http://pastebin.com/RtCB2kSZ

Bearing in mind the plane can be in in any position around the runway point

Upvotes: 6

Views: 10961

Answers (6)

trashgod
trashgod

Reputation: 205875

In this game, integral coordinates are used to represent squares in a grid. The method move(int row, int col) moves toward the specified row and column by advancing one square in one of eight semi-cardinal directions, as seen here.

Upvotes: 0

Martin Wickman
Martin Wickman

Reputation: 19925

Vectors to the rescue!

Given points A and B. Create a vector V from A to B (by doing B-A). Normalize vector V into a unit vector and then just multiply it with the distance, d, you want and finally add the resulting vector to point A. ie:

  A_moved = A + |(B-A)|*d

Java(ish)

  Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d));

No angles, no nasty trig needed.

Upvotes: 4

user75832
user75832

Reputation:

double angle = Math.atan2(landingCoordinate.y-coordinate.y, landingCoordinate.x-coordinate.x);

coordinate.x += Math.cos(angle)*distance;
coordinate.y += Math.sin(angle)*distance;

//Add 90 degress to the plane angle
plane.setAngle(angle + 1.57079633);

Upvotes: 3

John Feminella
John Feminella

Reputation: 311755

The shortest distance between two points is a line, so simply move that point x units along the line that connects the two points.


Edit: I didn't want to give away the specifics of the answer if this is homework, but this is simple enough that it can be illustrated without being too spoiler-y.

Let us assume you have two points A = (x1, y1) and B = (x2, y2). The line that includes these two points has the equation

(x1, y1) + t · (x2 - x1, y2 - y1)

where t is some parameter. Notice that when t = 1, the point specified by the line is B, and when t = 0, the point specified by the line is A.

Now, you would like to move B to B', a point which is a new distance d away from A:

 A                       B'            B
(+)---------------------(+)-----------(+)

 <========={ d }=========>

The point B', like any other point on the line, is also governed by the equation we showed earlier. But what value of t do we use? Well, when t is 1, the equation points to B, which is |AB| units away from A. So the value of t that specifies B' is t = d/|AB|.

Solving for |AB| and plugging this into the above equation is left as an exercise to the reader.

Upvotes: 6

Jack
Jack

Reputation: 133669

You can minimize the difference along both axis by a percent (that depends on how much you want to move the points).

For example:

Point2D.Double p1, p2;
//p1 and p2 inits

// you don't use abs value and use the still point as the first one of the subtraction
double deltaX = p2.getX() - p1.getX();
double deltaY = p2.getY() - p1.getY();

// now you know how much far they are
double coeff = 0.5; //this coefficient can be tweaked to decice how much near the two points will be after the update.. 0.5 = 50% of the previous distance

p1.setLocation(p1.getX() + coeff*deltaX, p1.getY() + coeff*deltaY);

So you moved p1 halfway toward p2. The good thing avoid abs is that, if you choose which point will be moved and which one will stand still you can avoid if tests and just use the raw coefficient.

Upvotes: 6

Gordon Gustafson
Gordon Gustafson

Reputation: 41259

(point A is to be moved closer to B)

if (A.x > B.x)
    //decrement A.x
if (A.x < B.x)
    //increment A.x

that might be the basic idea in pseudocode, but there's a lot more to it than that. How do you define 'best' way?

Certain things need to be considered:

  • Is there a specific measure/ratio you want the points to be apart from each other, or do you just want them closer?
  • Should both coordinates always be modified? (e.g. if you want to move (1,50) closer to (0,0), do you make it (.5, 25) or just (1,25)?
  • if the point is to be 1 unit away, should it be 1 unit horizontally? directly diagonally? 1 unit away on the line between the 2 points?

Give us a little more detail and we'll see what we've got. :D

Upvotes: 0

Related Questions