Square trees related to a 2d collision

I am studying this:

https://github.com/mikechambers/ExamplesByMesh/blob/master/JavaScript/QuadTree/src/QuadTree.js

and I believe that I understand the general idea of ​​ATVs, although I have two questions about how they work and the implementation above:

  • No need to rebuild the whole tree every few ms? In Javascript, would that be very slow to do?

  • If I have something like this: http://davzy.com/screenshots/skitched-20120318-180324.png , then it is easy enough to find other points in the same square, but I have a rectangle that hits in 3 different ATVs, is there any way to make him display as a child of all three of these squares?

  • 144 of the above example says that this is Node.prototype._classConstructor = Node ;, I'm just wondering what is going on. I thought a prototype is a way to define a function or variable for future use inside a class, so I'm not sure what this line does.

+6
source share
1 answer

1. No need to rebuild the whole tree every few ms? In Javascript, would that be very slow to do?

I suppose it depends on what you use it for; but yes, an example of an author’s collision detection in his blog post about its implementation, QuadTree will clear the tree and repeat it about 24 times per second (for example, about once in 40 ms). You yourself can judge that it is "very slow"; on my car it looks pretty smooth. (And even if not, I would expect that restoring QuadTree would actually be cheaper / faster than redrawing all the circles on the canvas.)

2. [& hellip;] I have a rectangle that falls into 3 different quads, is there a way to make it display as a child from all three of these squares?

I'm not sure what you mean by “mapping”, but: if you call the constructor with the pointQuad parameter set to false , then the elements are two-dimensional (that is, have width and height in addition to x and y ), and each element will be a child of the smallest square that fits completely inside. In your example, since the rectangle intersects the vertical midline of the canvas, it will be a direct descendant of the root square.

3. The 144 from the above example says this Node.prototype._classConstructor = Node ;, I'm just wondering what is happening. [& Hellip;]

Node "class" has a "subclass" named BoundsNode (used when elements are two-dimensional), and BoundsNode.prototype._classConstructor is BoundsNode (which overrides the inherited Node.prototype._classConstructor ). This allows the Node subdivide method to write new this._classConstructor(...) to build a new BoundsNode if this is a BoundsNode , and a new simple Node if this is a simple Node .

+5
source

Source: https://habr.com/ru/post/911047/


All Articles