Java-Gaming.org Hi !
 Featured games (88) games approved by the League of Dukes Games in Showcase (681) Games in Android Showcase (196) games submitted by our members Games in WIP (744) 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 3157 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: 435
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: 435
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 Wizard

Medals: 135

 « 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
Administrator

« JGO Overlord »

Medals: 1220
Projects: 4
Exp: 16 years

Hand over your head.

 « 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: 435
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
Whoops my bad

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: 435
Projects: 3
Exp: 5 years

I'm the King!

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

Glad to help

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

« JGO Bitwise Duke »

Medals: 798
Projects: 4
Exp: 8 years

 « 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

You cannot reply to this message, because it is very, very old.

 CopyableCougar4 (42 views) 2016-06-25 16:56:52 Hydroque (78 views) 2016-06-22 02:17:53 SwampChicken (79 views) 2016-06-20 13:22:57 SwampChicken (79 views) 2016-06-20 13:22:49 SwampChicken (74 views) 2016-06-20 13:22:26 Hydroque (117 views) 2016-06-15 08:22:50 Hydroque (110 views) 2016-06-13 06:40:55 DarkCart (232 views) 2016-05-29 02:30:33 Hydroque (193 views) 2016-05-26 14:45:46 Mac70 (183 views) 2016-05-24 21:16:33
 Spasi 49x theagentd 45x NegativeZero 39x KaiHH 28x LiquidNitrogen 23x princec 19x CoDi^R 15x kingroka123 15x ags1 14x ra4king 13x Opiop 10x purenickery 9x Hydroque 9x Archive 8x SHC 7x czak 7x
 Making a Dynamic Plugin Systemby Hydroque2016-06-25 00:13:25Java Data structures2016-06-13 21:22:09Java Data structures2016-06-13 21:20:42FPS Camera Tutorialby Hydroque2016-05-22 05:40:58Website offering 3D Models specifically for games for freeby vusman2016-05-18 17:23:09Website offering 3D Models specifically for games for freeby vusman2016-05-09 08:50:56Website offering 3D Models specifically for games for freeby vusman2016-05-06 11:10:21Website offering 3D Models specifically for games for freeby vusman2016-04-29 12:56:17
 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