I was searching for a couple of hours, but I didn't find final solution, after trying many ways.

I'm trying to get heightmap heights data, and pass it to btHeightfieldTerrainShape.

Let's say my heightmap has 1024x1024 resolution. I've stored each pixel color of my heightmap inside dynamic array as a final vertex height.

I use them to create and render terrain's model and it works fine.

But my question is: how to correctly convert those heights for bullet? I've tried like this:

std::vector<float> dataVector;

for (size_t i = 0; i < terrainWidth; ++i)
    for (size_t j = 0; j < terrainHeight; j++)
        dataVector.push_back(getHeight(i, j));

Then pass it to the constructor: new btHeightfieldTerrainShape(terrainWidth, terrainHeight, &dataVector[0], ...);

...but I expected that it won't be working.

  • 1
    \$\begingroup\$ You should use dataVector.data() instead of &dataVector[0] for clarity, otherwise this looks fine. What's the issue ? \$\endgroup\$
    – Quentin
    Jul 7, 2016 at 11:52

1 Answer 1


Don't create dataVector as you do, it will be destroyed when reaching the end of his environment, better store that array in an object member, or if you can't do otherwise in a global variable.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .