Thread: Finding Camera Y coordinate on a heightmap

  1. #16
    Bored Programmer
    Join Date
    Jul 2009
    Tomball, TX
    linear interpolation
    Wow don't know how such a simple formula
    pu = p0 + (p1 - p0) * u

    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.
    Virtual reality hello world in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    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.


    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).
    Last edited by VirtualAce; 01-10-2013 at 06:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with finding the shortest coordinate of a maze
    By chickenlittle in forum C++ Programming
    Replies: 10
    Last Post: 10-24-2011, 12:27 PM
  2. Puget sound heightmap discription
    By fleurdelys77 in forum Game Programming
    Replies: 18
    Last Post: 09-15-2011, 05:49 AM
  3. Is it a good method (BMP heightmap to raw) ?
    By fleurdelys77 in forum Game Programming
    Replies: 7
    Last Post: 09-11-2011, 05:17 AM
  4. Displaying a heightmap
    By Dark_Phoenix in forum Game Programming
    Replies: 6
    Last Post: 11-11-2006, 03:24 PM
  5. Finding Y coordinate
    By Extol in forum Windows Programming
    Replies: 3
    Last Post: 04-16-2003, 07:50 PM