没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Box2DFlash v2.0.2 User manual
Largely cribbed unauthorized from the Box2D manual, copyright 2007-2009 Erin Catto.
About
Box2DFlash is a 2D rigid body simulation library for games. Programmers can use it in
their games to make objects move in believable ways and make the world seem more
interactive. From the game's point of view a physics engine is just a system for
procedural animation. Rather than paying (or begging) an animator to move your actors
around, you can let Sir Isaac Newton do the directing. Box2DFlash is written in AS3, and
resides in the Box2d namespace. Most of the types defined in the engine begin with the
b2 prefix, to match the C++ version.
Prerequisites
In this manual I'll assume you are familiar with basic physics concepts, such as mass,
force, torque ( 扭转力 ) , and impulses ( 推力 ) . If not, please first consult ( 翻阅 ) the many
tutorials provided by Chris Hecker and David Baraff (google these names). You do not
need to understand their tutorials in great detail, but they do a good job of laying out the
basic concepts that will help you use Box2D.
Wikipedia is also an excellent source of physics and mathematics knowledge. In some
ways it is more useful than Google, because it has carefully crafted content.
This is not a prerequisite, but if you are curious about the inner workings of Box2D, you
can look at these articles .
Since Box2DAS3 is written in Actionscript 3, you are expected to be experienced in this. If
you come from an AS2 background, you may find it easier to start on an easier project
before using Box2D, but it is not impossible.
Core Concepts
Box2D works with several fundamental objects. We briefly define these objects here and
more details are given later in this document.
rigid body (刚体)
A chunk of matter (物质) that is so strong that the distance between any two bits of
matter on the chunk is completely constant. They are hard like a diamond. In the
following discussion we use body
body
body
body interchangably with rigid body.
shape
A 2D piece of collision geometry that is rigidly attached to a body. Shapes have
material properties of friction and restitution. (依附于 body 的 2d 碰撞几何结构,具有
摩擦和恢复特性)
Constraint (约束)
A constraint is a physical connection that removes degrees of freedom from bodies. In
2D a body has 3 degrees of freedom. If we take a body and pin it to the wall (like a
pendulum) we have constrained
constrained
constrained
constrained the body to the wall. At this point the body can only
rotate about the pin, so the constraint has removed 2 degrees of freedom.
contact constraint (接触约束)
A special constraint designed to prevent penetration (穿透) of rigid bodies and to
simulate ( 模仿 ) friction and restitution. You will never create a contact constraint, they
are created automatically by Box2D.
joint
This is a constraint used to hold two or more bodies together. Box2D supports these
joint types: revolute, prismatic, distance, and more. Joints may support limits
limits
limits
limits and
motors
motors
motors
motors .
joint limit
A joint limit restricts the range of motion of a joint. For example, the human elbow only
allows a certain range of angles.
joint motor
A joint motor drives the motion of the connected bodies according to the joint's
degrees of freedom. For example, you can use a motor to drive the rotation of an
elbow.
world
A physics world is a collection of bodies, shapes, and constraints that interact together.
Box2D supports the creation of multiple worlds, but this is usually not necessary or
desirable.
Hello Box2D
This is a small example program that creates a large ground box and a small dynamic box.
This code does not contain any graphics, so prepare to be underwelmed. :). The
matching documents for this example have yet to be created.
Creating a World
Every Box2D program begins with the creation of a world object. This is the physics hub
that manages objects, and simulation.
To
create a world object, first we need to define a bounding box for the world. Box2D
uses the bounding box to accelerate collision detection. The size isn't critical, but a better
fit will improve performance. It is better to make the box too big than to make it too
small.
view source print ?
1 varworldAABB:b2AABB =newb2AABB();
2 worldAABB.lowerBound.Set(-100.0, -100.0);
3 worldAABB.upperBound.Set(100.0,100.0);
Caution
Caution
Caution
Caution
The world
world
world
world AABB
AABB
AABB
AABB should always be bigger then the region where your bodies are located.
It is better to make the world AABB too big than too small. If a body reaches the
boundary of the world AABB it will be frozen and will stop simulating.
Next we define the gravity vector . Yes, you can make gravity go sideways (or you could
just rotate your monitor). Also we tell the world to allow bodies to sleep when they come
to rest. A sleeping body doesn't require any simulation.
view source print ?
1 vargravity:b2Vec2 =newb2Vec2 (0.0, -10.0);
2 vardoSleep:Boolean=true;
Now we create the world object.
view source print ?
1 varworld:b2World =newb2World(worldAABB, gravity, doSleep);
So now we have our physics world, let's start adding some stuff to it.
Creating a Ground Box
Bodies are built using the following steps:
1 Define a body with a position, damping, etc.
2 Use the world object to create the body.
3 Define shapes with geometry, friction, density, etc.
4 Create shapes on the body.
5 Optionally adjust the body's mass to match the attached shapes.
For step 1 we create the ground body. For this we need a body
body
body
body definition
definition
definition
definition . With the
body definition we specify the initial position of the ground body.
view source print ?
1 vargroundBodyDef:b2BodyDef =newb2BodyDef();
2 groundBodyDef.position.Set(0.0, -10.0);
For step 2 the body definition is passed to the world object to create the ground body.
The world object does not keep a reference to the body definition. The ground body is
created as a static body. Static bodies don't collide with other static bodies and are
immovable. Box2D determines that a body is static when it has zero mass. Bodies have
zero mass by default, therefore they are static by default.
view source print ?
1 vargroundBody:b2Body = world.CreateBody(groundBodyDef);
For step 3 we create a ground polygon definition. We use the
SetAsBox
shortcut to form
the ground polygon into a box shape, with the box centered on the origin of the parent
body.
view source print ?
1 vargroundShapeDef:b2PolygonDef =newb2PolygonDef();
2 groundShapeDef.SetAsBox(50.0,10.0);
The
SetAsBox
function takes the half-width and half-height. So in this case the ground
box is 100 units wide (x-axis) and 20 units tall (y-axis). Box2D is tuned for meters,
kilograms, and seconds. So you can consider the extents to be in meters. However, it is
possible to change unit systems, as discussed later in this document.
We finish the ground body in step 4 by creating the ground polygon shape on the ground
body.
view source print ?
1 groundBody.CreateShape(groundShapeDef);
Again, Box2D does not keep a reference to the shape or body definitions. It copies the
data into the
b2Body
structure.
Note that every shape must have a parent body, even shapes that are static. However,
you can attach all static shapes to a single static body. This need for static bodies is done
to make the Box2D code more uniform internally, reducing the number of potential bugs.
You might notice a pattern here. Most Box2D types are prefixed with
b2
. This is done to
reduce the chance for naming conflicts with your code.
Creating a Dynamic Body
So now we have a ground body. We can use the same technique to create a dynamic
body. The main difference, besides dimensions, is that we must establish the dynamic
body's mass properties.
First we create the body using
CreateBody
.
view source print ?
1 varbodyDef:b2BodyDef =newb2BodyDef();
2 bodyDef.position.Set(0.0,4.0);
3
4 varbody:b2Body = world.CreateBody(bodyDef);
Next we create and attach a polygon shape. Notice that we set density to 1. The default
density is zero. Also, the friction on the shape is set to 0.3. Once the shape is attached,
we instruct the body to compute it's mass properties from the attached shapes using the
method
SetMassFromShapes
. This gives you a hint that you can attach more than one
shape per body. If the computed mass is zero, then the body becomes truly static. Bodies
have a mass of zero by default, that's why we didn't need to call
SetMassFromShapes
for
the ground body.
view source print ?
1 varshapeDef:b2PolygonDef =newb2PolygonDef();
2 shapeDef.SetAsBox(1.0,1.0);
3 shapeDef.density =1.0;
4 shapeDef.friction =0.3;
5 body.CreateShape(shapeDef);
6 body.SetMassFromShapes();
That's it for initialization. We are now ready to begin simulating.
Simulating the World (of Box2D)
So we have initialized the ground box and a dynamic box. Now we are ready to set
Newton loose to do his thing. We just have a couple more issues to consider.
Box2D uses a bit of numerical code called an integrator
integrator
integrator
integrator . Integrators simulate the
physics equations at discrete points of time. This goes along with the traditional game
loop where we essentially have a flip book of movement on the screen. So we need to
pick a time step for Box2D. Generally physics engines for games like a time step at least
as fast as 60Hz or 1/60 seconds. You can get away with larger time steps, but you will
have to be more careful about setting up the definitions for your world. We also don't like
the time step to change much. So don't tie the time step to your frame rate (unless you
really, really have to). Without further ado, here is the time step.
view source print ?
1 vartimeStep:Number=1.0/60.0;
In addition to the integrator, Box2D also uses a larger bit of code called a constraint
constraint
constraint
constraint
solver
solver
solver
solver . The constraint solver solves all the constraints in the simulation, one at a time. A
single constraint can be solved perfectly. However , when we solve one constraint, we
slightly disrupt other constraints.
To
get a good solution, we need to iterate over all
constraints a number of times. The suggested iteration count for Box2D is 10. You can
tune this number to your liking, just keep in mind that this has a trade-off between speed
and accuracy. Using fewer iterations increases performance but accuracy suffers.
Likewise, using more iterations decreases performance but improves the quality of your
simulation. Here is our chosen iteration count.
view source print ?
1 variterations:Number=10;
Note that the time step and the iteration count are completely unrelated. An iteration is
not a sub-step. One iteration is a single pass over all the constraints withing a time step.
You can have multiple passes over the constraints within a single time step.
We are now ready to begin the simulation loop. In your game the simulation loop can be
merged with your game loop. In each pass through your game loop you call
b2World.Step
. Just one call is usually enough, depending on your frame rate and your
physics time step.
剩余39页未读,继续阅读
资源评论
- wutao_sc2012-03-03英文的,英语不好的就不用下了
- shijiezhenqimiao2012-11-13还不错哦。就是英文的
西溪漫步
- 粉丝: 60
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功