Game Development Reference
In-Depth Information
The Allow Sleep and Awake properties are used by the simulation engine to make some
optimizations. Since it is computationally expensive to simulate a body, it makes sense to skip
bodies that are not moving. If Allow Sleep is set to true, the simulation will put a body to sleep
(Awake = false) if it comes to rest, temporarily removing it from the simulation. If a body is hit by
another body, it will be awakened and continue participating in the simulation. In general, you use
these flags only when you want to mark a body as not being allowed to sleep. You may do this
because the body is a particularly important part of the game. The property Active is similar to
Awake, in that an inactive body is very much like a sleeping body—it does not participate in the
simulation. However, an inactive body will never be activated by the simulation; you have to do that
in your code.
You will notice in the above list that there is no mention of shape, density, or other physical
characteristics. These other attributes are covered by the concept of a fixture, as discussed in the
next section.
Fixtures
A fixture attaches a shape to a body, as well as some additional physical properties. Those
properties are:
Shape - The shape of the body
User Data - A place to attach game-specific data
Friction - The friction coefficient—how much the body is slowed when it slides
on another body
Restitution - How bouncy an object is
Density - The density of the body, in k/m 2
Is Sensor - A sensor that records that a body has touched other bodies but has
not created a collision—a nonsolid body
The shape of a fixture can be a convex polygon or a circle. There is also the option for the shape to
be a chain or an edge (used with chains). We are only going to use circles in our example, but these
other shapes could be used to make some interesting games. The friction, restitution (not in the legal
sense), and density all describe addition physical characteristics of the body. Friction describes how
a body slows down when sliding on another body. This is different from linear damping, which is
more like air resistance. Restitution describes how a body comes to rest after colliding with another
body; low restitution means that the body will not bounce much, while high restitution is very
bouncy, like a super ball. Density is a calculation of the mass of a dynamic body based on the area
of its shape. Notice that density is given in terms of m 2 , because we are working in a 2D world.
We have looked at the world, bodies, and fixtures. While Box2D offers a bit more, such as joints,
chains, and a number of other features, we have enough background to explore the code that makes
up our example. I leave it up to the interested reader to explore the Box2D documentation and
examples in more detail. Covering a physics engine in full detail would require a whole book!
 
Search WWH ::




Custom Search