Game Development Reference
In-Depth Information
Can you see how easy it is? We took almost a chapter and an half to place our first
body, and just a few more lines to add another body.
Now it's time to worry about gravity, as the ball should fall down.
Different body types - static, dynamic,
and kinematic
There are three types of Box2D bodies: static, dynamic, and kinematic.
A static body does not react to any force, impulse, or collision and does not move.
A static body can only be moved manually by the user. By default, every Box2D
body is a static body, and that's why the ball does not move. A static body also
does not collide with other static or kinematic bodies.
A dynamic body reacts to forces, impulses, collisions, and any other world event.
Dynamic bodies can also be moved manually, although I'd suggest to let them be
moved by world forces, and collide with all body types.
A kinematic body is something hybrid between a static and a dynamic body.
Kinematic bodies do not react to forces, but can be moved both manually and
by setting their velocity. Kinematic bodies do not collide with other static or
kinematic bodies.
Back to our simulation now. Which type of body would you assign to the ball and
the floor?
The floor must be a static body, as it does not have to move, while the ball will be
a dynamic body to be moved by world forces.
To tell Box2D the type of each body, you just have to set the type property of the
body definition, which can be b2Body.b2_staticBody , b2Body.b2_dynamicBody ,
or b2Body.b2_kinematicBody respectively for static, dynamic, or kinematic bodies.
Your new Main function is shown as follows:
public function Main() {
world=new b2World(new b2Vec2(0,9.81),true);
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,30/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
var circleShape:b2CircleShape;
circleShape=new b2CircleShape(25/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
 
Search WWH ::




Custom Search