forked from: HunyaBall
Math & Physics DesignのTakakkeiを参考にしました。
動く頂点
元の形状
頂点数, 半径, 色
Stardust Particle Engine
Homepage
http://code.google.com/p/stardust-particle-engine/
PDF Manual
http://stardust-particle-engine.googlecode.com/svn/trunk/manual/Stardust%20Particle%20Engine%20Manual.pdf
/**
* Copyright karmawaffle ( http://wonderfl.net/user/karmawaffle )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uYd9
*/
// forked from matsu4512's HunyaBall
/*
Math & Physics DesignのTakakkeiを参考にしました。
*/
package{
import flash.display.Sprite;
import flash.events.Event;
[SWF(width=465, height=465, frameRate=30)]
public class HunyaBall extends Sprite{
private var ballList:Vector.<Ball> = new Vector.<Ball>;
private var N:int = 20;
public function HunyaBall(){
for(var i:int = 0; i < N; i++){
var ball:Ball = new Ball(30, Math.random()*20+40, Math.random()*0xFFFFFF, Math.random()*10-5, Math.random()*10-5);
ball.x = Math.random()*465;
ball.y = Math.random()*465;
addChild(ball);
ballList.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void{
for(var i:int = 0; i < N; i++){
var ball:Ball = ballList[i];
ball.update();
if(ball.x > 465+70) ball.x = -70;
else if(ball.x < -70) ball.x = 465+70;
if(ball.y > 465+70) ball.y = -70;
else if(ball.y < -70) ball.y = 465+70;
}
}
}
}
import flash.display.Sprite;
import flash.filters.DropShadowFilter;
import flash.geom.Point;
class Ball extends Sprite{
//動く頂点
private var vertexList:Vector.<Vertex> = new Vector.<Vertex>;
//元の形状
private var pointList:Vector.<Point> = new Vector.<Point>;
//頂点数, 半径, 色
private var vertex:int, radius: Number, color:uint;
private var preMouseX:Number, preMouseY:Number;
private var vx:Number, vy:Number;
private const D:Number = 200;
public function Ball(vertexNum:int, radius:Number, color:uint, vx:Number, vy:Number){
this.vertex = vertexNum; this.radius = radius; this.color = color; this.vx = vx; this.vy = vy;
for(var i:Number = 0; i < vertex; ++i){
pointList.push(new Point(0, 0));
vertexList.push(new Vertex(0, 0));
}
filters = [new DropShadowFilter(30, 45, 0x000000, 0.5, 50, 50)];
shape();
}
private function shape():void{
for(var i:Number = 0; i < pointList.length; ++i){
var k:Number = pointList.length/vertex;
var num:Number = Math.abs(k/2 - i % k);
var h:Number = radius*Math.cos(Math.PI*2/vertex/2);
var r: Number = h/Math.cos((Math.PI*2/vertex/2)*(num/(k/2)));
pointList[i].x = r*Math.sin(i/pointList.length*Math.PI*2);
pointList[i].y = -r*Math.cos(i/pointList.length*Math.PI*2);
}
}
private function draw(v:Vector.<Vertex>, c: uint):void{
graphics.clear();
if(v.length >= 3){
graphics.moveTo((v[v.length-1].x+v[0].x)/2, (v[v.length-1].y+v[0].y)/2);
graphics.beginFill(c);
for(var i:Number = 0; i < v.length; ++i)
graphics.curveTo(v[i % v.length].x, v[i % v.length].y, (v[i % v.length].x+v[(i+1) % v.length].x)/2, (v[i % v.length].y+v[(i+1) % v.length].y)/2);
graphics.endFill();
}
}
public function update():void{
for(var i:Number = 0; i < vertexList.length; ++i){
vertexList[i].update();
vertexList[i].setAccel((pointList[i].x - vertexList[i].x)*10, (pointList[i].y - vertexList[i].y)*10);
var dist:Number = Point.distance(new Point(mouseX, mouseY), vertexList[i]);
if(dist < D){
var par:Number = (D-dist)/D;
vertexList[i].setAccel((mouseX - preMouseX)*par*50, (mouseY - preMouseY)*par*50);
}
}
preMouseX = mouseX;
preMouseY = mouseY;
draw(vertexList, color);
x += vx;
y += vy;
}
}
class Vertex extends Point{
private var vx:Number, vy:Number, ax:Number, ay:Number;
private const friction:Number = 0.95;
private var preTime:Number;
public function Vertex(xx: Number=0, yy: Number=0){
x = xx; y = yy;
vx = vy = ax = ay = 0;
preTime = new Date().getTime();
}
public function update():void{
var nowTime:Number = new Date().getTime();
var t:Number = (nowTime - preTime)/1000;
x += vx*t + 0.5*ax*t*t;
y += vy*t + 0.5*ay*t*t;
vx += ax*t;
vy += ay*t;
vx *= friction;
vy *= friction;
ax = ay = 0;
preTime = nowTime;
}
public function setAccel(aax:Number=0, aay:Number=0):void{
ax += aax;
ay += aay;
/**
* Stardust Particle Engine
*
* Homepage
* http://code.google.com/p/stardust-particle-engine/
*
* PDF Manual
* http://stardust-particle-engine.googlecode.com/svn/trunk/manual/Stardust%20Particle%20Engine%20Manual.pdf
*/
package {
import com.bit101.components.PushButton;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import idv.cjcat.stardust.common.clocks.SteadyClock;
import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
[SWF(backgroundColor="#000000", frameRate=60)]
public class Main extends Sprite {
public static const RATE:Number = 1;
private var container:Sprite;
private var emitter:FireflyEmitter;
private var canvasBMP:Bitmap;
private var canvasBMPD:BitmapData;
private var glow:GlowFilter = new GlowFilter(0xFFCC00, 1, 8, 8, 5, 2, false, true);
public function Main():void {
//Stardust
//------------------------------------------------------------------------------------------------
//container
container = new Sprite();
addChild(container);
//emitter & renderer
emitter = new FireflyEmitter(new SteadyClock(RATE));
var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(container);
renderer.addEmitter(emitter);
//------------------------------------------------------------------------------------------------
//canvas BMP
//------------------------------------------------------------------------------------------------
canvasBMPD = new BitmapData(233, 233, false, 0);
canvasBMP = new Bitmap(canvasBMPD);
canvasBMP.scaleX = canvasBMP.scaleY = 2;
addChildAt(canvasBMP, 0);
//------------------------------------------------------------------------------------------------
//UI
//------------------------------------------------------------------------------------------------
//speed buttons
new PushButton(this, 10, 10, "0.25X", changeSpeed).setSize(60, 20);
new PushButton(this, 10, 30, "0.5X", changeSpeed).setSize(60, 20);
new PushButton(this, 10, 50, "1X", changeSpeed).setSize(60, 20);
new PushButton(this, 10, 70, "2X", changeSpeed).setSize(60, 20);
new PushButton(this, 10, 90, "4X", changeSpeed).setSize(60, 20);
addEventListener(Event.ENTER_FRAME, mainLoop);
//------------------------------------------------------------------------------------------------
}
private var point:Point = new Point(0, 0);
private var matrix:Matrix = new Matrix(0.5, 0, 0, 0.5);
private function mainLoop(e:Event):void {
container.filters = [glow];
canvasBMPD.lock();
canvasBMPD.fillRect(canvasBMPD.rect, 0);
canvasBMPD.draw(container, matrix);
canvasBMPD.unlock();
container.filters = [];
emitter.step();
}
private function changeSpeed(e:Event):void {
var btn:PushButton = e.target as PushButton;
switch (btn.label) {
case "0.25X":
emitter.stepTimeInterval = 0.25;
break;
case "0.5X":
emitter.stepTimeInterval = 0.5;
break;
case "1X":
emitter.stepTimeInterval = 1;
break;
case "2X":
emitter.stepTimeInterval = 2;
break;
case "4X":
emitter.stepTimeInterval = 4;
break;
}
}
}
}
//------------------------------------------------------------------------------------------------
import flash.display.Shape;
import idv.cjcat.stardust.common.actions.Age;
import idv.cjcat.stardust.common.actions.AlphaCurve;
import idv.cjcat.stardust.common.actions.DeathLife;
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.initializers.Life;
import idv.cjcat.stardust.common.initializers.Scale;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.DeathZone;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.actions.RandomDrift;
import idv.cjcat.stardust.twoD.actions.SpeedLimit;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.initializers.DisplayObjectClass;
import idv.cjcat.stardust.twoD.initializers.Position;
import idv.cjcat.stardust.twoD.initializers.Velocity;
import idv.cjcat.stardust.twoD.zones.LazySectorZone;
import idv.cjcat.stardust.twoD.zones.RectZone;
//------------------------------------------------------------------------------------------------
const FIREFLY_COLOR:uint = 0xFFCC00;
const FIREFLY_SPEED_AVG:Number = 0.7;
const FIREFLY_SPEED_VAR:Number = 0.6;
const FIREFLY_MAX_SPEED:Number = 1.5;
const FIREFLY_RANDOM_DRIFT:Number = 0.2;
const FIREFLY_LIFE_AVG:Number = 150;
const FIREFLY_LIFE_VAR:Number = 30;
const FIREFLY_SCALE_VAR:Number = 0.3;
const FIREFLY_RADIUS_AVG:Number = 2;
//------------------------------------------------------------------------------------------------
class FireflyEmitter extends Emitter2D {
public function FireflyEmitter(clock:Clock) {
super(clock);
//initializers
addInitializer(new DisplayObjectClass(Firefly));
addInitializer(new Life(new UniformRandom(FIREFLY_LIFE_AVG, FIREFLY_LIFE_VAR)));
addInitializer(new Position(new RectZone(0, 0, 465, 465)));
addInitializer(new Velocity(new LazySectorZone(FIREFLY_SPEED_AVG, FIREFLY_SPEED_VAR)));
addInitializer(new Scale(new UniformRandom(1, FIREFLY_SCALE_VAR)));
//actions
addAction(new Age());
addAction(new DeathLife());
addAction(new Move());
addAction(new AlphaCurve(40, 40));
addAction(new RandomDrift(FIREFLY_RANDOM_DRIFT, FIREFLY_RANDOM_DRIFT));
addAction(new SpeedLimit(FIREFLY_MAX_SPEED));
addAction(new DeathZone(new RectZone( -60, -60, 585, 585), true));
}
}
//------------------------------------------------------------------------------------------------
class Firefly extends Shape {
public function Firefly() {
graphics.beginFill(0xFFFFFF);
graphics.drawCircle(0, 0, FIREFLY_RADIUS_AVG);
}
}
}
}