USI
Flash Math & Physics Design:ActionScript 3.0による数学・物理学表現[入門編]
* と、wonderful内のSaqooshaさんの「Snow」を参考、というか、そのまま組み合わせただけです。
*
* http://www.sbcr.jp/books/products/detail.asp?sku=4797351415
* http://wonderfl.kayac.com/code/71344f9a655053d9f793a32c68f00921c67f1977
*
/*
* Flash Math & Physics Design:ActionScript 3.0による数学・物理学表現[入門編]
* と、wonderful内のSaqooshaさんの「Snow」を参考、というか、そのまま組み合わせただけです。
*
* http://www.sbcr.jp/books/products/detail.asp?sku=4797351415
* http://wonderfl.kayac.com/code/71344f9a655053d9f793a32c68f00921c67f1977
*
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.display.PixelSnapping;
import flash.display.BlendMode;
import flash.geom.Point;
[SWF(width=465, height=465, backgroundColor=0x0, frameRate=120)]
public class FuwaFuwa extends Sprite
{
private static const DRAG:Number = 0.3;
private static const MAX_DIST:Number = 60;
private var _ppt :Array;
private var _loc :Array;
private var _canvas :BitmapData;
private var _particles:Array;
private static const _yokoNum :uint = 30;
private static const _tateNum :uint = 20;
private static const _interval :uint = 5;
private var _num :Number = 0;
private var _forceMap:BitmapData;
public function FuwaFuwa()
{
_canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x0);
this.addChild(new Bitmap(_canvas)) as Bitmap;
_particles = [];
_ppt = [];
_loc = [];
_num = _yokoNum * _tateNum;
for (var i:uint = 0; i < _tateNum; ++i)
{
for (var j:uint = 0; j < _yokoNum; ++j)
{
var yokoLoc:Number = this.stage.stageWidth / 2-(_yokoNum - 1)*_interval/2 + j * _interval;
var tateLoc:Number = this.stage.stageHeight / 2-(_tateNum - 1)*_interval/2 + i * _interval;
_ppt.push(new PhysicalPoint(yokoLoc, tateLoc));
_loc.push(new Point(yokoLoc, tateLoc));
}
}
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat('Verdana', 80, 0xffffff, true);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = 'うし';
tf.x = (this.stage.stageWidth - tf.width) / 2;
tf.y = (this.stage.stageHeight - tf.height) / 2;
this._forceMap = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x0);
this._forceMap.draw(tf, tf.transform.matrix);
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
var d:Number;
_canvas.lock();
_canvas.fillRect(_canvas.rect, 0x0);
var dist:Number;
for (var i:uint = 0; i < _ppt.length; i++)
{
_ppt[i].setKasokudo((_loc[i].x - _ppt[i].x) * 50, (_loc[i].y - _ppt[i].y) * 50);
dist = Point.distance(new Point(this.mouseX, this.mouseY), _ppt[i]);
if (dist < MAX_DIST)
{
var par:Number = (MAX_DIST - dist) / MAX_DIST;
d = 1 - (this._forceMap.getPixel(_ppt[i].x, _ppt[i].y) / 0xffffff) * DRAG;
if (d != 1)
{
par = par * (d / 6);
}
_ppt[i].setKasokudo((_ppt[i].x - this.mouseX) * par * 80, (_ppt[i].y - this.mouseY) * par * 80);
}
_canvas.setPixel(_ppt[i].x, _ppt[i].y, 0x666666);
}
_canvas.unlock();
}
}
}
class PixelParticle {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var s:Number;
public var c:int;
public function PixelParticle() {
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
this.s = 1;
this.c = 0xffffff;
}
}
import flash.geom.Point;
import flash.events.TimerEvent;
import flash.utils.Timer;
class PhysicalPoint extends Point
{
public var vx :Number;
public var vy :Number;
private var ax :Number;
private var ay :Number;
public var b :Number;
public var sakkiTime:Number;
public var timer:Timer;
public function PhysicalPoint(xx:Number = 0, yy:Number = 0 )
{
x = xx;
y = yy;
b = 0.8;
vx = 0;
vy = 0;
ax = 0;
ay = 0;
sakkiTime = new Date().getTime();
timer = new Timer(33);
timer.addEventListener(TimerEvent.TIMER, loop);
timer.start();
}
private function loop(e:TimerEvent):void
{
var nowTime:Number = new Date().getTime();
var t:Number = (nowTime - sakkiTime) / 1000;
x += vx * t + 0.5 * ax * t * t;
y += vy * t + 0.5 * ay * t * t;
vx += ax * t;
vy += ay * t;
vx *= b;
vy *= b;
ax = 0;
ay = 0;
sakkiTime = nowTime;
}
public function setKasokudo(aax:Number = 0, aay:Number = 0):void
{
ax += aax;
ay += aay;
}
}