mourner / rbush

RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hi i can't figure out how to use this library can anyone help?

i-am-jabriel opened this issue · comments

in my code i have AABB already set up as x,y,right,bottom;

I tried mapping them over to minX,minY,maxX,maxY like so:

    get minX(){return this.x;}
    get minY(){return this.y;}
    get maxX(){return this.right;}
    get maxY(){return this.bottom;}

However I can never manage to get collisions working...

I know the tree has the items in it. Tree.all() returns all my objects, but no collision ever works. tree.search(obj) returns an empty array no matter what, and tree.collision(obj) always returns false.

My hunch resides with the fact that when I inspect the properties of the tree it has a data object that contains a minX, maxX, minY & maxY of NAN.

Anyone else run into this issue or is this to be as expected and I just missed something very obvious?

ok i refactored my code so now that my objs have an aabb object that i am inserting into the tree with a minX maxX minY maxY and i still can't get it to work.

const tree = new rbush(12);
let interactables = [];
Class Interactable{
    constructor(){
        this.id = interactables.push(this) - 1;
        this.x = this.y =  0;
        this.height = this.width = 40;
        tree.insert(this.aabb = {id:this.id});
     }
   get bottom(){
        return this.y + this.height;
    }
    get right(){
        return this.x + this.width;
    }
//And anytime the obj moves I call:
    updateAABB(){
        Object.assign(this.aabb,{
            minX: this.x, 
            minY: this.y,
            maxX: this.right,
            maxY: this.bottom,
        });
    }

//And when I want to search for collisions after moving:
console.log(tree.search(this.aabb)); // always [];

I am a student trying to understand how to create my own rtree, but I cant manage to get any examples to work.

sorry to tag @mourner if you have any free time within the next week can you help me understand what very fundamental mistake I am making.

https://github.com/i-am-jabriel/big-picture-game

I tried to push my game to this site:
https://i-am-jabriel.github.io/big-picture-game/

Even after making sure to format everything correctly I still get the tree.data.min/max x/y = NAN.

I figured it out. DO NOT insert a object before setting its minx max miny and maxy. Lmao how silly.

@i-am-jabriel yep! Also, to update an object, you have to remove it, update the AABB, then reinsert it. See also #28.