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
 Move from point A to point B  (Read 3579 times) 0 Members and 1 Guest are viewing this topic.
saucecode

Senior Newbie

Medals: 1

 « Posted 2011-12-16 19:24:57 »

A few things first. I've been up all night (literally) trying to figure this out. Being a 14 year old kid, this is just enraging, and I vow to not sleep until i nail this, or else leave programming for a long time. Second, I request you only give me code, if you intend to respond, do it with something that will advance my current situation.

The Problem:
How to move an object from point a to point b, bullet style.

I heard all about this atan2 magic, and if you plan to respond, do so with some explanation of this function.

edit: if no useful replys come up, i will assume that this is all impossible, and every other game does it hard coded through if statements, or pure magic.
mlaux

Junior Devvie

Medals: 3

 « Reply #1 - Posted 2011-12-16 19:52:14 »

This moves between (x1, y1) and (x2, y2) in 50 steps using linear interpolation.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `public class Test {   public static void main(String[] args) {      int x1 = 10, y1 = 60;      int x2 = 230, y2 = 400;            for(int t = 0; t <= 50; t++) {         double x = lerp(x1, x2, t / 50.0);         double y = lerp(y1, y2, t / 50.0);         System.out.println("t = " + t + ", " + x + ", " + y);      }   }      private static double lerp(int x1, int x2, double t) {      return x1 + (x2 - x1) * t;   }}`

sample output:
 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  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51 `t = 0, 10.0, 60.0t = 1, 14.4, 66.8t = 2, 18.8, 73.6t = 3, 23.2, 80.4t = 4, 27.6, 87.2t = 5, 32.0, 94.0t = 6, 36.4, 100.8t = 7, 40.800000000000004, 107.6t = 8, 45.2, 114.4t = 9, 49.6, 121.19999999999999t = 10, 54.0, 128.0t = 11, 58.4, 134.8t = 12, 62.8, 141.6t = 13, 67.2, 148.4t = 14, 71.60000000000001, 155.2t = 15, 76.0, 162.0t = 16, 80.4, 168.8t = 17, 84.80000000000001, 175.60000000000002t = 18, 89.2, 182.39999999999998t = 19, 93.6, 189.2t = 20, 98.0, 196.0t = 21, 102.39999999999999, 202.79999999999998t = 22, 106.8, 209.6t = 23, 111.2, 216.4t = 24, 115.6, 223.2t = 25, 120.0, 230.0t = 26, 124.4, 236.8t = 27, 128.8, 243.60000000000002t = 28, 133.20000000000002, 250.4t = 29, 137.6, 257.2t = 30, 142.0, 264.0t = 31, 146.4, 270.8t = 32, 150.8, 277.6t = 33, 155.20000000000002, 284.4t = 34, 159.60000000000002, 291.20000000000005t = 35, 164.0, 298.0t = 36, 168.4, 304.79999999999995t = 37, 172.8, 311.6t = 38, 177.2, 318.4t = 39, 181.6, 325.2t = 40, 186.0, 332.0t = 41, 190.39999999999998, 338.8t = 42, 194.79999999999998, 345.59999999999997t = 43, 199.2, 352.4t = 44, 203.6, 359.2t = 45, 208.0, 366.0t = 46, 212.4, 372.8t = 47, 216.79999999999998, 379.59999999999997t = 48, 221.2, 386.4t = 49, 225.6, 393.2t = 50, 230.0, 400.0`

You can also do it using trigonometry to find the angle between the 2 points (with the atan2 function you mentioned) and then stepping towards the second point using cos() and sin():

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public class Test {   private static final double SPEED = 0.25;      public static void main(String[] args) {      int x1 = 10, y1 = 60;      int x2 = 230, y2 = 400;            double angle = Math.atan2(y2 - y1, x2 - x1);      double x = x1, y = y1;            while(x < x2 && y < y2) {         x += SPEED * Math.cos(angle);         y += SPEED * Math.sin(angle);         System.out.println(x + ", " + y);      }   }}`

(note that this example won't work if x2 > x1 or y2 > y1, I just wrote it in like 2 minutes so it's not too complete)

Also, if you're getting this frustrated, maybe it's a sign that you need to go to sleep. Seriously, programming is so much easier when you're not so tired that you're falling out of your chair.

Second, I request you only give me code, if you intend to respond, do it with something that will advance my current situation.

Some people might take offense to being told how to respond, and other people like to explain concepts instead of providing code.

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 (94 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