I am aware that this is a frequently asked question, but I am stuck on how to solve it with my code.
I have a button on my GUI that allows me to add extra "turtles" to the graphics window, this adds a turtle to an arraylist which is showing flocking behaviour, from a gameloop structure. How can i make it so i can add as many turtles as i wish without this error being shown?
The game loop is as follows:
private void gameLoop()
{
while(true) //The order in which the turtles move, and when which algorithm is executed
{
for (int i = 0; i < turtles.size(); i++) //for each turtle, perform the set algorithm
{
(turtles.get(i)).unDrawTurtle();
(turtles.get(i)).update(1000);
hello.setText("X: " + (turtles.get(i)).getPositionX() + " Y: " + (turtles.get(i)).getPositionY());
}
for (int i = 0; i < turtles.size(); i++) //for each turtle, perform the set algorithm
{
(turtles.get(i)).cohesian(turtles); //MAKE BOIDS ATTRACT
}
for (int i = 0; i < turtles.size(); i++) //for each turtle, perform the set algorithm
{
(turtles.get(i)).drawTurtle();
}
for (int i = 0; i < turtles.size(); i++) //for each turtle, perform the set algorithm
{
(turtles.get(i)).wrapPosition((turtles.get(i)).getPositionX(), (turtles.get(i)).getPositionY());
}
//Here we are making sure the turtles are on the screen
Utils.pause(deltaTime/2);
}
Here is the code for the button adding turtles to the ArrayList:
addTurtleButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
turtles.add(new RandomTurtleB(canvas, 400, 300, currentSpeed, 0));
}
} );
removeTurtleButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
turtles.remove(turtles.size() - 1);
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
canvas.removeMostRecentLine();
}
} );
And in a different class is the cohesion method which is adding turtles/changing the contents of the ArrayList:
public void cohesian(ArrayList<DynamicTurtle> turtles)
{
double flockingDistanceLimit = 50;
double numberOfFlockers = 0;
double combinedX = 0;
double combinedY = 0;
double averageCombinedX;
double averageCombinedY;
//ouble moveToFlock;
//double turnToFlock;
double distanceToPotentialFlock;
for (DynamicTurtle t : turtles) //SCAN FOR ALL TURTLES
{
if(this.getPositionX() != t.getPositionX() && this.getPositionY() != t.getPositionY()) //MAKE SURE TURTLE ISNT SCANNING ITS SELF
{
distanceToPotentialFlock = Math.sqrt(Math.pow((this.getPositionX()-t.getPositionX()),2 ) + Math.pow((this.getPositionY()-this.getPositionY()),2)); //FIND DISTANCE TO TURTLE
System.out.println("distanceToPotentialFlock: " + distanceToPotentialFlock); //PRINT TO SCREEN HOW FAR AWAY THE TURTLE IS
if(distanceToPotentialFlock < flockingDistanceLimit) //MAKE SURE THE FOUND TURTLE IS WITHIN RANGE
{
combinedX = combinedX + t.getPositionX(); //FIND SUMMATION OF X POSITIONS
combinedY = combinedY + t.getPositionY(); //FIND SUMMATION OF Y POSITIONS
numberOfFlockers++; //AS A FLOCKER HAS BEEN FOUND INCREMENT THE NUMBER OF FLOCKERS
System.out.println("distanceToPotentialFlock: " + distanceToPotentialFlock);
System.out.println("numberOfFlockers: " + numberOfFlockers);
if(numberOfFlockers > 0)
{
averageCombinedX = (combinedX / numberOfFlockers); //FIND AVERAGE X POSITION
averageCombinedY = (combinedY / numberOfFlockers); //FIND AVERAGE Y POSITION
System.out.println("XFLOCK: " + averageCombinedX + "YFLOCK: " + averageCombinedY);
double angleRad = Math.atan2(averageCombinedY, averageCombinedX);
double angleDeg = Math.toDegrees(angleRad);
// place in 0,360 range
if(angleDeg < 0)
{
angleDeg = 360 - (-angleDeg);
}
this.turn(angleDeg);
this.move(10);
}
}
}
}
I would greatly appreciate any assisance in helping me solving this problem. Regards, James.