Applying Force vs Setting Velocity in Matter.js
In Matter.js, manipulating body movement is primarily achieved either
by applying a force or directly setting velocity. While applying a force
alters an object's acceleration gradually by respecting its mass and the
physics engine's time step, setting velocity instantly overwrites the
object's speed and direction regardless of its mass. Understanding the
mechanics, practical differences, and appropriate use cases of
Body.applyForce versus Body.setVelocity is
essential for building stable and realistic 2D physics simulations.
How Applying Force Works
Forces in Matter.js adhere to classic Newtonian mechanics (\(F = ma\)). When you use
Matter.Body.applyForce(body, position, force), you apply an
impulse vector to a specific coordinate on the body.
Matter.Body.applyForce(body, body.position, { x: 0.05, y: -0.05 });Key characteristics of applying a force include:
- Mass Dependence: A heavier object (greater mass) requires a larger force vector to achieve the same change in speed as a lighter object.
- Torque and Rotation: Because
applyForceaccepts a world position argument, applying a force away from the body's center of mass automatically induces angular velocity (torque), causing the body to spin realistically. - Continuous and Additive: Forces accumulate. They combine with environmental variables such as gravity, friction, and collision reactions during the engine update cycle.
How Setting Velocity Works
Setting velocity directly modifies the rate of displacement per
physics step using Matter.Body.setVelocity(body, velocity).
This bypasses the engine's internal force integration steps.
Matter.Body.setVelocity(body, { x: 5, y: -10 });Key characteristics of setting velocity include:
- Mass Independence: The object's mass is ignored. A feather and an immovable boulder will both travel at identical speeds if assigned the same velocity vector.
- Instantaneous Overwrite: Calling
setVelocityreplaces the current movement vector completely, discarding any momentum or trajectory the body previously possessed. - No Inherent Torque: Setting linear velocity moves
the body linearly without inducing spin, unless you independently modify
angular velocity using
Matter.Body.setAngularVelocity.
Key Differences Summary
| Feature | Applying Force
(Body.applyForce) |
Setting Velocity
(Body.setVelocity) |
|---|---|---|
| Physics Realism | High (accords with \(F = ma\)) | Low (direct kinematic override) |
| Mass Factor | Directly affects resulting acceleration | Ignored |
| Rotation | Generated automatically if applied off-center | None (linear motion only) |
| Accumulation | Adds to gravity, collisions, and other forces | Overwrites current speed and direction |
| Frame Consistency | Must often be applied continuously over time | Can be set once or set per-frame |
When to Use Which Method
Use Body.applyForce when simulating realistic
physical phenomena:
- Implementing thrusters, wind, water buoyancy, or magnetic fields.
- Simulating explosions or impulse-based impacts where the point of contact matters.
- Launching objects via springs, catapults, or slingshots.
Use Body.setVelocity when you need direct,
responsive control:
- Designing arcade-style character controllers where input must produce immediate movement without acceleration delays.
- Creating moving platforms, elevators, or conveyor belts that must move at fixed speeds regardless of weight placed on them.
- Completely halting an object's momentum (e.g., setting velocity to
{ x: 0, y: 0 }).