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.
dotnet add package RecursiveAI.OnePhysicsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
The snippet prints the default downward acceleration in pixels per second squared:
Gravity Y: -313.92The 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.
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")); }
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):
Gravity = new(0, -160)It prints Gravity Y: -160.00: explicit gravity is already in pixels per second squared.
Related recipes
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.
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. 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.