@gridworkjs/hashgrid 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,35 +14,30 @@ npm install @gridworkjs/hashgrid
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
+
A particle simulation where thousands of uniform-sized particles need fast collision checks every frame:
|
|
18
|
+
|
|
17
19
|
```js
|
|
18
20
|
import { createHashGrid } from '@gridworkjs/hashgrid'
|
|
19
|
-
import { point,
|
|
20
|
-
|
|
21
|
-
// create a hash grid - cellSize should match your data's density
|
|
22
|
-
const grid = createHashGrid(item => bounds(item.position), { cellSize: 50 })
|
|
23
|
-
|
|
24
|
-
// insert items
|
|
25
|
-
grid.insert({ id: 1, position: point(10, 20) })
|
|
26
|
-
grid.insert({ id: 2, position: point(50, 60) })
|
|
27
|
-
grid.insert({ id: 3, position: rect(70, 70, 90, 90) })
|
|
21
|
+
import { point, bounds } from '@gridworkjs/core'
|
|
28
22
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
// => [{ id: 1, ... }, { id: 2, ... }]
|
|
23
|
+
// particles are ~10 units across, so a 20-unit cell size works well
|
|
24
|
+
const particles = createHashGrid(p => bounds(p.position), { cellSize: 20 })
|
|
32
25
|
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
particles.insert({ id: 'a', position: point(10, 20), vx: 1, vy: 0 })
|
|
27
|
+
particles.insert({ id: 'b', position: point(15, 22), vx: -1, vy: 1 })
|
|
28
|
+
const c = { id: 'c', position: point(300, 400), vx: 0, vy: -1 }
|
|
29
|
+
particles.insert(c)
|
|
35
30
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
// => [
|
|
31
|
+
// each frame, check for collisions near each particle
|
|
32
|
+
const nearby = particles.search({ minX: 5, minY: 15, maxX: 25, maxY: 30 })
|
|
33
|
+
// => [particle a, particle b] - fast O(1) cell lookup
|
|
39
34
|
|
|
40
|
-
//
|
|
41
|
-
|
|
35
|
+
// find the closest neighbor for attraction/repulsion forces
|
|
36
|
+
particles.nearest({ x: 10, y: 20 }, 1)
|
|
37
|
+
// => [particle b]
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
grid.clear()
|
|
39
|
+
// particle leaves the simulation (by reference)
|
|
40
|
+
particles.remove(c)
|
|
46
41
|
```
|
|
47
42
|
|
|
48
43
|
## When to Use a Hash Grid
|