hardshell=1.5 NumberOfSquares=4;
mutual attraction: proportional 1/d^2, but only if opposite direction
initially all direction downwards
for (j=0; j<NumberOfSquares; j++){ ..... if(j!=SquareNum){ ..... if ( ( Math.abs(x1+w1/2-(x2+w2/2)) < hardshell * (w2+w1)/2 ) && ( Math.abs(y1+h1/2-(y2+h2/2)) < hardshell * (h2+h1)/2 ) ) { } else { if ((gravX[SquareNum] == - gravX[j]) && (gravY[SquareNum] == - gravY[j])) { distanceSquare=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); m1=mass[SquareNum]; m2=mass[j]; attractiveForce=m1*m2*attraction/distanceSquare; distance=Math.sqrt(distanceSquare); xspeed[SquareNum]+= - attractiveForce/m1 * (x1-x2)/distance; yspeed[SquareNum]+= - attractiveForce/m1 * (y1-y2)/distance; xspeed[j] += - attractiveForce/m2 * (x2-x1)/distance; yspeed[j] += - attractiveForce/m2 * (y2-y1)/distance; } } } }