views:

141

answers:

3

I'm not very experienced with Java, just started a couple weeks ago, but I have a simple applet that has two user controlled balls, drawn through java.awt and I need a way to detect a collision with between them. I have an algorithm for detecting collision with the walls:

 while (true){
        if (xPositon > (300 - radius)){
           xSpeed = -xSpeed; 
        }
        else if (xPositon < radius){
           xSpeed = -xSpeed; 
        }
        else if (yPositon > (300 - radius)) {
           ySpeed = -ySpeed;
        }
        else if (yPositon < radius){
           ySpeed = -ySpeed;
        }
        xPositon += xSpeed;
        yPositon += ySpeed;

and for the second ball

            if (xPositon2 > (300 - radius)){
           xSpeed2 = -xSpeed2; 
        }
        else if (xPositon2 < radius){
           xSpeed2 = -xSpeed2; 
        }
        else if (yPositon2 > (300 - radius)) {
           ySpeed2 = -ySpeed2;
        }
        else if (yPositon2 < radius){
           ySpeed2 = -ySpeed2;
        }
        xPositon2 += xSpeed2;
        yPositon2 += ySpeed2;

the applet is 300 pixels by 300 pixels radius stores the radius of the circles xPositon and xPositon2 store the x cordanents for the two balls yPositon and yPositon store the y cordanents for the two balls xSpeed and xSpeed2 store the x velocities for the two balls ySpeed and ySpeed2 store the y velocities for the two balls I've only taken algebra 1 so please no advanced math or physics.

+4  A: 

Use http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html, there's a distance method there, if it's less than the radius they're colliding.

EDIT: Err, less than the radius * 2 , sorry

Lucass
Kool thanks! I had heard of an intersecting command.. but apparently that only works with rectangles.
Bob Twinkles
I would +1 you but I can't
Bob Twinkles
In memory of Bob Twinkles, I had +1
Anthony Forloney
That is from Bob :)
Romain Hippeau
I just looked at it, but how do you pass the positions to the method
Bob Twinkles
You'll have ti instantiate the Point2d. I would recomend you to store the position using this class.
Lucass
umm how do you do that?
Bob Twinkles
int startingXPosition =0; int startingYPosition =0; java.awt.Point position = new java.awt.Point(startingXPosition,startingYPosition);declare point as a field and use position.getX() and position.getY().The collision teste:boolean isColliding = position.distance(anotherBall.getPosition()) < radius + anotherBall.getRadius;
Lucass
+1  A: 

There's Point2D in Java or you can do it yourself, it is trivially easy for circle/circle collisions or sphere/sphere collisions.

int distXX = (xPosition1 - xPosition2) * (xPosition1 - xPosition2);
int distYY = (yPosition1 - yPosition2) * (yPosition1 - yPosition2);
if ( radius*radius > distXX * distYY ) {
   ...  // There's a collision
}
Webinator
Note that the typical game developer newbie error would be to start using "square roots" to compute distance and check with the radius, instead of not square-rooting and checking with the *radius x radius*. The solution above is using only discrete match and actually *is* what is used in a *lot* of games (been writing games decades ago, this was *the* way to do it).
Webinator
oooh thanks to you to!
Bob Twinkles
ok I just compiled and ran and the ball colided with everyhing but the other ball
Bob Twinkles
A: 
 public boolean colliding(Ball anotherBall) {
    double xDelta = (this.x + this.ballSize/2 + this.dx) - (anotherBall.x + anotherBall.ballSize/2 + anotherBall.dx);
    double YDelta = (this.y + this.ballSize/2 + this.dy) - (anotherBall.y + anotherBall.ballSize/2 + anotherBall.dy);
    double distance = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(YDelta, 2));

    return (distance <= this.ballSize/2 + anotherBall.ballSize/2);

}
upt1me
erm that's nice but I'm running the entire app of one class
Bob Twinkles