int nbTilesW = 30, nbTilesH = 30;
float gapW = 4, gapH = 4;
final static float TILE_WIDTH = 35;
float tileW = TILE_WIDTH, tileH = TILE_WIDTH;
totalW = (tileW + gapW) * nbTilesW - gapW;
totalH = (tileH + gapH) * nbTilesH - gapH;
tiles = new Tile[nbTilesW][nbTilesH];
for (int i = 0; i < nbTilesW; i++)
for (int j = 0; j < nbTilesH; j++)
tiles[i][j] = new Tile(i*(tileW + gapW)-totalW/2, map(j, 0, nbTilesH, 0, totalH)-totalH/2, i, j);
tileW = constrain(tileW, 15, width/2);
tileH = constrain(tileH, 15, height/2);
} else if (myKey == ENTER || myKey == RETURN)
tileW = tileH = TILE_WIDTH;
translate(width/2, height/2);
PVector strength = new PVector(mouseX-pmouseX, mouseY-pmouseY);
for (int i = 0; i < nbTilesW; i++)
for (int j = 0; j < nbTilesH; j++)
tiles[i][j].process(mouseX, mouseY, strength);
for (int i = 0; i < nbTilesW; i++)
for (int j = 0; j < nbTilesH; j++)
PVector othersImpact = new PVector(0, 0);
PVector r = new PVector(0, 0);
Boolean mouseImpacted = false;
Tile(float p_x, float p_y, int p_i, int p_j)
float X = x + width/2 - tileW/2;
float Y = y + height/2 - tileH/2;
minXY = new PVector(screenX(X, Y, 0), screenY(X, Y, 0));
maxXY = new PVector(screenX(X + tileW, Y + tileH, 0), screenY(X + tileW, Y + tileH, 0));
void process(int p_mX, int p_mY, PVector p_strength)
if (minXY.x < p_mX && p_mX < maxXY.x && minXY.y < p_mY && p_mY < maxXY.y)
dr.x = map(p_strength.y, -15, 15, PI/4, -PI/4);
dr.y = map(p_strength.x, -15, 15, -PI/4, PI/4);
processNeighbors(dr.get());
void processNeighbors(PVector p_strength)
PVector l_strength = p_strength.get();
if (!mouseImpacted) othersImpact.add(l_strength);
if (l_strength.mag() > .1)
if (i > 0) tiles[i-1][j].processNeighbors(l_strength);
if (i < nbTilesW-1) tiles[i+1][j].processNeighbors(l_strength);
if (j > 0) tiles[i][j-1].processNeighbors(l_strength);
if (j < nbTilesH-1) tiles[i][j+1].processNeighbors(l_strength);
if (i > 0 && j > 0) tiles[i-1][j-1].processNeighbors(l_strength);
if (i < nbTilesW-1 && j > 0) tiles[i+1][j-1].processNeighbors(l_strength);
if (i > 0 && j < nbTilesH-1) tiles[i-1][j+1].processNeighbors(l_strength);
if (i < nbTilesW-1 && j < nbTilesH-1) tiles[i+1][j+1].processNeighbors(l_strength);
fill(color(map(r.x, 0, PI/4, 210, 0), map(r.y, 0, PI/4, 210, 0), 210));
box(tileW, tileH, min(tileW, tileH)/2);
othersImpact = new PVector(0, 0);