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.
This moves between (x1, y1) and (x2, y2) in 50 steps using linear interpolation.
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:
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():
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.
Some people might take offense to being told how to respond, and other people like to explain concepts instead of providing code.