<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mlokogrgel on Medium]]></title>
        <description><![CDATA[Stories by Mlokogrgel on Medium]]></description>
        <link>https://medium.com/@jakubdka?source=rss-54262238ff71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sxFASzbqMVUN9xAfDLiIkw.jpeg</url>
            <title>Stories by Mlokogrgel on Medium</title>
            <link>https://medium.com/@jakubdka?source=rss-54262238ff71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:27:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jakubdka/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Quadtree and how to make it better]]></title>
            <link>https://medium.com/@jakubdka/quadtree-and-how-to-make-it-better-e0f0809bbd9a?source=rss-54262238ff71------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0f0809bbd9a</guid>
            <category><![CDATA[better-ways]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[quadtree]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Mlokogrgel]]></dc:creator>
            <pubDate>Sun, 18 Oct 2020 19:16:35 GMT</pubDate>
            <atom:updated>2020-10-18T19:16:35.194Z</atom:updated>
            <content:encoded><![CDATA[<p>If you ewer tried to make game with loads of entities that has to see and collide with each other, you probably in a short time realized that O(n*n) isn’t sufficient approach. Probably best and most flexible solution you would find is quadthree, and if you don’t, well then, you may skip one step and use something even better. But first for those who don’t know what a quadthree is lets explain it real quick.</p><p>Now because code can speak for it self better then anything else, there is an pseudo code example of how would quadtree data composition look like:</p><pre>Quadtree {<br>    bounds: Rectangle // or AABB<br>    max_objects: int<br>    objects: HashSet&lt;*Object&gt;<br>    branches: Array&lt;*Quadtree; 4&gt;<br>    leaf: bool // true == QuadTree has no children<br>}</pre><p>As you can see its not too complex as it is recursive. Every quadtree can split to 4 smaller ones. It splits only if object count exceeds maximal value and objects are passed to subnodes if they fit to any or kept in parent if they don’t. Then when you are checking collisions you look just at objects that are in quadrants witch targeted object overlaps. If you still have no idea this image may help:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*MN3JEk0P5rtXHeJCitcSfg.png" /></figure><p>Using quadtrees is in deed more efficient then naive approach though its not perfect. Issue comes with updating shapes, deeper your quadthree is, slower it is to update it. In order to access, remove, insert or update any object you have to recursively calculate its quadrant. Wouldn’t it be better if you could access them directly? Thats why getting rid of recursion is a right call.</p><p>Now am not going to pretend that my solution has no flaws. In order to get it working you have to allocate quite a bit of memory you don’t even need when you are dealing with small amount of objects its truly an overkill but lets say we are dealing with 10 000 boids. Pure boids has O(n*n) complexity and thats where our approach comes handy. Okay, lets get right into it.</p><pre>Tile {<br>    objects: HashSet&lt;*Object&gt;<br>    bounds: Rectangle<br>}</pre><pre>QuadMap {<br>   map: Vec&lt;Vec&lt;Vec&lt;Tile&gt;&gt;&gt;<br>   sizes: Vec&lt;Vector&gt;<br>   resolution: int<br>}</pre><p>This is little bit more confusing in my opinion but it does same thing as quadtree, just without recursion. This is in deed loading screen friendly approach. Allocations can be really huge, BUT, performance while gameplay is considerably higher. Resolution field determinate how deep the QuadMap is. Lets say we have resolution 4, we would end up with QuadMap that has 4 levels of quadrants. Every level is 2D vector/array/list that acts as tilemap. Upper tilemap has only one tile, one under it has 4, then 16 and finally 64. First difference, other then structural, comes with a way we are inserting objects into QuadMap. All we care about is a right address witch means…</p><pre>Address {<br>    level: int<br>    x: int<br>    y: int<br>}</pre><p>…is all we need to access the object and complexity is O(1). In comparison quadtree complexity is O(n) (where n is a depth) because it is similar to linked list. It is also very easy to store this address in our object and then use it to remove or update it. another nice thing is that you can easily calculate x and y like this:</p><pre>// (int, int) is an tuple<br>(int, int) get_tile_index(position: Vector, level: int) {<br>    return ((position.x / sizes[level].x) as int, <br>            (position.y / sizes[level].y) as int)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*2dAyvpT7ece-9fNg6sL_ng.gif" /></figure><p>Though this is not complete as we have to provide level on witch the tile is. All we are consorned about is whether object fits in to tile, thats why tile has bounds. If it does not fit to deepest level move up and try it with bigger tile, so finding out address is in the worst case as fast as inserting it to quadtree,witch happends rarely in case of small objects. Another benefit is that we have to bother with finding an address only if shape is not on a deepest level it can fit into, or it no longer fits into its original tile. If objects are not moving crazy fast, witch means they don’t change quadrants too often (we are talking about 60fps) performance really gets decent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*2dAyvpT7ece-9fNg6sL_ng.gif" /></figure><p>In case you are interested in implementation of QuadMap you can find it <a href="https://docs.rs/rustbatch/0.4.0/src/rustbatch/entities/detection/quadmap.rs.html#1-194">here</a> its part of my <a href="https://crates.io/crates/rustbatch">game engine</a>. Of corse i should to provide some demonstration and that is <a href="https://github.com/jakubDoka/rustbatch_examples">here</a>, and yes tere are the 10 000 boids running on 55 fps on one thread (at least on my slow computer).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0f0809bbd9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>