# How to get the terrain elevation (z) at specific (x,y) location

I am interesting in programmatically spawn models on a terrain in a random fashion, so i am wondering whether, there is some tool that allow querying for the z coordinate of a terrain given the (x,y) coordinates

The approach of drooping objects from a height, and letting them find the ground by free fall, seems not so elegant solution..

does anyone have an idea ?
thanks

edit retag close merge delete

Sort by » oldest newest most voted

After i came across NoWiS answer over here,
I managed, with some small corrections, to achieve this working code :

  double heightMapZ(double x, double y)  {

// getting the pointer to the HeightmapShape
private: physics::WorldPtr world = this->model->GetWorld();
physics::ModelPtr model = world->GetModel("worldHightmap");
physics::HeightmapShapePtr heightmap = boost::dynamic_pointer_cast<physics::HeightmapShape>(collision->GetShape());

// coordinate transform from regular Word (x,y) to the HeightmapShape (index_x,index_y)
math::Vector3 size = this->heightmap->GetSize();
math::Vector2i vc = this->heightmap->GetVertexCount();
int index_x = (  ( (x + size.x/2)/size.x ) * vc.x - 1 ) ;
int index_y = (  ( (-y + size.y/2)/size.y ) * vc.y - 1 ) ;

//  getting the height :
double z =  this->heightmap->GetHeight( index_x , index_y ) ;

return z;
}

more

I've never used it, but the HeightmapShape class has a GetHeight(int _x, int _y) function which seems to be what you want. You could use it from a world plugin.

more

## Stats

Asked: 2017-09-04 08:28:42 -0500

Seen: 911 times

Last updated: Sep 07 '17