Game Development Reference
In-Depth Information
Static bodies will be drawn in green . Dynamic bodies will be drawn in red when
they're not sleeping and in gray where they are put to sleep. Kinematic bodies,
not shown in the previous screenshot, will be drawn with blue .
Now it should also clear the concept of putting bodies to sleep and saving CPU
work. As you can see, when the ball hits the ground, no other forces are acting
on it, so it can be put to sleep until something happens.
Now, a new problem. The ball did not bounce. We need to give our bodies some
more attributes if we want to run a complete simulation.
Density, friction, and restitution
As you already know how to add bodies to the world, I want to introduce you to
three attributes that will affect bodies' behavior: density, friction, and restitution.
The density is used to set the mass of a body, measured in kilograms by square
meter. Higher density means heavier bodies, and it can't be negative.
The friction comes into play when two bodies slide on each other, and it's defined
by a coefficient, usually from 0 (no friction) to 1 (strong friction). It can't be negative.
The restitution determines how much a body will bounce after a collision. Like
density and friction, it can't be negative and it's defined by a coefficient usually from
0 to 1. A ball falling on the ground with restitution zero won't bounce ( inelastic
collision ), whereas a value of one would have made the ball bounce with the same
velocity it had at the moment of the collision ( perfectly elastic collision ).
Density, friction, and restitution must be added to the fixture,
so change the Main function by adding the following lines: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();
fixtureDef.shape=circleShape;
fixtureDef.density=1;
fixtureDef.restitution=0.6;
fixtureDef.friction=0.1;
var theBall:b2Body=world.CreateBody(bodyDef);
theBall.CreateFixture(fixtureDef);
bodyDef.position.Set(320/worldScale,470/worldScale);
 
Search WWH ::




Custom Search