Well this is my version of a terrain class for the Engine, it is a very simple class. I took the initial idea and I guess framework from Riemers Series 4 tutorial and got it to fit my engine. I added a few methods to it to make it a bit more user friendly, for instance there is a method to tell if an object has collided with the terrain object, there is a method that will return the given height of the terrain at a given Vector3 and another method that I use to raise and lower the terrain with a mouse click. This terrain picker is derived from my picking method found in the last post, it was also inspired (again) by Riemers post on his forum concerning terrain picking, what he was doing was very similar to my object picker and so gave me the inspiration to come up with my method. I also have to add that if it was not for Chr0n1x I would not have gone looking for examples to do terrain picking.
I also created my own shader for the terrain as I found that Riemers, although very good as it had great detail, slowed my engine a little. The shader I came up with, while not being as pretty gives a faster render, I will put that up here to.
So on to the code. What I will do is break it down into chunks again and then offer the whole class at the end. I am not going to go over the basic terrain code as this is all covered in Riemers tutorial, for this please go to his site and read though the tutorial to get a better understanding, saves me repeating his tutorial. Also there are some methods I have commented out as they are unfinished, for example I am working on a random map generator, this is not complete so has been removed, also there is the color map generator for the terrain, this I have left as it is completed, but it was more of a folly rather than for any real purpose, I have left it in as a matter of completeness.
Terrain Position
private Vector3 basePosition;
public new Vector3 Position
{
get { return basePosition; }
set { basePosition = value; }
}
This field and property were added so that I could have my terrain centered on the position rather than the position vector be the bottom left hand corner of the terrain mesh.
GetHeightAt
public Vector3 GetHeightAt(Vector3 objPosition)
{
Vector3 retVal = new Vector3();
int x, y;
y = (int)objPosition.X + (myWidth / 2);
x = (int)objPosition.Z + (myHeight / 2);
if ((x >= 0 && y >= 0) && (x < myWidth && y < myHeight))
retVal = new Vector3(x, y, myPosition.Y + myHeightData[x, y]);
return retVal;
}
This method basicaly returns the height value of the terrain at a given position. I used this for debugging my collision method using it to display the height of the terrain as I drove my camera over it.
Collision
public bool Collision(Vector3 objPosition,float heightBuffer)
{
bool retVal = false;
int x, y, objHeight;
y = (int)objPosition.X + (myWidth / 2);
x = (int)objPosition.Z + (myHeight / 2);
objHeight = (int)(objPosition.Y - heightBuffer);
if ((x >= 0 && y >= 0) && (x < myWidth && y < myHeight))
{
if (objHeight < myPosition.Y + myHeightData[x, y])
retVal = true;
}
return retVal;
}
This method returns true if the object is colliding with the terrain and false if it is not, simple enough. The heightBuffer is there as your models position may well be taked from it's center and so you need to specify the distance from the center to the base of the model. I guess you could use the radius of the objects native BoundingSphere, I did not do this as this as just occured to me as I write this.
Here is an example of this in action, this code placed in the Update of your Game will stop your camera from going through the terrain and give it the impression of being solid.
// Terrain collision.
RCTerrain terrain = (RCTerrain)(((RCObjectNode)game.Scene.GetObject("terrain")).Object);
game.WriteText(game.Font, 10, 100, terrain.GetHeightAt(RCCameraManager.ActiveCamera.Position).ToString(), Color.White);
if (terrain.Collision(RCCameraManager.ActiveCamera.Position, 2.5f))
RCCameraManager.ActiveCamera.Position = new Vector3(RCCameraManager.ActiveCamera.Position.X, RCCameraManager.ActiveCamera.Position.Y + .2f, RCCameraManager.ActiveCamera.Position.Z);
PickTerrain
Before Mouse Clicks

After wasfting the mouse, clicking left and right mouse buttons

public void PickTerrain(GraphicsDevice device, Point mousecoords,bool Up)
{
RCCameras.RCCamera camera = RCCameras.RCCameraManager.ActiveCamera;
Vector3 nearSource = camera.Viewport.Unproject(new Vector3(mousecoords.X, mousecoords.Y, camera.Viewport.MinDepth), camera.Projection, camera.View, Matrix.Identity);
Vector3 farSource = camera.Viewport.Unproject(new Vector3(mousecoords.X, mousecoords.Y, camera.Viewport.MaxDepth), camera.Projection, camera.View, Matrix.Identity);
Vector3 direction = farSource - nearSource;
float zFactor = nearSource.Y / direction.Y;
Vector3 zeroWorldPoint = nearSource + direction * zFactor;
Ray ray = new Ray(nearSource, direction);
for (int x = 0; x < myWidth; x++)
for (int y = 0; y < myHeight; y++)
{
BoundingBox tmp = new BoundingBox(myVertices[x + y * myWidth].Position + myPosition, (myVertices[x + y * myWidth].Position + myPosition) + new Vector3(1f, 1f, 1f));
if (ray.Intersects(tmp) != null)
{
float val = 0;
if (Up)
val += .5f;
else
val -= .5f;
if (x > 0)
myHeightData[x - 1, y] += val;
myHeightData[x, y] += val;
if (x < myWidth)
myHeightData[x + 1, y] += val;
if (x > 0 && y > 0)
myHeightData[x - 1, y - 1] += val;
if (y > 0)
myHeightData[x, y - 1] += val;
if (x < myWidth && y > 0)
myHeightData[x + 1, y - 1] += val;
if (x > 0 && y < myHeight)
myHeightData[x - 1, y + 1] += val;
if (y < myHeight)
myHeightData[x, y + 1] += val;
if (x < myWidth && y < myHeight)
myHeightData[x + 1, y + 1] += val;
break;
}
}
BuildTerrain(device);
}
This is basicaly my Picking code, but what I am doing here is checking to see if my ray intersects a vertex of the terrain. I do this by moving through each of the terrains verts and using their possition Vector3 build a bounding box around them to check if my ray intersects it, if it does then I do what I want to do, in this case raise or lower the trrain depending on which mouse button was clicked, a bit like POPULOUS, a very old game I used to love. What I need to add to this method is the check to see if the intersecting vertex is the nearest to the camera like in my scene object picking method. Oh well at least this gives you an idea of how it can be done.
[Class and shader was here, but can't post it, please see original blog post]
Well I guess my Water class is next, here is an image of it in action:
First some nice calm water, waves at an amplitude of .1

Now at amplitude 1

"lets go surfing now, everybody's learning how..."
Posted
Mon, Oct 15 2007 2:59 PM
by
Nemo Krad