Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (806) Games in Android Showcase (239) games submitted by our members Games in WIP (868) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Movement on a line on a 2D plane Help!  (Read 6760 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?
 Games published by our own members! Check 'em out!
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: 195

 « 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: 1371
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.
 Games published by our own members! Check 'em out!
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?
Id much rather ask here than start a new thread.
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

 Riven (587 views) 2019-09-04 15:33:17 hadezbladez (5528 views) 2018-11-16 13:46:03 hadezbladez (2410 views) 2018-11-16 13:41:33 hadezbladez (5790 views) 2018-11-16 13:35:35 hadezbladez (1233 views) 2018-11-16 13:32:03 EgonOlsen (4669 views) 2018-06-10 19:43:48 EgonOlsen (5688 views) 2018-06-10 19:43:44 EgonOlsen (3205 views) 2018-06-10 19:43:20 DesertCoockie (4104 views) 2018-05-13 18:23:11 nelsongames (5125 views) 2018-04-24 18:15:36
 princec 9x KaiHH 7x Ali-RS 6x VaTTeRGeR 5x gouessej 5x elect 4x mudlee 4x Rain8 3x philfrei 2x SteveSmith 2x SHC 1x ral0r2 1x saocrinn 1x Akazan 1x bullen 1x
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08
 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