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.

  • \$\begingroup\$ 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? \$\endgroup\$
    – House
    Commented Jul 20, 2012 at 19:46
  • \$\begingroup\$ 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] (rockpapershotgun.com/images/11/june/terr/terr8.jpg), However I'm not entirely sure how I could implement it. \$\endgroup\$ Commented Jul 20, 2012 at 19:57
  • \$\begingroup\$ These are great, but it doesn't have much to do with what I'm trying to do... \$\endgroup\$ Commented Jul 20, 2012 at 21:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Jul 20, 2012 at 21:34


You must log in to answer this question.

Browse other questions tagged .