Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Move from point A to point B  (Read 1513 times)
0 Members and 1 Guest are viewing this topic.
Offline saucecode

Senior Newbie


Medals: 1



« Posted 2011-12-16 20: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.

Please and thank you.

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.
Offline mlaux

Junior Member


Medals: 3



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

 

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

The first screenshot will be displayed as a thumbnail.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (58 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (41 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!