1
\$\begingroup\$

I have some terrain being generated using the following algorithm

double rand1 = rand.NextDouble() + 1;
double rand2 = rand.NextDouble() + 2;
double rand3 = rand.NextDouble() + 3;

float offset = screenHeight / 2;
//does what it says.
float peakheight = 30;
//really just adjusts the amount of "waves" in the generation
float flatness = 70;

for (int x = 0; x < screenWidth; x++)
{
    double height = peakheight / rand1 * Math.Cos((float)x / flatness * rand1 + rand1);
    height += peakheight / rand2 * Math.Cos((float)x / flatness * rand2 + rand2);
    height += peakheight / rand3 * Math.Cos((float)x / flatness * rand3 + rand3);
    height += offset;
    terrainContour[x] = (int)height;

Now to display the "terrain" on the screen I use this block of code to color it

Color[] foregroundColors = new Color[screenWidth * screenHeight];

for (int x = 0; x < screenWidth; x++)
{
    for (int y = 0; y < screenHeight; y++)
    {
        if (y > terrainContour[x])
            foregroundColors[x + y * screenWidth] = Color.Black;
        else
            foregroundColors[x + y * screenWidth] = Color.Transparent;
    }
}

foregroundTexture = new Texture2D(device, screenWidth, screenHeight, false, SurfaceFormat.Color);
foregroundTexture.SetData(foregroundColors);

Is it possible to set a texture to draw the terrain instead of the Color.Black property? If I had a square texture and I used it to draw the terrain then it would look somewhat blocky and I would then be able to further implement collisions easier.

\$\endgroup\$
5

0

You must log in to answer this question.

Browse other questions tagged .