Sign in to follow this  
DroxXodia

Interpolation for Terrain Following

Recommended Posts

DroxXodia    122
So I have a mesh terrain and I store it's height values etc.. I've read a bit on interpolation, and it seems I understand the concept, but perhaps a bit shaky on the implementation. Here is my function I whipped up, It works, but it seems it is taking the overall average of the quad instead of the height value at my current location. Also every time I run over one of the lines in the grid mesh, my character bumbs up for an instant much higher then where he should be. That's my main grief, is when I hit a line the height gets messed up. I can live with the average height for now until I create a better solution.
float getHeight(float posX, float posZ);
float getHeight(float posX, float posZ)
{
	int Xa = posX;
	int Xb = posX+1;

	int Za = posZ;
	int Zb = posZ+1;

	float HTL = fl.getMapY(Xa, Za);
	float HTR = fl.getMapY(Xb, Za);
	float HBL = fl.getMapY(Xa, Zb);
	float HBR = fl.getMapY(Xb, Zb);

	float lerpY1 = HTL + (((posZ - Za) * (HBL - HTL)) / ((Zb + 1) - Za));
	float lerpY2 = HTR + (((posZ - Za) * (HBR - HTR)) / ((Zb + 1) - Za));
	float lerpY3 = HTL + (((posX - Xa) * (HTR - HTL)) / ((Xb + 1) - Xa));
	float lerpY4 = HBL + (((posX - Xa) * (HBR - HBL)) / ((Xb + 1) - Xa));

	return ((lerpY1 + lerpY2 + lerpY3 + lerpY4) / 4);
}
[Edited by - DroxXodia on March 19, 2007 1:22:19 AM]

Share this post


Link to post
Share on other sites
oliii    2196
I don't think that's how bilinear interpolation works.


float getHeight(float posX, float posZ)
{
int Xa = posX;
int Xb = posX+1;

int Za = posZ;
int Zb = posZ+1;

float HTL = fl.getMapY(Xa, Za);
float HTR = fl.getMapY(Xb, Za);
float HBL = fl.getMapY(Xa, Zb);
float HBR = fl.getMapY(Xb, Zb);

float dx = (posX - Xa) / (Xb - Xa); //(Xb - Xa = 1)
float dz = (posZ - Za) / (Zb - Za); //(Zb - Za = 1)

float lerpT = HTL + dx * (HTR - HTL);
float lerpB = HBL + dx * (HBR - HBL);
float lerp = lerpT + dz * (lerpB - lerpT);
return lerp;
}




try that.

Share this post


Link to post
Share on other sites
DroxXodia    122
That's perfect oliii. Thanks a lot.

I am still in college, I am gonna talk to my math professor this week. She is really great and I'm sure she can explain how the whole concept works overall.

What I tried to do was take all for lines, find my position on them, and take the average. Your modification works perfect. So I'm gonna iron out the details with my professor so I don't just use the solution but truly understand.

Again thanks so much for your help oliii

Share this post


Link to post
Share on other sites
oliii    2196
no problemo :). basically, take the linear interpolation of the top and bottom line, and take the linear interpolation of the two results along the other axis. Can work both ways (do 2 x top-bottom, then left-right interpolations).

This is a problem with that sort of interpolation, it wont give you the perfect result. Depending on the way you do things, you will get slightly different outcomes. A bit like splitting your quads into two triangles with the diagonal going from (TL->BR), or doing with the diagonal going from (TR->BL). Not the same triangles.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this