Box2d (1)

Source: Internet
Author: User
Tags modulus

From: http://hi.baidu.com/goodlad/blog/item/80e8613680381acba2cc2beb.html

1. helloworld

Before that, I assume that you have read the source code of helloworld, read the instructions about helloworld in the user manual, and have understood most of the content.

In fact, helloworld has already described the operating mechanism of the box2d physical engine in an extremely simple language. We can summarize the steps below:

1. Create a world based on a b2aabb box and set up a G value and a Boolean variable that allows sleep.

2. Create a static rigid body surface. The detailed process of defining a rigid body, the most important thing in the box2d physical engine, is described here: first, define a shape (can be a composite shape, in part 2), add the shape to the rigid body definition through addshape to create the rigid body.

3. Repeat the process of creating a rigid body until you have no need.

4. Add the UPDATE function of the world to your loop.

In fact, the above steps are also used by many physical engines or even other engines.

The helloworld tutorial is quite simple. At this time, you don't even need to think about how the world works. You can use the relevant functions to get the position and Rotation Angle of the rigid body, then update and render your role object in the rendering part of the game.

After watching helloworld, you don't have to think about what the whole world is like, because the world is too complicated for you, and when you calm down, let's take a look at our concepts and data types.

Concept

There are several concepts in this example,

World (b2world): The world is an environment where all physical operations are performed.

Shape Definition (b2shapedef): What is Shape Definition? To put it simply, shape definition is to define the appearance of your object. What is it used? Is used to determine your collision.

Rigid Body definition (b2bodydef): The Rigid Body definition is to set the initial details of the rigid body. Currently, the biggest function is to add the shape you have defined to the rigid body you think.

Rigid Body (b2body): a rigid body is something (object) in the physical engine. It can be used to change or rotate the current position. All objects you want to use in the world are currently rigid bodies.

Type Definition

Several types of definitions (familiar with the type definitions in box2d can be of great help for correct value assignment in the future ):

Typedef signed Char int8;

Typedef signed short int16;

Typedef signed int int32;

Typedef unsigned char uint8;

Typedef unsigned short uint16;

Typedef unsigned int uint32;

Typedef float float32;

Const float32 b2_pi = 3.14159265359f;

Data Type

1. b2vec2

Like vector3 in 3D, b2vec2 is also widely used in box2d, and you use it almost at every moment, such as defining coordinates and box sizes.

B2vec2 is composed of X and y of the float32 type. It supports negative vectors, including the + =,-=, and * = operators,

Supported methods include:

Void setzero (); set X and Y to 0

Void set (float32 X _, float32 Y _); set X and Y to the specified value.

B2vec2 make (float32 X _, float32 Y _), generate a b2vec2 with the specified value

Float32 length () gets the length or modulus of a vector.

Float32 normalize () Standardization Vector

Bool isvalid () check validity

If you do not understand some mathematical concepts I mentioned here, such as standardized vectors and modulus, you can refer to b2math. H files or look for a mathematical book.

2. b2mat22

In the helloworld tutorial, although b2mat22 is not used, we will first propose this concept for discussion in the next section.

In fact, b2mat22 is a 2*2 matrix composed of two b2vec2 elements. You can directly construct them by two b2vec2 (col1, col2) or by an angle value.

The main methods include:

Void set (const b2vec2 & C1, const b2vec2 & C2)

Void set (float32 angle ),

Two assignment methods are provided.

Void setidentity (), set the equality

Void setzero (), clear the x and y values of col1 and col2 to 0.

B2mat22 invert (), convert related data

B2vec2 solve (const b2vec2 & B), solving a * x = B

3. b2aabb

B2aabb is a box consisting of two vectors. One is minvertex, which is the smallest vertex, and the other is maxvertex, which represents the most common AABB box.

4. b2shapedef

B2shapedef is defined as a shape. It uses a b2shapetype type to represent the shape type, a function pointer to represent user data (userdata), and a b2vec2 vector localposition to represent the current position, use the localrotation of float32 to represent the current angle, and use the friction, density, and restitution of float32 to represent the friction, density, and elasticity coefficient, use the categorybits and maskbits of uint16 to represent the collision bit and bit ID (which can be used to filter some collisions), and use the groupindex of int16 to represent the Group number. This group number can be used to filter the group number, which is more than the bit ID.

Related Constants

Enum b2shapetype

{

E_unknownshape =-1,

E_circleshape,

E_boxshape,

E_polyshape,

E_meshshape,

E_shapetypecount,

};

Related Shape Definition

B2circledef inherits from b2shapedef, type is e_circleshape, and has a float32-type radius to represent the radius value.

B2boxdef is inherited from b2shapedef and type is E _ boxshape. In addition, it carries a number extents of Type b2vec2 to represent the region value.

B2polydef inherits from b2shapedef and type is E _ polyshape. In addition, it carries an array of b2vec2 vertices to represent vertices, and an int32-type vertexcount is used to represent the number of vertices, currently, the maximum number of vertices is 8.

5. b2bodydef

B2bodydef is a rigid-body definition structure. A function pointer userdata is used to represent user data. A group of Type b2shapedef * pointer array shapes is used to represent the shape queue. Currently, a maximum of 64 shapes are supported, A b2vec2 vector position is used to represent the current position, and float32 is used to represent the current angle. linearvelocity is used to represent the line speed, angularvelocity of float32 is used to represent the angular velocity, and linear damping is represented by the amount of lineardamping of float32. angulardamping of float32 is used to represent the angle impedance, and bool is used as the type.
Allowsleep of indicates whether sleep is allowed. An issleeping of the bool type indicates whether sleep is in progress. A preventrotation of the bool type indicates whether rotation is prevented. Supported methods:

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.