Velocity Reviews > Java > Trigonometry Issues

# Trigonometry Issues

AurimasMB
Guest
Posts: n/a

 11-04-2008
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?

Stefan Rybacki
Guest
Posts: n/a

 11-04-2008
AurimasMB schrieb:
> 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°.

> Any suggestions?

Roedy Green
Guest
Posts: n/a

 11-04-2008
On Mon, 3 Nov 2008 23:45:02 -0800 (PST), AurimasMB
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

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

there are two atan functions. You want the one that takes x and y
separately to get the right quadrant.

See http://mindprod.com/jgloss/trigonometry.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
Make your vote count. Use a paper ballot.
Voting machines invite massive fraud.

Patricia Shanahan
Guest
Posts: n/a

 11-04-2008
AurimasMB wrote:
> 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

Joshua Cranmer
Guest
Posts: n/a

 11-04-2008
Patricia Shanahan wrote:
> 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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Patricia Shanahan
Guest
Posts: n/a

 11-04-2008
Joshua Cranmer wrote:
> Patricia Shanahan wrote:
>> 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.
>

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

John B. Matthews
Guest
Posts: n/a

 11-04-2008
In article
<(E-Mail Removed)>,
AurimasMB <(E-Mail Removed)> wrote:

> I am working on a simple particle simulator[...]
> Any suggestions?

Instead of explicit trigonometry, you can do 2-D elastic collisions with
just vector algebra:

<http://www.geocities.com/vobarian/2dcollisions>

Here's a Java implementation:

Understanding how this works may aid your study of the trigonometric
functions in relation to the unit circle.

--
John B. Matthews
trashgod at gmail dot com

Daniel Pitts
Guest
Posts: n/a

 11-04-2008
AurimasMB wrote:
> 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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Daniel Pitts
Guest
Posts: n/a

 11-04-2008
Joshua Cranmer wrote:
> Patricia Shanahan wrote:
>> 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.
>

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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Mike Schilling
Guest
Posts: n/a

 11-04-2008
Patricia Shanahan wrote:
> Joshua Cranmer wrote:
>> Patricia Shanahan wrote:
>>> 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.
>>

>
> 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().