Box2D.NativeBindings 3.1.1.3

dotnet add package Box2D.NativeBindings --version 3.1.1.3
                    
NuGet\Install-Package Box2D.NativeBindings -Version 3.1.1.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Box2D.NativeBindings" Version="3.1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Box2D.NativeBindings" Version="3.1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="Box2D.NativeBindings" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Box2D.NativeBindings --version 3.1.1.3
                    
#r "nuget: Box2D.NativeBindings, 3.1.1.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Box2D.NativeBindings@3.1.1.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Box2D.NativeBindings&version=3.1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=Box2D.NativeBindings&version=3.1.1.3
                    
Install as a Cake Tool

Box2D.NativeBindings

A high-performance .NET 8, 9, and 10 wrapper for the Box2D physics engine (v3).

Box2D.NativeBindings provides direct C# bindings to the modern Box2D C API, utilizing the new Handle/ID-based architecture (WorldId, BodyId, ShapeId) for improved performance and memory management.

Features

  • Box2D v3 API: targets the latest iteration of Box2D with the new C-style API.
  • High Performance: Uses unsafe code and P/Invoke with minimal overhead.
  • Zero-Allocation: Heavy usage of ref structs and stack allocation to minimize GC pressure.
  • Modern .NET: Built for .NET 8, 9, and 10.
  • NuGet Package: easy installation via Box2D.NativeBindings.
  • Cross-Platform: Includes native binaries for Windows, Linux, and macOS (x64).

Prerequisites

  • .NET 8.0 SDK or newer.

Getting Started

Installation

Install the package via NuGet:

dotnet add package Box2D.NativeBindings

Basic Usage

The API mirrors the C-style Box2D v3 API but adapted for C#.

using System.Numerics;
using Box2D;
using Box2D.Collision;
using Box2D.Types;
using Box2D.Types.Bodies;
using Box2D.Types.Shapes;

// 1. Initialize the World
WorldDef worldDef = WorldDef.Default();
worldDef.Gravity = new Vector2(0, -10f);

using World world = new World(ref worldDef);

// 2. Create a Ground Body
BodyDef groundBodyDef = BodyDef.Default();
groundBodyDef.Position = new Vector2(0.0f, -10.0f);
Body groundBody = new Body(world.Id, ref groundBodyDef);

// 3. Add a Shape to the Ground
Polygon groundBox = Polygon.MakeBox(50.0f, 10.0f);
ShapeDef groundShapeDef = ShapeDef.Default();
Shape groundShape = new Shape(groundBody.Id, ref groundShapeDef, ref groundBox);

// 4. Create a Dynamic Body
BodyDef bodyDef = BodyDef.Default();
bodyDef.Type = BodyType.DynamicBody;
bodyDef.Position = new Vector2(0.0f, 4.0f);
Body body = new Body(world.Id, ref bodyDef);

// 5. Add Shape to Dynamic Body
Polygon dynamicBox = Polygon.MakeBox(1.0f, 1.0f);
ShapeDef shapeDef = ShapeDef.Default();
shapeDef.Density = 1.0f;
shapeDef.Material.Friction = 0.3f;
Shape bodyShape = new Shape(body.Id, ref shapeDef, ref dynamicBox);

// 6. Step the Simulation
float timeStep = 1.0f / 60.0f;
int subStepCount = 4;

Console.WriteLine("Simulation started...");
for (int i = 0; i < 60; ++i)
{
    world.Step(timeStep, subStepCount);
    Vector2 position = body.GetPosition();
    Console.WriteLine($"Step {i}: Position={position}");
}

Running the Demo

A sample project is provided in Box2D.Demo.

dotnet run --project Box2D.Demo

Architecture

This wrapper differs from previous C# ports (like Box2DX or older Box2DSharp versions) by strictly adhering to the Handle-based design of Box2D v3. Instead of managing object lifecycles directly, you manipulate simulation objects via IDs (BodyId, ShapeId), which ensures memory safety and cache coherence on the native side.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.1.3 342 1/1/2026
3.1.1.2 139 12/27/2025
3.1.1.1 127 12/27/2025