# Calculating Height of terrain

This topic is 4374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I'm having problems calculating the height of the terrain. Here's a brief description: I'm using and exported .X file from 3ds and using that for the terrain as a mesh. Now the question is: Is it possible to use the terrain that way(and get the height)? How can i find out the height of the terrain at a giving x,z? I already got the vertex buffer from the mesh but i'm not being able to do anything from that nor do i seam to find anything on the web that talks about doing the terrain this way. Here's a screenshot.

##### Share on other sites
It's usually easier and more effective to store the terrain as a heightmap.

That is, represent the terrain as a 2-dimensional array with height values.

float heightmap[width][height];

If you were to do this, the height would simply be

y = heightmap[x][z];

If you wan't to load the heightmap as a mesh, you will have to eighter process the mesh data into some kind of heightmap-like-data or do ray-terrain intersection to find the height in a specific coordinate.

##### Share on other sites
Hi Daniel,

I really didn't wanted to use a heightmap because i think it would be easier to get a terrain out of 3d max.
I liked the option you saied about the "process the mesh data into some kind of heightmap".
Do you have any ideia on how i could do this?
I already tryed to do it with the vertex buffer data but with not luck!
Also the ray-tracing might be an option but i suppose its faster to calculate the heightmap from the mesh data at startup, so i don't have to ray-trace always?

Thanks

##### Share on other sites
Does anyone know how i can extract the x,z info from the vertex buffer of the mesh, and correctly calculate the height at a given x,z ?
What i have right now is:

{

long NVertices=mesh->GetNumVertices();
CUSTOMVERTEX *pVertices=NULL;
LPDIRECT3DVERTEXBUFFER9 pVB;
mesh->GetVertexBuffer(&pVB);
//height_values=new float[350][350];
char *tmp=new char[512];
int tmp2=0;
for(int i=0;i<NVertices;i++)
{
tmp2++;
height_values[int(pVertices.position.x)][int(pVertices.position.y)]=pVertices.position.z;
sprintf(tmp,"%d - %f ,%f, %f\n",tmp2,pVertices.position.x,pVertices.position.y,pVertices.position.z);
OutputDebugString(tmp);
}
delete tmp;
pVB->Unlock();
//delete [] pVertices;
}

But it isn't working....

##### Share on other sites
Quote:
 Original post by nitoicfI really didn't wanted to use a heightmap because i think it would be easier to get a terrain out of 3d max.

So basically you're trading Ease of Programming with Ease of using 3dsMax? *sarcasm* Uh Yeah, thats a Great descision there. Let me guess, you come from a background of computer Art; and are just now deciding to learn about the graphics Programming end?
A heightmap approach is going to be a lot simpler to code for, and the Editor is simpler too, just use mspaint to draw the map (color=height). Avoid all of this unecessary 3dsmax import stuff...

Now if you really want to try converting your Raw Mesh from Max. There are a few things to consider.
Is your mesh already in a nice grid arrangement? Heightmaps need regular vertex spacing; so if you don't have that then it wont be storable as a heightmap.

assuming it is a grid though, my approach would be to process 1 vertex at a time from the .x file, read coordinates and insert into a 2d sorting algorithm -a quadtree, or 2 passes of a bsp.
Once you have them in coord-sorted order...(and verify that they approximatly form a regular grid) extract the height coord from each one in order and insert into a heightmap array.

if your mesh wasnt a grid (seriously consider dropping this 3dsMax stuff in favor of a standard heightmap and mspaint editor)
then your approach is probably going to be to read polygons from the .x file and insert them into a space paritioning scheme for game representation.
As there is no grid structure you can use no 'tricks' for finding height; yer just going to have to do ray intereection tests everytime your player moves. Space Partitioning will help to reduce the cost... but its still pretty ugly.

##### Share on other sites
Hi!

Yes you are right, about the height map being easier, but i've once already implemented it and deleted it after a while.
This because the 3d artist is a friend of mine who is doing me a favor on doing the terrain, so basicly i can't ask him to do a heightfield because he wouldn't know how to do it, also i understand that its a lot easier for him to do it 3d max.
Anyway, if you can see the screenshot, all the stuff other that the 3 mesh's that are standing.
Is the "terrain" mesh(It has Wall's and the ground) but now i'm only concerned about the gound.

Any toughts on how i can do this?
Is the previous statment all wrong?

##### Share on other sites
Ok you've got 2 problems here.
1. Your friend the 3d artist, is an Artist. Not a Level Designer. Those are two different things. He is currently stuck in the mindset of making meshes and assuming whatyouseeiswhatyouget. This is in general not how levels are made; you need some kind of formalized level format or nothings going to be representable when it comes to the code. Sure he's going to be useful for making character models later on, but you need an actual Level Designer; someone who can work with the programmer (you) to understand how the game engine code works and how he can work within that framework to create maps.(not off in his own 3dsMax world)

2. Walls and the ground are two different things. And should be reprsented separatly; Not as one big mesh!
The ground is a heightmap, plan and simple. The walls should be objects that are positioned onto that heightmap.
They will have separate collision detection routines -the heightmap uses the simple stuff in the previous posts, the walls will use some kind of simple box primitive collisions. Two separate things that are simple, is better than 1 giant mesh that is complex.

##### Share on other sites
your friend should have the understanding of how to use height maps because there use is very common within 3d modeling packages to add details to object, they are also commonly known as displacement maps

##### Share on other sites
I got the point.
but now a have another question related to this of course.
Imagine i have a mesh Table inside a Mesh House and i want the player to be able to rise up to the table.
How can i do this?

##### Share on other sites
Well after re-reading my last post i tought i should refrase.
Imagine i wanted the player to be able to go Under the table.
Over the table we can just use a standard Bounding Box.
Thanks all.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634046
• Total Posts
3015219
×