# Finding Camera Y coordinate on a heightmap

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 01-10-2013
Lesshardtofind
Quote:

linear interpolation
Wow don't know how such a simple formula
LERP
pu = p0 + (p1 - p0) * u

where
pu - result
p0 - start
p1 - finish
u - progress

Has eluded me up to this point but.... wow. So useful. Thank you very much for the information on that one.

My brain is being alot more difficult when trying to learn the Barycentric Coordinates, but I am sure another day or two and it will sink in.

I was able to solve the entire problem using linear interpolation, but it isn't perfect. I am using the difference of the X and Z camera Values from a Vertex divided by the total width and height to get my X progress and Z progress then I'm using the linear interpolation along the triangle edges with the respective progresses and giving the larger value of Y to the Camera. Its not perfect yet, but quite a bit better than before.

Now I have to look into how to use linear interpolation to smooth out the Vector to look at and the motion inbetween frames as things are very joltish right now.

As always you have been a great help Virtual Ace (I think your name used to be Bubba)! Thanks again.
• 01-10-2013
VirtualAce
Glad I could help. The barycentric coordinates are just a fancy way of saying how far into the triangle the camera is. After you compute this, as I said, the rest is just linear interpolation (or moreso bilinear). Use the x coefficient and the y component of the points from the triangles in the first two lerps to get two values. Now use the y coefficient to lerp between the two values you just computed. The result is the height where the camera is.

Essentially:

value1 = LERP(p1.y,p2.y,xCoef);
value2 = LERP(p1.y,p3.y,xCoef);
finalValue = LERP(value1,value2,yCoef);

Close inspection of these operations will show you that they create a line...which is a result of a bi-linear interpolation (an interpolation on x and an interpolation on y...or in this case z).

There are several ways to smooth out the look vector but it may require that you alter your rotational formulas a bit. Before I proceeed I am assuming you are attempting to rotate your camera to face a certain direction. This requires at the least linear interpolation (LERP) but is better with spherical linear interpolation (SLERP) or even better yet spherical quadrangle interpolation (SQUAD).
However if you are using Euler angles it is mathematically impossible to correctly linear interpolate between two Euler angle representations. Of course you can hack it and make it look good but I recommend you use quaternions since they allow SLERP and SQUAD. Switching to quaternions is not nearly as hard as it sounds. If you are using axis-angle representation now you are very close to using quaternions.

There are several ways to rotate the camera to face a direction. The hard way is as follows:
• Create a vector from the camera to the object or direction you wish to face.
• Store the dot product of this vector and the look vector of the camera or object to rotate.
• The dot product is the cosine of the angle between these two vectors and will be used to rotate the camera or object.
• Cross the look vector of the camera or object and the vector you just created.
• This is the axis of rotation about which you will rotate the camera or object to face the other object
• Rotate along this axis by the arc cosine of the dot product stored from earlier.

If you are using quaternions the last step can be accomplished easily via SLERP and if you are using Euler you will have to use linear interpolation to interpolate the right, look and up vectors to match the compute right, look and up vectors. However with Euler angles it will be difficult to determine which direction to start rotating in since there is overlap in the Euler angle system. That is there are two or more or infinite representations that yield the exact same result.

The second method you can use to do this is to create a look-At matrix. Create a look-At matrix that will point to the object in question. Extract the right, up and look vectors from the resulting matrix. Linear interpolate between the current camera right, up and look vectors and the look-at matrix right, up and look vectors.

Note that these approaches are for 3D. If you want simple 2D rotation (and you don't b/c of the terrain) then you can use a simple atan2f(y,x).
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12