asaarinen / qtree

Simple JavaScript quadtree
MIT License
38 stars 9 forks source link

Line segment tests return all objects? #8

Open ayebear opened 7 years ago

ayebear commented 7 years ago

For some reason, when dealing with line segments using get(), all objects are returned. I'm thinking maybe I am misunderstanding what using a line segment actually does, but it definitely is not what I would expect.

Code example (es6):

import QuadTree from 'simple-quadtree'

let quadTree = QuadTree(0, 0, 100000, 100000)

// This should be the only collision
quadTree.put({x: 10, y: 10, w: 4, h: 4, obj: 'Yay'})

// Far away objects
quadTree.put({x: 200, y: 200, w: 4, h: 4, obj: 'Boo 1'})
quadTree.put({x: 300, y: 100, w: 4, h: 4, obj: 'Boo 2'})

// Here is where my confusion lies: all objects are returned instead of just the first one
let collisions = quadTree.get({x: 1, y: 11, dx: 1, dy: 0})
console.log('Quadtree Tests:')
console.log(collisions)
ayebear commented 7 years ago

I ran some more tests: Using get() with an area will return exact matches, but using line segments may return non-overlapping objects. I guess I ignored this in the documentation:

Please also note that getting objects close to a line segment will guarantee that all objects close to the line will be iterated over, but other non-overlapping objects may also be returned.

I don't think it should be inconsistent like this though, between using line segments versus using areas.