Jump to content
• ### What is your GameDev Story?

• Advertisement

#### Archived

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

# heightfield interpolation

This topic is 6621 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
• ### What is your GameDev Story?

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

(You must login to your GameDev.net account.)

• ### Popular Now

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

• Total Topics
634047
• Total Posts
3015231
×

## 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!