/Sight

A spatial search μlibrary powered by GameplayKit 👾

Primary LanguageSwiftMIT LicenseMIT

Sight

Build status Swift Package Manager MacOS + iOS + iPadOS + tvOS Twitter: @zntfdr

Welcome to Sight, a Swift spatial search μlibrary. Its primary goal is to extend Apple's GameplayKit framework.

Usage

Sight helps you quickly determine which object is closest to a given position.

This is done via a Region instance, which represents the space where all the objects are positioned in.

import Sight

// Define a region with bounds and search radius.
let region = Region<String>(
  minBounds: SIMD2(x: 0, y: 0), 
  maxBounds: SIMD2(x: 1, y: 1), 
  searchRadius: 0.3
)

// Place objects in the region.
region.add("A", at: SIMD2(x: 0, y: 0.1))
region.add("B", at: SIMD2(x: 0.5, y: 0))
region.add("C", at: SIMD2(x: 0, y: 0.8))

// Find the closest object to any position.
region.closestValue(to: SIMD2(x: 0.5, y: 0.2)) // "B" 
region.closestValue(to: SIMD2(x: 1, y: 0)) // nil 

Region is optimized for spatial searches, this is why during its initialization both search radius and its bounds are required.

You can find many more examples in the Tests folder.

Installation

Sight is distributed using the Swift Package Manager. To install it into a project, follow this tutorial and use this repository URL: https://github.com/zntfdr/Sight.git.

Credits

Sight was built by Federico Zanetello as a component of Bangkok Metro.

If you'd like to know more on how this library works, please refer to GameplayKit's GKQuadtree overview.

Contributions and Support

All users are welcome and encouraged to become active participants in the project continued development — by fixing any bug that they encounter, or by improving the documentation wherever it’s found to be lacking.

If you'd like to make a change, please open a Pull Request, even if it just contains a draft of the changes you’re planning, or a test that reproduces an issue.

Thank you and please enjoy using Sight!