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

[Stardust] + [QuickBox2d]

Box2D+Stardust
@author paq89
/**
 * Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7LXU
 */

// forked from paq's [Stardust] + [Box2D]
package 
{
	import flash.display.MovieClip;
	import Box2D.Collision.b2AABB;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2World;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import idv.cjcat.stardust.common.clocks.SteadyClock;
	import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
	import com.actionsnippet.qbox.QuickBox2D;
	
	/**
	 * Box2D+Stardust
	 * @author paq89
	 */
	[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]
	public class Main extends MovieClip 
	{
		static private var DRAW_SCALE:Number = 100;
		
		private var container:Sprite;
		private var emitter:Box2DEmitter;
		private var world:b2World;
		
		/**
		 * コンストラクタ
		 */
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			// 背景を作成
			var background:Sprite = new Sprite();
			var g:Graphics = background.graphics;
			g.beginFill(0x000000);
			g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			addChild(background);
			container = new Sprite();
			addChild(container);
			
			// QuickBox2D
			quickBox2DInit();
			
			// Stardust
			stardustInit();
			
			// イベントリスナーの登録
			addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(e:Event):void 
		{
			// エミッターの更新
			emitter.step();
			
			// Box2Dを更新
			world.Step(1 / 24, 10);
		}
		
		private function stardustInit():void 
		{
			emitter = new Box2DEmitter(new SteadyClock(0.1), world)
			var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(container);
			renderer.addEmitter(emitter);
		}
		
		
		private function quickBox2DInit():void
		{
			var sim:QuickBox2D = new QuickBox2D(this ,{debug:true});
			sim.createStageWalls();
			world = sim.w;
		}
	}
	
}

import Box2D.Collision.Shapes.b2CircleDef;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import idv.cjcat.stardust.common.actions.Age;
import idv.cjcat.stardust.common.actions.CompositeAction;
import idv.cjcat.stardust.common.actions.DeathLife;
import idv.cjcat.stardust.common.actions.ScaleCurve;
import idv.cjcat.stardust.common.actions.triggers.DeathTrigger;
import idv.cjcat.stardust.common.actions.triggers.LifeTrigger;
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.initializers.Life;
import idv.cjcat.stardust.common.initializers.Mask;
import idv.cjcat.stardust.common.initializers.Scale;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.Gravity;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.fields.UniformField;
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.Line;
import idv.cjcat.stardust.twoD.zones.SinglePoint;

class Box2DEmitter extends Emitter2D
{
	public function Box2DEmitter(clock:Clock, world:b2World)
	{
		super(clock);
		
		var doc:DisplayObjectClass = new DisplayObjectClass();
		doc.displayObjectClass = Circle;
		doc.constructorParams = [world, false];
		
		addInitializer(doc);
		var margin:uint = 50;
		addInitializer(new Position(new Line(margin, margin, 1200, margin)));
		addInitializer(new Life(new UniformRandom(200, 0)));
		addInitializer(new Scale(new UniformRandom(3, 3)));
		
		var commonActions:CompositeAction = new CompositeAction();
		commonActions.mask = 1;
		commonActions.addAction(new Age());
		commonActions.addAction(new DeathLife());
		commonActions.addAction(new ScaleCurve(5, 20));
		
		addAction(commonActions);
	}
}

class Circle extends Sprite
{
	static private var DRAW_SCALE:Number = 100;
	
	private var _world:b2World;
	private var _circleBody:b2Body;
	private var _circleShape:b2CircleShape;
	private var _scaleX:Number;
	private var _x:Number;
	private var _y:Number;
	private var _move:Boolean;
	
	public function Circle(world:b2World, move:Boolean = false):void 
	{
		super();
		
		_world = world;
		_move = move;
		
		var circleBodyDef:b2BodyDef = new b2BodyDef(); 
		circleBodyDef.position.Set(x / DRAW_SCALE, y / DRAW_SCALE); 
		
		var circleShapeDef:b2CircleDef = new b2CircleDef();  
		circleShapeDef.radius = 0.001;  
		circleShapeDef.density = 1;  
		circleShapeDef.friction = 0.5;  
		circleShapeDef.restitution = 0.5;  
		
		_circleBody = world.CreateBody(circleBodyDef);  
		_circleShape = _circleBody.CreateShape(circleShapeDef) as b2CircleShape;  
		_circleBody.SetMassFromShapes();  
		
		addEventListener(Event.REMOVED, onRemoved );
		addEventListener(Event.ENTER_FRAME, onEnterFrame)
	}
	
	private function onEnterFrame(e:Event):void 
	{
		if (_move) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, y / DRAW_SCALE), rotation);
		if (_scaleX != scaleX) _circleShape.m_radius = scaleX / 10;
		if (_x != x) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, _circleBody.GetPosition().y), rotation);
		if (_y != y) _circleBody.SetXForm(new b2Vec2(_circleBody.GetPosition().x, y / DRAW_SCALE), rotation);
		_scaleX = scaleX;
		_x = x;
		_y = y;
	}
	
	private function onRemoved(e:Event):void 
	{
		removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		removeEventListener(Event.REMOVED, onRemoved);
		_world.DestroyBody(_circleBody);
	}
}