Robotics StackExchange | Archived questions

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

Asked by dmeltz on 2017-09-04 08:28:42 UTC

Comments

Answers

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.

Asked by chapulina on 2017-09-04 11:40:34 UTC

Comments

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::CollisionPtr collision = model->GetLink("link")->GetCollision("collision");
     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;
     }

Asked by dmeltz on 2017-09-07 14:13:36 UTC

Comments