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;
}
comparing balls[i] to balls[i] where "i" is simultaneously the same number- what is that supposed to mean ? - Nir Alfasi