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: [Stardust] + [Box2D]

Box2D+Stardust
@author paq89
Get Adobe Flash player
by mitata 12 Feb 2010
/**
 * Copyright mitata ( http://wonderfl.net/user/mitata )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lEQV
 */

// forked from paq's [Stardust] + [Box2D]
package 
{
	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;
	
	/**
	 * Box2D+Stardust
	 * @author paq89
	 */
	[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]
	public class Main extends Sprite 
	{
		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);
			
			// Box2D
			box2DInit();
			
			// 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(1), world)
			var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(container);
			renderer.addEmitter(emitter);
		}
		
		private function box2DInit():void 
		{
			// ワールドを作成
			var worldAABB:b2AABB = new b2AABB();
            worldAABB.lowerBound.Set(-100, -100);
            worldAABB.upperBound.Set(100, 100);
			
			// 重力
            var gravity:b2Vec2 = new b2Vec2(0, 9.8);
			world = new b2World(worldAABB, gravity, true);
			
			// 床を作成
			var floorBodyDef:b2BodyDef = new b2BodyDef();
            floorBodyDef.position.Set(465 * 0.5 / DRAW_SCALE, 465 / DRAW_SCALE);
            var floorShapeDef:b2PolygonDef = new b2PolygonDef();
            floorShapeDef.SetAsBox(465 * 0.5 / DRAW_SCALE, 0.1);
            var floor:b2Body = world.CreateBody(floorBodyDef);
            floor.CreateShape(floorShapeDef);
			
			// デバッグ描画
			var debugDraw:b2DebugDraw = new b2DebugDraw();
            debugDraw.m_sprite = container;
            debugDraw.m_drawScale = DRAW_SCALE;
            debugDraw.m_fillAlpha = 0.8;
            debugDraw.m_lineThickness = 1;
            debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
            world.SetDebugDraw(debugDraw);
		}
	}
	
}

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.actions.Spawn;
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);
		addInitializer(new Position(new Line(0, 0, 465, 0)));
		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);
	}
}