Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
            foregroundColors[x + y * screenWidth] = Color.Transparent;

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

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.

share|improve this question
Do you actually mean texture? I'm not sure how a texture would make it easier to implement collisions. Do you actually want to change the terrain shape? –  Byte56 Jul 20 '12 at 19:46
The idea is to use a square texture (like a pixel) and generate the terrain with that instead of a Color. I thought that if I used a square texture to generate the terrain, it would appear in a blocky shape, like [Terraria terrain] (, However I'm not entirely sure how I could implement it. –  Noah Simpson Jul 20 '12 at 19:57
These are great, but it doesn't have much to do with what I'm trying to do... –  Noah Simpson Jul 20 '12 at 21:27
Sure it does. It's about taking noise and generating terrain. I think your idea of creating a texture that just has squares on it won't work very well. More research would be good for you at the moment. –  Byte56 Jul 20 '12 at 21:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.