Game Development Reference
In-Depth Information
world=new b2World(new b2Vec2(0,9.81),true);
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,30/worldScale);
var circleShape:b2CircleShape;
circleShape=new b2CircleShape(25/worldScale);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape=circleShape;
var theBall:b2Body=world.CreateBody(bodyDef);
theBall.CreateFixture(fixtureDef);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces;
}
}
}
Time to save the project and test it. Ready to see your first Box2D body in action?
Run the movie!
Ok, it did not display anything. Before you throw this topic, let me tell you that
Box2D only simulates the physic world, but it does not display anything.
This means your body is alive and kicking in your Box2D World; it's just that you
can't see it.
Using debug draw to test your simulation
Luckily, Box2D comes with a feature, debug draw , that will help us to see what's
going on.
1.
Debug draw will display what happens in the Box2D World, and we can
enable it by calling world's DrawDebugData method right after the Step
method, in the updateWorld function:
world.DrawDebugData();
2.
Once we've told the world to display the debug draw after each iteration, we
need to define the visual settings used by debug draw. Add the following
lines to your Main class:
package {
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
 
Search WWH ::




Custom Search