Skip to content

Instantly share code, notes, and snippets.

@Wavesonics
Created March 13, 2020 07:03
Show Gist options
  • Save Wavesonics/5de70bd6b230eb61819cf7d3ae90bfb6 to your computer and use it in GitHub Desktop.
Save Wavesonics/5de70bd6b230eb61819cf7d3ae90bfb6 to your computer and use it in GitHub Desktop.
Thermal diffusion algirothm
void DiffusingHeatMap::_physics_process(float delta)
{
clearBackBuffer();
for(int yy = 0; yy < mapSize; ++yy)
{
for(int xx = 0; xx < mapSize; ++xx)
{
diffuse(xx, yy);
}
}
swapBackBuffer();
}
void DiffusingHeatMap::diffuse(int x, int y)
{
const float heat = map[y][x];
constexpr float DIVISOR = 8.0f; // The number of sides it can defuse to
const float diffuse = heat / DIVISOR;
int numDiffused = 0;
// Top Left
numDiffused += diffuseCell(x - 1, y + 1, heat, diffuse);
// Top Center
numDiffused += diffuseCell(x, y + 1, heat, diffuse);
// Top Right
numDiffused += diffuseCell(x + 1, y + 1, heat, diffuse);
// Center Left
numDiffused += diffuseCell(x - 1, y, heat, diffuse);
// Center Left
numDiffused += diffuseCell(x + 1, y, heat, diffuse);
// Top Left
numDiffused += diffuseCell(x - 1, y - 1, heat, diffuse);
// Top Center
numDiffused += diffuseCell(x, y - 1, heat, diffuse);
// Top Right
numDiffused += diffuseCell(x + 1, y - 1, heat, diffuse);
// Set the new value for this voxel
// We subtract all of the heat that was diffused to other cells
backBuffer[y][x] = map[y][x] - (heat / DIVISOR) * (float) numDiffused;
}
int DiffusingHeatMap::diffuseCell(int x, int y, float heat, float diffuse)
{
int diffused = 0;
if(rangeCheck(x, y) && map[y][x] < heat)
{
backBuffer[y + 1][x + 1] = map[y + 1][x + 1] + diffuse;
diffused = 1;
}
return diffused;
}
bool DiffusingHeatMap::rangeCheck(int x, int y)
{
return x >= 0 && y >= 0 && x < mapSize && y < mapSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment