Problems interpolating height

Started by
4 comments, last by mikequinn 19 years, 5 months ago
As far as I can tell, this code should work. Obviously its not though, so I need some help. I can't seem to properly find the height at any given point on my heightmap made of triangle strips. I'm trying to use the equation of the plane that the triangle is in to find the height, but I'm getting some strange results. It works perfectly when above certain triangles, but is WAY off on others, and I can't seem to find any reason as to when it will or will not work. Here's my code: float Terrain::getHeightAtPoint(float x, float z) { float normal[3]; // set 'normal' to the normal of the triangle (x, z) falls within getNormalAtPoint(x, z, normal); // find the points of the triangle (x,z) falls within point p1, p2, p3; findSurroundingPoints(x, z, p1, p2, p3); // find d float d = -normal[0]*p1.x + -normal[1]*p1.y + -normal[2]*p1.z; float height = (-d - (normal[0]*x) - (normal[2]*z)) / normal[1]; return height; }
Advertisement
shouldnt the normal be derived from the points found?

also how do you know the p1 found is consistent? obviously it makes a difference what point out of three is considered p1.
- "shouldnt the normal be derived from the points found?"

True. The getNormalAtPoint actually recalculates them using the same points, so I changed that. My code now only calculates the 3 points once, but the same problem is still happening.

- "also how do you know the p1 found is consistent? obviously it makes a difference what point out of three is considered p1."

'findSurroundingPoints" will always set p2 and p3 to be the endpoints of the hypotenuse of the triangle, and p1 to be the point at the right angle. So...

1-----2
| /
| / |
|/ |
3-----4

p1 will always either be point 1 or point 4, depending on where (x,z) lies.
woah that little text graphic didnt quite work. I'm sure you get the idea though
in that case, simply do something like this:

dy2 = p2.y - p1.y
dy3 = p3.y - p1.y

dx = x - p1.x
dz = z - p1.z

h = p1.y + dy2 * dx + dy3 * dz

the only thing to look out for is the two seperate cases: the triangle facing one way and the one facing the opposite one. dx and dz will have their signs flipped depending on which triangle youre in. also, you need to make it so that p2 will always be either left or right from p1 on the x-axis, and p3 on the z-axis.

but both of these are simple to ensure.
hm, thank you. I'll go try this out.

This topic is closed to new replies.

Advertisement