algorithim for simple colision detection in Java

Posted by Bob Twinkles on Stack Overflow See other posts from Stack Overflow or by Bob Twinkles
Published on 2010-04-20T21:06:31Z Indexed on 2010/04/20 21:13 UTC
Read the original article Hit count: 287

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about collision-detection