What Vector Math Functions Does Planck.js Provide?

Planck.js, a 2D physics engine rewritten in JavaScript for cross-platform games, provides a dedicated Vec2 class equipped with utility functions for essential 2D vector math manipulations. This article outlines the core methods available in Planck.js for managing vector operations—such as addition, subtraction, scaling, dot products, and cross products—which are crucial for calculating forces, velocities, and positions within a physics simulation.

Core Vector Creation and Manipulation

At the heart of motion and geometry in Planck.js is the Vec2 object. Rather than forcing developers to manage raw coordinate objects, the engine includes built-in methods to create, clone, and modify 2D vectors efficiently.

Basic Geometric Operations

Planck.js simplifies standard algebraic operations on vectors, offering both static methods (which return a new vector) and instance methods (which often mutate the vector to optimize performance).

Addition and Subtraction

Scaling and Inversion

Advanced Vector Mathematics

For physics calculations like collision response, angular momentum, and directional alignment, Planck.js exposes standard geometric products.

Dot and Cross Products

Length and Normalization

Distance and Utility Methods

Planck.js also includes helper functions to evaluate the spatial relationships between different points in the simulation environment.