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

[Box2D]色を変えてみる

色々と制作中。
* まだpolygonShapeしか描画できない。
/*
 * 色々と制作中。
 * まだpolygonShapeしか描画できない。
*/
package
{

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Dynamics.Contacts.*;
	import Box2D.Common.*;
	import Box2D.Common.Math.*;

	[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]
	public class Intro extends Sprite
	{
		public function Intro()
		{
			addEventListener(Event.ADDED_TO_STAGE, initialize);
		}
		
		public function initialize(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initialize);
			
			var main:Main = new Main(stage);
			addChild(main);
		}
	}
}

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;

import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;

class Box2DMain extends Sprite
{
	public var layer:Stage;
	
	public var m_sprite:Sprite;
	public var m_world:b2World;
	public var m_physScale:Number = 30.0;
	public var m_iterations:int = 10;
	public var m_timeStep:Number = 1.0 / 30.0;

	public var m_mouseJoint:b2MouseJoint;

	static public var mouseXWorldPhys:Number;
	static public var mouseYWorldPhys:Number;
	static public var mouseXWorld:Number;
	static public var mouseYWorld:Number;

	private var mousePVec:b2Vec2 = new b2Vec2();
	private var mouseDown:Boolean = false;

	public function Box2DMain(stg:Stage)
	{
		layer = stg;
		initialize();
	}
	
	public function initialize():void
	{
		removeEventListener(Event.ADDED_TO_STAGE, initialize);
		
		//コンテナ
		m_sprite = new Sprite();
		addChild(m_sprite);
		
		addEventListener(Event.ENTER_FRAME, update);
		
		addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		addEventListener(MouseEvent.CLICK, onMouseUp);
		addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		addEventListener(Event.MOUSE_LEAVE, onMouseLeave);

		//WORLD
		var worldAABB:b2AABB = new b2AABB();
		worldAABB.lowerBound.Set(-1000.0, -1000.0);
		worldAABB.upperBound.Set(1000.0, 1000.0);
		var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
		var doSleep:Boolean = true;
		m_world = new b2World(worldAABB, gravity, doSleep);

		createWall();
		createObject();
	}
	
	public function createObject():void
	{

	}
	
	private function createWall():void
	{
		var wallObject:b2PolygonDef = new b2PolygonDef();
		wallObject.density = 0.0;
		var wallDef:b2BodyDef = new b2BodyDef();
		var wallBody:b2Body;
		
		//Left
		wallDef.position.Set(-11/ m_physScale, layer.stageWidth/2 / m_physScale);
		wallObject.SetAsBox(10 / m_physScale, layer.stageWidth/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Right
		wallDef.position.Set((layer.stageWidth+11) / m_physScale, layer.stageWidth/2 / m_physScale);
		wallObject.SetAsBox(10 / m_physScale, layer.stageWidth/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Top
		wallDef.position.Set(layer.stageWidth/2 / m_physScale, -11 / m_physScale);
		wallObject.SetAsBox(layer.stageWidth/2 / m_physScale, 10 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Bottom
		wallDef.position.Set(layer.stageWidth/2 / m_physScale, (layer.stageWidth-10/2) / m_physScale);
		wallObject.SetAsBox(layer.stageWidth/2 / m_physScale, 10/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();
	}
			
	private function onMouseDown(event:MouseEvent):void
	{
		mouseDown = true;
	}
	
	private function onMouseUp(event:MouseEvent):void
	{
		mouseDown = false;
	}
	
	private function onMouseMove(event:MouseEvent):void
	{
		if (mouseDown != event.buttonDown){
			mouseDown = event.buttonDown;
		}
	}
	
	public function onMouseLeave(e:Event):void
	{
		mouseDown = false;
		MouseDrag();
	}
	
	//======================
	// Update
	//======================
	private function update(event:Event):void
	{
		
		m_world.Step(m_timeStep, m_iterations);
		m_sprite.graphics.clear();
		
		UpdateMouseWorld();
		MouseDrag();
		
		for (var b:b2Body = m_world.m_bodyList; b; b = b.m_next) {
			for (var s:b2Shape = b.GetShapeList(); s != null; s = s.GetNext()) {
				if (b.m_userData != null)
				{
				    DrawShape(s, b.m_xf, b.m_userData.fillColor, false);
				}else {
				    DrawShape(s, b.m_xf, 0xFFFFFF, false);
				}
			}
		}
	}
	
	//======================
	// Update mouseWorld
	//======================
	public function UpdateMouseWorld():void
	{
		mouseXWorldPhys = (m_sprite.mouseX)/m_physScale; 
		mouseYWorldPhys = (m_sprite.mouseY)/m_physScale; 
		
		mouseXWorld = (m_sprite.mouseX); 
		mouseYWorld = (m_sprite.mouseY); 
	}
	
	//======================
	// Mouse Drag 
	//======================
	public function MouseDrag():void
	{
		// mouse press
		if (mouseDown && !m_mouseJoint){
			
			var body:b2Body = GetBodyAtMouse();
			
			if (body)
			{
				var md:b2MouseJointDef = new b2MouseJointDef();
				md.body1 = m_world.GetGroundBody();
				md.body2 = body;
				md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
				md.maxForce = 300.0 * body.GetMass();
				md.timeStep = m_timeStep;
				m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
				body.WakeUp();
			}
		}
		
		// mouse release
		if (!mouseDown){
			if (m_mouseJoint)
			{
				m_world.DestroyJoint(m_mouseJoint);
				m_mouseJoint = null;
			}
		}
		
		// mouse move
		if (m_mouseJoint)
		{
			var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
			m_mouseJoint.SetTarget(p2);
		}
	}
	
	//======================
	// GetBodyAtMouse
	//======================
	public function GetBodyAtMouse(includeStatic:Boolean = false):b2Body
	{
		// Make a small box.
		mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
		var aabb:b2AABB = new b2AABB();
		aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
		aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
		
		// Query the world for overlapping shapes.
		var k_maxCount:int = 10;
		var shapes:Array = new Array();
		var count:int = m_world.Query(aabb, shapes, k_maxCount);
		var body:b2Body = null;
		for (var i:int = 0; i < count; ++i)
		{
			if (shapes[i].GetBody().IsStatic() == false || includeStatic)
			{
				var tShape:b2Shape = shapes[i] as b2Shape;
				var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
				if (inside)
				{
					body = tShape.GetBody();
					break;
				}
			}
		}
		return body;
	}
	
	//======================
	// DrawShape
	//======================
	public function DrawShape(shape:b2Shape, xf:b2XForm, color:Number, core:Boolean) : void{
		
		var coreColor:b2Color = null;
		
		switch (shape.m_type)
		{
		case b2Shape.e_circleShape:
			{
				var circle:b2CircleShape = (shape as b2CircleShape);
				
				var center:b2Vec2 = b2Math.b2MulX(xf, circle.m_localPosition);
				var radius:Number = circle.m_radius;
				var axis:b2Vec2 = xf.R.col1;
				
				//m_debugDraw.DrawSolidCircle(center, radius, axis, color);
			}
			break;
		
		case b2Shape.e_polygonShape:
			{
				var i:int;
				var poly:b2PolygonShape = (shape as b2PolygonShape);
				var vertexCount:int = poly.GetVertexCount();
				var localVertices:Array = poly.GetVertices();
				
				var vertices:Array = new Array(b2Settings.b2_maxPolygonVertices);
				
				for (i = 0; i < vertexCount; ++i)
				{
					vertices[i] = b2Math.b2MulX(xf, localVertices[i]);
				}
				
				DrawSolidPolygon(vertices, vertexCount, color);
			}
			break;
		}
	}

	public virtual function DrawSolidPolygon(vertices:Array, vertexCount:int, color:Number) : void{
		
		m_sprite.graphics.lineStyle(1.0, color, 1.0);
		m_sprite.graphics.moveTo(vertices[0].x * m_physScale, vertices[0].y * m_physScale);
		m_sprite.graphics.beginFill(color, 1);
		for (var i:int = 1; i < vertexCount; i++){
				m_sprite.graphics.lineTo(vertices[i].x * m_physScale, vertices[i].y * m_physScale);
		}
		m_sprite.graphics.lineTo(vertices[0].x * m_physScale, vertices[0].y * m_physScale);
		m_sprite.graphics.endFill();
		
	}
}

class Main extends Box2DMain
{
	public function Main(stg:Stage)
	{
		super(stg);
	}

	override public function createObject():void
	{
		var bd:b2BodyDef;
		var box:b2PolygonDef = new b2PolygonDef();
		
		box.SetAsBox(20 / m_physScale, 20 / m_physScale);
		box.density = 1.0;
		box.friction = 0.4;
		box.restitution = 0.1;
		bd = new b2BodyDef();
		bd.position.Set(232 / m_physScale, 232 / m_physScale);
		
		for (var i:uint; i < 4; i++ )
		{
			box.SetAsBox((20+i) / m_physScale, (20+i) / m_physScale);
			var box1:b2Body = m_world.CreateBody(bd);
			box1.m_userData = new b2Material(0x1C1F26)
			box1.CreateShape(box);
			box1.SetMassFromShapes();
			
			box.SetAsBox((19-i) / m_physScale, (21+i) / m_physScale);
			var box2:b2Body = m_world.CreateBody(bd);
			box2.m_userData = new b2Material(0x69788C)
			box2.CreateShape(box);
			box2.SetMassFromShapes();
			
			box.SetAsBox((18-i) / m_physScale, (22+i) / m_physScale);
			var box3:b2Body = m_world.CreateBody(bd);
			box3.m_userData = new b2Material(0x434F59)
			box3.CreateShape(box);
			box3.SetMassFromShapes()
			
			box.SetAsBox((17-i) / m_physScale, (23+i) / m_physScale);
			var box4:b2Body = m_world.CreateBody(bd);
			box4.m_userData = new b2Material(0xA4B3BF)
			box4.CreateShape(box);
			box4.SetMassFromShapes()
			
			box.SetAsBox((16-i)  / m_physScale, (24+i) / m_physScale);
			var box5:b2Body = m_world.CreateBody(bd);
			box5.m_userData = new b2Material(0xF2F2F2)
			box5.CreateShape(box);
			box5.SetMassFromShapes()
		}
	}
}

class b2Material
{
	public var fillColor:Number;
	
	public function b2Material(fillcol:Number = 0xFFFFFF)
	{
		fillColor = fillcol;
	}
}