
numberImages = 8;
headDiameter = 100;
elastic = 1;


imageArray = new Array();

function hypot(dx, dy){
	return Math.sqrt(dx*dx + dy*dy);
	}

function setupHeads() {
for (i=0; i < numberImages; i++){
	curImg = document.images[i];
	curImg.style.left = curImg.xval = (Math.random() * document.body.clientWidth);
	curImg.style.top = curImg.yval = (Math.random() * document.body.clientHeight);
	curImg.velx = (Math.random() * 11)-5;
	curImg.vely = (Math.random() * 11)-5;
	curImg.mass = 1;
	}
moveHeads();
}

function moveHeads() {
	for (i=0; i < numberImages; i++){
		curImg = document.images[i];

		//STORE OLD POSITIONS
		curImg.oldX = curImg.xval;
		curImg.oldY = curImg.yval;

		//ADDS VELOCITIES
		curImg.xval += curImg.velx;
		curImg.yval += curImg.vely;
	}
	for (i=0; i < numberImages; i++){
		curImg = document.images[i];

		//CHECK FOR COLLISIONS BETWEEN HEADS
		for (j=i+1; j<numberImages; j++){
			curImg2 = document.images[j];
			d1 = hypot(curImg2.xval-curImg.xval, curImg2.yval-curImg.yval);
			d0 = hypot(curImg2.oldX-curImg.oldX, curImg2.oldY-curImg.oldY);
			
			if(d1<headDiameter && d0>headDiameter){
				t = (d0 - headDiameter) / (d0 - d1);
				
				xt1 = curImg.oldX + curImg.velx * t;
				yt1 = curImg.oldY + curImg.vely * t;
				
				xt2 = curImg2.oldX + curImg2.velx * t;
				yt2 = curImg2.oldY + curImg2.vely * t;
				
				//FIND UNIT NORMAL VECTOR
				nx = (xt1 - xt2) / headDiameter;
				ny = (yt1 - yt2) / headDiameter;
				
				//FIND UNIT TANGENT VECTOR
				tx = -ny;
				ty = nx;
				
				dvx = curImg.velx - curImg2.velx;
				dvy = curImg.vely - curImg2.vely;
				
				//FIND COMPONENT OF VELOCITY IN THE NORMAL AND TANGENT
				n1 = nx * curImg.velx + ny * curImg.vely;
				t1 = tx * curImg.velx + ty * curImg.vely;
				
				n2 = nx * curImg2.velx + ny * curImg2.vely;
				t2 = tx * curImg2.velx + ty * curImg2.vely;
				
				//CALCULATE THE NEXT VELOCITY WITH RESPECT TO THE NORMAL
				newVN1 = (curImg.mass * n1 + curImg2.mass * n2 - elastic * curImg2.mass * n1 + elastic * curImg2.mass * n2)/(curImg.mass + curImg2.mass);
				newVN2 = (curImg2.mass * n2 + curImg.mass * n1 - elastic * curImg.mass * n2 + elastic * curImg.mass * n1)/(curImg.mass + curImg2.mass);
				
				//CALCULATE THE NEW VELOCITIES
				curImg.velx = tx * t1 + newVN1 * nx;
				curImg.vely = ty * t1 + newVN1 * ny;
				curImg2.velx = tx * t2 + newVN2 * nx;
				curImg2.vely = ty * t2 + newVN2 * ny;
				
				//FIND POSITIONS AT END OF TIME
				curImg.xval = xt1 + curImg.velx *(1 - t);
				curImg.yval = yt1 + curImg.vely *(1 - t);
				curImg2.xval = xt2 + curImg2.velx *(1 - t);
				curImg2.yval = yt2 + curImg2.vely *(1 - t);
				}
				 
		}

		//EDGE COLLISIONS
		if (curImg.xval<0){ 
			curImg.xval *= -1;
			curImg.velx *= -1;
		}

		if (curImg.yval<0){
			curImg.yval *= -1;
			curImg.vely *= -1;
		}

		if (curImg.xval>document.body.clientWidth-(headDiameter + 20)){
			curImg.xval = 2* (document.body.clientWidth-(headDiameter + 20)) - curImg.xval;
			curImg.velx *= -1;
		}

		if (curImg.yval>document.body.clientHeight-(headDiameter + 20)){
			curImg.yval = 2* (document.body.clientHeight-(headDiameter + 20)) - curImg.yval;
			curImg.vely *= -1;

		}

		//SETS POSITIONS
		curImg.style.left = curImg.xval;
		curImg.style.top = curImg.yval;
		}
setTimeout("moveHeads()",30);
}


