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