Managed and unmanaged DirectX development.
The evolution of the Randomchaos 3D Engine and Generic XNA samples.

Enter your search termsSubmit search form

Web

www.riemers.net

www.ziggyware.com

www.xna-uk.net

Thursday, June 07, 2007

Engine Design - Terrain

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.

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.

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.

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.

Can you send me your code and I can step through it and see what the problem is. You could be drawing the terrain at position 0,0,0 in which case if your terrains height in the height map is above your camera you may not see the terrain as it is above your camera.