Game Development Community

Terrain magic number :)

by b2fxna · in Torque X 3D · 10/02/2009 (9:11 am) · 2 replies

In The RawData of terrain. The loading code is:

// read the heightfield
for (Int32 iy = 0; iy < gridSize; iy++)
{
for (Int32 ix = 0; ix < gridSize; ix++)
{
UInt16 h = br.ReadUInt16();
float height = ((float)h *0.0014725; // Magic number here
_heightMap[ix, iy] = height;

totalHeight += height;
if (height < minHeight)
minHeight = height;
if (height > maxHeight)
maxHeight = height;
}
}
0.0014725 is a magic, Does anyone know what it is? I guess it map the terrain height from 0-65335 to 0-468 meter, but the terrain itself also have a scale value, and I found out it hard to understand the scale of the terrain, especially when you design the heighmap in another application and need to do the conversion.

#1
10/02/2009 (12:20 pm)
It definitely looks like a scale multiplier to reduce the height. With maxint = 65535, this multiplier translates to a range more like 0 to 96.5. I'm guessing that the purpose of the magic number is to simply translate the heightmap range of 0-65535 into a range of 0-100.

So, assuming the target range is 0-100, you need to find out what your heightmap editor's range is. For example, if your editor sets values 0-32767, then the magic number should become 0.003051. In other words...
magic_number = ( 100 / maximum_editor_height_range )

This is still just a guess on my part, I've never worked in this area of the code.

John K.
www.envygames.com
#2
10/02/2009 (12:42 pm)
From my calculation the maxuint16 is 65535 and the magic number in Torque X is 0.007125f (sorry it is not 0.0014725) -> max height = 466.9 which to me have no meaning. If it maps to 100.0 then I can understand. Anyway hardcode a magic number in the code is not a very good implement, but I guess it has a story behind, maybe due to the compatibility with TGE or something like that. I hope somebody can help to explain the story to clarify. :D