Physics Material

In Cocos Creator, the physics material is a asset, which records the surface information of the object. This is used to calculate the friction and elastic force of the colliding object.

Properties of Materials

The properties of Physics Materials are shown below:

physics material

PropertiesDescription
frictionCoefficient of friction
restitutionCoefficient of restitution

When in contact with other surfaces, these coefficients are used to calculate the corresponding friction and elastic forces.

Creating Physics Materials

Physics Materials can be created in two ways:

  1. Create in editor
  2. Code instantiation

The way to create with the editor is shown below:

Create Physics materials

Instantiated in the code:

  1. let newPmtl = new PhysicMaterial();
  2. newPmtl.friction = 0.1;
  3. newPmtl.restitution = 0.1;

Application of materials

The physics material is set in units of collision bodies, and each Collider has a material property (when not set, Collider will refer to the default physics material in the physics system). The application to Collider is also divided into editor operation and code operation.

To operate in the editor, just drag the asset into the cc.PhysicsMaterial property box, as shown in the following figure:

apply physics material

Operation in the code:

  1. const collider = this.node.getComponent(ColliderComponent);
  2. collider.material = newPmtl;

Because of the design of Material Sharing, you can actually do this directly in the code (because an instance will be created when you get material)

  1. collider.material.friction = 0.1;
  2. collider.material.restitution = 0.1;