GridMath
AUTO-GENERATED FILE – DO NOT EDIT MANUALLY
This page documents the supported public API surface only. Private, internal, benchmark, test, and implementation-detail types are intentionally omitted.
Declaration
Summary
Static utility class for grid-based mathematical operations.
Provides POCS implementation of grid positioning logic without Godot dependencies. Supports coordinate conversion, tile-based movement, and grid geometry calculations. Shared-safe: stateless and thread-safe; safe to reuse across user scopes.
Ported from: godot/addons/grid_building/shared/utils/positioning/gb_positioning_2d_utils.gd
Metadata
Namespace: GridPlacement.Core.Grid
Source File: cs/Core/Math/GridMath.cs
Assembly: GridPlacement.Core
Type: class
Methods
WorldToGrid
Convert a world position to tile coordinates.
Parameters
| Name | Description |
|---|---|
worldPosition | The world position to convert |
tileSize | Size of each tile in world units |
Returns
Grid position representing the tile coordinates
GridToWorld
Convert grid coordinates to world position (tile center).
Parameters
| Name | Description |
|---|---|
gridPosition | The grid position to convert |
Returns
World position at the center of the tile
GridToWorld
Convert grid coordinates to world position with custom tile size.
Parameters
| Name | Description |
|---|---|
gridPosition | The grid position to convert |
tileSize | Custom tile size |
Returns
World position at the center of the tile
SnapToTileCenter
Snap a world position to the nearest tile center.
Parameters
| Name | Description |
|---|---|
worldPosition | World position to snap |
tileSize | Size of each tile |
Returns
Snapped world position
GetTileBounds
Calculate the tile bounds (rectangular area) for a given grid position.
Parameters
| Name | Description |
|---|---|
gridPosition | Grid position |
tileSize | Size of each tile |
Returns
Rectangle representing the tile bounds in world coordinates
IsPositionInTile
Check if a world position is within the bounds of a specific tile.
Parameters
| Name | Description |
|---|---|
worldPosition | World position to check |
gridPosition | Grid position of the tile |
tileSize | Size of each tile |
Returns
True if the world position is within the tile bounds
GetTileAtPosition
Get the tile at a world position.
Parameters
| Name | Description |
|---|---|
worldPosition | World position |
tileSize | Size of each tile |
Returns
Grid position of the tile containing the world position
GridDistanceToWorld
Calculate the distance between two grid positions in world units.
Parameters
| Name | Description |
|---|---|
from | Starting grid position |
to | Target grid position |
Returns
Distance in world units
LimitToMaxDistance
Limit a target tile to be within maximum distance from source tile.
Parameters
| Name | Description |
|---|---|
source | Source grid position |
target | Target grid position to limit |
maxDistance | Maximum allowed distance in tiles |
useManhattanDistance | If true, use Manhattan distance; if false, use Chebyshev distance |
Returns
Limited grid position
SnapToRegion
Snap a grid position to be within a rectangular region.
Parameters
| Name | Description |
|---|---|
position | Position to snap |
regionMin | Minimum bounds of region |
regionMax | Maximum bounds of region |
Returns
Snapped grid position
IsValidPosition
Check if a grid position is within a used rectangle.
Parameters
| Name | Description |
|---|---|
position | Position to check |
usedRect | Rectangle representing used tiles (x, y, width, height) |
Returns
True if position is within the used rectangle
IsValidPosition
Check if a grid position is valid within bounds.
Parameters
| Name | Description |
|---|---|
position | Position to check |
minBounds | Minimum bounds |
maxBounds | Maximum bounds |
Returns
True if position is within bounds
GetRegionCenter
Get the center position of a rectangular region.
Parameters
| Name | Description |
|---|---|
regionMin | Minimum bounds of region |
regionMax | Maximum bounds of region |
Returns
Center grid position
GetRegionArea
Calculate the area of a rectangular region in tiles.
Parameters
| Name | Description |
|---|---|
regionMin | Minimum bounds of region |
regionMax | Maximum bounds of region |
Returns
Area in number of tiles
GetRegionPositions
Get all positions in a rectangular region.
Parameters
| Name | Description |
|---|---|
regionMin | Minimum bounds of region |
regionMax | Maximum bounds of region |
Returns
Enumerable of all grid positions in the region
RegionsOverlap
Check if two rectangular regions overlap.
Parameters
| Name | Description |
|---|---|
min1 | Minimum bounds of first region |
max1 | Maximum bounds of first region |
min2 | Minimum bounds of second region |
max2 | Maximum bounds of second region |
Returns
True if regions overlap
GetOverlapRegion
Get the overlapping region between two rectangular regions.
Parameters
| Name | Description |
|---|---|
min1 | Minimum bounds of first region |
max1 | Maximum bounds of first region |
min2 | Minimum bounds of second region |
max2 | Maximum bounds of second region |
Returns
Overlapping region as (min, max) tuple, or null if no overlap
GridRectToWorld
Convert a grid-based rectangle to world coordinates.
Parameters
| Name | Description |
|---|---|
gridMin | Minimum grid position |
gridMax | Maximum grid position |
tileSize | Size of each tile |
Returns
Rectangle in world coordinates
WorldRectToGrid
Convert a world rectangle to grid coordinates.
Parameters
| Name | Description |
|---|---|
worldRect | Rectangle in world coordinates |
tileSize | Size of each tile |
Returns
Rectangle in grid coordinates as (min, max) tuple