Search code examples
c++game-physicsbullet

btHeightfieldTerrainShape constructor arguments not clear


I am having a hard time understanding the consturctor parameters of btHeightfieldTerrainShape and how the height field data is supposed to be set up.

The first two arguments heightStickField and widthStickField. What do they represent? Is it the width and height of the entire terrain so that the height field is scaled to fit inside? or does it represent the total number of "tiles" per with and height so that the linear heightfield data can be broken down to hieghts per tile? Also what is the ordering of the height map?

I generate my terrain heights like this. Would this match what the height field of btHeightfieldTerrainShape ?

for (int i = 0; i <= mHeight; i++) {
                    for (int j = 0; j <= mWidth; j++) {
                        
                        double _noiseX = j + _xOffset;
                        double _noiseZ = i + _zOffset;
                        //float _height = 0.2f + ((float)mPerlinNoiseGenerator.GetValue(_noiseX, _noiseZ, 0.0) * 2);
                        float _height = (float)mPerlinNoiseGenerator.GetValue(_noiseX, _noiseZ, 0.0);
                        if (!i && !j) {
                            _terrainData->mMaxHeight = _height;
                            _terrainData->mMinHeight = _height;
                        }else {
                            _terrainData->mMaxHeight = std::max(_terrainData->mMaxHeight, _height);
                            _terrainData->mMinHeight = std::min(_terrainData->mMinHeight, _height);
                        }
                        _terrainData->mHeightMap.push_back(_height);
                    }
                }

I looked everywhere and was not able to find any clear cut explanation to the use of btHeightfieldTerrainShape . Any help is appreciated.

Thanks!


Solution

  • Figured it out. For those wondering the ordering is -x to +x and z+ to z- . So a for loop such as the following will get you the right ordering. width and height here are in terms of the number of tiles/height values per row/column

    for (int i = 0; i < mHeight; i++) {
                        for (int j = 0; j < mWidth; j++) {}}