First heres what i think you want: You''ve got some piece of terrain and you want your character to walk over it and not through it. To do that were going to translate the character to its x and z co-ordinate and use those values to calculate the y coordinate of the character from your terrain.
First thing you have to change is that your terrain must be made up of triangles. Squares arn''t planer so we cant interpolate easily between them. Ok, in my solution, your terrain needs to be made up of triangles and to make things easier im going to use a system like Trent''s game tutorials.
The terrain mesh will look like this:
_________________|\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\|-----------------
It is important that you draw the triangles going from upper left to bottom right - you''ll see why in a minute. One important thing to note is that each square is made up of two planes - in this case there is the one made by the bottom left triangle and the one made by the upper right triangle.
Ok, now lets write a function for your terrain that takes two variables, your character''s x and z position and have it generate a y position. The function i write will make it unable to translate your terrain because we are going to compare the world co-ordinates of the character to the terrain - you can easily compensate for this. Our terrain will be stored as an array of vertices like so: Data[j]. It must also have world coordinates in it''s vertices.
//the distance between the vertices of the terrain - terrain must be a square#define DIST_Vfloat y TERRAIN::GenY(float X,float Z){//first thing we have to do is find out which triangle our//character is onint i,j;bool Tri = false, done = false;//loop through all the rows values of the terrainfor(i = 0; loop < (NumI - 1); i ++){ for(j = 0; j < (NumJ -1); j ++) { //check to see if our point is in the square if((X < Data[i+1][j+1].x) && (Z < Data[i+1][j+1].z)) { //now check to see which triangle in the square the //point is //we assume that it is in the bottom left corner //but we check the top right here float tx = (X - Data[i][j].x), tz = (Z < Data[i][j].z); if( (tx + tz) > DIST_V) Tri = true; done = true; break; } } if(done) break;}//ok, now the values of i and j represent the lower left vertex//of the plane that our character is on//we can use these values to generate our y value//there are two situations - the plane we have to check is the //lower left or it is the upper right - our Tri variable will//tell usfloat t1 = 0, t2 = 0, answer = 0;//this means its in the upper rightif(Tri){ //this a bit complicated - you need planer geometry t2 = ( ( (Data[i+1][j].x - Data[i+1][j+1].x)*(Z - Data[i+1][j+1].z) - (Data[i+1][j].z - Data[i+1][j+1].z)*(X - Data[i+1][j].z) ) / ( (Data[i+1][j].x - Data[i+1][j+1].x)*(Data[i][j+1].z - Data[i+1][j+1].z) - (Data[i+1][j].z - Data[i+1][j+1].z)*(Data[i][j+1].x - Data[i+1][j+1].x) ) ); t1 = ( X - t2 * *(Data[i][j+1].x - Data[i+1][j+1].x) - Data[i+1][j+1].x ) / (Data[i+1][j].x - Data[i+1][j+1].x); answer = t1 * (Data[i+1][j].y - Data[i+1][j+1].y ) + t2 * (Data[i][j+1].y - Data[i+1][j+1].y) + Data[i+1][j+1]; return answer;}else{ //this a bit complicated - you need planer geometry //hopefully this works :) t2 = ( ( (Data[i+1][j].x - Data[i][j].x)*(Z - Data[i][j].z) - (Data[i+1][j].z - Data[i][j].z)*(X - Data[i+1][j].z) ) / ( (Data[i+1][j].x - Data[i][j].x)*(Data[i][j+1].z - Data[i][j].z) - (Data[i+1][j].z - Data[i][j].z)*(Data[i][j+1].x - Data[i][j].x) ) ); t1 = ( X - t2 *(Data[i][j+1].x - Data[i][j].x) - Data[i][j].x ) / (Data[i+1][j].x - Data[i][j].x); answer = t1 * (Data[i+1][j].y - Data[i][j].y ) + t2 * (Data[i][j+1].y - Data[i][j].y) + Data[i][j]; return answer;}}
The last part of this is sort of unclear. All you really have to do is relate those i and j values to this:
//if we had a square the the index values would be here:i+1,j_____ i+1,j+1 | | | |i,j ----- i, j+1
now all you have to do is recode that and then if you move your character''s x and z coordinates and use this to generate the y then it should walk over the cliffs