# Erosion Algorithm (source code)

This topic is 4201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ive implemented the the thermal erosion described in the article: http://www.oddlabs.com/download/terrain_generation.pdf however ive changed it a bit... im doing something i call materialbased erosion... where i generate another perlinnoise map which represents the hardness of the materials... white represent soft material which can be moved and black is hard rock which cant be eroded... and anything betwen depends on the brightness..this way i can keep the small details whihc erosion normally would remove.. this is waht it looks like... I use the Von Neuman neighborhood

// Calculate the heightdifference of the surrounding vertices
// 0 = (x, y+1), 1 = (x-1, y), 2 = (x+1, y), 3 = (x, y-1)
void CTERRAIN::CalculateSlope()
{

for (int y = 1; y < m_size; y++)
{
for (int x = 1; x < m_size; x++)
{

SlopeMap[x][y][0] = 0;
SlopeMap[x][y][1] = 0;
SlopeMap[x][y][2] = 0;
SlopeMap[x][y][3] = 0;

if (HeightMap1[x][y] > HeightMap1[x][y+1])
{
SlopeMap[x][y][0] = HeightMap1[x][y] - HeightMap1[x][y+1];
}
if (HeightMap1[x][y] > HeightMap1[x-1][y])
{
SlopeMap[x][y][1] = HeightMap1[x][y] - HeightMap1[x-1][y];
}
if (HeightMap1[x][y] > HeightMap1[x+1][y])
{
SlopeMap[x][y][2] = HeightMap1[x][y] - HeightMap1[x+1][y];
}
if (HeightMap1[x][y] > HeightMap1[x][y-1])
{
SlopeMap[x][y][3] = HeightMap1[x][y] - HeightMap1[x][y-1];
}

}
}
}

//Run thermalerosion
// MaterialMap is generated using perlinnoise
void CTERRAIN::ThermalErosion()
{
float slopetotal;
float slopemax;

for (int m = 0; m < duration; m++)
{

CalculateSlope();

for (int y = 1; y < m_size-1; ++y)
{
for (int x = 1; x < m_size-1; ++x)
{
slopetotal = 0.0f;
slopemax = T;

for (int n = 0; n < 4; n++)
{
if (SlopeMap[x][y][n] > T)
{
slopetotal += SlopeMap[x][y][n];

if (SlopeMap[x][y][n] >  slopemax)
{
slopemax = SlopeMap[x][y][n];

}

}
}

if (slopetotal != 0) // If there is slope than move material
{
HeightMap1[x][y+1] = HeightMap1[x][y+1] + MaterialMap[x][y] * c * ( slopemax - T ) * (SlopeMap[x][y][0] / slopetotal); // Add material to lower points
MaterialMap[x][y+1] += MaterialMap[x][y]  * ((c * ( slopemax - T ) * (SlopeMap[x][y][0] / slopetotal))/thickness); //Add material to the materialmap, lower values of thickness will cause more material to be move each timestep

HeightMap1[x-1][y] = HeightMap1[x-1][y] + MaterialMap[x][y]  * c * ( slopemax - T ) * (SlopeMap[x][y][1] / slopetotal);
MaterialMap[x-1][y] += MaterialMap[x][y]  * ((c * ( slopemax - T ) * (SlopeMap[x][y][1] / slopetotal))/thickness);

HeightMap1[x+1][y] = HeightMap1[x+1][y] + MaterialMap[x][y]  * c * ( slopemax - T ) * (SlopeMap[x][y][2] / slopetotal);
MaterialMap[x+1][y] += MaterialMap[x][y]  * ((c * ( slopemax - T ) * (SlopeMap[x][y][2] / slopetotal))/thickness);

HeightMap1[x][y-1] = HeightMap1[x][y-1] + MaterialMap[x][y]  * c * ( slopemax - T ) * (SlopeMap[x][y][3] / slopetotal);
MaterialMap[x][y-1] += MaterialMap[x][y]  * ((c * ( slopemax - T ) * (SlopeMap[x][y][3] / slopetotal))/thickness);

HeightMap1[x][y] -= MaterialMap[x][y] * c * ( slopemax - T ); // remove material from the currentpoint
MaterialMap[x][y] -= MaterialMap[x][y] * ((c * ( slopemax - T ))/thickness); // remove material from the materialmap

//Make sure we dont get to hight or to low values in materialmap
if (MaterialMap[x][y+1] > 1)
{
MaterialMap[x][y+1] = 1;
}
if (MaterialMap[x-1][y] > 1)
{
MaterialMap[x-1][y] = 1;
}
if (MaterialMap[x+1][y] > 1)
{
MaterialMap[x+1][y] = 1;
}
if (MaterialMap[x][y-1] > 1)
{
MaterialMap[x][y-1] = 1;
}
if (MaterialMap[x][y-1] < 0)
{
MaterialMap[x][y] = 0;
}

}

}
}

}

}


and here is the results after 50 timesteps... http://img117.imageshack.us/img117/7338/erosion6lo.jpg

##### Share on other sites
Rather than just throwing a secondary perlin noise to adjust where the thhermal erosion is taking place, you should instead, analyze the actually terrain itself for areas that would have a higher percentage of getting thermally erodd than others.

IE the bottom of a canyon would recieve less thermal weathering depending on how tight the walls are, and how much weather would actually occur.

~Main

##### Share on other sites
What is "thermal erosion"?

##### Share on other sites
sounds like the sum of all forms of erosion caused by temperatur. like thermal expansion or freeze-thaw(though i'm not 100% sure about that one)

regards,
m4gnus

##### Share on other sites

This topic is 4201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628645
• Total Posts
2984023

• 9
• 9
• 9
• 10
• 21