Default Friction Value in Matter.js Bodies
When simulating 2D physics in web applications using the Matter.js
engine, understanding default physical properties is essential for
predictable behavior. In a Matter.js body, the default value for
standard kinetic friction is 0.1. This article explains how
Matter.js handles friction by default, covers related friction
properties like static and air resistance, and demonstrates how to
customize these settings for rigid bodies.
The Default Friction Value
In Matter.js, the standard friction property defines the
kinetic (sliding) friction of a rigid body. When a body is instantiated
without explicitly specifying this property, the engine assigns it a
default value of 0.1.
Friction values typically range from 0 to
1:
0: Represents a completely frictionless surface where objects slide indefinitely.1: Represents high friction where movement against another surface is significantly resisted. Values greater than1are allowed to simulate extremely rough surfaces.
When two bodies collide or slide against one another, Matter.js
calculates the effective friction between them using the minimum value
of both bodies:
Math.min(bodyA.friction, bodyB.friction).
Related Friction Properties
Matter.js distinguishes between different types of friction.
Alongside the standard kinetic friction, bodies feature two
additional friction settings:
frictionStatic(Default:0.5): Defines the friction required to start an object moving from a complete standstill. It must be overcome before standard kinetic friction applies.frictionAir(Default:0.01): Simulates aerodynamic drag or air resistance. A higher value slows down linear movement through empty space, while a value of0creates a vacuum-like environment.
Setting Custom Friction
To override the default friction values, pass the desired configurations into the options object when creating a body:
const box = Matter.Bodies.rectangle(400, 200, 80, 80, {
friction: 0.05, // Custom kinetic friction (default: 0.1)
frictionStatic: 0.2, // Custom static friction (default: 0.5)
frictionAir: 0.005 // Custom air resistance (default: 0.01)
});You can also modify the friction dynamically during runtime using
Matter.Body.set:
Matter.Body.set(box, 'friction', 0.8);