Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trigonometry Problem (Probably due to Math.atan)

5 views
Skip to first unread message

Hamel Ajay Kothari

unread,
Nov 22, 2009, 2:48:41 PM11/22/09
to
So I'm writing a game similar to this one:
http://homepage.ntlworld.com/bartle/volleyball-original/index.html
I'm using an Arc2D for the "slime" and an Ellipse2D for the ball, and
what I'm doing is when the ball intersects the Arc2D, I'm having it
rebound, the way I'm doing this is the following:
this._direction = Math.atan((slime.getY() - this._y)/(slime.getX() -
this._x));

Where "this" is the ball, and "slime" is the slime. The problem is
when the x position of the ball is greater than the x position of the
slime, the ball rebounds properly, but when the ball is to the left of
the slime and it intersects the arc, it seems to go through the arc.
Does anyone know why this is? Does it have to do with the fact that
atan only returns a value between -pi/2 and pi/2
? Thanks in advance.

Eric Sosman

unread,
Nov 22, 2009, 2:53:01 PM11/22/09
to

Impossible to say without seeing some code, but it sounds
plausible. Have you tried the Math.atan2() function? (Pay
attention to the order of the arguments, by the way.)

--
Eric Sosman
eso...@ieee-dot-org.invalid

Hamel Ajay Kothari

unread,
Nov 22, 2009, 3:03:17 PM11/22/09
to
> esos...@ieee-dot-org.invalid

I tried atan2 also, I still have the same problem, but here's my
Ball.checkCollision(Slime slime) method:
public void checkCollision(Slime slime) {
if(slime.getArc().intersects(this._circle.getBounds2D())) {


this._direction = Math.atan((slime.getY() - this._y)/(slime.getX()
- this._x));
}
}

The slime is defined as an Arc2D.Double like so:
new Arc2D.Double(this._x - (this._diameter / 2), this._y -
(this._diameter / 2), this._diameter, this._diameter, 0, 180,
Arc2D.CHORD);

An example of the numbers at collision:
slime.getX() = 180
slime.getY() = 300

ball._x = 152
ball._y = 264

What other code would you need to help me? Thanks.

John B. Matthews

unread,
Nov 22, 2009, 3:32:57 PM11/22/09
to
In article
<5a1bf568-205a-496c...@j4g2000yqe.googlegroups.com>,

Eric Sosman suggested, the range of Math.atan2() may be more suited to
this application. Alternatively, you might look at this vector based
approach, "2-Dimensional Elastic Collisions without Trigonometry":

<http://www.vobarian.com/collisions/2dcollisions2.pdf>

A Java example may be found here:

<http://sites.google.com/site/drjohnbmatthews/kineticmodel>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Hamel Ajay Kothari

unread,
Nov 22, 2009, 3:38:25 PM11/22/09
to
On Nov 22, 3:32 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <5a1bf568-205a-496c-9b21-025968acd...@j4g2000yqe.googlegroups.com>,

Thank you very much, I'm going to try that.

Roedy Green

unread,
Nov 22, 2009, 4:23:58 PM11/22/09
to
On Sun, 22 Nov 2009 11:48:41 -0800 (PST), Hamel Ajay Kothari
<hkot...@hydronitrogen.com> wrote, quoted or indirectly quoted
someone who said :

>this._direction = Math.atan((slime.getY() - this._y)/(slime.getX() -
>this._x));

This is going to blow up when slime.x is close to ball.x.

Java has two functions for computing arctangent: java.lang.Math.atan
and atan2. They work in radians. The result of atan will always be
range -pi/2 to +pi/2. The result of atan2 will be in the range -pi to
+pi in the quadrant consistent with the signs of the two arguments.

You want atan2.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Finding a bug is a sign you were asleep a the switch when coding. Stop debugging, and go back over your code line by line.

Daniel Pitts

unread,
Nov 22, 2009, 11:16:45 PM11/22/09
to

You probably want to use Math.atan2, which takes into account quadrant.

0 new messages