Notes 31-cocos2d-x-3.2 box2d physical engine fixture

Source: Internet
Author: User

Previous review

This famous saying goes:Good wood does not grow in good condition; the stronger the wind, the stronger the tree.[Mariote]

In the previous article, we learned about the body class in the dynamics module, covering everything from definition to creation and usage. This is actually a problem (this is done later in this article ).

Continue to learn the classes in the dynamics module. This time we will learn the fixture class.

 

Introduction

We know that shape can be used independently of the body for physical simulation. Therefore, box2d requires the fixture class to append the shape to the body. One body can have one or more fixture. A body with multiple fixture is usually called a compound body.

Fixture includes the following:

Has a separate shape

Broad-phase proxy

Density, friction, recovery characteristics

Collision filter mark

Pointer to the parent body

User Data

Sensor indicators

Fixture Creation

Fixture is created by initializing the fixture definition. Then, see pass these definitions to the parent body.

B2fixturedef fixturedef; // fixture Definition

Fixturedef. Shape = & myshape; // Add a shape

Fixturedef. density = 1.0f; // define the density.

B2fixture * myfixture = mybody-> createfixture (& fixturedef); // create a fixture by defining and pass it to the body.

The b2fixture variable does not need to be saved because it is automatically destroyed when the body is destroyed.

Density

Density is used to calculate the quality attribute of the parent body. We 'd better set fixture to something similar to this to facilitate the stability of the stack. In addition, the body quality does not change immediately as the density changes.

Body-> resetmassdata.

 

Friction

Box2d provides static and dynamic friction, but uses the same parameter.

We learned physics, and the friction is equal to the positive pressure multiplied by the friction coefficient. We can set the friction coefficient to 0 to 1, but it is actually any non-negative.

The friction coefficient of two shapes is obtained by multiplication. Therefore, if the friction coefficient of a shape is 0, there is no friction between them.

 

Restore

Recovery is used to set the physical rebound. It is usually set to 0 to 1. If it is set to 0, the physical will not rebound.

If it is 1, it will rebound completely. It's like a bullet ball. When the speed is very small, it will not rebound. This is mainly used to prevent jitter.

 

Filter

Filtering allows us to filter out the collisions we don't need. For example, if a role is riding a bicycle, we need a collision between the bicycle and the ground, but the collision between the role and the automatic car is not required. We can implement this through filtering.

Box2d provides the collision type in 16. We can define one of fixture.

You can also define a group, but the group filtering priority is higher than the category filtering priority.

Box2d comes with some filtering mechanisms:

Static body can only conflict with dynamic body

The running body can only conflict with the dynamic body.

Fixture on the same body will never collide.

You can manually choose whether to enable Collision for Fixture connected to a connection organization.

 

Sensor

Sometimes we need to know that the fixture is overwritten, but no action is required. This requires sensors.

We can mark a fixture as a sensor. The sensor can be dynamic, static, and moving.

The sensor does not generate contact points. We have two ways to obtain the sensor:

B2contact: istouching

B2contactlistener: begincontact andendcontact

Summary

This time, I learned the fixtures class in the dynamcis module. I don't know how to explain it clearly. I hope I can make a mistake. Later I learned the joints class.

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.