forked from: forked from: forked from: バネる (質量値を追加してみたらプルプルになった。)
package{
import flash.display.*;
public class BaneBane extends Sprite{
private var n:uint = 10;
private var springConst:Number = 0.2;//0.3;
private var damp:Number = 0.6;//0.8;
function BaneBane(){
for(var i:uint=0; i<n; i++){
var myX:Number = 40 * i + 40;//Math.random() * stage.stageWidth;
var myY:Number = stage.stageHeight/2;
var myZ:Number = new Number(Math.random()*1000);
var mass:Number = Math.random() + 0.1;
var ball:Ball = new Ball(springConst,damp,myX+20,myY+10,myZ,mass);
stage.addChild(ball);
var ball_1:Ball = new Ball(springConst,damp,myX+30,myY+20,myZ,mass);
stage.addChild(ball_1);
var ball_2:Ball = new Ball(springConst,damp,myX+40,myY+30,myZ,mass);
stage.addChild(ball_2);
var ball_3:Ball = new Ball(springConst,damp,myX+50,myY+40,myZ,mass);
stage.addChild(ball_3);
}
}
}
}
import flash.display.*;
import flash.events.*;
class Ball extends Sprite{
private var _sp:Number;
private var _damp:Number;
private var _mass:Number;
private var _myX:Number;
private var _myY:Number;
private var _tagX:Number;
private var _tagY:Number;
private var _xVal:Number;
private var _yVal:Number;
private var _limit:Number = 400;//125;
private var _expansion:Number; //= 50;
function Ball(s:Number,d:Number,tx:Number,ty:Number,myZ:Number,mass:Number){
_sp = s;
_damp = d;
this.x = _myX = _tagX = tx;
this.y = _myY = _tagY = ty;
this.z = myZ;
_xVal = _yVal = 0;
_mass = mass;
_expansion = _mass *300;
this.graphics.beginFill(0x00CCFF,1);
//_massの値によってballの大きさをかえる。
this.graphics.drawCircle(0,0,10);
this.graphics.endFill();
this.addEventListener(Event.ENTER_FRAME,onLoop,false,0,true);
}
private function onLoop(eventObj:Event):void {
var xx:Number = _tagX - stage.mouseX;
var yy:Number = _tagY - stage.mouseY;
var DIST:Number = Math.sqrt( xx * xx + yy * yy );
if(DIST < _limit){
var ANGLE:Number = Math.atan2( yy, xx );
var LENG:Number = (_expansion / (_limit * _limit)) * (DIST - _limit) * (DIST - _limit);
_myX = _tagX + LENG * Math.cos(ANGLE) * (1-(DIST/_limit)) * (1-(DIST/_limit)) * (1-(DIST/_limit)) * (1-(DIST/_limit));
_myY = _tagY + LENG * Math.sin(ANGLE) * (1-(DIST/_limit)) * (1-(DIST/_limit)) * (1-(DIST/_limit)) * (1-(DIST/_limit));
//this.scaleX = this.scaleY = LENG / (_expansion / _mass) * 30 + 1;
}else {
//this.scaleX = this.scaleY = 1;
_myX = _tagX;
_myY = _tagY;
}
_xVal = valElastic(this.x,_myX,_xVal);
_yVal = valElastic(this.y,_myY,_yVal);
this.x += _xVal;
this.y += _yVal;
}
private function valElastic(orig:Number,dist:Number,elas:Number):Number {
var force:Number = -_sp * (orig - dist);
var acceleration:Number = force / _mass;
return _damp * (elas + acceleration);
}
}