Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57

 Error when I try to run.
  Posted by: Anonymous
  When: 15/03/2013 at 19:21:06

 Error With the Effect File
  Posted by: Anonymous
  When: 15/03/2013 at 18:21:01

 Can only get shadowmap
  Posted by: Anonymous
  When: 15/03/2013 at 15:48:52

 Vertex and Pixel Shader Versions?
  Posted by: Anonymous
  When: 15/03/2013 at 15:07:16

 Unsupported properties
  Posted by: Anonymous
  When: 15/03/2013 at 14:23:00

 Problem Loading Skybox
  Posted by: Rana
  When: 15/03/2013 at 10:34:45

 Black Screen Of Death - Help!
  Posted by: Anonymous
  When: 15/03/2013 at 03:43:43

 2.0 anyone?
  Posted by: Anonymous
  When: 15/03/2013 at 02:19:48

 Defitinition of tha rotation axis
  Posted by: Anonymous
  When: 15/03/2013 at 00:55:14




Topic: Terrain Collision...



  
Goto parent category
  
Create a new user account


   Terrain Collision...
 Poster : void
 Posts: 2
 Country : canada
 City: vancouver

  
Posted by void on 14/03/2007 at 22:17:53
Hi all...

I noticed a few people asking questions regarding terrain collision, so I thought I'd share with you a method for interpolating a positon on the terrain...

// this function takes in two floats for x and y position and returns the z(height) for that position...

float GetTerrainHeight( float fTerX, float fTerY )
        {
            // we first get the height of 4 points of the quad underneath the point
            int x = (int)fTerX;
            int y = (int)fTerY;
            float fTriY0 = (heightData[x , y]);
            float fTriY1 = (heightData[x + 1, y]);
            float fTriY2 = (heightData[x , y + 1]);
            float fTriY3 = (heightData[x + 1 , y + 1]);
            // find which of the 2 triangles the point is over (depends on how you render)
            // then take the height at a triangle point
            // and adjust by the slope down right-angle edges * distance along that edge.
            float fHeight;
            float fSqX = fTerX - x;
            float fSqY = fTerY - y;
            if ( (fSqX + fSqY) < 1 )
            {
                fHeight = fTriY0;
                fHeight += ( fTriY1 - fTriY0 ) * fSqX;
                fHeight += ( fTriY2 - fTriY0 ) * fSqY;
                } else {
                fHeight = fTriY3;
                fHeight += ( fTriY1 - fTriY3 ) * ( 1.0f - fSqY);
                fHeight += ( fTriY2 - fTriY3 ) * ( 1.0f - fSqX);
            }
            return fHeight;
        }

// then in your ProcessInput() function do:

cameraPosition.Z = GetTerrainHeight(cameraPosition.X, cameraPosition.Y)+2.0f;

// after the following line:

cameraPosition += Vector3.Transform(moveVector, cameraRotation);


Your camerae will now follow the terrain... from here you can easily add gravity and so on...


Great tutorials Riemer, keep up the good work! :)

 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 15/03/2007 at 07:51:46
Very nice
 Poster : Lordikon
 Posts: 57
 Country : USA
 City: Denver

  
Posted by Lordikon on 15/03/2007 at 12:09:12
Alternatively, this works as well:

// This keeps the camera from checking collision when it's off the sides of the heightMap
if ( cameraPosition.X < WIDTH && cameraPosition.X < 0 && cameraPosition.Y < HEIGHT && cameraPosition.Y > 0 )
{
// This keeps the camera above ground
    if ( cameraPosition.Z - 1.0f) < heightData[(int)cameraPosition.X, (int)cameraPosition.Y])
cameraPosition.Z = heightData[cameraPosition.X, cameraPosition.Y] + 0.75f;
}


This is from memory, I didn't cut and paste, so the syntax may not be exact.
 Poster : Lordikon
 Posts: 57
 Country : USA
 City: Denver

  
Posted by Lordikon on 15/03/2007 at 12:19:18
For the post above, I forgot to add that mine is simplified, however, it is less accurate, and has more chance for glitches. It's a quick fix is all.
 Poster : emuer
 Posts: 17
 Country : USA
 City: Los Angeles

  
Posted by emuer on 02/05/2007 at 18:39:59
i tried the simplified solution, one thing is that the camera.Position.X is not the actual value of the  "x" in heightData. Since heightData's x and y depends on the size of the heightMap. so you need to scale it down.

I think the interpolate method is the way to go, as it provides a smooth walking feeling, the simplified version is just too "jittering".
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 04/05/2007 at 06:28:20
Well, im new to this. So i can't figure out where the code should go.. (Again, im new!) Can anyone put it like riemer writes "Put this in the <nameofmethod>" ? Would be so great!
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 04/05/2007 at 09:48:53
put it after this line
cameraPosition += Vector3.Transform(moveVector, cameraRotation);
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 04/05/2007 at 10:23:08
Ok, that worked. But i got one problem. I still can't figure out; what to fill all the x's and y's.  Can someone tell me the full form of it?
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 04/05/2007 at 10:24:50
i mean like the full form for
" float GetTerrainHeight(float fTerX, float fTerY)"
 Poster : me26493
 Posts: 87
 Country : Australia
 City: Melbourne

  
Posted by me26493 on 04/05/2007 at 17:16:52
there is another thread about this:

http://www.riemers.net/Forum/index.php?var=712&var2=0

that might help.
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 28/08/2012 at 09:33:52
How i can use this code in XNA 4.0

  
Post a new reply
 



Useronline Insert Failed >

Google
 
Web www.riemers.net
If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !
- Website design & DirectX code : Riemer Grootjans -
©2006 Riemer Grootjans


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)