Note that there are some explanatory texts on larger screens.

plurals
  1. POData oriented access to several indexed data arrays
    text
    copied!<p>I am working on an entity component system for a game engine. One of my goals is to use a data oriented approach for optimal data processing. In other words, I want to follow the guideline of rather wanting structs of arrays than arrays of structs. However, my problem is that I haven't managed to figure out a neat way to solve this for me.</p> <p>My idea so far is that every component in the system is responsible for a specific part of the game logic, say that the Gravity Component takes care of calculating forces every frame depending on mass, velocity etc. and other components take care of other stuff. Hence every component is interested in different data sets. The Gravity Component might be interested in mass and velocity while the Collision Component might be interested in bounding boxes and position etc.</p> <p>So far I figured I could have a data manager which saves one array per attribute. So say that entities may have one or more of weight, position, velocity, etc and they would have a unique ID. The data in the data manager would be represented as follows where every number represents an entity ID:</p> <pre><code>weightarray -&gt; [0,1,2,3] positionarray -&gt; [0,1,2,3] velocityarray -&gt; [0,1,2,3] </code></pre> <p>This approach works good if all entities have each one of the attributes. However if only entity 0 and 2 have all tree attributes and the other ones are entities of the type that does not move, they will not have velocity and the data would look:</p> <pre><code>weightarray -&gt; [0,1,2,3] positionarray -&gt; [0,1,2,3] velocityarray -&gt; [0,2] //either squash it like this velocityarray -&gt; [0 ,2 ] //or leave "empty gaps" to keep alignment </code></pre> <p>Suddenly it isn't as easy to iterate throught it. A component only interested in iterating over, and manipulating the velocity would have to either somehow skip the empty gaps if I went by the second approach. The first approach of keeping the array short wouldn't work well either in more complicated situations. Say if I have one entity 0 with all three attributes, another entity 1 having only weight and position, and an entity 2 which only has position and velocity. Finally there is one last entity 3 which only has weight. The arrays squashed would look like:</p> <pre><code>weightarray -&gt; [0,1,3] positionarray -&gt; [0,1,2] velocityarray -&gt; [0,2] </code></pre> <p>The other approach would leave gaps like so:</p> <pre><code>weightarray -&gt; [0,1, ,3] positionarray -&gt; [0,1,2, ] velocityarray -&gt; [0, ,2, ] </code></pre> <p>Both of these situations are nontrivial to iterate if you are only interested in iterating over the set of entities that only has a few of the attributes. A given component X would be interested in processing entities with position and velocity for instance. How can I extract iterable array pointers to give to this component to do its calculations? I would want to give it an array where the elements are just next to each other, but that seems impossible.</p> <p>I've been thinking about solutions like having a bit field for every array, describing which spots are valid and which are gaps, or a system that copies over data to temporary arrays that have no holes and are then given to the components, and other ideas but none that I thought of was elegant and didn't have additional overhead for the processing (such as extra checks if data is valid, or extra copying of data). </p> <p>I am asking here because I hope that someone of you might have experience with something similar or might have ideas or thoughts helpful in pursuing this issue. :) Also if this whole idea is crap and impossible to get right and you have a much better idea instead, please tell me. Hopefully the question isn't too long or cluttery. </p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload