Box2d v2.1.0 user manual translation-Chapter 03rd common)

Source: Internet
Author: User

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

Chapter 2 common modules 03rd

The common module includes settings, memory management, and vector math)

3.2 Configuration

The header file b2settings. h contains:

• Type, such as int32 and float32

• Constant

• Allocation wrappers)

• Version number

• Mixed friction and mixed recovery functions

Type

Box2d defines different types, such as float32 and int8, to facilitate the determination of the structure size.

Constant

Box2d defines some constants, which are recorded in b2settings. h. Usually you do not need to adjust these constants.

The collision calculation and object simulation of box2d use floating point mathematics. Considering the rounding errors, we need to define some numerical tolerances, some of which are absolute and some are relative. Absolute tolerances are measured in mks units.

Distributor Packaging

The configuration file defines b2alloc and b2free for large memory allocation. You can have b2alloc and b2free call your memory management system ).

Version

The b2version structure stores the current version information, which can be queried at run time.

Friction mixing and recovery Mixing

If your application needs to customize these mixed functions, you can find them in the configuration file.

3.3 Memory Management

Many box2d designs aim to quickly and effectively use the memory. In this section, I will discuss how and why box2d allocates memory.

Box2d tends to allocate a large number of small objects (about 50-300 bytes ). In this way, it is too inefficient to allocate memory on the heap of the system through malloc or new, and it is easy to generate memory fragments. Most of these small objects have a short life cycle. For example, a contact may only maintain several time steps. Therefore, we need to provide a valid heap Allocator for these objects ).

Box2d uses a small object distributor (SOA) named b2blockallocator. SOA maintains some memory pools of varying sizes and scalability. When there is a memory allocation request, SOA returns the most matched memory. When a memory block is released, it is recycled to the pool. These operations are very fast, with only a small amount of heap traffic.

Because box2d uses SOA, you should never go to new or malloc with a body, fixture or joint. You only need to allocate a b2world which provides the factory for creating the body, fixture, and joint ). This allows box2d to use SOA and hide naked details. Do not delete or free a body, fixture, or joint.

When you execute a time step, box2d requires some temporary memory. Therefore, it uses a stack distributor to eliminate single-step heap allocation, which is named b2stackallocator. You don't need to care about the stack distributor, but it is good to know about it.

3.4 mathematics

Box2d contains a simple and fine-grained vector and matrix module to meet the internal and API needs of box2d. All classes are public, and you can use them freely in your own applications.

The mathematical library is kept as simple as possible, making box2d easy to transplant and maintain.



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.