Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# heightfield interpolation

This topic is 6560 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

i have a heightfield and i am moving objects over it.. but how do i get correct y-value for object from any point at heightfield? i''ve tried to write interpolation routine but i haven''t succeed. this is what i''ve tried:  // x = object_x (0-300), z = object_z (0-200) int cx = (int)(x/10.0f); int cz = (int)(z/10.0f); float mixx = x/10.0f-cx; float mixz = z/10.0f-cz; int xheight1 = gridx[cx][cz]; int xheight2 = gridx[cx+1][cz]; int zheight1 = gridy[cx][cz]; int zheight2 = gridy[cx][cz+1]; int u = xheight1*mixx + xheight2*(1.0f-mixx); int v = zheight1*mixz + zheight2*(1.0f-mixz); // object_y = u+v  at some areas this routine works.. but i would like to get it working everywhere

#### Share this post

##### Share on other sites
Advertisement
I assume you draw the heightfield with polygons.

2 polygons like this:

-------|    /||   / ||  /  || /   ||/    |-------

Right? Well ordinary bilinear interpolation is no good here (and you did it wrong anyway).

You''ll have to check on which of those two polygons you are on at the moment and then use

  int x1 = grid[cx][cz+1];int x2 = grid[cx+1][cz+1];int u = x1*mixx + x2*(1.0f-mixx);int v = grid[cx+1][cz];object_y = u*mixz + v*(1.0f-mixz);[/source]or[source]int x1 = grid[cx][cz];int x2 = grid[cx+1][cz];int u = x1*mixx + x2*(1.0f-mixx);int v = grid[cx][cz+1];object_y = u*mixz + v*(1.0f-mixz);

depending on the polygon you are on. As you note the three points taken from the grid are the corners of a single polygon.

I have no idea why you have gridx and gridy and both are 2-dimensional arrays. You only need one 2-dimensional grid for heightfield.

And also note that

u*mixz + v*(1-mixz)

can be written as

u + (v-u)*mixz

where you only need one multiplication.

Hope this helped.

#### Share this post

##### Share on other sites
Could someone please tell me why the source-tags didn''t work properly in my former post?

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011389
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!