In my previous post I update the “Hello world” for box2D so that you can see something visual interesting.
You can find the original here: AS3: Hello Box2D.
Today I will post the same code but now how it should be.
Keeping the coding conventions in mind and making it easier to reuse code.
Still keeping in mind the original Hello world
[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 HelloWorld extends MovieClip {
public var world:b2World;
public var iterations:int = 10;
public var timeStep:Number = 1.0 / 60.0;
public var worldScale:Number = 30.0;
// to see what you have created
private var debugDraw:b2DebugDraw;
private var isDebugDrawing:Boolean = true; // change to false and there is no debugDraw
// constructor
public function HelloWorld() {
//trace( “HelloWorld.HelloWorld” );
init();
}
/**
* lets start building our “Hello world” box2D program
*/
private function init():void {
trace( “HelloWorld.init” );
createWorld();
creatingGroundBox();
creatingDynamicBody();
setupDebugDraw();
// Add event for main loop
addEventListener(Event.ENTER_FRAME, onUpdateHandler, false, 0, true);
}
/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_World
*/
private function createWorld():void {
// Creat 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
world = new b2World(worldAABB, gravity, doSleep);
}
/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Creating_a_Ground_Box
*/
private function creatingGroundBox():void {
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0.0, -9.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
*/
private function creatingDynamicBody ():void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(10.0, 10.0);
var body:b2Body = world.CreateBody(bodyDef);
var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;
shapeDef.restitution = 0.8;
body.CreateShape(shapeDef);
body.SetMassFromShapes();
}
// extra to visualize
private function setupDebugDraw():void {
if(isDebugDrawing) {
debugDraw = 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);
}
}
/**
* http://www.box2d.org/wiki/index.php?title=Manual/AS3#Simulating_the_World_.28of_Box2D.29
*/
public function onUpdateHandler(e:Event):void{
world.Step(timeStep, iterations);
}
} // end class
} // end package
[/as]
So all you need to do is CTRL+ENTER.
And you will have something like this:
3 replies on “Hello box2D – part 4”
Cheers for the tutorials. Helped a lot.
Nice. Will Part 5 comes?
I wanted to write a post about a starting class you could use for all you Box2d stuff.
But I haven’t found any time for that one
If you want to know more about Box2d I could recommend you read the tutorials written by Emanuele Feronato. He explains everthing you will ever need to know about Box2d.