Skip to content

project-roadwork/spatial-grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Spatial Grid

A typed, spatial partitioning grid for fast proximity queries, where entities are stored in a uniform grid, allowing lookups of nearby objects without querying the entitre dataset.

API

const grids = require(THIS_MODULE)
const grid = gridModule.newGrid(1900)
const entityId = grids.spawn(grid, part, Vector3)

grids.updatePosition(grid, entityId, Vector3)
grids.remove(grid, entityId or part)

const parts = grids.query(grid, Vector3, radius?)
const entityIds = grids.queryAllInRadius(grid, Vector3, radius)

const x, z = grids.getCellCoords(grid, position)
const x, z = grids.getCellKey(grid, id or position)

grids.empty(grid)
grids.destroy(grid)            

Notes

This module contains heavy AI generated material from ChatGPT 5.3 or lower (free plan). It was a bit frustrating since ChatGPT would remove some of the public functions and change local to const since it believed that it doesn't exist in Luau, while it most certainly did in later releases.

Additionally, it mixed up Luau with vanilla Lua versions past 5.1, claiming that ~ is a pure operator in Luau while it is not in the time of writing.

This module was a bit of an experiment, as I challenged ChatGPT to replace this particular function:

const function _getNearbySources(signPos: Vector3, sources: types.SourceList, maxDistance: number): { types.LightSource }
	const nearby: { types.LightSource } = {}
	for _, source in sources do
		if source.isActive() then
			const dist = (source.getPosition() - signPos).Magnitude
			if dist <= maxDistance then
				nearby[#nearby + 1] = source
			end
		end
	end
	return nearby
end

License

This work is openly licensed under the BSD-2-Clause-Patent OR the Apache License 2.0. You may choose either license at your discretion.

About

A typed, spatial partitioning grid for fast proximity queries.

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
Unknown
LICENSE-BSD

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages