Skip to main content
gamedev.net
🔒 Locked

heightfield interpolation

Started by 303 Nov 30, 2000 at 2:26 AM 1 replies 500+ views
Original Post
303
303
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
civguy
civguy
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.
civguy
civguy
Could someone please tell me why the source-tags didn''t work properly in my former post?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.