1
votes

I'm working on a small exercise where I have to initialize an array of ball objects that move around the screen at random speeds. I need the balls to reverse speeds when they collide with each other. The problem is that I can only compare each ball to itself so that they're constantly "intersecting" and spazing back and forth. I have a good feeling the problem is within the if statement beneath this because I'm comparing balls[i] to balls[i] where "i" is simultaneously the same number. When I pass balls[i] through the intersect function I need to be comparing every element to every other element except itself. I've tried a handful of ways but they didn't work and were superfluous.

//for loop within draw()

   for(int i = 0; i < balls.length; i++){
    //balls[i].drawBalls();
    //balls[i].moveBalls();
    ***if (balls[i].intersect(balls[i])) {
        balls[i].moveIntersectingBalls();
    }***
  }

//ball intersecting method

boolean intersect(Ball b) {
    float distance = dist(bXpos,bYpos,b.bXpos,b.bYpos);

    if (distance < bRadius + b.bRadius) {
      return true;
    } else {
      return false;
    }
  }

//ball moving if intersecting method

void moveIntersectingBalls(){
    bXspd *= -1;
    bYspd *= -1;
  }
3
comparing balls[i] to balls[i] where "i" is simultaneously the same number - what is that supposed to mean ? - Nir Alfasi
Ok, you appear to have noted the issue ("The problem is that I can only compare each ball to itself so that they're constantly "intersecting""). Could you explain better why you cannot compare two different balls? - SJuan76

3 Answers

1
votes

To compare each ball with all the other balls, you need two for loops:

for (int i = 0; i < balls.length; i++)
{
    for (int j = 0; j < balls.length; j++)
    {
        // Check first that you are not comparing a ball to itself.
        if (i != j)
            if (balls[i].intersect(balls[j]))
                balls[i].moveIntersectingBalls();
    }
}
0
votes

I mean, this code doesn't seem to make much sense:

if (balls[i].intersect(balls[i])) {
    balls[i].moveIntersectingBalls();
}

But you pointed that out yourself I think.

Are you looking for something like:

for(int i = 0; i < balls.length; i++){
    for(int j = (i+1); j < balls.length; j++){
        if (balls[i].intersect(balls[j])) {
            balls[i].moveIntersectingBalls();
            // Maybe also add:
            balls[j].moveIntersectingBalls();
        }
    }
}

You can start the inner loop at i instead of 0 because you've already "checked" those previously.

In fact, if you start it at 0, you will check each ball-pair twice (e.g. (1,3) and again at (3,1)) perhaps causing the twitching you've described.

-1
votes

What you need here is a double loop :

        for(int i = 0; i < balls.length; i++){
        for(int j = 0; j < balls.length; j++){
            if (balls[i].intersect(balls[j]) && i != j) {
                balls[i].moveIntersectingBalls();
            }
        }
    }

With the double loop you compare each ball (first loop) to all the others balls (second loop) and if you hit one, you call moveIntersecingBalls(); for the first ball.

This algo could probably be optimized because when two balls collide it is useless to check them again but it is a good start.