Trigonometry Issues

A

AurimasMB

I am working on a simple particle simulator, one of my first JAVA
apps, and I'm having some difficulty with Inverse Tangents. My first
goal is to have the particles move towards the mouse. Finding the
angle between those two points, from what I understand of geometry, is
done using the Inverse Tangent of (Y Distance / X Distance) between
the two points.

m_ang = Math.toDegrees(Math.atan((y_pos - my)/(x_pos - mx)));

x_pos and y_pos is the location of the particle, while mx and my are
the location of the mouse.

I've yielded all manner of interesting results with the simulator, but
I have not been able to make the particles all point at the mouse.
Sometimes the particles will all point towards the mouse from above,
but once past it, will continue to point away from it. Does this mean
I have to split the function into two parts?
Any suggestions?
 
S

Stefan Rybacki

AurimasMB said:
I am working on a simple particle simulator, one of my first JAVA
apps, and I'm having some difficulty with Inverse Tangents. My first
goal is to have the particles move towards the mouse. Finding the
angle between those two points, from what I understand of geometry, is
done using the Inverse Tangent of (Y Distance / X Distance) between
the two points.

m_ang = Math.toDegrees(Math.atan((y_pos - my)/(x_pos - mx)));

What do you need the angle for? Just use some vector arithmetic. Way easier.
x_pos and y_pos is the location of the particle, while mx and my are
the location of the mouse.

I've yielded all manner of interesting results with the simulator, but
I have not been able to make the particles all point at the mouse.
Sometimes the particles will all point towards the mouse from above,
but once past it, will continue to point away from it. Does this mean
I have to split the function into two parts?

Yes. Since atan only returns values that map to the degree range of 0 to 180°.
 
P

Patricia Shanahan

AurimasMB said:
I am working on a simple particle simulator, one of my first JAVA
apps, and I'm having some difficulty with Inverse Tangents. My first
goal is to have the particles move towards the mouse. Finding the
angle between those two points, from what I understand of geometry, is
done using the Inverse Tangent of (Y Distance / X Distance) between
the two points.

m_ang = Math.toDegrees(Math.atan((y_pos - my)/(x_pos - mx)));

x_pos and y_pos is the location of the particle, while mx and my are
the location of the mouse.

I've yielded all manner of interesting results with the simulator, but
I have not been able to make the particles all point at the mouse.
Sometimes the particles will all point towards the mouse from above,
but once past it, will continue to point away from it. Does this mean
I have to split the function into two parts?
Any suggestions?

Math.atan2(y_pos-my, x_pos-mx)

There two angles in the circle that have the same tangent, so Math.atan
can only select from a semicircle, -pi/2 through pi/2. Math.atan2 gets
two sign bits, one from each coordinate, and so can select the correct
quadrant of the circle.

Patricia
 
J

Joshua Cranmer

Patricia said:
Math.atan2(y_pos-my, x_pos-mx)

You know, out of curiosity, is there any common use case that would use
atan instead of atan2? All of the (admittedly few) times I had to do an
arctangent, atan2 was a better match for the problem.
 
P

Patricia Shanahan

Joshua said:
You know, out of curiosity, is there any common use case that would use
atan instead of atan2? All of the (admittedly few) times I had to do an
arctangent, atan2 was a better match for the problem.

My experience matches yours. My best guess is that atan is provided for
symmetry with asin and acos.

However, I am not sure that really makes sense. Arcsine and arccosine
arise in solving triangles, where there is an inherent limit on the
angle range. Math.asin and Math.acos pick the angles that could appear
in a triangle, if you include the degenerate case in which the angles
are pi, 0, and 0.

The only use I've actually seen for arctangent is exactly what the OP is
doing, calculating a direction from the difference in the Cartesian
coordinates of two points. Atan2 is indeed a better match for that job.

Patricia
 
D

Daniel Pitts

AurimasMB said:
I am working on a simple particle simulator, one of my first JAVA
apps, and I'm having some difficulty with Inverse Tangents. My first
goal is to have the particles move towards the mouse. Finding the
angle between those two points, from what I understand of geometry, is
done using the Inverse Tangent of (Y Distance / X Distance) between
the two points.

m_ang = Math.toDegrees(Math.atan((y_pos - my)/(x_pos - mx)));

x_pos and y_pos is the location of the particle, while mx and my are
the location of the mouse.

I've yielded all manner of interesting results with the simulator, but
I have not been able to make the particles all point at the mouse.
Sometimes the particles will all point towards the mouse from above,
but once past it, will continue to point away from it. Does this mean
I have to split the function into two parts?
Any suggestions?

First, you can use Math.atan2 instead of atan. It correctly handles -x/y
vs x/-y and x/y vs -x/-y

Second, you can "move in the direction" much more simply, without
actually calculating angles at all, using vector algebra

You have the particle point (lets call it P): P = <Xp, Yp>,
and the Mouse Point (lets call it M): M = <Xm, Ym>
The Delta (difference) vector between them is:
D = M - P = <Xd, Yd> = <Xm - Xp, Ym - Yp>
Now find the "unit" direction vector:
U = D / Math.hypot(Xd, Yd) // Math.hypot is Math.sqrt(Xd*Xd + Yd*Yd)

<Xu, Yu> = <Xd / Math.hypot(Xd, Yd), Yd / Math.hypot(Xd, Yd)>

Now, you can add any multiple of U to P to get your new point closer to
the mouse.
 
D

Daniel Pitts

Joshua said:
You know, out of curiosity, is there any common use case that would use
atan instead of atan2? All of the (admittedly few) times I had to do an
arctangent, atan2 was a better match for the problem.
atan2 probably uses atan internally :)

atan formally finds the reference angle, where atan2 finds the actual
angle by using the reference angle in the appropriate quadrant depending
on the signs of x and y. atan is useful, but atan2 is useful in many
more situations.
 
M

Mike Schilling

Patricia said:
My experience matches yours. My best guess is that atan is provided
for symmetry with asin and acos.

However, I am not sure that really makes sense. Arcsine and arccosine
arise in solving triangles, where there is an inherent limit on the
angle range. Math.asin and Math.acos pick the angles that could appear
in a triangle, if you include the degenerate case in which the angles
are pi, 0, and 0.

The only use I've actually seen for arctangent is exactly what the OP
is doing, calculating a direction from the difference in the Cartesian
coordinates of two points. Atan2 is indeed a better match for that
job.

I'm just speculating here, not having tried it, but I'd think atan2() can
also handle the case where the y-coordinate is 0, making it impossible to
use atan() (its parameter would be undefined.) There's no analogous
situation for asin() and acos().
 
P

Patricia Shanahan

Mike said:
I'm just speculating here, not having tried it, but I'd think atan2() can
also handle the case where the y-coordinate is 0, making it impossible to
use atan() (its parameter would be undefined.) There's no analogous
situation for asin() and acos().

Yes, there is a problem with atan() if the x and y are both zero. The
y/x division results in a NaN.

In terms of the normal use-case, that amounts to asking for the
direction of a line from a point to itself. In other systems, in which
division by zero is an error, the division is an issue when asking for
the direction of a line from a point to a point with the same x coordinate.

Patricia
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top