I have been studying about quadtrees and their usage in collision detection in videogame code.
However, all the implementations so far rely on object-oriented features of C++,C#, javascript and Lua to do every node, and I have absolutely no idea of how to translate that to raw C.
The objective is to test multiple objects (shots) against actors (constantly moving) and terrain (static). Then actors with terrain.
Since I cannot find an example I can read in “pure” C terms (that is, not using methods or self-referencing objects), I cannot even grasp the basic idea of how to code it, while I do understand the idea behind the algorithm. I don’t know how to set it up, how to reference it, what datatypes should I use, or anything at all. I know absolutely nothing of C++, which makes translating it to C impossible.
As well, I will be using tilemaps for terrain, and I want to do things like maps that are tall or wide, not perfect squares. A quadtree still works with such a map?
Also, there will be a number of moving elements, being the terrain the only static part in play (elements like moving blocks or doors are separate entities). Is it worth it using a quadtree if updates will be required often? Do I even need to make it global data? (could as well be forged inside some function and then passed around when collisions are enabled). Do I need to allocate memory for it in such a case?
Because you are asking for help with absolutely nothing to start with, I’ll show you some example data structures that might work, as well as an API.
In C, one would implement nodes with structures. Something like this:
And then to stick objects in the quadtree, you can add some extra fields.
The quadtree interface would give you some basic operations:
That’s it, basically. But the implementation will not be trivial. Note that the maximum depth of this quadtree is about 32, which can simplify the implementation somewhat.
If you’re having problems here, I suggest taking a step back and tackling a similar but simpler data structure first. For example, try implementing a Red-Black or AVL tree without using source code as a reference. If you’re not somewhat well-versed in C programming, then a quad tree may be a poor choice for a first project due to its moderate complexity.