PHP code example of php-io-extensions / box2d

1. Go to this page and download the library: Download php-io-extensions/box2d library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-io-extensions / box2d example snippets


$world = b2CreateWorld(b2DefaultWorldDef());                 // gravity (0, -10)

$ground = b2CreateBody($world, b2DefaultBodyDef());
b2CreatePolygonShape($ground, b2DefaultShapeDef(), b2MakeBox(50.0, 1.0));

$def = b2DefaultBodyDef();
$def->type = b2_dynamicBody;
$def->position = new b2Vec2(0.0, 4.0);
$box = b2CreateBody($world, $def);
$shape = b2DefaultShapeDef();
$shape->density = 1.0;
$shape->material->friction = 0.3;
b2CreatePolygonShape($box, $shape, b2MakeBox(1.0, 1.0));

for ($i = 0; $i < 90; $i++) {
    b2World_Step($world, 1.0 / 60.0, 4);
}
$p = b2Body_GetPosition($box);
printf("%.2f %.2f\n", $p->x, $p->y);                         // 0.00 2.00: at rest on the ground

b2DestroyWorld($world);
console
$ php falling-box.php
0.00 2.00
bash
git clone https://github.com/php-io-extensions/box2d.git
cd box2d

./install-debian-trixie.sh             # Debian Trixie, Ubuntu 24.04+, Raspberry Pi OS: the php on PATH
./install-macos.sh                     # Homebrew [email protected] and [email protected]
./install-macos.sh /path/to/bin/php    # either script takes specific PHP binaries
bash
git clone https://github.com/php-io-extensions/box2d.git
cd box2d
phpize && ./configure --enable-box2d && make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)"
composer install
mkdir -p /tmp/box2d-ini && echo "extension=$PWD/modules/box2d.so" > /tmp/box2d-ini/box2d.ini
PHP_INI_SCAN_DIR=":/tmp/box2d-ini" php -d memory_limit=128M vendor/bin/pest