Thx this complicated stuff with the arrays should only get the distance between the objects :)
Now my problem might be easier to understand.
I dont get why the if statement never gives true..
what is x[][] and why do you change it in the class Life?
Well the x[][] and y[][] are arrays that hold the x or y coordinate of the single life.
The sX and the sY are their x and y ticket.
so x[sX][sY] is the x coordination of the current live.
xA and yA are the maximum of pixels in coordinations.
This flocking code seems to be very promising but i cant find my bug.
Its exactly what im searching for. You would help me alot by finding this bug.
The goal is the pixels are going to their closest neighbour.
The reason im not using arraylist is because i want to programm much more into this pixels (i wnat them later to join to obsticles and form social 2d life organism :)
And thx again it helped a lot :)
- int xA=20;
- int yA=20;
- int[][] x;
- int[][] y;
- life lif[][];
- void setup()
- {
- x=new int[xA][yA];
- y=new int[xA][yA];
- lif=new life[xA][yA];
- size(500, 500);
- smooth();
- ellipseMode(CENTER);
- for (int i=0;i<xA;i++)
- {
- for (int j=0;j<yA;j++) {
- lif[i][j]=new life((i*5)+int(random(5, 10)), (j*5)+int(random(5, 10)), i, j);
- }
- }
- }
- void draw()
- {
- background(0);
- for (int i=0;i<xA;i++)
- {
- for (int j=0;j<yA;j++) {
- lif[i][j].display();
- }
- }
- }
- class life {
- int sX;
- int sY;
- life(int curx, int cury, int cursX, int cursY)
- {
- sX=cursX;
- sY=cursY;
- x[sX][sY]=curx;
- y[sX][sY]=cury;
- }
- void display() {
- int dirX=0;
- int dirY=0;
- float Small=1000000000;
- for (int i=0;i<xA;i++)
- {
- for (int j=0;j<yA;j++)
- {
- if (dist(x[sX][sY], y[sX][sY], x[i][j], y[i][j])<Small)
- {
- Small=dist(x[sX][sY], y[sX][sY], x[i][j], y[i][j]);
- dirX=x[i][j];
- dirY=y[i][j];
- }
- }
- }
- if (dirX<x[sX][sY])
- {
- x[sX][sY]--;
- }
- else {
- x[sX][sY]++;
- }
- if (dirY<y[sX][sY])
- {
- y[sX][sY]--;
- }
- else {
- y[sX][sY]++;
- }
- ellipse(x[sX][sY], y[sX][sY], 2, 2);
- }
- }