Random heightmaps?

Started by
3 comments, last by CutterSlade 22 years, 3 months ago
Hello. I''m making a game similar to Worms, only in 3D, and for that I''ve been looking for ways to create random heightmaps on-the-fly to be used by the terrain engine (TrueVision 3D). Do any of you know where I could find a piece of code (preferrably in Visual Basic or C/C++) or small standalone utility that would generate a random heightmap and then store it in grayscale image file? Thanks in advance
--------------------------------------------------------- There are 10 kinds of people: those who know binary and those who don't
Advertisement
Look for a program called ''Wilbur'', or read up on something called ''Perlin noise''. All your questions will be answered.
Why not just write your own? It really isn''t that hard. Something like this could work:

  #include <stdio.h>#include <stdlib.h> //For Rand()//Used to "wrap" points around the bitmap#define Pnt(x,y) map[((y+SizeY)%SizeY)*SizeX+ (x+SizeX)%SizeX]void SmoothMap(short SizeX, short SizeY, char *map){ short tx,ty; short rx,ry; short nVal; //new value! for (ty=0;ty!=SizeY;++ty) {  for (tx=0;tx!=SizeY;++ty)  {//Gets 9 points +/- rand(-5,5);   nVal = Pnt(tx,ty) + Pnt(tx-1,ty) + Pnt(tx+1,ty) + Pnt(tx,ty-1) + Pnt(tx,ty+1) + Pnt(tx-1,ty-1) + Pnt(tx+1,ty-1) + Pnt(tx+1,ty+1) + Pnt(tx-1,ty+1) + rand()%11-5;   nVal = nVal/9;   if (nVal<0)    nVal = 0;   else if (nVal > 255)    nVal = 255;   Pnt(tx,ty) = nVal;  } }}void CreateRandomHeightMap(short SizeX, short SizeY, char *outfile, unsigned char Smooths){ FILE *out; unsigned char *hMap; unsigned long tSize; short tx,ty; tSize = SizeX*SizeY; hMap = new unsigned char[tSize]; tSize=0; for (ty=0;ty!=SizeY;++ty) {  for (tx=0;tx!=SizeX;++tx)  {   hMap[tSize]=rand()%256; //0-255!   ++tSize;  } }/*Now we have a random height map... but it''s not smooth at all!So, lets smooth it out a bit!*/for (tx=0;tx!=Smooths;++tx) SmoothMap(SizeX,SizeY,map); //Smooth map out *smooths* timesout = fopen(outfile,"wb");putw(SizeX,out); //Output SizeXputw(SizeY,out); //Output SizeYtSize=0;for (ty=0;ty!=SizeY;++ty){ for (tx=0;tx!=SizeX;++tx) {  putc(map[tSize],out); //Put out char!  ++tSize; }}fclose(out);delete hMap;}  


You can even use this at runtime, or create a file to be loaded, etc, etc.

Billy - BillyB@mrsnj.com

ps. I typed this completely in this little text window, so it may contain some errors, but this is a "simple" random terrain generator + smoother.
Perlin noise could do the trick: http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

So could something simpler to program like the fault algorithm. The fault algorithm works like this: Generate two random points. These define a line. Randomly decide whether to make one side "high" or the other "low." Then add a small constant to all points on the "high" side and subtract it from all points on the "low" side. Repeat a thousand or so times. Done.
And remember, rand() is horribly repetitive in its lower bits, so don''t do rand()%11-5, use the more significant bits instead.

Though on second thought, maybe that''s actually ok for terrain.

-scott

This topic is closed to new replies.

Advertisement