Box2d v2.1.0 user manual translation-Chapter 07th objects (bodies)

Source: Internet
Author: User

Many excerpted content
Translated by Aman JiangBox2d v2.0.1 User Manual

Chapter 1 bodies)

7.1 about

The position and speed of an object. You can apply the forces, torques, and impulses to an object. Objects can be static, moving but not forced (kinematic), and dynamic (dynamic ). This is the definition of the object type:

B2_staticbody

Static objects do not move during simulation, as if they have infinite quality. In box2d, the quality of static objects is stored as zero. Static objects can be moved manually at zero speed, and will not collide with other static or kinematic objects.

B2_kinematicbody

A kinematic object moves at a certain speed during simulation, but is not forced. They allow users to move manually, but the common practice is to set a certain speed. The behavior of a kinematic object is like that it has an infinite quality. box2d stores its quality to zero. Kinematic objects do not collide with other static or kinematic objects.

B2_dynamicbody

Dynamic Objects are completely simulated. They allow users to move manually, but usually they are all moving by force. Dynamic Objects can collide with all other types of objects. Dynamic Objects are always of limited and non-zero quality. If you try to set its quality to zero, it will automatically change the quality to 1 kg.

An object is the skeleton of fixtures and carries fixture in the world. Objects in box2d are always rigid bodies ). That is to say, the two fixture on the same rigid body will never move relative.

Fixture has a collision-able geometric shape and density (density ). An object usually obtains the quality attribute from its fixture. After an object is constructed, you can modify its quality attributes. This will be discussed below.

Generally, you will save all the pointers of the created object, so that you can query the object location and update the position of the graphic object. You can also use pointers to destroy them when they are not needed.

7.2 body Definition)

Before creating an object, you must first create an object definition (b2bodydef ). The object definition contains the data required to initialize the object.

Box2d copies data from the object definition and does not save its pointer. This means that you can use the same object definition to create multiple objects.

Let's take a look at some key elements of object definition.

Body Type)

This chapter has already said that there are three object types: static, kinematic, and dynamic. You should determine the object type during creation, because it will be costly to modify it later.

Bodydef. type = b2_dynamicbody;

The object type must be set.

Position and Angle)

Object definition provides you with an opportunity to initialize the location at the time of creation. This is more efficient than creating an object at the world origin before moving it to a certain position.

Note:

Do not move an object after it is created at the origin. If you create several objects at the same time on the origin, the performance will be poor.

There are mainly two interesting points on objects. The first is the origin of the object. Fixture and joints are attached to objects relative to the origin. The second is the center of the object. The center of mass is determined by the Quality Distribution of the shape, or explicitly set by b2massdata. In box2d, the center of the object is used for many internal calculations. For example, b2body stores the line speed of the center.

When you construct an object definition, you may not know where the center is. You can specify the origin of an object, or specify the angle of the object in radians. The angle is not affected by the center position. If you change the object's quality attributes, the center of gravity will also move, but the source and the shape and joints on the object will not change.

Bodydef. position. Set (0.0f, 2.0f); // The Origin of the body

Bodydef. angle = 0.25f * b2_pi; // The angle of the body in radians.

Damping)

Damping is used to reduce the speed of an object in the world. Damping is different from friction. friction occurs only when an object is in contact. Damping does not replace friction. These two effects must be used at the same time.

The range of the damping parameter can be between 0 and infinity. 0 indicates no damping, and infinity indicates full damping. Generally, the damping value should be between 0 and 0.1. I usually do not use linear damping because it will make the object look a little floating.

Bodydef. lineardamping = 0.0f;

Bodydef. angulardamping = 0.01f;

Damping is similar to stability and performance. When the value is small, the damping effect is almost independent of the time step. When the value is large, the damping effect will change with the time step. If you use a fixed time step (recommended), this is not a problem.

Sleep parameters)

What does sleep mean? The cost of a simulated object is high, so if the object is less, the simulated effect will be better. When an object stops motion, we want to stop simulating it.

When box2d determines that an object (or a group of objects) has stopped moving, the object enters sleep state. Sleeping objects only consume a small amount of CPU consumption. If a awake object is exposed to a sleep object, the object in sleep will wake up. When the joints or contacts on an object are destroyed, they will also wake up. You can also manually wake up an object.

Through object definition, you can specify whether an object can sleep or create a sleep object.

Bodydef. allowsleep = true;

Bodydef. Awake = true;

Fixed Rotation)

You may want a rigid body, such as a role, with a fixed rotation angle. In this way, the object will not rotate even under the load. You can set fixedrotation to achieve this goal:

Bodydef. fixedrotation = true;

Fixed rotation marks make the moment of inertia set to zero.

Bullets)

Game simulation usually produces a series of images at a certain frame rate. This is the so-called discrete simulation. In discrete simulation, the rigid body may move a large distance within a certain time step. If a physical engine does not handle a large amount of motion, you may see some objects mistakenly pass through each other. This is called tunneling ).

By default, box2d uses continuous collision detection (CCD) to prevent dynamic objects from traversing static objects. This is done by scanning the shape from the old location to the new location. The engine searches for new collisions in the scan and calculates the collision time (TOI) for these collisions ). Objects are first moved to their first toi, and then simulated until the end of the original time step.

Generally, CCD is not used between dynamic objects to maintain performance. In some game environments, you need to use CCD on dynamic objects. For example, you may want to use a high-speed bullet to fire a dynamic brick. Without CCD, bullets may penetrate bricks.

In box2d, high-speed moving objects can be marked as bullets ). CCD is executed between bullets and static or dynamic objects. You need to decide which objects are bullets according to the game design. If you decide that an object should be handled by bullets, use the following settings.

Bodydef. Bullet = true;

The bullet mark only affects dynamic objects.

Box2d performs continuous collision detection, So bullets may also miss fast moving objects.

Activation)

You may want to create an object that is not involved in collision and dynamic simulation. This state is similar to sleep, but it will not be awakened by other objects, and its fixture will not be placed in broad-phase. In other words, objects are not involved in collision detection, Ray casts, and so on.

You can create an inactive object and then activate it.

Bodydef. Active = true;

Joints can also be connected to inactive objects. However, these joints are not simulated. When activating an object, its joints are not distorted ).

User Data)

User Data is a void pointer. It associates objects with your applications. You should maintain consistency. All Object User data points to the same object type.

B2bodydef bodydef;

Bodydef. userdata = & myactor;

7.3 object Factory)

Objects are created and destroyed using the factory provided by the world class. This allows the world to create an object through an efficient distributor and add the object to the data structure of the world.

The object can be either dynamic or static, depending on the quality attribute. The methods for creating and destroying two types of objects are the same.

B2body * dynamicbody = myworld-> createbody (& bodydef );

... Do stuff...

Myworld-> destroybody (dynamicbody );

Dynamicbody = NULL;

Note:

Never use new or malloc to create an object. Otherwise, the world will not know the existence of this object and the object will not be properly initialized.

Static objects are not moved by other objects. You can manually move static objects, but be careful not to squeeze them into dynamic objects. In addition, when you move a static object, the friction will not work correctly. Static objects do not collide with other static or kinematic objects. Attaching multiple shapes to a single static object has better performance than attaching only one shape to multiple static objects. Internally, box2d sets the static object to zero quality. This makes most algorithms do not have to treat static objects as special cases.

Box2d does not store object definition references, nor store any data (except user data pointers ). So you can create a temporary object definition and reuse it.

Box2d allows you to delete b2world objects to destroy objects. It will clean up all objects for you. However, you must be careful to clear the body pointer stored in the game engine.

When you destroy an object, the fixture and joint attached to it are automatically destroyed. Understanding this is important for you to manage fixture and joint pointers.

7.4 using an object (using a body)

After creating an object, you can perform many operations on it. These include setting quality, accessing its location and speed, applying force, and conversion points and vectors.

Mass Data)

Each object has mass (scalar), center of mass (two-dimensional vector), and rotation inertia (scalar ). For a static object, its quality and rotation inertia are zero. When an object is set to fixed rotation, its rotation inertia is also zero.

Generally, when fixture is added to an object, the object's quality attribute is automatically determined. You can also adjust the object Quality During Run-Time. You have a special game solution that you can do when you need to change the quality.

Void setmassdata (const b2massdata * data );

After directly setting the object quality, you may want to use fixture again to specify the quality. You can do this:

Void resetmassdata ();

To obtain the object quality data, you can use the following function:

Float32 getmass () const;

Float32 getinertia () const;

Const b2vec2 & getlocalcenter () const;

Void getmassdata (b2massdata * Data) const;

State Information)

Objects are in multiple States. You can use the following functions to efficiently access status data:

Void settype (b2bodytype type );

B2bodytype GetType ();

 

Void setbullet (bool flag );

Bool isbullet () const;

 

Void setsleepingallowed (bool flag );

Bool issleepingallowed () const;

 

Void setawake (bool flag );

Bool isawake () const;

 

Void setactive (bool flag );

Bool isactive () const;

 

Void setfixedrotation (bool flag );

Bool isfixedrotation () const;

Position and velocity)

You can access the position and Rotation Angle of an object, which is often used when rendering related game roles. Generally, you can use box2d to simulate motion or set the position, but it is not commonly used.

Bool settransform (const b2vec2 & position, float32 angle );

Const b2transform & gettransform () const;

Const b2vec2 & getposition () const;

Float32 getangle () const;

You can access the local coordinate system and the center of the world coordinates. Many interior simulations of box2d use centers. Generally, you do not need to access the centroid. Instead, you should focus on Object transformation. For example, you have a square object. The origin of an object may be at a corner of a square, while the center of the center is at the center of the square.

Const b2vec2 & getworldcenter () const;

Const b2vec2 & getlocalcenter () const;

You can access the wire speed and angular velocity. The wire speed is stated in the center of the center. Therefore, the quality attribute changes, and the line speed may also change.

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.