Download the PHP package php-io-extensions/box2d without Composer

On this page you can find all versions of the php package php-io-extensions/box2d. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package box2d

A game, a simulation or an interactive display needs things to fall, slide, bounce and push each other, and to do it the same way every frame. Writing that in PHP means integrating velocities, finding which shapes overlap, resolving the overlaps without jitter, and keeping stacks of boxes from sinking into each other: a rigid-body solver, which is years of work to get stable and still too slow in PHP once the scene has a few hundred bodies.

Box2D is that solver. Erin Catto's engine has run the 2D physics in games for two decades, and version 3 is a rewrite in C: a world holds bodies, bodies carry shapes, joints tie bodies together, and one b2World_Step() per frame moves everything forward with contacts, friction, restitution and sensors resolved. It's deterministic, it parallelises across cores, and its documentation covers every call.

The only problem? PHP can't reach it. The route was FFI: parse box2d.h, declare every struct by hand, keep ids and callbacks straight across the C boundary, and run with ffi.enable on. This extension binds Box2D v3.1.1 one to one under the C names, with the library compiled into the module from an unchanged copy of the upstream tag, so there is nothing to install from the system and nothing for a package manager to version-skew. Structs are typed classes, ids are checked live before Box2D sees them, Box2D's assertions become exceptions instead of aborts, and a world can step on its own thread pool while your callbacks stay on the PHP thread. No defaults, no composites, no opinions: what Box2D documents is what you call.

Basic usage

Most ways you will interface with ext-box2d are in two parts -

Build the world. Every b2*Def comes from its b2Default*Def() function, you set the fields you care about, and the create functions hand back ids. Here a static ground box and a dynamic box above it:

Step it. One b2World_Step() per frame with the time step and sub-step count; read positions, velocities, contacts and events between steps. Destroy the world when you're done, which frees everything in it.

Requirements

OS Linux or macOS. composer.json lists those two families.
PHP 8.4 or newer, NTS or ZTS.
Libraries None. Box2D ships inside the extension, from tag v3.1.1 (commit 8c661469c9507d3ad6fbd2fea3f1aa71669c2fe3), copied unchanged into lib/box2d/ and compiled into the module. On Linux the module links pthread for the worker pool.

To build it you need a C compiler and the development files of the PHP you intend to load it into:

Installation

With PHP PIE (recommended)

PIE is the PHP Foundation's installer for extensions. It downloads the tagged source from Packagist, builds it against the PHP on your PATH, installs the module and enables it. sudo where your PHP's extension directory is root-owned, as on Debian; not on a Homebrew Mac:

Manually (install script)

The repository ships two installers, one per platform. Each one builds for each PHP it targets, installs box2d.so into that PHP's extension_dir, writes 30-box2d.ini into its conf.d scan directory, and checks the module loads. The macOS one builds in a temporary copy, ad-hoc signs the module so the system loads it on first use, and keeps an ini that already loads box2d instead of writing its own; the Debian one builds in the checkout, writes the ini into every conf.d of that PHP (CLI, FPM), and cleans the build products out afterwards.

Manually (phpize)

Then enable it: add extension=box2d.so to your php.ini, or drop that line into a file in the scan directory that php --ini reports.

Verify

API

Function, struct, enum and constant names are the C names, so Box2D's documentation is the reference: 505 functions, 72 classes and 27 constants, one stub per Box2D header in stubs/ (base, id, math_functions, collision, types, box2d), each declared with its PHP types.

Header Functions What it covers
box2d.h 313 Worlds, bodies, shapes, chains, the eight joint types, events, queries, casts, debug draw.
collision.h 72 Shapes as geometry: polygons, circles, capsules, segments, hulls, rays, manifolds, the dynamic tree.
math_functions.h 73 b2Vec2, b2Rot, b2Transform, b2AABB and the vector math on them.
types.h 17 The b2Default*Def() constructors: world, body, shape, chain, filters, surface material, every joint, explosion, debug draw.
id.h 10 Packing each id kind to an int and back (b2StoreBodyId(), b2LoadBodyId() …).
base.h 7 The version, the byte count, timers, b2Yield() and b2Hash().
values and macros 13 b2_nullBodyId, b2Vec2_zero, b2Transform_identity and the other static values, as functions.

Translations

The only differences from C:

Errors

Workers

Set b2WorldDef::$workerCount above 1 and the world gets its own pool of min(workerCount, 64) threads for its steps. b2World_Step() still blocks until the step is done; the pool only spreads it over more cores. Results are identical bit for bit for every worker count.

Friction, restitution, custom filter and pre-solve callables still run on the PHP thread that called b2World_Step(): a worker thread hands the call over and waits. A pool does not survive fork(), so stepping a pooled world in a child is an Error; serial worlds keep working. Each request's worlds, pools and dynamic trees are freed when the request ends.

Extensions you might use with this one

AI-assisted development

This codebase was built with LLM assistance under human direction. Prompts, reviews, and architectural decisions were owned by a professional software engineer — the model accelerates drafting and exploration; it does not replace judgment on APIs, dependency direction, or what ships.

Agentic Documentation

This repository carries an Open Knowledge Format bundle at .okf/: Markdown with YAML frontmatter, readable by you and loadable by a coding agent. It records what the README doesn't: every function, class and constant by header and the complete list of translations from C, each guard that runs before Box2D and the error it raises, how the worker pool is built, how callbacks reach the stepping thread, what fork() and request end do to a pool, and a log of every change that altered behaviour.

Start at AGENTS.md is the short rule sheet an agent reads before touching the C, the stubs, the vendored Box2D, or packaging; each rule points into the bundle for the reason behind it.

Point your agent at the repo and it has the same context the maintainer has. Point yourself at it when the README's answer is "it does X" and you want "because".

Resources

Running the test suite

The suite is Pest v4: worlds, bodies, shapes, chains, joints, events, geometry, the dynamic tree, debug draw, guards, callback errors, workers, and a lifecycle test that spawns child PHP processes to check pools, fork() and request end. It runs on Linux and macOS. Build the module, then enable it through an ini scan directory rather than -d, so the child processes the lifecycle test starts load it too:

The leading : keeps your PHP's own scan directory and adds the second one. If that PHP already loads an installed box2d.so, the suite warns Module "box2d" is already loaded and tests the installed one instead; copy the scan directory somewhere, replace its box2d ini with the line above, and point PHP_INI_SCAN_DIR at the copy without the colon. A module only loads in the PHP whose phpize built it: for Homebrew's ZTS build, run phpize and pass --with-php-config from /opt/homebrew/opt/[email protected]/bin. Set XDEBUG_MODE=off for any timing you intend to read.

Contributing

Open an issue first for anything beyond a typo. Pull requests change a stub in stubs/ and the C under src/ together, regenerate the *_arginfo.h files with gen_stub, and add a Pest test for the behaviour. lib/box2d/ is upstream and is never edited: a newer Box2D lands only by replacing it from a published tag. CI must be green on every cell of the matrix, NTS and ZTS alike. Read AGENTS.md for the rules the code follows.

Security

ext-box2d runs Box2D's C code on values from PHP, and threads when a world has workers. See SECURITY.md for what it guards against and how to report a vulnerability.

License

MIT. See lib/box2d/LICENSE.


All versions of box2d with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package php-io-extensions/box2d contains the following files

Loading the files please wait ...