KWEngine Part 08: Creating terrain - KWEngine/KWEngine2 Wiki

With the KWEngine you can create custom terrain by using a height map:

Height map example file

A height map is a monochrome image file with a resolution up to 256x256 pixels.

Create a terrain model

A terrain is a model like any other 3d model that you import to your scene. But instead of just loading the model, KWEngine will generate it by analyzing the given height map.

Create an empty GameObject subclass for your terrain:

public class Terrain : GameObject
{
    public override void Act(KeyboardState ks, MouseState ms)
    {
        // Hint: a terrain may change its position, but it may not rotate or scale!
    }
}

Generate the terrain:

In your World class (i.e. GameWorld) add the following line to your Prepare() method:

KWEngine.BuildTerrainModel(
    "MyTerrainName",             // name of terrain model
    @".\textures\heightmap.png", // link to the heightmap image
    @".\textures\texture.png",   // link to the terrain texture image
    100,                         // width of the terrain (x axis)
    4,                           // height of the terrain (white pixels will be 4 units high, dark ones will be 0)
    100,                         // depth of the terrain (z axis)
    10,                          // number of texture repetitions on x axis
    10                           // number of texture repetitions on z axis
);

Assign the terrain model to your Terrain instance:

Terrain t = new Terrain();
t.Name = "My wonderful terrain";
t.SetModel("MyTerrainName");
t.IsCollisionObject = true; // optional, but useful in most cases
t.IsShadowCaster = true;    // optional
AddGameObject(t);

Terrain collision detection

A terrain instance itself may not check for collisions, but any other GameObject instance may check for collisions with a terrain.

Retrieve a list of intersections (collisions)

Inside your Player's Act() method, write down these lines:

public override void Act(KeyboardState ks, MouseState ms)
{
    List<Intersection> intersections = GetIntersections();
    foreach(Intersection i in intersections)
    {
        if(i.IsTerrain == true) // if the current intersection is because of a terrain...
        {
            // ...change the player's y-position to the terrain's y-position:
            MoveOffset(i.MTVUp);
        }
    }
}

You can also use the field MTV instead of MTVUp which considers the terrain's slope at contact point.