var Scene = _root.createEmptyMovieClip("Scene", _root.getNextHighestDepth()); var speedVector = _root.createEmptyMovieClip("Vector", _root.getNextHighestDepth()); Scene.mouseIsDown = false; speedVector.lineStyle(1, 0x000000, 100); var startpos = new Array(); var endpos = new Array(); var Balls = new Array(); var oldmousex = 0; var oldmousey = 0; function spawnBall(spawnx:Number, spawny:Number, velx:Number, vely:Number) { var i = Balls.length; Balls[i] = _root.createEmptyMovieClip("Ball"+i, _root.getNextHighestDepth()); Balls[i].attachMovie("Ball", "mc_ball"+i, Balls[i].getNextHighestDepth()); Balls[i]._width = 20; Balls[i]._height = 20; Balls[i]._x = spawnx; Balls[i]._y = spawny; Balls[i].velocity = new Array(); Balls[i].accelleration = new Array(); Balls[i].velocity[0] = velx; Balls[i].velocity[1] = vely; Balls[i].mass = 10; Balls[i].accelleration[0] = velx; Balls[i].accelleration[1] = vely; Balls[i].onEnterFrame = function() { this.accelleration[0] = 0 / this.mass; this.accelleration[1] = 4 / this.mass; this.velocity[0] += this.accelleration[0]; this.velocity[1] += this.accelleration[1]; this._x += this.velocity[0]; this._y += this.velocity[1]; if(this._x > Stage.width - 10) { this._x = Stage.width - 10; this.velocity[0] *= -0.8; } if(this._x < 10) { this._x = 11; this.velocity[0] *= -0.6; } if(this._y > Stage.height - 10) { this._y = Stage.height - 10; this.velocity[0] *= 0.97; this.velocity[1] *= -0.8; } } } Scene.onMouseDown = function() { _root.startpos[0] = _xmouse; _root.startpos[1] = _ymouse; _root.speedVector.clear(); _root.speedVector.lineStyle(1, 0x000000, 100); _root.speedVector._alpha = 100; this.mouseIsDown = true; _root.speedVector.attachMovie("Start", "Start", 1, {_x: _xmouse, _y: _ymouse}); } Scene.onMouseUp = function() { _root.endpos[0] = _xmouse; _root.endpos[1] = _ymouse; var d1 = _root.startpos[0]-_root.endpos[0]; var d2 = _root.startpos[1]-_root.endpos[1]; var angle = (Math.atan2(d1, d2)) / Math.PI * 180; var distance = Math.sqrt(d1*d1 + d2*d2); if(angle < 0) angle = -angle; _root.spawnBall(_root.startpos[0], _root.startpos[1], -d1/10, -d2/10); this.mouseIsDown = false; } Scene.onMouseMove = function() { if(this.mouseIsDown == true) { _root.speedVector.clear(); _root.speedVector.lineStyle(1, 0x000000, 100); _root.speedVector.moveTo(startpos[0], startpos[1]); _root.speedVector.lineTo(_xmouse, _ymouse); /* _root.speedVector.createTextField("Speed", 2, _xmouse, _ymouse-20, 200, 20); var d1 = _root.startpos[0]-_xmouse; var d2 = _root.startpos[1]-_ymouse; var angle = (Math.atan2(d1, d2)) / Math.PI * 180; var distance = Math.sqrt(d1*d1 + d2*d2); if(angle < 0) angle = -angle; _root.speedVector.Speed.text = Math.round(angle) + '° ' + Math.round(distance/2) + 'px/s'; */ } } Scene.onEnterFrame = function() { if(this.mouseIsDown) { if(_root.speedVector._alpha < 100) _root.speedVector._alpha+=2; } else _root.speedVector._alpha-=5; if(_root.Balls.length > 1) { for(i=0; i < _root.Balls.length; i++) { for(j=i+1; j < _root.Balls.length; j++) { var dijx = _root.Balls[i]._x - _root.Balls[j]._x; var dijy = _root.Balls[i]._y - _root.Balls[j]._y; var djix = _root.Balls[j]._x - _root.Balls[i]._x; var djiy = _root.Balls[j]._y - _root.Balls[i]._y; if((dijx*dijx + dijy*dijy) < ((10+10) * (10+10))) // Hit { var hitangle = (Math.atan2(dijx, dijy)) / Math.PI * 180; var s1 = Math.sqrt(_root.Balls[i].velocity[0] * _root.Balls[i].velocity[0] + _root.Balls[i].velocity[1] * _root.Balls[i].velocity[1]); var s2 = Math.sqrt(_root.Balls[j].velocity[0] * _root.Balls[j].velocity[0] + _root.Balls[j].velocity[1] * _root.Balls[j].velocity[1]); //trace('s1: '+s1 + ' s2: '+s2); //trace('before: ix: '+_root.Balls[i].velocity[0] + ' iy: '+_root.Balls[i].velocity[1]); //trace('before: jx: '+_root.Balls[j].velocity[0] + ' jy: '+_root.Balls[j].velocity[1]); if(s1 > 0) { _root.Balls[i].velocity[0] = _root.Balls[i].velocity[0] / s1; _root.Balls[i].velocity[1] = _root.Balls[i].velocity[1] / s1; } if(s2 > 0) { _root.Balls[j].velocity[0] = _root.Balls[j].velocity[0] / s2; _root.Balls[j].velocity[1] = _root.Balls[j].velocity[1] / s2; } //trace('after: ix: '+_root.Balls[i].velocity[0] + ' iy: '+_root.Balls[i].velocity[1]); //trace('after: jx: '+_root.Balls[j].velocity[0] + ' jy: '+_root.Balls[j].velocity[1]); var dijl = Math.sqrt(dijx*dijx + dijy*dijy); var djil = Math.sqrt(djix*djix + djiy*djiy); if (dijl > 0) { dijx = dijx / dijl; dijy = dijy / dijl; } if(djil > 0) { djix = djix / djil; djiy = djiy / djil; } if(s1 > 0) { var scale = ((_root.Balls[i].velocity[0] * dijx) + (_root.Balls[i].velocity[1] * dijy)) * -2.0; _root.Balls[i].velocity[0] = _root.Balls[i].velocity[0] + (dijx * scale); _root.Balls[i].velocity[1] = _root.Balls[i].velocity[1] + (dijy * scale); } else { _root.Balls[i].velocity[0] = dijx; _root.Balls[i].velocity[1] = dijy; } if(s2 > 0) { var scale = ((_root.Balls[j].velocity[0] * djix) + (_root.Balls[j].velocity[1] * djiy)) * -2.0; _root.Balls[j].velocity[0] = _root.Balls[j].velocity[0] + (djix * scale); _root.Balls[j].velocity[1] = _root.Balls[j].velocity[1] + (djiy * scale); } else { _root.Balls[j].velocity[0] = djix; _root.Balls[j].velocity[1] = djiy; } //trace('before s3: ix: '+_root.Balls[i].velocity[0] + ' iy: '+_root.Balls[i].velocity[1]); //trace('before s3: jx: '+_root.Balls[j].velocity[0] + ' jy: '+_root.Balls[j].velocity[1]); var s3 = (s1 + s2) * 0.5; //trace('s3: '+s3); //trace('------'); _root.Balls[i].velocity[0] *= s3; _root.Balls[i].velocity[1] *= s3; _root.Balls[j].velocity[0] *= s3; _root.Balls[j].velocity[1] *= s3; var frac1 = s1 / (s1 + s2); var frac2 = s2 / (s1 + s2); var move1 = (10.1 - dijl) * frac1; var move2 = (10.1 - djil) * frac2; _root.Balls[i]._x += dijx * move1; _root.Balls[i]._y += dijy * move1; _root.Balls[j]._x += djix * move2; _root.Balls[j]._y += djiy * move2; //trace('after s3: ix: '+_root.Balls[i].velocity[0] + ' iy: '+_root.Balls[i].velocity[1]); //trace('after s3: jx: '+_root.Balls[j].velocity[0] + ' jy: '+_root.Balls[j].velocity[1]); } } } } }