return Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2));
}
MovieClip.prototype.drawCircle = function(r, x, y) {
this.moveTo(x+r, y);
a = Math.tan(22.5 * Math.PI/180);
for (var angle = 45; angle<=360; angle += 45) {
// endpoint:
var endx = r*Math.cos(angle*Math.PI/180);
var endy = r*Math.sin(angle*Math.PI/180);
// control:
// (angle-90 is used to give the correct sign)
var cx =endx + r*a*Math.cos((angle-90)*Math.PI/180);
var cy =endy + r*a*Math.sin((angle-90)*Math.PI/180);
this.curveTo(cx+x, cy+y, endx+x, endy+y);
}
}
onLoad = function(){
_root.circle_size = 15;
_root.number_of_circles = 15;
_root.circles = new Array();
_root.count = 0;
var c80 = this.createEmptyMovieClip("box", 100);
c80.moveTo(2,77);
c80.lineStyle(3, 0x000000, 100);
c80.lineTo(548,77);
c80.lineTo(548,398);
c80.lineTo(2,398);
c80.lineTo(2,77);
for(i =0; i < _root.number_of_circles; i++){
var c80 = this.createEmptyMovieClip("c"+i, i);
c80.lineStyle(3, 0xaaaaaa, 100);
c80.beginFill(0xcccccc, 100);
xpos = (25+Math.random()*500);
ypos = (100+Math.random()*275);
xvel = (Math.random()*10 - 5);
yvel = (Math.random()*10 - 5);
c80.drawCircle(_root.circle_size, xpos, ypos);
c80.endFill();
_root.circles.push([xpos,ypos,xvel,yvel]);
}
}
onEnterFrame = function(){
for(i =0; i < _root.number_of_circles; i++){
var c80 = this.createEmptyMovieClip("c"+i, i);
c80.lineStyle(3, 0xaaaaaa, 100);
c80.beginFill(0xcccccc, 100);
current = _root.circles[i];
current[0] = current[0] + current[2];
current[1] = current[1] + current[3];
xpos = current[0];
ypos = current[1];
xvel = current[2];
yvel = current[3];
if(xpos < _root.circle_size+5 and xvel < 0){
xvel = xvel*-1;
}
if(ypos < _root.circle_size+80 and yvel < 0){
yvel = yvel*-1;
}
if(xpos > 545-_root.circle_size and xvel > 0){
xvel = xvel*-1;
}
if(ypos > 395-_root.circle_size and yvel > 0){
yvel = yvel*-1;
}
for(j = i+1; j < (_root.number_of_circles); j++){
vy1 = yvel;
vx1 = xvel;
vx2 = _root.circles[j][2];
vy2 = _root.circles[j][3];
x1 = xpos;
y1 = ypos;
x2 = _root.circles[j][0];
y2 = _root.circles[j][1];
d = distance(x1,y1,x2,y2);
d2 = distance(x1+vx1*2,y1+vy1*2,x2+vx2*2,y2+vy2*2);
d3 = distance(x1-vx2,y1-vy1,x2,y2);
r12 = (_root.circle_size*2);
if(d < r12 and d2 < r12){
vy1 = yvel;
vx1 = xvel;
vx2 = _root.circles[j][2];
vy2 = _root.circles[j][3];
x1 = xpos;
y1 = ypos;
x2 = _root.circles[j][0];
y2 = _root.circles[j][1];
gammav=Math.atan(vy1-vy2,vx1-vx2);
gammaxy=Math.atan(y2-y1,x2-x1);
dr=d*Math.sin(gammaxy-gammav)/r12;
// **** calculate impact angle if balls do collide ***
alpha=Math.asin(dr);
// *** update velocities ***
a=Math.tan( Math.atan(vy1-vy2,vx1-vx2) +alpha);
dvx2=2*(vx1-vx2 +a*(vy1-vy2)) /((1+a*a)*(2));
_root.circles[j][2] = vx2+dvx2;
_root.circles[j][3] = vy2+a*dvx2;
xvel=vx1-dvx2;
yvel=vy1-a*dvx2;
}
}
c80.drawCircle(_root.circle_size, xpos, ypos);
c80.endFill();
_root.circles[i] = [xpos,ypos,xvel,yvel];
}
}
©2003-2008 Kris Brower All Rights Reserved
Bottom of the Page Quick Links: Soccer Squared Free Web Hosting Search
Bottom of the Page Quick Links: Soccer Squared Free Web Hosting Search