Friday, April 15, 2005 Torque Game Engine World

(This article is incomplete and I may never have time to complete it, however, feel free to take what useful pieces out of it you can find.)

The next step is to actually open the 3D game world. Unlike previous steps, a lot more work has to go into making our world before we can ever start to view it, so it will be a while before you actually run the game engine again.

An empty world isn't very useful, so before we create our world we'll need to have a terrain to place everything on. There are two methods to create a ground: external, with a terrain object model, or internal, calculated through functions and properties. External gives you precise control over the shape, layout and size of your terrain, but requires more resouces since you are working with an existing object model. Internal allows you more freedom to create terrains based on the properties you set, created from a height map, which is just a grey-scale image file using shades of grey to indicate elevation, similar to contour lines on maps.

Torque uses World Units (WUs) to measure terrains, where one WU is equal to one scaled inch. The default size for a terrain is 65,536 WUs per side. The terrain is made up of smaller squares, with each square being 256WU by 256WU. To fill our terrain, we arrange a grid of 256 x 256 squares. So each side is 256 squares x 256WUs = 65,536WUs. Converting inches to miles we get 1.03 miles.

Our height maps are 256 pixels by 256 pixels, so each pixel is the height of one terrain square. You can create your height maps manually, or from real geological data, such as from US Geological Survey.

Create an elevation table, assigning a greyscale color to each elevation level. It can be any of the 256 possible combintations so for an even distribution of ranges, determine the number of elevations and divide 256 by that number minus one to get the difference between colors. Then based on your minimum and maximum elevations, assign heights to the elevations. For example:

6 elevations
256 color levels / (6 elevs. - 1) colors =
51 levels
elev.color (r,g,b)height
(meters)
10,0,00
251,51,5120
3102,102,10240
4153,153,15360
5204,204,20480
6255,255,255100

Using these colors, color an existing map on the contour lines, or create your own map. Remember, in nature the only pattern is the lack of patterns. Anything consistant, or regular will appear unreal. You can use any image creation program to create your height map, even MS Paint, however GIMP, available at http://www.gimp.org/, is very full featured and available for free. Here's an example map based on the values listed above: termap1.png

Now if you were to use this map, even though the lines are somewhat blended due to the drawing tool I used, you will still get a very stepped terrain, going quickly from one elevation to the next. What you want to do is use some sort of "blur" tool to blend the edges of one level to the next, making the change in terrain elevation more smooth. Here's the above map after being blurred with Firework's Gaussian Blur filter with a setting of 8: termap.png. Another method of getting a good blend is to create the image much larger, then scale it down.

To convert your height map into a terrain file, you must be in the Torque game. Save your height map file to the game demo directory under common\editor\heightscripts (create the folder if it doesn't exist). Run the game demo, load any mission, then press F11 to switch to the World and Terrain Editor. Click on the Window menu and select Terrain Terraform Editor.

The first option on the right says "Min Terrain Height" This will be where we put our maximum terrain height that we decided on earlier. Yes, the option is mislabled. In our example, it is 100. Next is Height Range, which is the difference from our maximum and minimum. Since our example is 0 based, this one will also be 100, but you may have to subtract if you start with a number higher than 0.

Next click on the Operation button and select Bitmap so we can load in our height map. Observe that is should already have your map listed. Select your map then click Load and observe the contour map in the bottom left corner of the screen. Click Apply and it will apply your new map. If the height settings are too different from the current settings, as they are in our example, use ALT + C to switch from player to camera view. Use ALT + L or select the Edit menu and click Relight Scene to relight the new map.

If the map still looks took stepped, go back to your original image and change your blur settings again. The white lines around the contour map show the area of the terrain before repeating.

Click on the File menu and select Save Mission As... to save the mission to the demo\data\missions directory with a unique name. When you save your mission, it also saves the terrain file with a TER extension. If you open the mission file with the MIS extension (another simple text file) you'll see the reference to your terrain object, something similar to this:

...
new TerrainBlock(Terrain) {
   rotation = "1 0 0 0";
   scale = "1 1 1";
   detailTexture = "~/data/terrains/details/detail1";
   terrainFile = "./tank.ter";
   squareSize = "8";
   bumpScale = "1";
   bumpOffset = "0.01";
   zeroBumpScale = "8";
      position = "-1024 -1024 0";
      locked = "true";
};
...

The default setting for squareSize is what determines the distance in your terrain. If you need a larger relative terrain before it starts repeating, by doubling the squareSize you double the length of your terrain. Be sure to also double the position, so it is placed in proper relation to the world.

Next