So I'm using Java (processing) and I have an array of objects and I need them to do be able to collide with themselves but I'm not too sure how to do it efficiently. I have a rudimentary way of doing it but it is very poorly written and doesn't allow for a dynamic array.
I currently have a static array but I would like to have a dynamic one in the future. I currently run the method that detects a collision by check for each possible combination. There will always be 3 objects, and it is not possible for there to ever be less than 3.
Here is my code:
Circle[] arrayOfCircles = new Circle[3]; //declare array
void setup() {
size(1000, 1000);
for (int i = 0; i < arrayOfCircles.length; i++) {
createPuck(i);
} // creates objects
}
void createPuck(int i) {
float speedY;
float speedX;
if (tempSpeedX == 0 && tempSpeedY == 0) {
speedY = createSpeedY();
speedY = speedY * multiplier;
speedX = 3 * multiplier;
} else {
speedX = tempSpeedX;
speedY = tempSpeedY;
tempSpeedX = 0;
tempSpeedY = 0;
}
arrayOfPucks[i] = new Puck(width, int(random(0, height)), speedX, speedY);
}
int createSpeedY() {
int tempSpeed = int(random(-3, 3));
do {
if (tempSpeed == 0) {
tempSpeed = int(random(-3, 3));
}
} while (tempSpeed == 0);
return tempSpeed;
}
void draw() {
detectCollisions();
}
void detectCollisions() {
for (int i = 0; i < arrayOfCircles.length; i++) {
boolean collision = false;
if (i==0) {
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+1]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i+1]);
}
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+2]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i+2]);
}
} else if (i==1) {
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-1]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i-1]);
}
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i+1]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i+1]);
}
} else if (i==2) {
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-1]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i-1]);
}
collision = arrayOfCircles[i].detectCollision(arrayOfCircles[i-2]);
if (collision == true) {
arrayOfCircles[i].bounce(arrayOfCircles[i-2]);
}
} // end of if
} // end of for loop
} // end of function
class Circle {
float x, y;
float speedX, speedY;
final int radius = 10;
Circle(float x, float y, float speedX, float speedY) {
this.x = x;
this.y = y;
this.speedX = speedX;
this.speedY = speedY;
}
// constructor
void move() {
this.x = this.x-speedX; //move left
this.y = this.y-speedY;
}
//methods
void render() {
fill(0);
ellipse(this.x, this.y, this.radius, this.radius);
}
void bounce() {
this.speedX = this.speedX * -1;
this.speedY = this.speedY * -1;
}
boolean detectSelfCollision(Puck other) {
for (int i = 0; i == 2; i++) {
}
float distX = this.x - other.x;
float distY = this.y - other.y;
float distance = sqrt( (distX*distX) + (distY*distY) );
if (distance <= radius*2) {
return true;
} else {
return false;
}
}
}
The code does work in that it does what I want but it to but it isn't very neat, there's lots of repetition and isn't very expandable.
Any suggestions or help would be greatly appreciated, I could even recreate any suggestions based upon pseudo-code. I'm just hitting a brick wall here.