XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Getting the tilt of terrain

Last post 1/11/2008 1:22 PM by Shawn Hargreaves. 6 replies.
  • 1/9/2008 8:56 AM

    Getting the tilt of terrain

    Hi,

    I have came up with a simple height-map based terrain, and using the terrain collision sample, get a hovercraft not to sink into the terrain itself. I am just wondering how I can change the orientation of the hovercraft such it will tilt according to the slope of the terrain?
  • 1/9/2008 10:23 AM In reply to

    Re: Getting the tilt of terrain

    Answer
    Reply Quote
    This is a case for you friendly neighbourhood cross product.

    From the top of my head, something similar to this should do it:
    * Start out with your orientation before applying the tilting. We call the forward, right and up vectors:
    forwardBefore, rightBefore, and upBefore. These vectors need not be units vectors, only the direction matter.
    * Get the normal of the terrain where you are currently located. (Not that this will not work if the normal is identical to forwardBefore or rightBefore). I call that vector upAfter since this will be your new up vector.
    * Find your new forward vector by crossing upAfter and rightBefore. I call the result forwardAfter:
    forwardAfter = Vector3.Cross(upAfter, rightBefore); // Note the order!
    [* It is not needed for this exercise, but if you want your new right vector, cross forwardAfter and upAfter. I call the result rightAfter:
    rightAfter = Vector3.Cross(forwardAfter, upAfter); // Note the order! ]
    * You can now create the world matrix using Matric.CreateWorld(...) using your position, forwardAfter and upAfter. I don't believe you will need to normalize the vectors before, but you'll have to verify this. Alternatively, you can create the world matrix yourself using forwardAfter, upAfter, rightAfter, and position.

    If you are unfamiliar with cross products (and the right-hand rule) invest some time in understanding it, e.g. http://en.wikipedia.org/wiki/Cross_product, you will not regret it!

    Hope this helps!


    ---
    Sharing my TDD XNA experience on XNA's Nicely Addictive
  • 1/10/2008 11:28 PM In reply to

    Re: Getting the tilt of terrain

    I am just wondering; I only have the height of the terrain at each point - how do I obtain the normal?
  • 1/11/2008 1:51 AM In reply to

    Re: Getting the tilt of terrain

    Once again with the help of the cross product.

    If you want to do it on the fly:
    As you probably have not only the height (y), but also x and z components for each point, you have its position. Now get the position of three neighbourhood points of the terrain; where you are standing and just to the right as well as just in front of it. We call these vectors p0 (where you are) p1, and p2. Use the three points to create two non-parallell vectors in the plane v0 = p1 - p0 and v1 = p2 - p0. Finally, then youhave two non-parallell vectors, together they will span a plane. To get the normal of the plane, use the cross product between the vectors:
    Vector3 n = Vector3.Cross(v0, v1);
    n.Normalize();
    Voila, you have your normal. The problem is that if you are not deterministic while choosing your two points, your normal might point in the opposite direction. However, since you (probably) know that Y? is your up direction, check if the Y component is negative and in that case negate the normal.


    If you want to do most of the math in advance:
    When you store your height map, make sure that you also store the normal for each point along side with it. Later, when you sample your height map to find the current height, also sample the normals. For creating the normals for the height map, use the technique described earlier.

    Hope this was helpful.
    ---
    Sharing my TDD XNA experience on XNA's Nicely Addictive
  • 1/11/2008 1:15 PM In reply to

    Re: Getting the tilt of terrain

    Oh geez! I have no idea how I managed to missed this...

    Thanks a lot, to everyone!
  • 1/11/2008 1:22 PM In reply to

    Re: Getting the tilt of terrain

    That sample is new as of yesterday.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (7 items) Previous Next