Movement on a line on a 2D plane Help!

I guess this is more of a math question but I will just leave this here ;D any comment would be great! I’m self taught in java and new to programming games sorta…
Here is what I need to do.
Get int Ax and Ay to equal int Bx and By while staying on the line so for example:
Ax = 5
Ay = 7

Bx = 9
By = 5

Now on a graph this would make a line segment. I need the Ax and Ay to reach Bx and By simultaneously while keeping the
Ax and Ay on the line segment to make it look smooth so what constant number or anything for that matter would I be able to add or subtract to the A points to eventually get them to the B points without Ax equaling Bx before Ay equals By or vice-versa. Any comment will be helpful unless it’s not…

lets say “n” is the number of steps you want to take:

(Bx-Ax)/n = deltaX
(By-Ay)/n = deltaY

Then each tick you add deltaX and deltaY to Ax and Ay :wink:

This:

	private static final void moveLine(int x1, int y1, int x2, int y2) {
		int dist = Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
		double dx = (double)(x2-x1)/dist;
		double dy = (double)(y2-y1)/dist;
		for (int d = 1; d <= dist; d++) {
			int x = (int)Math.round(x1 + dx*d);
			int y = (int)Math.round(y1 + dy*d);
			move(x, y);
		}
	}

or this:

     public static final void lineAlgorithm(int x0, int y0, int x1, int y1) {
        boolean steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);
        if (steep) {
                int tmp = x0;
                x0 = y0;
                y0 = tmp;
                tmp = x1;
                x1 = y1;
                y1 = tmp;
        }
        if (x0 > x1) {
                int tmp = x0;
                x0 = x1;
                x1 = tmp;
                tmp = y0;
                y0 = y1;
                y1 = tmp;
        }
        int deltax = x1 - x0;
        int deltay = Math.abs(y1 - y0);
        int error = deltax / 2;
        int ystep = -1;
        int y = y0;
        if (y0 < y1)
                ystep = 1;
        for (int x = x0; x <= x1; x++) {
                if (steep) move(y, x); else move(x, y);
                error -= deltay;
                if (error < 0) {
                        y += ystep;
                        error += deltax;
                }
         }
    }

can’t really comment on accuracy of first one (you try it, also there is some problems with the slope if it is 0 I think, I don’t remember, once again try it out :P), and as for speed you do the benchmarks, they’re both pretty fast

at least I think this is what you wanted?

@counterp
In your first method “moveLine”, in the loop you only add dx and dy; you shouldn’t multiply them by “d” or else you will be moving exponentially to time.

?

the maximum value of dx and dy is 1, nothing is growing exponentially

[quote=“counterp,post:3,topic:37091”]
This:

	private static final void moveLine(int x1, int y1, int x2, int y2) {
		int dist = Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
		double dx = (double)(x2-x1)/dist;
		double dy = (double)(y2-y1)/dist;
		for (int d = 1; d <= dist; d++) {
			int x = (int)Math.round(x1 + dx*d);
			int y = (int)Math.round(y1 + dy*d);
			move(x, y);
		}
	}

You are right, but

	private static final void moveLine(int x1, int y1, int x2, int y2) {
		int dist = Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
		float dx = (float)(x2-x1)/dist;
		float dy = (float)(y2-y1)/dist;
		float x = x1;
		float y = y1;
		for (int d = 1; d <= dist; d++) {
			x += dx;
			y += dy;
			move( (int) Math.round(x), (int)Math.round(y) );
		}
	}

seems to be the common way of doing it, since it avoids the multiplication (which at least in my learning days was much more costly than an addition), so ra4king misinterpreted your code.

multiplication is just as fast as addition and subtraction. division and modulo are slow.

Whoops my bad :slight_smile:

Hey sorry I haven’t been home in a while… still not home… Thanks for all the replies I need to try this stuff out asap it looks like all the solutions might work so this will be great it was pissing me off that I couldn’t figure this out thanks again.

WooHoo it’s working… and it makes so much sense now that I see it actually working. Tried it as soon as I got home I used a slightly modfied ra4king’s reply. Thanks again guys I’m making an RTS and I have it working in a text space and now I am gunna start working on the graphics of it and controls.

Glad to help :smiley:

Im having the same problem too. All this code works but where did the move() method come from?
Id much rather ask here than start a new thread.

it’s just showing you how to use your newly found coordinates (i.e. the move method doesn’t exist).

this is how it would look:

public static void move(int x, int y) {
    // I obtained the point (x, y) from my line. what should I do with it?
}
double dx = targetX - x;
double dy = targetY - y;
double scale = movementSpeed * delta / (Math.sqrt(dx*dx + dy*dy));
x += dx*scale;
y += dy*scale;

if((dx != 0 && Math.abs(dx) <= Math.abs(targetX - x)) || (dy != 0 && Math.abs(dy) <= Math.abs(targetY - y))){
    //Target has been overshot, set position to target.
    x = targetX;
    z = targetY;
}

This?