box2d README

The project's README from github.com/erincatto/box2d. As one markdown file: README.md.

Box2D

Box2D is a 2D physics engine for games.

Box2D Logo

Box2D Version 3.0 Release Demo

Build Status

Build Status

Features

Collision

Physics

System

Samples

Building All Platforms

Building with CMake presets

The presets in CMakePresets.json give one build flow on every platform and are picked up automatically by Visual Studio, VS Code, and CLion (open the folder and choose a preset). From the command line:

Use the *-debug build presets for a debug build (not recommended for the replay viewer). The presets use the default native toolchain (the installed Visual Studio on Windows, Make on Linux, Xcode on macOS), so no specific compiler version is required.

Building for Visual Studio

Building for Linux

Building for Xcode

Building and installing

Installing also provides a pkg-config file, so pkg-config --modversion box2d reports the installed version and pkg-config --cflags --libs box2d gives the build flags.

Building with zig

Fetch and link Box2D from a Zig project:

In build.zig:

const box2d_dep = b.dependency("box2d", .{});
exe.root_module.addImport("box2d", box2d_dep.module("box2d"));

In Zig code, start using box2d

const box2d = @import("box2d");

pub fn main(init: std.process.Init) !void {
    var world_def = box2d.b2DefaultWorldDef();
    world_def.gravity.y = 9.8;
    const world_id = box2d.b2CreateWorld(&world_def);
}

Building with Swift Package Manager

Add box2d as a dependency in your Package.swift:

.package(url: "https://github.com/erincatto/box2d.git", from: "main")

And add the product to your target:

.target(
    name: "MyGame",
    dependencies: [.product(name: "box2d", package: "box2d")]
),

In Swift code, the C API is imported directly:

import box2d

var worldDef = b2DefaultWorldDef()
let worldId = b2CreateWorld(&worldDef)

C structs are passed as inout arguments using &.

Replay viewer

The samples app doubles as a viewer for Box2D recordings (.b2rec files). Any preset above builds it. Pass a recording on the command line to open it directly:

On Windows you can also drag a .b2rec file onto samples.exe. The viewer runs from any directory. Without an argument, open a recording from the Replay menu. See docs/recording.md for how to make a recording.

Compatibility

The Box2D library and samples build and run on Windows, Linux, and Mac.

You will need a compiler that supports C17 to build the Box2D library.

You will need a compiler that supports C++20 to build the samples.

Box2D uses SSE2 and Neon (AArch64) SIMD math to improve performance. This can be disabled by defining BOX2D_DISABLE_SIMD.

Documentation

Community

Contributing

Please do not submit pull requests. Instead, please file an issue for bugs or feature requests. For support, please visit the Discord server.

Giving feedback

Please file an issue or start a chat on discord. You can also use GitHub Discussions.

License

Box2D is developed by Erin Catto and uses the MIT license.

Sponsorship

Support development of Box2D through Github Sponsors.

Please consider starring this repository and subscribing to my YouTube channel.

LLM Usage

LLMs are used in the following areas:

Elsewhere all code is developed and written by me. I take responsibility for every line of code in Box2D/3D.

External ports, wrappers, and bindings (unsupported)

box2d (erincatto/box2d)

Searching Database…

10.4K ·GitHub
这是什么?Box2D 是一个用于游戏的二维物理引擎,负责碰撞检测、刚体动力学和关节等功能,让你能在游戏中模拟逼真的物理效果。
这是完整的游戏引擎,还是只是我可以接入自己项目的物理库?它是一个 C 语言库,你需要将其编译并链接到自己的游戏或引擎中。它使用可移植的 C17 编写,不是独立的引擎或编辑器。
如何编译并把 Box2D 添加到项目中?安装 CMake 和 git,然后使用随附的 CMake 预设,例如先运行 'cmake --preset windows',再运行 'cmake --build --preset windows-release'(也有类似的 linux-release/macos 预设);或者手动使用 'cmake .. && cmake --build . && cmake --install .' 进行构建。
编译前需要先安装什么?每个平台都需要 CMake 和 git;库本身需要支持 C17 的编译器;如果还要编译随附的示例程序,则还需要支持 C++20 的编译器。
它只能在特定平台上运行吗?该库和示例程序可以在 Windows、Linux 和 macOS 上使用各自的原生工具链(分别为 Visual Studio、Make、Xcode)编译和运行。
我能在 Zig 或 Swift 中使用 Box2D,而不是 C/C++ 吗?可以。它可以通过 'zig fetch --save git+...' 作为 Zig 包获取,也可以通过 Swift Package Manager 添加为依赖项;在 Swift 中,C API 会被直接导入,结构体则通过 '&' 以 inout 参数的形式传递。
回放查看器有什么用?示例程序同时也是录制的物理模拟会话(.b2rec 文件)的查看器;你可以在命令行中传入录制文件,或在 Windows 上把它拖到 samples.exe 上来回放。
它支持哪些形状和关节?它支持凸多边形、胶囊体、圆形、圆角多边形和链条,以及旋转关节、平移关节、距离关节、鼠标关节、焊接关节和轮子关节,并支持限位、马达、弹簧和摩擦力。
它是否使用多线程或 SIMD 来提速?是的,Box2D 广泛使用多线程以及 SSE2/Neon SIMD 数学运算来提升性能;如有需要,可以通过编译选项将其关闭。
Creator
erincatto
Size
18 MB
Stars
10.4K
Age
11y
Commits · 90 Days