Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Movement on a line on a 2D plane Help!  (Read 5438 times) 0 Members and 1 Guest are viewing this topic.
WhenNDoubtGTFO

Junior Newbie

 « Posted 2011-08-13 11:32:32 »

I guess this is more of a math question but I will just leave this here  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....
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #1 - Posted 2011-08-13 23:11:34 »

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

counterp

Senior Devvie

Medals: 11

 « Reply #2 - Posted 2011-08-13 23:20:32 »

This:

 1  2  3  4  5  6  7  8  9  10 `   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:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 `     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 ), and as for speed you do the benchmarks, they're both pretty fast

at least I think this is what you wanted?
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #3 - Posted 2011-08-14 10:09:28 »

@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.

counterp

Senior Devvie

Medals: 11

 « Reply #4 - Posted 2011-08-14 10:24:14 »

?

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

JGO Kernel

Medals: 180

 « Reply #5 - Posted 2011-08-14 11:40:35 »

This:

 1  2  3  4  5  6  7  8  9  10 `   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);      }   }`

?

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

You are right, but

 1  2  3  4  5  6  7  8  9  10  11  12 `   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.

Mathias - I Know What [you] Did Last Summer!
Riven

« JGO Overlord »

Medals: 1339
Projects: 4
Exp: 16 years

 « Reply #6 - Posted 2011-08-14 11:43:55 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #7 - Posted 2011-08-16 02:52:45 »

?

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

WhenNDoubtGTFO

Junior Newbie

 « Reply #8 - Posted 2011-08-16 22:19:17 »

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.
WhenNDoubtGTFO

Junior Newbie

 « Reply #9 - Posted 2011-08-20 12:08:06 »

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.
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #10 - Posted 2011-08-21 07:50:36 »

saucecode

Senior Newbie

Medals: 1

 « Reply #11 - Posted 2011-10-11 15:00:09 »

 1  2  3  4  5  6  7  8  9  10  11  12 `   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) );      }   }`

Im having the same problem too. All this code works but where did the move() method come from?
counterp

Senior Devvie

Medals: 11

 « Reply #12 - Posted 2011-10-11 18:20:12 »

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:

 1  2  3 `public static void move(int x, int y) {    // I obtained the point (x, y) from my line. what should I do with it?}`
theagentd
 « Reply #13 - Posted 2011-10-12 01:35:37 »

 1  2  3  4  5  6  7  8  9  10  11 `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?

Myomyomyo.
Pages: [1]
 ignore  |  Print

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (579 views) 2018-03-24 14:43:53 Solater (95 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (362 views) 2018-03-02 22:15:33 buddyBro (1022 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org