Configs2026年5月31日·1 分钟阅读

Box2D — 2D Physics Engine for Games

Industry-standard rigid body physics simulation library for 2D games, providing collision detection, joints, and continuous physics.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Box2D
直接安装命令
npx -y tokrepo@latest install c43b858b-5d2f-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Box2D is the most widely used open-source 2D physics engine in game development. Created by Erin Catto, it powers physics in thousands of games across every major platform and has been ported to virtually every programming language used in game development.

What Box2D Does

  • Simulates rigid body dynamics with gravity, forces, impulses, and torque
  • Detects collisions between circles, polygons, edges, and chain shapes
  • Provides constraint solvers for joints (revolute, prismatic, distance, weld, gear, motor)
  • Implements continuous collision detection (CCD) to prevent fast-moving objects tunneling
  • Supports sensors, contact filtering, and pre/post-solve event callbacks

Architecture Overview

Box2D v3 is written in C (previously C++) with a data-oriented design. The world owns arrays of bodies, shapes, joints, and contacts stored in contiguous memory. The broadphase uses a dynamic AABB tree for spatial queries. Each simulation step runs broadphase overlap detection, narrowphase contact generation, and an iterative constraint solver (Split Impulse PGS) in sequence.

Self-Hosting & Configuration

  • Build with CMake; the library has zero external dependencies
  • Link the static or shared library and include box2d/box2d.h
  • Configure world gravity, sleep thresholds, and solver iterations at creation time
  • Tune substep count for stability versus performance tradeoff
  • Language bindings exist for C#, Java, Python, JavaScript, Rust, and Go

Key Features

  • Deterministic simulation when using fixed timestep and identical input
  • SIMD-optimized collision detection and solver (AVX2, NEON)
  • Multi-threaded world stepping via task system in v3
  • Built-in debug draw interface for visualizing shapes, joints, and contacts
  • Minimal memory allocator with pool-based allocation for predictable performance

Comparison with Similar Tools

  • Chipmunk2D — similar scope but less actively maintained; Box2D has broader community
  • Rapier — Rust-native physics with 2D and 3D; Box2D is C-based with more language ports
  • LiquidFun — Google fork adding particle simulation; Box2D v3 supersedes its improvements
  • Matter.js — JavaScript 2D physics; Box2D is faster and more precise
  • Planck.js — JavaScript rewrite of Box2D; faithful port but limited to web

FAQ

Q: Should I use Box2D v2 or v3? A: v3 (current main branch) is a rewrite in C with better performance and a cleaner API. New projects should use v3.

Q: Can Box2D handle hundreds of bodies at 60fps? A: Yes. v3 with SIMD and multithreading handles thousands of bodies on modern hardware.

Q: Is Box2D suitable for non-game simulations? A: It is designed for real-time interactive use; scientific accuracy is not its goal, but it works well for robotics prototyping and UI physics.

Q: How do I integrate Box2D with my rendering? A: After each step, read body positions and angles, then draw your sprites at those transforms. Box2D has no rendering code.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产