Jump to content
  • Advertisement
Sign in to follow this  
psyjax

Finding Arbitrary Points on a Heightmap

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings and Salutations, I have created a heightmap using a grayscale image to generate points. These points are then saved to a file. When it becomes necissary to display this map, I load the points in this file into a 2D array of points. The following is some quick psudo-code as I don't have the source right in front of me at the moment :) The structure is something like this:

typedef struct
{
    float x;
    float y;
    float z;
}Point3D;




The array would look somthing like this:

Point3D HeightMap[200][200];




Anyway, to draw the map I simply itterate thrugh the array and use GL_QUADS and glVertex3f, like so:

for(int x=0; x < 200; x++)
{
    for(int y=0; y < 200; y++)
    {
        glBegin(GL_QUADS);
        glVertex3f(HeightMap[x][y].x, HeightMap[x][y].y, HeightMap[x][y].z);
        glEnd();
    }
}




Works great. But now, I wan't to be able to find an arbitrary point on this mesh and determin it's height. This is so I can make a little character be able to realisticaly go up mountains, down hills, etc. I would imagine that to do this I should find the nearest vertex to the character, then calculate a slope using that vertex and the midpoint of the other two verticis in the triangle where the character stands. Am I close on this? Also, does anyone know any good tutotials on doing things like this? Thanks alot in advance!

Share this post


Link to post
Share on other sites
Advertisement
"I would imagine that to do this I should find the nearest vertex to the character, then calculate a slope using that vertex and the midpoint of the other two verticis in the triangle where the character stands. Am I close on this?"

Sounds like you do know what you do need to do. With 3 points you can define a plane, and simply intersect that with a line going in the Y direction.

Share this post


Link to post
Share on other sites
Hey Skow,

Guess I'm smarter than I gave myself credit for :)

Im gonna give that a shot then after I get off work. In the meantime however, if anyone can offer some sample code or tutorials out there, that could be helpfull as well.

Thanx a bunch!

Share this post


Link to post
Share on other sites
Well this isn't somthing complex enough to ahve a tutorial for, you just need to review your math and get at it. I myself would have review getting an equation of a plane from 3 points (though I've done it in the past).

Finding the y value when you know the x & z value is really easy once you get the plane equation.

Good luck

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

for position (x,y,z)

percentage into the map

realmapwidth/realmapheight are the real coordinates of the endpoints of the map (if the map is not starting at the origin this will have to be translated)

px = x/realmapwidth
pz = z/realmapheight

mapwidth/mapheight are the dimensions of the heightmap (ie hm[200][200])

xcell = (int)(px*mapwidth)
zcell = (int)(pz*mapwidth)

nx = (px*mapwidth)-xcell
nz = (pz*mapwidth)-zcell

so the 4 points around it

h[0] = hm[xcell][zcell]
h[1] = hm[xcell+1][zcell]
h[2] = hm[xcell+1][zcell+1]
h[3] = hm[xcell][zcell+1]

then bi-lerp

(h[0]*nx + h[1]*(1.0 - nx))*nz +
(h[2]*nx + h[3]*(1.0 - nx))*(1.0 - nz)

or something similar to that...
pressed for time

something is backwards I am sure...

but hope that helps

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!