In my previous post I started with a “Hello world” for box2DFlash.
You can find the original here: AS3: Hello Box2D.
When you get it working you will not be satisfied: a black swf, with only a trace in the output panel.
Today I will make the “Hello world” more visible.
Debug_Drawing
This the preferred method of drawing these physics entities for debugging, rather than accessing the data directly. The reason is that much of the necessary data is internal and subject to change.
source: Debug_Drawing
Box2D has a debugDraw
class that lets you see what you have built, they call it DebugDraw
.
Take the code from yesterday and place this code before Simulating_the_World
code.
[as]
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();
addChild(debugDraw.m_sprite);
debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);
[/as]
world.SetDebugDraw (debugDraw);
. The class at the bottom of the page was correct!So all you need to do is CTRL+ENTER.
Nice effect isn’t it? Almost? Well at least you see something although it’s not impressive…
Yeah, it’s isn’t much. Lets see how we can improve this.
Improve Debug_Drawing
The problem is that the debug window is created and executed 60 times, but because it is done in a for loop, you only see the end result (after 60 updates).
To see animation (if you follow the trace you see that something is moving) we need to update (visible) the data in dbgDraw
more and we will use the frame rate to update it (yesterday set to 60 frames).
But for that there needs to be changed more than just a couple of lines
First we need to add some variables:
[as]
private var world:b2World;
private var body:b2Body;
[/as]
and change some related code:
[as]
// Construct a world object
// var world:b2World = new b2World(worldAABB, gravity, doSleep); // [mck]: old code
world = new b2World(worldAABB, gravity, doSleep);
[/as]
and
[as]
// var body:b2Body = world.CreateBody(bodyDef); // [mck]: old code
body = world.CreateBody(bodyDef);
[/as]
and remove the code add at Simulating_the_World
with
[as]
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
[/as]
and add
[as]
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
public function onUpdateHandler(e:Event):void{
var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;
world.Step(timeStep, iterations);
var position:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
trace(position.x +’,’+ position.y +’,’+ angle);
}
[/as]
End result
The document class (HelloWorld.as
) will look like this:
[as]
package {
import flash.display.*;
import flash.events.*;
// Box2D Classes used in this "Hello world"
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
// [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
public class HelloWorld3 extends Sprite {
private var body:b2Body;
private var world:b2World;
// constructor
public function HelloWorld() {
trace( "HelloWorld.HelloWorld" );
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
////////////////////////////////////////////////////////////////////////////////////////////////////
// Create world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
// var world:b2World = new b2World(worldAABB, gravity, doSleep); // [mck]: old code
world = new b2World(worldAABB, gravity, doSleep);
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
////////////////////////////////////////////////////////////////////////////////////////////////////
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);
var groundBody:b2Body = world.CreateBody(groundBodyDef);
var groundShapeDef:b2PolygonDef = new b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);
groundBody.CreateShape(groundShapeDef);
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Dynamic_Body
////////////////////////////////////////////////////////////////////////////////////////////////////
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(0.0, 4.0);
// var body:b2Body = world.CreateBody(bodyDef); // [mck]: old code
body = world.CreateBody(bodyDef);
var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Debug_Drawing
////////////////////////////////////////////////////////////////////////////////////////////////////
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = new Sprite();
addChild(debugDraw.m_sprite);
debugDraw.m_drawScale = 30;
debugDraw.m_fillAlpha = .25;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit ;
world.SetDebugDraw (debugDraw);
//
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
////////////////////////////////////////////////////////////////////////////////////////////////////
public function onUpdateHandler(e:Event):void{
var timeStep:Number = 1.0 / 60.0;
var iterations:Number = 10;
world.Step(timeStep, iterations);
var position:b2Vec2 = body.GetPosition();
var angle:Number = body.GetAngle();
trace(position.x +’,’+ position.y +’,’+ angle);
}
} // end class
} // end package
[/as]
So all you need to do is CTRL+ENTER.
Now we see something moving, and we have an endless stream of trace data.
It’s better, but there is more room for improvement…
tomorrow more.