Math Module

The Math module provides essential types for 2D physics calculations in Box2D. It includes vectors, transformations, rotations, matrices, and bounding boxes that form the foundation of the physics engine.

Key design principles of the Math module:

  • Immutable objects: Operations return new instances rather than modifying existing ones

  • Pythonic interfaces: Classes support Python operators, iteration protocols and unpacking

  • Robust calculations: Handles edge cases safely with proper numerical handling

  • Box2D integration: All classes provide conversion to and from Box2D’s native C++ types

Vec2

Rot

Transform

ScaledTransform

AABB

Mat22

Utility Functions

box2d.math.to_vec2(vec: Iterable[float] | b2Vec2_zero) Vec2[source]

Convert a VectorLike object into a Vec2 instance, checking that it has exactly 2 elements. If ‘vec’ is already a Vec2, it is returned as is.

box2d.math.format_num(n: float) str[source]

Format a float with 3 decimal places but trim trailing zeros.

Parameters:

n (float) – The number to format.

Returns:

The formatted number as a string.

Return type:

str