Hello everyone!
I have to make a tank-game in Java for school
Obviously the tank should be able to shoot, in a parabola
Our teacher provided us with the formula to calculate the y-coordinate for by looping through a given amount of x-coordinates
Printscreen of the formula: formule.png
G = 9.81 m/s^2 (gravitation constant)
alpha = the angle under which the shot is fired
v0 is the power of the shot
w = the wind
x = the x-value of the map
y0 = y-value of the tank
y = path of bullet
Now, I *tried* to convert it to Java and I came up with this:
I've put it in a loop and add the coord object to an arraylist so I get the path of the bulletcoord.setY( (int) (((-G/2) / Math.pow(power * Math.cos(angle) - wind, 2) * Math.pow(currentX, 2)) + ((currentX * Math.sin(angle)) / (Math.cos(angle) - (wind / power))) + tankPosition.getY()) );
If I for example enter an angle of 20 and power of 30, this would be the content of the arraylist:
Did I do something wrong in converting the formula to java or something? :/X - Y
160 - -13778
161 - -13950
162 - -14123
163 - -14296
164 - -14471
165 - -14647
166 - -14824
167 - -15002
168 - -15181
169 - -15362
170 - -15543
171 - -15725
172 - -15908
173 - -16092
174 - -16277
175 - -16464
176 - -16651
177 - -16839
178 - -17029
179 - -17219
180 - -17411
181 - -17603
182 - -17796
183 - -17991
184 - -18187
185 - -18383
186 - -18581
187 - -18779
188 - -18979
189 - -19180
190 - -19381
191 - -19584
192 - -19788
193 - -19993
194 - -20199
195 - -20406
196 - -20613
197 - -20822
198 - -21032
199 - -21243
200 - -21455
201 - -21669
202 - -21883
203 - -22098
204 - -22314
205 - -22531
206 - -22749
207 - -22969
208 - -23189
209 - -23410
Thanks for your time!
Actual code:
public ArrayList<Coord> createPath(int power, int angle, int wind, Coord tankPosition) { final double G = 9.81; Coord coord; boolean shouldStop = false; ArrayList<Coord> list = new ArrayList<>(); int currentX = tankPosition.getX(); while (!shouldStop) { coord = new Coord(); coord.setX(currentX); coord.setY((int) (((-G / 2) / Math.pow(power * Math.cos(angle) - wind, 2) * Math.pow(currentX, 2)) + ((currentX * Math.sin(angle)) / (Math.cos(angle) - (wind / power))) + tankPosition.getY())); list.add(coord); currentX++; if (isOutOfBounds(coord) || isCollision(coord)) shouldStop = true; } return list; }
Thanks for your time!