Spatial.lua - A minimal spatial database.

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
veethree
Inner party member
Posts: 854
Joined: Sat Dec 10, 2011 7:18 pm

Spatial.lua - A minimal spatial database.

Post by veethree »

Spatial.lua is a minimal spatial database library.

Proper documentation on Github as usual.

Basic usage:
Load it

Code: Select all

spatial = require("spatial")
Make a database

Code: Select all

data = spatial.new()
The new function can optionally take a cell size argument. It defaults to 64

Code: Select all

data = spatial.new(64)
Add items to it

Code: Select all

item = {x = 100, y = 100, foo = "foo", bar = {"b", "a", "r"}}
data:insert(item.x, item.y, item)
The insert function also returns the item back to you.

Code: Select all

item = data:insert(item.x, item.y, item)
Remove items

Code: Select all

data:remove(item)
Query the database

Code: Select all

--Get all items
items = data:query()

-- Get filtered items, This will return all items with an x value greater than 100.
items = data:query(function(item) return item.x > 100 end)

-- Get items inside a rectangle
items = data:queryRect(10, 10, 100, 100)

-- Get items from a point
items = data:queryPoint(64, 64)
Note that the queryRect & queryPoint return all items that are inside the cells that the rectangle or point intersect with. This is because Spatial doesn't require that the items you put in it actually have a position. Also all query functions also return the number if items the query found.

Code: Select all

items, len = data:query()
Also also, All query functions have an optional filter argument.

Code: Select all

items, len = data:queryPoint(100, 100, function(item) return item.x > 100 end)
About filters
A filter is just a function, That gets called for every item in the query with the following arguments:
  • item: A reference to the item
  • cell_x & cell_y: The cell the item lives in
  • index: The index the item occupies inside the cell
If the filter function returns true (Or any truthy value), The item is included, Otherwise it's omitted.

The demo creates 1.000.000 objects and scatters them across a large area, Then uses spatial to render only the ones visible on the screen. It can take a little while to load if you're on a older machine. Or just crash because it runs out of memory. in which case sorry.
Although it runs just fine on my mid 2012 macbook pro running manjaro.

Hope someone finds this useful. If you're looking for something more comprehensive, Have a look at bump.lua, It does all the same stuff, Plus collisions!

Image
Attachments
Spatial_demo.love
(3.85 KiB) Downloaded 96 times
spatial.lua
1.0
(4.2 KiB) Downloaded 93 times
dusoft
Party member
Posts: 173
Joined: Fri Nov 08, 2013 12:07 am

Re: Spatial.lua - A minimal spatial database.

Post by dusoft »

This is way too cool :) You can also use it as a parallax scroll with a few changes, I guess.
User avatar
veethree
Inner party member
Posts: 854
Joined: Sat Dec 10, 2011 7:18 pm

Re: Spatial.lua - A minimal spatial database.

Post by veethree »

dusoft wrote: Tue Sep 21, 2021 11:36 am This is way too cool :) You can also use it as a parallax scroll with a few changes, I guess.
Thank You!
The camera module in the demo actually offers that functionality. Although enabling it would take a few extra steps because the actual position of the object, And the position it ends up being rendered at are different.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests