Sign in to follow this  
BillM

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

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 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), 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!

Share this post


Link to post
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.



function degreeToRadian(angle){
  return (angle / (180.0 / Math.PI)); 
}


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

function getAngle(a, b){
  return radianToDegree(Math.atan2(b.y-a.y, b.x-a.x));;
} 
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 this post


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

Yep, atan returns a value between -pi/2..+pi/2, see also

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this