This is a problem I've been having, When making a set number of filled circles at random locations on a JPanel and applying a gravity (a negative change in the y), each of the circles collide. I want them to have collision detection and push in the opposite direction using vectors but I don't know how to apply that to my scenario could someone help?

public void drawballs(Graphics g){
    g.setColor (Color.white); 

    //displays circles
    for(int i = 0; i<xlocationofcircles.length-1; i++){
    g.fillOval( (int) xlocationofcircles[i],  (int) (ylocationofcircles[i]) ,16 ,16 );
    ylocationofcircles[i]+=.2; //gravity
    if(ylocationofcircles[i] > 550) //stops gravity at bottom of screen

    //Check distance between circles(i think..)
    float distance =(xlocationofcircles[i+1]-xlocationofcircles[i]) + (ylocationofcircles[i+1]-xlocationofcircles[i]);

    if( Math.sqrt(distance) <16)

2 Answers 2


Please consider using camel case in your code like this:


First there is a typo here:

float distance =(xlocationofcircles[i+1]-xlocationofcircles[i]) + (ylocationofcircles[i+1]-xlocationofcircles[i])  ;

I think you meant:

... (ylocationofcircles[i+1]-ylocationofcircles[i]) ;

Second, this is incorrect.

Use a^2 + b^2 = c^2.

Meaning distance = sqrt(a*a + b*b)

distance =
(xlocationofcircles[i+1]-xlocationofcircles[i]) * 
(xlocationofcircles[i+1]-xlocationofcircles[i]) + 
(ylocationofcircles[i+1]-ylocationofcircles[i]) *

instead of:

Assuming Radius = 8

Math.sqrt(distance) <16

I would recommend you use this instead:

distance < 16*16

sqrt is a wasteful function in processing time.


That will allow you to detect collisions. The issue is that collisions occur in-between the movements. When you calculate and check for collisions, you need to find the time of collision and base the result of the collision on that factor. It is easier to use a pre-built tested physics engine if you wish to address this.

  • \$\begingroup\$ One last thing, I think you are using gravity in a linear way. Try this instead: yspeedofcircles[i] += gravity; ylocationofcircles[i] += yspeedofcircles[i]; \$\endgroup\$
    – AturSams
    Commented Oct 10, 2012 at 6:52
  1. Draw the circle after, not before calculations. Otherwise you're always a frame behind. It's not bad if there's no user input, and you keep your code consistent - but if you draw some things before calculating the frame, and some after, you will get problems...

  2. Gravity is a constant acceleration, not constant speed. If you want your effect to be realistic, you need to make two additional arrays: yVelocityOfCircles and xVelocityOfCircles.

  3. Put literals (arbitral values) into constants, making the code easier to read and change. const GRAVITY = .2;.

  4. Don't just rebound the circle from floor by a constant value. Calculate where it should stop: centerY = floorTop - circleRadius.

  5. If there is a circle-circle collision, and I guess there is, the problem is more complicated than it seems, because you're dealing with racing conditions (which collision occurs first?). Because of your low gravity I'm assuming racing conditions won't be much of a problem. This will help you with circle-circle collision resolution: How can I know in which direction 2 balls will go in billiard game?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .