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.
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
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.
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>
Thank you very much, I'm going to try that.
>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.
You probably want to use Math.atan2, which takes into account quadrant.