Java 2D - Cannot get code to work for generating points for a line with a start point and Angle

This topic is 592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi,

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

float m= ((float)((int) yloc - subYcoord))/((int) xloc - subXcoord);

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);

}

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), and subXcoord, subYcoord are: 169, 500

I get a slope of: -.947 (should be like: 245 degrees)

I get degrees of: -43 (now this doesn't look right)

Appreciate any help with this!

Thanks!

Share on other sites

do you really need angle, cant you just represent movement by direction vector x,y ?

Share on other sites

Hey.

For starters, look into vectors. Because once you come to actually animating your projectile, you will want to use a vector and in fact, a normalized vector.

Basicly, divide the total X and total Y of the vector (route) by its magnitude (length), and you will have a unit vector (basicly a value that indicates how much to move the object to x/y for each frame (* its innate speed). Im not a math guru as well, but vector math is very much mandatory for what you want to do (i do 2d, too).

In regards to your angles: I can only compare this to html 5 canvas, there 0, 0 is also located at the top left and X runs to the right while Y runs to the bottom.

What you want it a function that takes in two parameters and returns an angle.

Also, you want functions that convert from radians to degree and degree to radians.
Im going to offer my mathlib functions which you can adjust for your required syntax.



return (angle / (180.0 / Math.PI));
}

var x = radian * (180.0 / Math.PI);
return x;
}

function getAngle(a, b){
} 
var proj = {x: 50, y: 50};
var sub = {x: 100, y: 100};

console.log(getAngle(proj, sub)); // should return 45;



To make myself perfectly clear: I would not advise you to animate your items in the way you are attempting to animate them. You waypoints are unnecesarry and i suggest you take the proven approach (= vector).

Edited by AncientSion

Share on other sites
This does not seem to work for angles of 245 degrees.

https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan(double)

You can fix this by checking the sign of dx and dy, and adding an angle if you're outside the returned range.

Another option is to use atan2, which should work out-of-the-box

https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double)

Edited by Alberth

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• Forum Statistics

• Total Topics
633769
• Total Posts
3013759
×