Game Development Reference
In-Depth Information
groundBox.Set(b2Vec2(0, boxHeight), b2Vec2(boxWidth, boxHeight));
groundBody-> CreateFixture(&groundBox, density);
// left
groundBox.Set(b2Vec2(0, boxHeight), b2Vec2(0, 0));
groundBody-> CreateFixture(&groundBox, density);
// right
groundBox.Set(b2Vec2(boxWidth, boxHeight), b2Vec2(boxWidth, 0));
groundBody-> CreateFixture(&groundBox, density);
Look at how the b2EdgeShape groundBox variable is reused. Each Set method
call is followed by a call to groundBody-> CreateFixture() , which uses the
- > operator to denote that groundBody is a C-style pointer and passes
&groundBox . Because Box2D makes a copy of groundBox , you can safely reuse
the same shape to create all four sides enclosing the screen area without modifying or
overriding the previous lines. Because the body is a static body (the default setting for
Box2D bodies), density has no effect and is set to 0 .
Notice that the screen width and height is divided by a PTM_RATIO constant to con-
vert them from pixels to meters. Why meters, and what's PTM_RATIO ? Box2D is op-
timized to work best with dimensions in the range of 0.1 to 10 meters (1 meter is ap-
proximately 3.28 feet). It's tuned for the metric system, so all distances are considered
to be meters, all masses are in kilograms, and time is measured in—quite
oddly—seconds. If you're not familiar with the meters, kilograms, and seconds (MKS)
system, don't worry—you don't have to meticulously convert feet into meters and
pounds into kilograms. The conversion to meters is just a way to keep the distance val-
ues for Box2D in the desirable range of 0.1 to 10, and the masses used by bodies don't
resemble real-world masses anyway. The masses of bodies often need to be tweaked by
feel rather than by using realistic weights.
You should try to keep the dimensions of objects in your world as close to 1 meter as
much as possible. That's not to say that you can't have objects that are smaller than 0.1
meters or larger than 10 meters, but you may run into glitches and strange behavior if
you create relatively small or large bodies.
The PTM_RATIO is defined like this:
#define PTM_RATIO 32
Search Nedrilad ::

Custom Search