Math.cos() problems

  • Thread starter Brandon McCombs
  • Start date
B

Brandon McCombs

Hello,

I'm trying to do something simple but Java turns it into a federal case.
All I want to do is draw a couple circles on a Graphics2D canvas with
one movable and attracted to the 2nd one which is stationary. Basically
I'm just trying to get basic functionality working that simulates
gravitational attraction between the spheres.

I had everything working concerning the movement calculations (force,
acceleration, velocity) up to the point of performing the calculation
that would generate each new pixel positions for the circle that I am
allowing to move. I ran into problems because I had to determine the
angle between the moving circle and the stationary one and use the angle
in the equations x = rCos(angle) and y = rSin(angle) so I could
determine how much the movable circle should move.

The problem is that I have no clue how Java is calculating the cosine
and xine of the angle I calculate (even calculating the angle is a chore
which may be confusing the problem even further). With respect to what
screen location are the trig functions being evaluated? (0,0) on the
drawing canvas? Does it matter? I thought it did especially since my
angle is based on the location of one of my circles and it isn't at 0,0
on the canvas. Where am I going wrong? This is frustrating. If you need
more info let me know.

thanks
 
K

Knute Johnson

Brandon said:
Hello,

I'm trying to do something simple but Java turns it into a federal case.
All I want to do is draw a couple circles on a Graphics2D canvas with
one movable and attracted to the 2nd one which is stationary. Basically
I'm just trying to get basic functionality working that simulates
gravitational attraction between the spheres.

I had everything working concerning the movement calculations (force,
acceleration, velocity) up to the point of performing the calculation
that would generate each new pixel positions for the circle that I am
allowing to move. I ran into problems because I had to determine the
angle between the moving circle and the stationary one and use the angle
in the equations x = rCos(angle) and y = rSin(angle) so I could
determine how much the movable circle should move.

The problem is that I have no clue how Java is calculating the cosine
and xine of the angle I calculate (even calculating the angle is a chore
which may be confusing the problem even further). With respect to what
screen location are the trig functions being evaluated? (0,0) on the
drawing canvas? Does it matter? I thought it did especially since my
angle is based on the location of one of my circles and it isn't at 0,0
on the canvas. Where am I going wrong? This is frustrating. If you need
more info let me know.

thanks

Could it be that you are getting messed up because they y axis is upside
down? Add pi to your angle before calculating.
 
E

Eric Sosman

Brandon said:
Hello,

I'm trying to do something simple but Java turns it into a federal case.
All I want to do is draw a couple circles on a Graphics2D canvas with
one movable and attracted to the 2nd one which is stationary. Basically
I'm just trying to get basic functionality working that simulates
gravitational attraction between the spheres.

I had everything working concerning the movement calculations (force,
acceleration, velocity) up to the point of performing the calculation
that would generate each new pixel positions for the circle that I am
allowing to move. I ran into problems because I had to determine the
angle between the moving circle and the stationary one and use the angle
in the equations x = rCos(angle) and y = rSin(angle) so I could
determine how much the movable circle should move.

The problem is that I have no clue how Java is calculating the cosine
and xine of the angle I calculate (even calculating the angle is a chore
which may be confusing the problem even further). With respect to what
screen location are the trig functions being evaluated? (0,0) on the
drawing canvas? Does it matter? I thought it did especially since my
angle is based on the location of one of my circles and it isn't at 0,0
on the canvas. Where am I going wrong? This is frustrating. If you need
more info let me know.

Angles are dimensionless numbers, not "with respect to"
anything at all. Two possible points of confusion:

- As Knute Johnson points out, the Y axis for Java graphics
is "upside down:" small values are at the top of the window
and large values are at the bottom.

- Java measures angles in radians, so if you think 90.0 is
a right angle you've got another think coming.
 
D

Daniel Pitts

Brandon said:
Hello,

I'm trying to do something simple but Java turns it into a federal case.
All I want to do is draw a couple circles on a Graphics2D canvas with
one movable and attracted to the 2nd one which is stationary. Basically
I'm just trying to get basic functionality working that simulates
gravitational attraction between the spheres.

I had everything working concerning the movement calculations (force,
acceleration, velocity) up to the point of performing the calculation
that would generate each new pixel positions for the circle that I am
allowing to move. I ran into problems because I had to determine the
angle between the moving circle and the stationary one and use the angle
in the equations x = rCos(angle) and y = rSin(angle) so I could
determine how much the movable circle should move.

The problem is that I have no clue how Java is calculating the cosine
and xine of the angle I calculate (even calculating the angle is a chore
which may be confusing the problem even further). With respect to what
screen location are the trig functions being evaluated? (0,0) on the
drawing canvas? Does it matter? I thought it did especially since my
angle is based on the location of one of my circles and it isn't at 0,0
on the canvas. Where am I going wrong? This is frustrating. If you need
more info let me know.

thanks


Math.cos, Math.sin, and Math.atan2 deal with radians. Make sure that
your code is using the appropriate units in the appropriate places.

It may help to create an Angle class that abstracts the unit from the
user, and implements the specific functions you need. This is all part
of avoiding primitive obsessions.

<http://virtualinfinity.net/wordpress/program-design/2007/10/28/primitive-obsession/>

For example, you might decide to use this approach:

class Angle {
private double radians;

public double sine() {
return Math.sin(getRadians());
}

public double cosine() {
return Math.cos(getRadians());
}

public void setFromVector(double x, double y) {
setRadians(Math.atan2(y, x));
}

public double getRadians() {
return radians;
}

public void setRadians(double radians) {
this.radians = radians;
}

public double getDegrees() {
return getRadians() / Math.PI * 180.0;
}

public void setDegrees(double degrees) {
setRadians(degrees /180.0 * Math.PI);
}
}

This particular version is a mutable instance. I personally prefer
making "radians" final and using factory methods in lieu of the set
methods. I also prefer using a vector class (not java.util.Vector), to
represent the x/y coordinates. vector could also include getAngle()
(which would delegate to Angle.fromVector(getX(), getY()).
 
B

Brandon McCombs

Knute said:
Could it be that you are getting messed up because they y axis is upside
down? Add pi to your angle before calculating.

Well that was one problem I noticed last night after I posted. I'm doing
other stuff that I shouldn't be but I haven't had time to fix it yet.
When I simplify the code I may end up with it working properly. I'm
aware of -pi/2 and pi/2 as the range for cosine although whether I was
correctly taking that into account last night is another issue. I'm also
aware that the java methods use radians for input/output which I've been
correctly taking into account. It's been a while since I did a lot of
trig stuff though. I'll try again when I have time. thanks guys
 
T

tam

Knute Johnson wrote:
Well that was one problem I noticed last night after I posted. I'm doing
other stuff that I shouldn't be but I haven't had time to fix it yet.
When I simplify the code I may end up with it working properly. I'm
aware of -pi/2 and pi/2 as the range for cosine although whether I was
correctly taking that into account last night is another issue

That's not quite right. Cos has a domain of all real numbers and a
range of -1 to 1. The cos function is periodic with a period of 2 pi
and samples all values of its range in the interval 0 to pi. Acos has
a domain of -1 to 1 and Java's implementation uses the reasonably
standard range of 0 to pi. Since cos is symmetric, a domain of -pi/2
to pi/2 would never get a negative result.

Regards,
Tom McGlynn
 
R

Richard F.L.R.Snashall

Brandon said:
I'm trying to do something simple but Java turns it into a federal case.
All I want to do is draw a couple circles on a Graphics2D canvas with
one movable and attracted to the 2nd one which is stationary. Basically
I'm just trying to get basic functionality working that simulates
gravitational attraction between the spheres.

If you are doing this for one body, RELATIVE to a given one, after
you took the sine and cosine, did you add back the screen coordinates
of the given body to get the screen coordinates of the one you want?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top