As FX Lead in DreamWorks Animation
Independent VFX Projects
As FX Lead in Carnegie Mellon University, Entertainment Technology Center
Interactive Projects
Create a project folder "helloWorld"
Download Box2DFlashAC3 and extract Box2D folder into flash class folder (FLASH_INSTALL_FOLDER\en\Configuration\ActionScript 3.0\Classes\Box2D)
or copy Box2D folder into current project folder (helloWorld/Box2D)
Create a scene file "helloWorld.fla"
In property editor, set the main class name to helloWorld. This tells flash compiler to look for Actionscript file "helloWorld.as" in the same folder.
Create an Actionscript file "helloWorld.as, and start editing it
package {
// Built-in flash modules
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
// Box2D modules
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class helloWorld extends Sprite {
public var m_world:b2World; // world object
public var m_iterations:int = 10; // iterations for verlet intergration
public var m_timeStep:Number = 1.0/30.0; // time step
public function helloWorld() {
// Implement in step 3
}
public function Update(e:Event):void {
// Implement in step
}
public function Reset(e:Event):void {
// Implement in step
}
}
}
var myText:TextField = new TextField(); myText.text = "Hello World - Click to reset particle position"; myText.x = 10; myText.y = 10; myText.width = 400; myText.height = 20; addChild(myText);
// Create bounding box (AABB) var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-1000.0, -1000.0); worldAABB.upperBound.Set(1000.0, 1000.0); // Define gravity var gravity:b2Vec2 = new b2Vec2(0.0, 10.0); // Allow bodies to sleep when rest var doSleep:Boolean = true; // Create world object m_world = new b2World(worldAABB, gravity, doSleep);
var body:b2Body; // for rigid body var bodyDef:b2BodyDef; // for body definition (position, density...) var boxDef:b2PolygonDef; // to define box collision shape var circleDef:b2CircleDef; // to define circle collision shape
// Create ground body definition bodyDef = new b2BodyDef(); bodyDef.position.Set(0, 11); // Create box shape boxDef = new b2PolygonDef(); boxDef.SetAsBox(30, 3); boxDef.friction = 0.3; boxDef.density = 0; // static bodies require zero density // Attach ground sprite to body definition bodyDef.userData = new PhysGround(); bodyDef.userData.width = 30 * 2 * 30; bodyDef.userData.height = 30 * 2 * 3; addChild(bodyDef.userData); // Create ground body body = m_world.CreateBody(bodyDef); body.CreateShape(boxDef); // Set body mass body.SetMassFromShapes();
for (var i:int = 1; i < 100; i++) {
// Implement step 8~9
}
// Create dynamic body definition bodyDef = new b2BodyDef(); bodyDef.position.x = Math.random() * 13; bodyDef.position.y = Math.random() * 5; // Set random x/y for later var rX:Number = Math.random() * 0.4 + 0.3; var rY:Number = Math.random() * 0.4 + 0.3;
// If we are creating a box
if (Math.random() < 0.5) {
// Create shape definition
boxDef = new b2PolygonDef();
boxDef.SetAsBox(rX, rY);
boxDef.density = 1.0;
boxDef.friction = 0.5;
boxDef.restitution = 0.2;
// Attach library item to body definition
bodyDef.userData = new PhysBox();
bodyDef.userData.width = rX * 2 * 30;
bodyDef.userData.height = rY * 2 * 30;
// Create box body
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
}
// Else we create a circle
else {
// Create circle shape definition
circleDef = new b2CircleDef();
circleDef.radius = rX;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
// Attach library graphic to body definition
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = rX * 2 * 30;
bodyDef.userData.height = rX * 2 * 30;
// Create circle body
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
}
// Set body mass body.SetMassFromShapes(); // Add user data to stage addChild(bodyDef.userData);
public function Update(e:Event):void {
// Update current step
// Advent solver to next step
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
}
}
}
public function Reset(e:Event):void {
// Reset body positions
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite && bb.IsDynamic()){
var newPos = new b2Vec2( Math.random() * 13,
Math.random() * 5 );
bb.SetXForm(newPos, 0);
}
}
}