In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 重力マウス(10万パーティクル int高速化)

リンクリストにしてみたけどそんなに速くない??
_bmd.fillRect()を_bmd.setPixel()に変更。
sin(),cos(),atan2(),sqrt()を排除。
Add final class / mouseEnalbled = false by clockmaker
CJ Cat: Used vector instead of linked list.
clockmaker: while文の処理を4回コピペしてイテレーションの回数を減らす 100万で30fps (FP10.1 Release))
if文をループの外に展開
vectorをintに
精度を上げて、ループ
/**
 * Copyright h_sakurai ( http://wonderfl.net/user/h_sakurai )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hkKz
 */

// forked from h_sakurai's 重力マウス(100万パーティクル int高速化)
// forked from clockmaker's 重力マウス(100万パーティクル)
// forked from cjcat2266's 重力マウス(プチ軽量化:25万パーティクル)
// forked from clockmaker's 重力マウス(プチ軽量化:10万パーティクル)
// forked from coppieee's 重力マウス(さらに軽量化してみた)
// forked from paq's forked from: 重力マウス(ちょっぴり軽量化してみた)
// forked from fumix's 重力マウス(リンクリストにしてみた)
// forked from undo's 重力マウス
// リンクリストにしてみたけどそんなに速くない??
//_bmd.fillRect()を_bmd.setPixel()に変更。
//sin(),cos(),atan2(),sqrt()を排除。
// Add final class / mouseEnalbled = false by clockmaker
// CJ Cat: Used vector instead of linked list.
// clockmaker: while文の処理を4回コピペしてイテレーションの回数を減らす 100万で30fps (FP10.1 Release))
// if文をループの外に展開
// vectorをintに
// 精度を上げて、ループ
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import net.hires.debug.Stats;

	[SWF(frameRate='48', width='465', height='465', backgroundColor='0x0')]

	public class Main extends Sprite {
		private var _bmp:Bitmap;
		private var _bmd:BitmapData;
		private var _bmdRect:Rectangle;
		private var _colorTransform:ColorTransform = new ColorTransform(0.9, 0.97, 0.9, 1.0);
		private const _maxNum:int = 100000;
		private var _cnt:int = 0;
		private var _rnd:uint = 431431;		
		private const _particleX:Vector.<int> = new Vector.<int>(_maxNum, true);
		private const _particleY:Vector.<int> = new Vector.<int>(_maxNum, true);
		private const _particleVx:Vector.<int> = new Vector.<int>(_maxNum, true);
		private const _particleVy:Vector.<int> = new Vector.<int>(_maxNum, true);
		

		public function Main() {
			if(stage) init(null);
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.quality = "low";
			this.mouseEnabled = false;
			this.mouseChildren = false;
		
			_bmd = new BitmapData(465, 465, false, 0x000000);
			_bmp = new Bitmap(_bmd);
			addChild(_bmp);
			this._bmdRect = _bmd.rect;
			
			var i0:int;
			i0 = 0;
			while (i0 < _maxNum) {
				
				_particleX[i0] = (Math.random() * 465-_x) << 23;
				_particleY[i0] = (Math.random() * 465-_x) << 23;
				_particleVx[i0] = 0;
				_particleVy[i0] = 0;
				
				++i0;
			}
			
			addChild(new Stats());
			addEventListener(Event.ENTER_FRAME, onEnter);
		}
		private const _x:uint=256;
		
		
		private function onEnter(evt:Event):void {
			var gravPoint_x:int = (mouseX-_x)<< 22;
			var gravPoint_y:int = (mouseY-_x)<< 22;
			var calc:int = _cnt % 4;
			var _bmd:BitmapData = this._bmd;
			_bmd.lock();
			
			var _particleX:Vector.<int> = this._particleX;
			var _particleY:Vector.<int> = this._particleY;
			var _particleVx:Vector.<int> = this._particleVx;
			var _particleVy:Vector.<int> = this._particleVy;
			
			
			var diff_x:int, diff_y:int, acc:Number, acc_x:Number, acc_y:Number;
			var i0:int,l3:int, j0:int, k0:int, l0:int;
			i0 = 0;
			var p:int=0;
			for(;p<4;p++) {
				var m:int = _maxNum / 4 * (p+1);
			if(calc==p){
				while (i0 < m) {
					j0 = ++i0;
					k0 = ++j0;
					l0 = ++k0;
						
					diff_x = gravPoint_x - (_particleX[i0]>>1);
					diff_y = gravPoint_y - (_particleY[i0]>>1);
					acc = (1717986918400.0*2000.0) / (diff_x * diff_x + diff_y * diff_y);
					acc_x = acc * diff_x;
					acc_y = acc * diff_y;
					_particleVx[i0] += acc_x;
					_particleVy[i0] += acc_y;
					
					diff_x = gravPoint_x - (_particleX[j0]>>1);;
					diff_y = gravPoint_y - (_particleY[j0]>>1);
					acc = (1717986918400.0*2000.0) / (diff_x * diff_x + diff_y * diff_y);
					acc_x = acc * diff_x;
					acc_y = acc * diff_y;
					_particleVx[j0] += acc_x;
					_particleVy[j0] += acc_y;
					
					diff_x = gravPoint_x - (_particleX[k0]>>1);;
					diff_y = gravPoint_y - (_particleY[k0]>>1);
					acc = (1717986918400.0*2000.0)/ (diff_x * diff_x + diff_y * diff_y);
					acc_x = acc * diff_x;
					acc_y = acc * diff_y;
					_particleVx[k0] += acc_x;
					_particleVy[k0] += acc_y;

					diff_x = gravPoint_x - _particleX[l0];;
					diff_y = gravPoint_y - _particleY[l0];
					acc = (1717986918400.0*2000.0) / (diff_x * diff_x + diff_y * diff_y);
					acc_x = acc * diff_x;
					acc_y = acc * diff_y;
					_particleX[l0] += acc_x;
					_particleY[l0] += acc_y;
					
				
					// 1 step
					_particleX[i0] += _particleVx[i0];
					_particleY[i0] += _particleVy[i0];
					_bmd.setPixel(_x+(_particleX[i0] >> 23), _x+(_particleY[i0] >> 23), 0xffffff);
					
					
					// 2 step
					_particleX[j0] += _particleVx[j0];
					_particleY[j0] += _particleVy[j0];
					_bmd.setPixel(_x+(_particleX[j0] >> 23), _x+(_particleY[j0] >> 23), 0xffffff);
					
					
					// 3 step
					_particleX[k0] += _particleVx[k0];
					_particleY[k0] += _particleVy[k0];
					_bmd.setPixel(_x+(_particleX[k0] >> 23), _x+(_particleY[k0] >> 23), 0xffffff);
					
					
					// 4 step			
					_particleX[l0] += _particleVx[l0];
					_particleY[l0] += _particleVy[l0];
					_bmd.setPixel(_x+(_particleX[l0] >> 23), _x+(_particleY[l0] >> 23), 0xffffff);
					
					
					i0 = ++l0;
				}
			} else {
				while (i0 < m) {
					j0 = ++i0;
					k0 = ++j0;
					l0 = ++k0;
					

					// 1 step
					_particleX[i0] += _particleVx[i0];
					_particleY[i0] += _particleVy[i0];
					_particleVx[i0] -= _particleVx[i0]>>5;
					_particleVy[i0] -= _particleVy[i0]>>5;
					_bmd.setPixel(_x+(_particleX[i0] >> 23), _x+(_particleY[i0] >> 23), 0xffffff);
					
					
					// 2 step
					_particleX[j0] += _particleVx[j0];
					_particleY[j0] += _particleVy[j0];
					_particleVx[j0] -= _particleVx[j0]>>5;
					_particleVy[j0] -= _particleVy[j0]>>5;
					_bmd.setPixel(_x+(_particleX[j0] >> 23), _x+(_particleY[j0] >> 23), 0xffffff);
					
					
					// 3 step
					_particleX[k0] += _particleVx[k0];
					_particleY[k0] += _particleVy[k0];
					_particleVx[k0] -= _particleVx[k0]>>5;
					_particleVy[k0] -= _particleVy[k0]>>5;
					_bmd.setPixel(_x+(_particleX[k0] >> 23), _x+(_particleY[k0] >> 23), 0xffffff);
					
					
					// 4 step			
					_particleX[l0] += _particleVx[l0];
					_particleY[l0] += _particleVy[l0];
					_particleVx[l0] -= _particleVx[l0]>>5;
					_particleVy[l0] -= _particleVy[l0]>>5;
					_bmd.setPixel(_x+(_particleX[l0] >> 23), _x+(_particleY[l0] >> 23), 0xffffff);
					
					
					i0 = ++l0;
				}

			}
			}			
			_bmd.colorTransform(this._bmdRect, this._colorTransform);
			_bmd.unlock();
			
			_cnt++;
		}
	}
}