Sign in to follow this  
mikequinn

Problems interpolating height

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
- "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.

Share this post


Link to post
Share on other sites
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.

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