Matter.Vector is a utility module for creating and manipulating 2D vectors. A vector in Matter.js is a plain JavaScript object with the shape { x: number, y: number }. This module provides essential methods for physics-related calculations such as addition, subtraction, rotation, and magnitude.
Most methods allow for an optional output parameter. If provided, the result is written into that object instead of creating a new one, which helps reduce garbage collection overhead in high-frequency physics loops.
// Example of creating and adding vectors
const v1 = Matter.Vector.create(10, 20);
const v2 = Matter.Vector.create(5, 5);
// Returns a new vector { x: 15, y: 25 }
const result = Matter.Vector.add(v1, v2);
// Using an output object to avoid allocation
const output = { x: 0, y: 0 };
Matter.Vector.add(v1, v2, output);