I am developing a Java 2D video game. I am trying to write code to generate points for a line (actually a projectile path), at a specified angle, from a given start point. There seems to be something wrong with my Math. The points are to be used for moving a projectile from the top of the screen, towards the target (a submarine) which sits at the bottom.
I am suspicuous issue has to do with fact the origin of Java 2D coordinate system is 0,0 at top of the screen, not at bottom left for cartesian coordinate system. Note: I am not a Math person. So please excuse me if I am getting the terms wrong.
Here is what I am doing:
xloc, yloc - the initial location of the projectile I want to move towards the sub location
// Get slope of the line
float m= ((float)((int) yloc - subYcoord))/((int) xloc - subXcoord);
double radians= Math.atan(m);
double ycoord= yloc;
double xcoord= xloc;
int speed= 1;
// This is how I am attempting to generate the points for the path for the projectile to take
while (true) {
xcoord += speed * Math.cos (radians);
ycoord -= speed * Math.sin (radians);
if (((int) ycoord)>= gunBaseYcoord) {
break;
}
// WayPoints is my list of points to move the Projectile towards the sub at bottom of the screen
Point aPoint= new Point((int) xcoord, (int) ycoord);
wayPoints.add(aPoint);
}
This does not seem to work for angles of 245 degrees.
As an example, if x,y are: 660, 35 (this would be my start point for the projectile), and subXcoord, subYcoord are: 169, 500
I get a slope of: -.947 (should be like: 245 degrees)
The radian is: -.758
I get degrees of: -43 (now this doesn't look right)
Appreciate any help with this! Thanks!