EntityComponentSystemSamples icon indicating copy to clipboard operation
EntityComponentSystemSamples copied to clipboard

Boids example possible bug in MergeCells

Open ondra-vaic opened this issue 3 years ago • 1 comments

I'm going through the boids example and I noticed the following piece of code:

            public void ExecuteNext(int cellIndex, int index)
            {
                cellCount[cellIndex]      += 1;
                cellAlignment[cellIndex]  += cellAlignment[cellIndex];
                cellSeparation[cellIndex] += cellSeparation[cellIndex];
                cellIndices[index]        = cellIndex;
            }

As I understand it, cellIndex is the index of the first boid in a specific cell. That would mean that the sum for cellAlignment and cellSeparation would be just the value for the first boid in the cell multiplied by the number of boids in that cell.

If I understand it correctly this would solve the issue.

            public void ExecuteNext(int cellIndex, int index)
            {
                cellCount[cellIndex]      += 1;
                cellAlignment[cellIndex]  += cellAlignment[index];
                cellSeparation[cellIndex] += cellSeparation[index];
                cellIndices[index]        = cellIndex;
            }

ondra-vaic avatar Nov 21 '22 13:11 ondra-vaic

As I saw in the GDC talk "Unity at GDC - A Data Oriented Approach to Using Component Systems" on youtube where they presented this sample project, a previous implementation of the code was :

public void ExecuteNext(int cellIndex, int index)
{
    cellCount[cellIndex]      += 1;
    cellAlignment[cellIndex]  = new Heading { Value = cellAlignment[cellIndex].Value + cellAlignment[index].Value };
    cellSeparation[cellIndex] = new Position{ Value = cellSeparation[cellIndex].Value + cellSeparation[index].Value };
    cellIndices[index]        = cellIndex;
}

So yeah, @ondra-vaic's solution seems fine.

creamos avatar Nov 24 '22 07:11 creamos