One of the key challenges facing budding new game programmers is getting artwork. You may have the greatest game concept ever conceived but without an ability to demonstrate it visually that is all it shall remain. The options available to one man band outfits (such as myself) are:

Find an artist to do it for you.

Find a source of free artwork.

Hope blue rectangles and red circles will hold up.

Steal artwork from an existing game.

Learn how to do it yourself.

This article will focus on option 5, which is not quite as difficult as you might imagine. It is actually reasonably easy to produce quality artwork in a short period of time if you limit yourself to producing the type of artwork that can be done quickly and easily. The main limitation you will face is animation. Animation is hard. Animation is really hard. You can’t fake animation, you can’t have rough animations that look pretty good. Animation is something that unless done really well, looks really bad. So basically, don’t do animation. This does cut down your options in the genre of game you can produce quite considerably, but hey creativity thrives when constrained. So basically go for something with blocks, balls, cars, planes, boats etc.

Ok, so on to how to do it. In part one of this article I will go over a quick way to produce nice looking artwork without going near a paint program. In part two I will go over how to make a nice looking vehicle graphics with minimal artistic talent.

Procedural content generation has many advantages especially for programmers. It doesn’t involve paint programs. It considerably reduces the size of the game. If done right it can look very professional. So without further ado, here is how the planets in StrafeFighter were done.

Step 1, Make some noise.

At it’s heart Procedural content generation is random. So first things first we need a source of randomness. This is actually much harder than you might imagine. Lets start with Math.random() that is pretty random, random looks like this:

Which is not particularly exciting. What we want is something that is not quite so random. Getting something not quite random and fast is actually pretty tricky. Luckily a guy by the name of Ken Perlin has already solved this problem for you and has come up with pretty much the ultimate noise function, Perlin noise. And if that wasn’t enough he went ahead and came up with an even better one than that, Simplex noise. Simplex noise looks pretty similar to Perlin and has less complicated code (although it is conceptually harder to understand) and is significantly faster for higher dimensions. I recommend this article: http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf. It has a pretty good explanation and Java source code. Here is what basic Simplex noise looks like:

This is starting to look interesting. Now you may be wondering looking at the above code what the function object is. At this point is worth introducing a very powerful interface for generating procedural content.

public interface Function2D {
float eval(float x, float y);
}

The function object is an instance of a class which implements this interface using a Simplex noise generator. We will see later on how defining this very simple interface allows a great deal of flexibility.

You will notice that the texture is looking a little bit smooth and lacks any detail. The easiest way to get some nice details is to add in some higher frequency noise to it. You do this by simply adding a smaller and smaller amounts of at noise generated with increasingly extreme coordinates. Luckily it is really easy do do using the Function2D interface:

I have called it Fractal because it shares many properties with them and is actually an implementation of something called Fractional Brownian Motion. Octaves basically says how many times we run the source function and fade describes how much of the result we add at each iteration. Now if we construct a Fractal passing it a Simplex noise source the result looks like this:

Now were talking. But it’s all gray, we need some colour. Now at the moment we are generating a noise value for each pixel and setting the brightness of that pixel to that value. One way of generating nice colourful images is to use the random value as an index into a colour table rather than for the brightness of the pixel. A nice way of generating a colour table is just to fire up a paint program (I know I said no paint programs but this is an exception) and go nuts with the gradient tool. Gimp is my preferred program (it’s free) and has some really nice preset gradients. For an earth-like planet I went with this one.

Which results in:

Ok so this is looking pretty nice, but it is square, we need round planets. Well this is pretty easy, let’s just set the pixels outside of a circle to be transparent (when dx*dx + dy*dy > 1). and hey presto:

Hmm… this is looking circular but planets are spherical. Well after playing around with a few ways of distorting the texture to make it look spherical I found the best way was using an elliptic lens. This ends up look like the following:

To do this, take the normalized coordinates you would have used to evaluate the fractal function and run them through this first:

And thats pretty much it. We can add a nice atmosphere simply by adding a blue colour and fading the alpha value to zero as we go out. Diffusely lighting a sphere is also pretty easy we just multiply each pixels brightness by a light value calculated with:

lightPos is a vector that is the direction of the light relative to the planet. AMBIENT_LIGHT and DIFFUSE light can be used to control the relative lighting across the planet between the dark/light side. The end result looks like this:

The great thing about the Function2D interface is it is really easy to cascade function blocks to get just about any planet you can imagine. The following Gas Giant was generated using

The Adder and Scalar classes are implementations of Function2D that add or scale other functions. The two parameters for scalar are the x scale and y scale. The adder parameters are the weights of the first and section functions.

Procedural content generation is a powerful technique particularly useful for the artistically inept. Although it is great for backgrounds and textured simple shapes it is really pretty difficult to procedurally generate a car. So for part 2 (coming soon) I will go over some techniques for drawing (gasp) mechanical things like ships and vehicles.

This entry was posted on Sunday, September 7th, 2008 at 6:29 pm
and is filed under Tutorials.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.