Calculate Rotational Kinetic Energy in Matter.js
This article explains how to determine the rotational kinetic energy of an irregularly shaped spinning body using the Matter.js 2D physics engine. By understanding the core rotational physics formula and leveraging the built-in properties that Matter.js calculates for custom rigid bodies—specifically moment of inertia and angular velocity—you can accurately compute rotational energy at any frame during a simulation.
The Rotational Kinetic Energy Formula
In classical mechanics, the rotational kinetic energy (\(E_r\)) of a rotating rigid body is defined as:
\[E_r = \frac{1}{2} I \omega^2\]
Where:
- \(I\) is the moment of inertia (resistance to angular acceleration).
- \(\omega\) is the angular velocity (speed of rotation in radians per unit time).
How Matter.js Handles Irregular Shapes
When you create an irregularly shaped body in Matter.js using
Matter.Bodies.fromVertices, the engine decomposes the
complex concave polygon into convex sub-parts. It automatically computes
the body's total mass, center of mass, and moment of inertia using the
geometry of these parts.
Matter.js exposes the variables required for the calculation directly
on the Body instance:
body.inertia: The calculated moment of inertia (\(I\)).body.angularVelocity: The current angular velocity (\(\omega\)) in radians per engine update step.
Implementation Steps
1. Create the Irregular Body
Define your irregular shape using a set of vertices and instantiate
it using Bodies.fromVertices:
const { Bodies, Body, Vector } = Matter;
// Define the vertices of an irregular shape
const vertices = [
{ x: 0, y: 0 },
{ x: 40, y: -20 },
{ x: 60, y: 30 },
{ x: 20, y: 50 },
{ x: -30, y: 20 }
];
// Create the irregular body
const irregularBody = Bodies.fromVertices(400, 300, vertices, {
frictionAir: 0.001 // Low air resistance to sustain rotation
});2. Apply a Spin
To generate rotational kinetic energy, apply an angular velocity or torque:
// Set a spin directly
Body.setAngularVelocity(irregularBody, 0.15); // radians per step3. Calculate Rotational Kinetic Energy
Access body.inertia and
body.angularVelocity to evaluate the kinetic energy
formula:
function getRotationalKineticEnergy(body) {
const inertia = body.inertia;
const omega = body.angularVelocity;
return 0.5 * inertia * (omega * omega);
}
// Example usage
const rotKE = getRotationalKineticEnergy(irregularBody);
console.log(`Rotational Kinetic Energy: ${rotKE}`);Continuous Tracking
To monitor rotational energy dynamically, calculate the value inside the engine's update loop:
Matter.Events.on(engine, 'afterUpdate', () => {
const currentRotKE = getRotationalKineticEnergy(irregularBody);
// Use or display the energy value
});Considerations
- Total Kinetic Energy: If the body is also moving through space (translating), total kinetic energy is the sum of translational kinetic energy (\(\frac{1}{2} m v^2\)) and rotational kinetic energy (\(\frac{1}{2} I \omega^2\)).
- Units: Matter.js operates in arbitrary simulation units. Velocity is defined as units per engine tick rather than per second, meaning calculated kinetic energy is relative to the simulation's coordinate system and time-step configuration.