Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 2589 times)
0 Members and 1 Guest are viewing this topic.
Offline 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  Grin 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....
Offline ra4king

JGO Kernel


Medals: 356
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 Wink

Offline 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 Tongue), 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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
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.

Offline 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
Offline cylab

JGO Ninja


Medals: 55



« 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!
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
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
Offline ra4king

JGO Kernel


Medals: 356
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 Smiley

Offline 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.
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


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

Glad to help Cheesy

Offline 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.
Offline 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?
}
Online theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!