Home | Tutorials | Wiki | Issues
Ask Your Question
0

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

asked 2017-09-04 08:28:42 -0600

dmeltz gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-09-07 14:13:36 -0600

dmeltz gravatar image

updated 2017-09-07 14:14:58 -0600

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;
     }
edit flag offensive delete link more
0

answered 2017-09-04 11:40:34 -0600

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 57 times

Last updated: Sep 07