Box2d v2.1.0 user manual translation-Chapter 1 Collision Module)

Source: Internet
Author: User
Chapter 2 collision module 04th about

The collision module contains the shape and operation Shape functions. This module also contains the dynamic tree and broad-phase to accelerate collision processing for large systems.

4.2 shape (SHAPES)

Shapes describe the geometric objects that can conflict with each other. They can be used independently without physical simulation. You can perform some operations on the shape.

B2shape is a base class. All shapes of box2d implement this base class. This base class defines several functions:

• Determine whether a vertex overlaps the shape

• Ray cast)

• AABB with computation shape

• Calculate the shape quality

In addition, each shape has two member variables: type and radius ). Radius is also meaningful for polygon. We will discuss it later.

4.3 circle (circle shapes)

The circle has a position and a radius.

The circle is solid, and you cannot make the circle hollow. However, you can use a polygon to create a series of line segments that are connected at the beginning and end of the line segment, and form a string to simulate a hollow circle.

B2circleshape circle;

Circle. m_p.set (1.0f, 2.0f, 3.0f );

Circle. m_radius = 0.5f;

4.4 polygon shapes)

The polygon of box2d is a solid convex polygon. Select any two points inside the polygon as the first-line segment. If all the line segments do not overlap with the sides of the polygon, the polygon is a convex polygon. The polygon is solid and cannot be hollow. However, you can use two points to create a polygon, which degrades to a line segment.

When creating a polygon, the points used must be arranged counterclockwise (CCW ). We must be very careful that the clockwise direction is relative to the right-hand coordinate system. In this coordinate system, the Z axis points to the outside of the plane. It may become clockwise relative to your screen, depending on how your own coordinate system regulates.

The member variables of polygon have the public access permission, but you should also use the initialization function to create polygon. The initialization function creates a normal vectors and checks the validity of the parameters.

When creating a polygon, you can pass an array containing vertices. The maximum array size is b2_maxpolygonvertices. The default value is 8. This is enough to describe most convex polygon.

// Define a triangle in a counter-clockwise order

B2vec2 vertices [3];

Vertices [0]. Set (0.0f, 0.0f );

Vertices [1]. Set (1.0f, 0.0f );

Vertices [2]. Set (0.0f, 1.0f );

Int32 COUNT = 3;

 

B2polygonshape polygon;

Polygon. Set (vertices, count );

Polygon has some defined initialization functions to create boxes and edges (edges, that is, line segments ).

Void setasbox (float32 HX, float32hy );

Void setasbox (float32 HX, float32hy, const b2vec2 & Center, float32 angle );

Void setasedge (const b2vec2 & V1, const b2vec2 & V2 );

A polygon inherits the radius from b2shape. A protective layer (skin) is created around the polygon through the radius ). In the case of stacking, this protective layer keeps polygon slightly separated. This enables continuous collision on the core polygon.

I don't quite understand this paragraph. The original Article is
Polygons inherit a radius from b2shape. the radius creates a skin around the polygon. the skin is used in stacking scenarios to keep polygons slightly separated. this allows continuous collision to work against the core polygon .)

4.5 shape point test)

You can test whether a vertex overlaps the shape. To use this function, we need to provide a shape transformation and a point in the world coordinate.

B2transfrom transform;

Transform. setidentity ();

B2vec2 point (5.0f, 2.0f );

Bool hit = shape-> testpoint (transform, point );

In box2d, a shape is attached to an object. The data stored in it is defined in the Local Coordinate System of the object, and the point to be tested is in the world coordinate system, there is no way to compare different coordinate systems. This transform is used to convert a shape from a local coordinate system to a world coordinate system before comparison. The inverse transformation of transform is to convert the world coordinate system to the local coordinate system. Therefore, to implement this function, you can first calculate the inverse conversion and convert the transmitted points to the local coordinate system. This can also be compared to see which one is convenient.

Look at the source code of box2d. When b2circleshape: testpoint is implemented, it is to convert the center of the circle into the world coordinate system and then compare. B2polygonshape: testpoint is used to convert the transmitted points into local coordinates before comparison. To convert a polygon to a world coordinate, multiple points must be converted at the same time, while the Circle only converts the center of the circle.

Using the local coordinate system, no matter how the object moves, rotates, and scales, the conversion matrix is changed, and the points stored in the shape do not need to be modified, which makes it very convenient. In the following document, you can see that many shape functions are passed into a conversion. The principle is the same .)

Shape Ray cast)

You can use the Ray-directed shape to obtain the intersection points and normal vectors between them. If the projection starts inside the shape, it is treated as no intersection, and false is returned.

B2transfrom transform;

Transform. setidentity ();

B2raycastinput input;

Input. p1.set (0.0f, 0.0f );

Input. p2.set (1.0f, 0.0f );

Input. maxfraction = 1.0f;

B2raycastoutput output;

Bool hit = shape-> raycast (& output, input, transform );

If (hit)

{

B2vec2 hitpoint = input. P1 + output. fraction * (input. P2-input. P1 );

}

The light here refers to the rays in the ry.

Let's look at the definition of b2raycastinput. Apart from specifying two vertices P1 and P2, there is also a maxfraction. What does maxfraction mean? We know that two points determine a straight line, two points in mathematics, and other points in a straight line are defined. parameter equations are often used. That is, define P (fraction) = p1 + fraction * (P2-P1 ). When fraction is 0, it indicates P1. When fraction is 1, it indicates P2. In this definition, the line segment between two points is the parameter variation from 0 to 1. If the parameter is smaller than 0, it indicates a reverse point. If the parameter is greater than 1, it indicates a point that is forward beyond the line segment. Maxfraction indicates that the parameter corresponding to the vertex to be tested is in [0,
Maxfraction. B2raycastoutput also has a fraction, which means the same.

In mathematics, we like to attribute some variables to a variation between 0 and 1, which is called normalization. The common means to solve the problem is to use a certain transformation (the transformation mentioned here is generalized) to convert the variable into a value between 0 and 1, and then calculate it under normalization, then, use an inverse transformation to obtain the answer to the original question. The linear parameterization mentioned above can be regarded as a kind of standardization. So why do we need to standardize it? This makes computing easy. Why is it convenient? I cannot answer the question. In box2d, where vectors are involved, the word fraction should all be described above .)

4.7 bilateral Functions)

The collision module contains a peering function. Two shapes must be passed to calculate the result. Including:

• Contact Manifolds

• Distance

• Impact Time

4.8 contact Manifolds

I don't know what manifold should translate into. I guess manifold refers to a bunch of things with the same attributes. It can be understood as a set, but it is not good to translate it into a set. Therefore, the English version is retained directly)

Some functions of box2d are used to calculate the contact points between coincident shapes. Consider the collision between the circle and the polygon. We will get only one contact point and one vector. The collision between a polygon and a polygon gives us two contact points. These contact points all have the same normal vectors, so box2d groups them into a group to form a manifold Structure. The contact solver further processes this structure to improve system stability when objects are stacked together.

Generally, you do not need to directly calculate manifold, but you may want to use the results that have been processed during this simulation.

The b2manifold structure contains a normal vector and a maximum of two contact points. Both the vector and the contact point are relative to the local coordinate system. To facilitate contact solver processing, each contact point stores a Forward Impulse and a shear (friction) impulse.

The b2worldmanifold structure can be used to generate contact vectors and points under the world coordinates. You need to provide b2manifold structure and shape conversion and radius.

B2worldmanifold worldmanifold;

Worldmanifold. initialize (& manifold, transforma, shapea. m_radius,

Transformb, shapeb. m_radius );

For (int32 I = 0; I <manifold. pointcount; ++ I)

{

B2vec2 point = worldmanifold. Points [I];

}

During the simulation, the shape will move and manifold may change. Touch points may be added or removed. You can use b2getpointstates to check the status.

B2pointstate state1 [2], state2 [2];

B2getpointstates (state1, state2, & manifold1, & manifold2 );

If (state1 [0] = b2_removestate)

{

// Process event

}

4.9 distance (distance)

The b2distance function can be used to calculate the distance between two shapes. The distance function requires two shapes to convert them into b2distanceproxy. There is also some caching used to warm start the distance function for repeated CILS ). For details, see b2distance. h.

4.10 impact time (IMPACT)

If the two shapes move fast, they may pass through each other in one time step.

The b2timeofimpact function is used to determine the collision time when two shapes are moving. This is called the time of impact (TOI ). B2timeofimpact is designed to prevent tunneling. In particular, it is designed to prevent moving objects from penetrating through Static geometric shapes and going out.

This function considers shape rotation and moving, but if the rotation is large enough, this function will miss the collision. The function reports a non-overlapping time and captures all translation collisions.

The impact time function defines a separation axis at the beginning and ensures that the shape does not exceed this axis. This may miss some collisions at the end position. However, this method is fast enough to prevent tunneling.

It is difficult to limit the rotation angle range. In some cases, even a small rotation angle will lead to missed collisions. In general, Even if you miss some collisions, it will not affect the fun of the game.

This function requires two shapes (converted to b2distanceproxy) and two b2sweep structures. The b2sweep structure defines the conversion between shapes at the beginning and end.

You can execute this collision time function when the rotation angle is fixed, so that no collision will be missed.

4.11 Dynamic Tree)

Box2d uses b2dynamictree to efficiently organize a large number of shapes. This class does not know the existence of shapes. Instead, it uses the User Data Pointer to operate the axis alignment package enclosure (AABB ).

A Dynamic Tree is a layered AABB tree. Each internal node of the tree has two subnodes. The child node on the left is the user's AABB.

This tree structure supports efficient Ray casts and region queries ). For example, there are hundreds of shapes in a scene. If you want to execute Light Projection on the scene, if you use brute force, you need to project each shape. This is very inefficient and does not take advantage of the shape distribution information. The alternative is that you maintain a dynamic tree and emit light on the tree. When traversing a tree, you can skip a large number of shapes.

Region Query uses a tree to locate all leaf nodes that overlap with the AABB to be queried. This is much more efficient than brute-force algorithms, and many shapes will be skipped directly.

Generally, you do not directly use the dynamic tree. You can use the b2world class to perform ray projection and area query. If you want to create your own dynamic tree, you can see how box2d uses the dynamic tree.

4.12 broad-phase

Collision processing in a physical step can be divided into two phases: narrow-phase and broad-phase. For narrow-phase, we calculate the contact points between two shapes. Assume that there are n shapes. If you use the brute force algorithm, you need to execute N x n/2 narrow-phase.

The tb2broadphase class uses a dynamic tree to reduce data management overhead. This can greatly reduce the number of narrow-phase calls.

Generally, you do not directly interact with broad-phase. Box2d creates and manages broad-phase internally. In addition, b2broadphase is designed for Physical Simulation in box2d and may not be suitable for other situations.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.