Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) 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 1831 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

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

 ags1 (20 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (47 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (32 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (59 views) 2015-03-10 11:36:42 ClaasJG (43 views) 2015-03-10 11:33:01
 BurntPizza 21x LiquidNitrogen 21x basil_ 19x KevinWorkman 18x EgonOlsen 17x theagentd 16x Roquen 16x wessles 11x Varkas 11x 65K 11x Riven 11x princec 9x Rayvolution 9x phu004 8x SHC 8x Ashedragon 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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