Jump to content
  • Advertisement
Sign in to follow this  
BlackSunStudios

SOLVED:Circle Cellular automata mating bug

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys im trying to make a cellular automation with circles that move across the screen but ive come up with a nasty bug.

Heres the Source code (im using slick 2d)



import java.util.Random;

import org.newdawn.slick.*;

public class MainClass extends BasicGame{

Cell[] cell;
int cellList = 1;
int maxCells = 9999999;


public void render(GameContainer arg0, Graphics g) throws SlickException {
if(cellList > 1){
for(int i = 1; i < cellList; i++){
cell.Render(g);
}
}

}


public void init(GameContainer gc) throws SlickException {
cell = new Cell[maxCells];

}



public void update(GameContainer gc, int delta) throws SlickException {
Input input = gc.getInput();
if(input.isKeyDown(input.KEY_ESCAPE)){
System.exit(0);
}
if(input.isKeyDown(input.KEY_SPACE)){
Random a = new Random();
int b = a.nextInt(1280);
Random d = new Random();
int c = a.nextInt(800);
cell[cellList] = new Cell(b,c,10,1,1,1000,Color.white);
cellList++;
}

if(cellList > 1){
for(int i = 1; i < cellList; i++){
if(cell.active == true){
cell.AgeCheck();
cell.Move();
for(int b = 1; b < cellList;b++){

if(i==b){
break;
}


if(cell.Collision(cell) == true){



cell.speedx =- cell.speedx;
cell.speedy =- cell.speedy;
cell.speedx =- cell.speedx;
cell.speedy =- cell.speedy;

if(cell.Gender != cell.Gender){
try{

cell[cellList] = new Cell(cell.x - cell.radius,cell.y ,cell.radius,cell.speedx,cell.speedy,cell.lifespan,cell.color);
cellList++;

cell.Destroy();

}catch(ArrayIndexOutOfBoundsException e){System.out.println("Out of bounds");}
}
else{
cell.Destroy();
}



}




}

}
}
}







}


public MainClass(){
super("Cellz");
}
public static void main(String[] args) throws SlickException{
AppGameContainer game = new AppGameContainer(new MainClass());
game.setDisplayMode(1280, 800, true);
game.start();

}





}








import java.util.Random;

import org.newdawn.slick.*;


public class Cell {
float x,y,radius,speedx,speedy,life,lifespan;
Color color;
boolean active;
int collidetimes = 0;
boolean Gender;

public void Render(Graphics g){
g.setColor(color);
g.fillOval(x, y, radius, radius);
}
public void AgeCheck(){
life++;
if(life > lifespan){
Destroy();
}
}
public void Destroy(){
if(active == true){
x = 0;
y = 0;
radius = 0;
speedx = 0;
speedy = 0;
life = 0;
lifespan = 0;
color = null;
active = false;
System.out.println("Destroy");
}
}

public void Move(){
x += speedx;
y += speedy;
if(x > 1280){
speedx =- speedx;
}
if(x < 0 ){
speedx =- speedx;
}
if(y > 800){
speedy =- speedy;
}
if(y < 0){
speedy =- speedy;
}


}

public boolean Collision(Cell c){



float xd = x - c.x;
float yd = y - c.y;

float sumRadius = radius + c.radius;
float sqrRadius = sumRadius + sumRadius;

float distSqr = (xd * xd) + (yd * yd);

if (distSqr <= sqrRadius)
{
collidetimes ++;

return true;

}else
{return false;
}


}

public Cell(float x, float y, float radius, float speedx,float speedy, float lifespan, Color color){
this.x = x;
this.y = y;
this.radius = radius;
this.speedx = speedx;
this.speedy = speedy;
this.life = 0;
this.lifespan = lifespan;
this.color = color;
active = true;
Random gender = new Random();
int a = gender.nextInt(2);
if(a == 0){
Gender = false;
}else{Gender = true;}


}

}









So what i want to happen is when two cells that hit eachother that are a different gender a new cell is made.
At the start its all well apart from when 3 cells die. when a cell dies its position is moved to 0 and all variables are set to false and such.
the program only checks collision if a cell is active(boolean) and when a cell dies its active boolean is set to false.
The program freezes and stops responding.
I made it so when a new cell is born its x and y coordinates are printed on the screen:

0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0

That times by alot!

now i know what the problem is( the dead cells are mating eww)
but i dont know how to fix it.
i tried to fix it by making it so it checks collision only if active but its still bugging out.

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!