Temp - ECNUPendulumRot/box3d-wiki GitHub Wiki

Fixtures

The function of b3Fixture is to attach a shape to a Body. A Body does not know its own shape. It contains one or more Fixtures, and each Fixture corresponds to a Shape. So multiple Shapes can be assembled into a Body.

Fixtures mainly include

  • a single shape
  • Broad-phase proxies
  • density, friction and restitution
  • pointer to the parent body

Fixture Creation

To simplify the process of creating a Fixture, the structure FixtureDef is provided, and the user sets the FixtureDef and uses it to initialize the Fixture.

b3Body* body;
b3SphereShape SphereShape;
b3FixtureDef fixtureDef;
fixturDef.m_shape = &SphereShape;
fixtureDef.m_density = 0.0;
fixtureDef.m_restitution = 1.0;
fixtureDef.m_friction = 0.0;
b3Fixture* fixture = body->create_fixture(fixtureDef);

This will create the Fixture pointer in the body, so there is no need to show the creation. And internally it will copy the shape on the heap, no need to worry about creating the Shape in a local scope and attaching it to the body via Fixture.

Density

The density in the Fixture is used to calculate the mass and inertia matrix of the body, it should be greater than or equal to 0. The reset_mass_data() function in the body is automatically called when it is created.If you want to get the shape mass held by a certain Fixture, you can get it by calling get_mass_data.

b3Fixture* fixture;
b3MassProperty massData;
fixture->get_mass_data(massData);

The relevant information is stored in massData.

Friction

Friction is used to simulate the sliding between objects. It is divided into static friction and dynamic friction. Box3d uses Coulomb's law, that is, the magnitude of friction is proportional to the friction coefficient and the support force (normal force). The friction coefficient is generally between 0 and 1. Of course, it can be designed to other values ​​to express certain special scenes. When there is contact between objects, it is used to model the tangential force (impulse) between objects.

b3Body* body;
b3FixtureDef def;
def.m_friction = 0.5;
b3Fixture* fixture = body->create_fixture(def);

Restitution

The coefficient of restitution is used to model the elasticity of an object, and can range from 0 to 1. When it is equal to 0, the collision between objects is a completely inelastic collision, for example, a small ball falling from the air to the ground will be stationary on the ground.When it is equal to 1, the collision between the objects is completely elastic collision, when the ball hit the ground will rebound, and will return to the height of the initial velocity of 0.When the velocity between objects is very small and the recovery coefficient is not 0, the object will shake, in order to deal with this situation, Box3d's solution is to set a threshold, when the relative normal velocity of the object is lower than the threshold, the recovery coefficient will be treated as 0 to deal with.

b3Body* body;
b3FixtureDef def;
def.m_restitution = 0.5;
b3Fixture* fixture = body->create_fixture(def);