Frogspawn(カエルの卵的ななにか)
Ballの回転と移動
// forked from demouth's flash on Ballの回転
// forked from demouth's Ballの回転
//Ballの回転と移動
// write as3 code here..
package
{
import flash.filters.GlowFilter;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]
public class Main extends Sprite
{
private var points:Vector.<Ball>; //Balls
private static const POINT_NUM:int = 100; //Ballの数
private static const RANGE_X:int = 200; //Ball範囲
private static const RANGE_Y:int = 200; //Ball範囲
private static const RANGE_Z:int = 200; //Ball範囲
private var rotateX:Number = 0;
private var rotateY:Number = 0;
private var rotateZ:Number = 0;
private var isMouseDown:Boolean = false;
private var oldMouseX:Number;
private var oldMouseY:Number;
public function Main()
{
this.init();
}
private function init():void
{
this.createPoints(); //Ball作成
this.rotateX = 10;
this.rotateY = 10;
this.filters = [new GlowFilter(0,1,30,30,50,3)];
this.addEventListener(Event.ENTER_FRAME , onEnterFrame);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN , mouseDownHandler);
this.stage.addEventListener(MouseEvent.MOUSE_UP , mouseUpHandler);
}
private function mouseUpHandler(e:MouseEvent):void
{
this.isMouseDown = false;
this.oldMouseX = NaN;
this.oldMouseY = NaN;
}
private function mouseDownHandler(e:MouseEvent):void
{
this.isMouseDown = true;
}
private function onEnterFrame(e:Event):void
{
this.rotate(); //回転
this.startMove(); //Ball移動の目標地点変更
this.move(); //Ballの移動
this.draw(); //Ballを描画
}
/**
* 回転する
*/
private function rotate():void
{
var f:Number = 0.98;
this.rotateX *= f;
this.rotateY *= f;
//if ( this.isMouseDown )
//{
if ( this.oldMouseX )
{
this.rotateX += ( this.stage.mouseY - this.oldMouseY ) * 0.08;
this.rotateY += ( this.stage.mouseX - this.oldMouseX ) * 0.08;
}
this.oldMouseX = this.stage.mouseX;
this.oldMouseY = this.stage.mouseY;
//}
var pow:Number = ( Math.abs(this.rotateX) + Math.abs(this.rotateY) ) * 0.05 + 1;
this.scaleX = this.scaleY = pow;
var mat:Matrix3D = new Matrix3D();
mat.appendRotation(this.rotateX, Vector3D.X_AXIS);
mat.appendRotation(-this.rotateY, Vector3D.Y_AXIS);
mat.appendRotation((pow-1)*2 , Vector3D.Z_AXIS);
this.x = this.stage.stageWidth / 2;
this.y = this.stage.stageHeight / 2;
this.z = 0;
var i:int;
for (i = 0; i < POINT_NUM; i++)
{
var point:Ball = this.points[i];
var vectorPoint:Vector3D = mat.transformVector(point as Vector3D);
point.x = vectorPoint.x;
point.y = vectorPoint.y;
point.z = vectorPoint.z;
}
}
/**
* Ballを作る
*/
private function createPoints():void
{
var i:int;
this.points = new Vector.<Ball>();
for (i = 0; i < POINT_NUM; i++)
{
var x:int = (Math.random() * RANGE_X) - RANGE_X / 2;
var y:int = (Math.random() * RANGE_Y) - RANGE_Y / 2;
var z:int = (Math.random() * RANGE_Z) - RANGE_Z / 2;
var point:Ball = new Ball();
point.x = x;
point.y = y;
point.z = z;
point.radius = 1;
point.initTween(); //Tweenオブジェクトを生成
this.points.push(point);
}
}
/**
* Ballの目標地点を変更する
*/
private function startMove():void
{
var i:int;
for (i = 0; i < this.points.length; i++)
{
if (Math.random() > 0.999)
{
var point:Ball = this.points[i];
var x:int = (Math.random() * RANGE_X) - RANGE_X / 2;
var y:int = (Math.random() * RANGE_Y) - RANGE_Y / 2;
var z:int = (Math.random() * RANGE_Z) - RANGE_Z / 2;
point.tweenObj.setTargetPoint(x,y,z);
}
}
}
/**
* Ballを目標地点へ移動する
*/
private function move():void
{
var i:int;
for (i = 0; i < this.points.length; i++)
{
var point:Ball = this.points[i];
point.tweenObj.move();
}
}
/**
* Ballを表示する
*/
private function draw():void
{
this.graphics.clear();
//Zソートする
this.points = this.points.sort(compare);
//描画する
var i:int;
for (i = 0; i < this.points.length; i++)
{
var point:Ball = this.points[i];
//色
var color8:int = int((-point.z + (RANGE_Z)) * 0.6);
var color:int = color8 << 16 | color8 << 8 | color8;
if ( color < 0 ) color = 0;
//直径
var radius:Number = ((point.z * point.radius) * 0.6 - (RANGE_Z / 2)) * 0.1;
this.graphics.lineStyle( Math.abs(0.8*radius), color, 1);
this.graphics.beginFill(0, 1);
this.graphics.drawCircle(point.x, point.y, radius);
this.graphics.endFill();
}
}
/**
* Zソート
*/
private function compare( x:Ball, y:Ball ):Number
{
return y.z - x.z;
}
}
}
/**
* Tweenクラスを持つ
*/
import flash.geom.Vector3D;
class Ball extends Vector3D
{
public var tweenObj:Tween;
public var radius:Number = 0;
public function initTween():void
{
this.tweenObj = new Tween(this);
}
}
/**
* Tweenクラス
*/
import flash.utils.Timer;
class Tween
{
//移動する対象
private var vector3dObj:Ball;
//Tweenタイマー
private var timer:Timer = new Timer(10 * 1000, 1);
//目標地点
private var targetX:Number = 0;
private var targetY:Number = 0;
private var targetZ:Number = 0;
//速度
private var speedX:Number = 0;
private var speedY:Number = 0;
private var speedZ:Number = 0;
//加速度
private var acX:Number = 0;
private var acY:Number = 0;
private var acZ:Number = 0;
private static const FRICTION:Number = 0.95;
private static const FIRST_VELOCITY:int = 20;
public function Tween(vec:Ball)
{
this.vector3dObj = vec;
this.targetX = this.vector3dObj.x;
this.targetY = this.vector3dObj.y;
this.targetZ = this.vector3dObj.z;
}
public function setTargetPoint(x:int, y:int, z:int):void
{
if (this.timer.running)
{
//タイマー動いていたら
this.timer.stop();
}
this.timer.reset();
//目標地点セット
this.targetX = x;
this.targetY = y;
this.targetZ = z;
//初速セット
this.speedX = (Math.random() * FIRST_VELOCITY) - FIRST_VELOCITY / 2;
this.speedY = (Math.random() * FIRST_VELOCITY) - FIRST_VELOCITY / 2;
this.speedZ = (Math.random() * FIRST_VELOCITY) - FIRST_VELOCITY / 2;
this.timer.start();
}
public function move():void
{
//タイマー動いていたら
if (this.timer.running)
{
//目標地点までの差
var difX:Number = this.targetX - this.vector3dObj.x;
var difY:Number = this.targetY - this.vector3dObj.y;
var difZ:Number = this.targetZ - this.vector3dObj.z;
this.speedX += difX * 0.01;
this.speedY += difY * 0.01;
this.speedZ += difZ * 0.01;
this.speedX *= FRICTION;
this.speedY *= FRICTION;
this.speedZ *= FRICTION;
this.vector3dObj.x += this.speedX;
this.vector3dObj.y += this.speedY;
this.vector3dObj.z += this.speedZ;
}
}
}