PyBox2D
raw JSON → 2.3.10 verified Fri May 01 auth: no python maintenance
Python bindings for Box2D, a 2D physics engine. Version 2.3.10 is the latest. Releases are infrequent; the library is stable but in maintenance mode.
pip install box2d Common errors
error ModuleNotFoundError: No module named 'box2d' ↓
cause Importing from 'box2d' (lowercase) but the correct module is 'Box2D'.
fix
from Box2D import ...
error AttributeError: module 'Box2D' has no attribute 'b2World' ↓
cause Usually due to a broken installation or mismatched package versions; also can happen if importing from 'Box2D' but the library is not properly installed.
fix
Reinstall via 'pip install --upgrade box2d' and verify import using 'import Box2D; print(dir(Box2D))'.
Warnings
gotcha The import module is 'Box2D' (capital B and D), not 'box2d' (lowercase). The PyPI package name is 'box2d' but the module name uses CamelCase. ↓
fix Use 'from Box2D import ...' instead of 'from box2d import ...'.
deprecated Python 2 is no longer supported. Ensure you are using Python 3. ↓
fix Use Python 3.6 or newer.
gotcha Setting body position using the 'position' attribute after creation may not work; use SetTransform() instead. ↓
fix Use body.position.Set(x, y) before creation or body.SetTransform(b2Vec2(x,y), angle) after creation.
Imports
- Box2D
from Box2D import b2World - b2World wrong
from box2d import b2Worldcorrectfrom Box2D import b2World
Quickstart
from Box2D import b2World, b2Vec2, b2BodyDef, b2_dynamicBody, b2CircleShape, b2FixtureDef
import math
world = b2World(gravity=b2Vec2(0, -10))
body_def = b2BodyDef()
body_def.type = b2_dynamicBody
body_def.position.Set(0, 10)
body = world.CreateBody(body_def)
circle = b2CircleShape(radius=1)
fixture_def = b2FixtureDef(shape=circle, density=1, friction=0.3)
body.CreateFixture(fixture_def)
for i in range(60):
world.Step(1/60, 6, 2)
print(body.position.y)