---
title: "Set 2D physics units to pixels per meter in C#"
url: https://recursiveai.org/onephysics/cookbook/set-physics-world-units-to-pixels/
description: "Set a 2D physics world to 32 pixels per meter in C#. See how OnePhysics scales default gravity and contact distance while keeping mass in kilograms."
version: "1.3.0"
---

# Set 2D physics units to pixels per meter in C#

**OnePhysics** is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePhysics) · [GitHub](https://github.com/recursive-ai-oss/onephysics-csharp) · [Docs](https://recursiveai.org/onephysics/docs/) · [Download example (.zip)](https://recursiveai.org/onephysics/cookbook/set-physics-world-units-to-pixels/set-physics-world-units-to-pixels.zip)

Set `LengthUnit.Pixels(32)` to use pixel coordinates and scale the world defaults.
OnePhysics keeps positive Y up, so flip Y if your renderer puts Y down.
Only defaults follow the unit; a value you set is in world units.

```shell
dotnet add package RecursiveAI.OnePhysics
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics2D;
var world = new PhysicsWorld2D(new()
{
    LengthUnit = LengthUnit.Pixels(32)
});
var gravity = world.Options.Gravity.Y;
Console.WriteLine(FormattableString.Invariant(
    $"Gravity Y: {gravity:F2}"));
```

Unzip [the example](https://recursiveai.org/onephysics/cookbook/set-physics-world-units-to-pixels/set-physics-world-units-to-pixels.zip), then `dotnet run`.

The snippet prints the default downward acceleration in pixels per second squared:

```text
Gravity Y: -313.92
```

## The full program

Create the same one-meter square in two worlds: `1 × 1` in meters and `32 × 32` in pixels. Neither supplies density or gravity.

```csharp
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics2D;

var meters = new PhysicsWorld2D();
var pixels = new PhysicsWorld2D(new WorldOptions2D
{
    LengthUnit = LengthUnit.Pixels(32)
});
var meterBox = meters.AddBox(new(1, 1));
var pixelBox = pixels.AddBox(new(32, 32));

Print(meters, meterBox, "m");
Print(pixels, pixelBox, "px");

static void Print(PhysicsWorld2D world, Body body, string unit)
{
    var gravity = world.Options.Gravity.Y;
    var distance = world.Options.SpeculativeDistance;
    Console.WriteLine(FormattableString.Invariant(
        $"Gravity Y: {gravity:F2} {unit}/s²; contact look-ahead: {distance:F2} {unit}; box mass: {body.Mass:F2} kg"));
}
```

```text
Gravity Y: -9.81 m/s²; contact look-ahead: 0.02 m; box mass: 1.00 kg
Gravity Y: -313.92 px/s²; contact look-ahead: 0.64 px; box mass: 1.00 kg
```

The speculative distance (how far ahead the solver looks for possible contacts) scales with gravity. The square’s mass does not: default density scales with the area unit. A zero, negative, or non-finite pixels-per-meter ratio is rejected with `ArgumentOutOfRangeException`.

## Variations

**Set your own gravity.** Add this field after `LengthUnit` in the snippet’s options initializer (with a comma after the `LengthUnit` field):

```csharp
Gravity = new(0, -160)
```

It prints `Gravity Y: -160.00`: explicit gravity is already in pixels per second squared.

## Related recipes

- [Simulate 2D rigid bodies](https://recursiveai.org/onephysics/cookbook/2d-physics/)
- [Create a collider from a sprite outline](https://recursiveai.org/onephysics/cookbook/create-collider-from-sprite-outline/)

## The short answer

Set `WorldOptions2D.LengthUnit` to `LengthUnit.Pixels(32)` when you construct `PhysicsWorld2D`. Supply positions, collider sizes, and distances in pixels. Continue to step the world in seconds and give angles in radians. See the [units guide](https://github.com/recursive-ai-oss/onephysics-csharp/blob/a5f45b1c8e34091892655a5f5536abb14d32528b/docs/guide/units.md#L19-L26).

## How do you convert meters to pixels in a 2D physics world?

Multiply a meter value by the pixels-per-meter ratio, or divide a pixel value by that ratio to get meters. At 32 pixels per meter, a 2-meter offset is 64 pixels, and a 16-pixel radius is half a meter. If your level already stores dimensions in pixels, pass those values directly to the pixel world. Convert only the values that come from data measured in meters. `AddBox` takes the full width and height, so a 32-pixel square takes `(32, 32)`, not `(16, 16)`.

Pixels are a length unit here, not a screen coordinate convention. OnePhysics still uses positive Y up and positive angles counterclockwise, as described in the [2D conventions](https://github.com/recursive-ai-oss/onephysics-csharp/blob/a5f45b1c8e34091892655a5f5536abb14d32528b/docs/guide/units.md#L4-L6). If your renderer puts positive Y down, flip Y when translating between the physics world and the screen. The camera can zoom without changing the world’s pixels-per-meter ratio.

## How does pixels per meter affect gravity and mass?

Default gravity of −9.81 m/s² becomes −313.92 px/s² because length is multiplied by 32 and seconds don’t change. A custom `Gravity` setting is different: if you copy `-9.81` into the pixel world’s options, that’s −9.81 **pixels** per second squared, not the same fall as in the meter world. The same rule applies to a custom contact distance: `5` is five pixels.

Density in 2D is mass per area, so the conversion uses the square of the ratio. One square meter covers `32 × 32 = 1,024` square pixels; 1 kg/m² is `1 / 1,024` kg/px². Leaving density unset lets OnePhysics make that adjustment. If your game sets an explicit density measured in kg/m², divide by `1,024` before passing it to the pixel world. A mass you set in kilograms needs no conversion.
