---
title: "Map a world position to a pathfinding grid cell in C#"
url: https://recursiveai.org/onepath/cookbook/convert-world-position-to-pathfinding-grid-cell/
description: "Map a world position to a pathfinding grid cell in C# with OnePath. Set the grid origin and cell size, handle outside points, and get the cell center."
version: "1.2.0"
---

# Map a world position to a pathfinding grid cell in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/convert-world-position-to-pathfinding-grid-cell/convert-world-position-to-pathfinding-grid-cell.zip)

Set the square grid’s origin and cell size before mapping a point.

```shell
dotnet add package RecursiveAI.OnePath
```

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

```csharp
using RecursiveAI.OnePath;
var grid = new Grid(4, 3);
grid.Space = new GridSpace(10, -4, 2);
var point = new WorldPoint(13, -1);
if (grid.TryGetCell(point, out var cell))
    Console.WriteLine($"({cell.X}, {cell.Y})");
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/convert-world-position-to-pathfinding-grid-cell/convert-world-position-to-pathfinding-grid-cell.zip), then `dotnet run`.

Prints the zero-based column and row:

```text
(1, 1)
```

## The full program

The square grid starts at the corner `(10, -4)` and each cell is two world units wide and tall. The program tries a point inside, one on a shared cell edge, and two outside the grid.

```csharp
using RecursiveAI.OnePath;

var grid = new Grid(4, 3) { Space = new GridSpace(10, -4, 2) };
WorldPoint[] points = [new(13, -1), new(12, -2), new(9.5, -1), new(18, -1)];

foreach (var point in points)
{
    string label = FormattableString.Invariant($"({point.X}, {point.Y})");
    if (!grid.TryGetCell(point, out var cell))
    {
        Console.WriteLine($"{label} -> outside grid");
        continue;
    }

    var center = grid.CellCenter(cell);
    Console.WriteLine(FormattableString.Invariant(
        $"{label} -> ({cell.X}, {cell.Y}), center ({center.X}, {center.Y})"));
}
```

```text
(13, -1) -> (1, 1), center (13, -1)
(12, -2) -> (1, 1), center (13, -1)
(9.5, -1) -> outside grid
(18, -1) -> outside grid
```

`TryGetCell` returns `false` for a point outside the grid; NaN, infinity, or a coordinate outside ±2²¹ instead throws `ArgumentOutOfRangeException`.

## Variations

**Get the world position at the center of a known cell.** Use this when placing an object on a grid coordinate:

```csharp
using RecursiveAI.OnePath;

var grid = new Grid(4, 3);
grid.Space = new GridSpace(10, -4, 2);
var center = grid.CellCenter(new Cell(0, 0));
Console.WriteLine($"({center.X}, {center.Y})");
```

```text
(11, -3)
```

## Related recipes

- [Find a path on a grid](https://recursiveai.org/onepath/cookbook/grid-pathfinding/)
- [Prevent diagonal corner cutting](https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/)

## The short answer

Set `grid.Space` to `new GridSpace(originX, originY, cellSize)` and call `grid.TryGetCell(point, out var cell)`. Use the returned `Cell` as a grid coordinate only when the method returns `true`. On a square grid, `originX` and `originY` are the world coordinates of the **corner** of cell `(0, 0)`, not its center. The default space has origin `(0, 0)` and size `1`.

## How do you calculate grid coordinates from a world position?

Subtract the origin, divide by the cell size, then round **down** on each axis. For the example point `(13, -1)` and origin `(10, -4)`, the offset is `(3, 3)`. Dividing by `2` gives `(1.5, 1.5)`, so the cell is `(1, 1)`. Column numbers increase with X and row numbers increase with Y. If your world uses a different pair of axes, supply the two coordinates of your pathfinding plane consistently when defining the origin and converting points.

Pick the origin where the first cell starts in your scene. If you know the world position of the first tile’s center instead, subtract half a cell along both axes to get the origin. `CellCenter` does the opposite: it adds half a cell to the cell’s corner. That gives you a position to draw or aim at inside the cell, not necessarily the original point that mapped there.

A square cell includes its smaller-coordinate edges but not its larger-coordinate edges. That’s why `(12, -2)`, directly on two internal boundaries in this example, lands in `(1, 1)` rather than `(0, 0)`. At the far edge, X is `10 + 4 × 2 = 18`. A point with X exactly `18` maps to column `4`, which is outside a four-column grid. On the near side, X `9.5` gives `−0.25` after subtracting the origin and dividing by cell size. Rounding down gives `−1`; casting straight to `int` would truncate to `0` and incorrectly put that point inside.

Finding a cell only tells you that the position lies within the grid. It doesn’t tell you whether that cell is walkable. If you’re selecting a destination, check walkability separately or let the path query tell you whether it can use the destination. The corner convention and divide-and-floor rule above are for `Grid`: on a `HexGrid`, the origin is the center of cell `(0, 0)` and the mapping uses the hex layout.
