Saturday, 3 January 2009

Those jagged edges

After patching together the crater (described previously), I noticed a deformation at the edges of the crater's upturn. Initially, I gave it little thought and blamed it on a side-effect associated with terrains with sharp edges.

However, the subconscious mind is apparently very persistent, and a possible fix dawned on me. The four vertexes used to create the two triangles for the terrain do not (in all likelihood) line up to form a plane. The quadrilateral create by the points A,B,C and D is divided into two triangles using either the diagonal AC or the diagonal BD. In the terrain construction, the same diagonal is always chosen. The picture above illustrates the consequence -- the edges of the crater is very jagged.

Instead of a fixed choice, a better result is obtained when the diagonal is chosen with more care. Here is the rule I applied: take the diagonal that is the highest. This means that the quadrilateral will be always be convex (as seen from the outside) -- and some concave aspects of the generated terrain will disappear. In other words the terrain will have less jagged edges. To compute the highest diagonal I take the diagonal that has the largest sum of the Y component of its vertexes. Other methods also seem reasonable: for instance take the diagonal with the highest Y.

The second picture shows the same view as the first using the largest sum method. Definitely a noticeable improvement.

Thursday, 1 January 2009

Riemers Components 1.1.0

A little bit of functionality has been added since the last time I published the code, and time for a version update has arrived.

The new stuff in Version 1.1.0 is briefly described in related blog entries:
  1. The terrain weights are adjusted not only based on height, based also on the normal.
  2. A new class was added to generate heightmaps using Perlin Noise.
  3. The ability to create a larger heightmap from a small one through tiling was added.
  4. Placing volcanoes and craters on the heightmap is also possible.

If you would like to use these components in your own game, I suspect the best way is to copy code from my project to your project, and make changes to the classes as you see fit. The components were not created to be generic or to be a reusable library of sorts. It simply presents you with an approach to combine the various aspects of creating a terrain into a reasonable set of classes.

The interesting thing about class design is that it is a little bit of an artform. Like other art, not everybody like what they see, but the artist is likely to stay true to himself. Sometimes, the artist might even cut off his ear.

However, class design can also be a little bit of a science form. There is a lot to learn, and I need my ear for that, thank you very much. You may have better ideas on the class composition I did here and I invite you to share your ideas and feedback with me if you get some time.

I published the code in a zip file that can be downloaded here.