EntityComponentSystemSamples
EntityComponentSystemSamples copied to clipboard
Boids example possible bug in MergeCells
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;
}
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.