Perlin Noise

I've been quite interested lately in procedurally generated terrain and how I might be able to use this in upcoming projects, primarily just to see how to do it but it may end up in some actual games I want to develop in the future. 

There are different ways to generate 3D terrain, but one of the slightly more common ways is by using height maps and Perlin noise. Using these we can make auto generated terrain that looks natural. 

Heightmap

So, first things first a height map is a gray scale 2D raster image where the gray channel represents height where white is the maximum height and black is the minimum height, with the various shades of gray being somewhere inbetween. This heightmap can then be used to create terrain like hills, valeys, rocks etc. Now we could just create this heightmap ourselves, but this will quickly get dull. What we would like is some way to create a heightmap automatically, perhaps using a random number generator. Let's say we did use a bog-standard pseudo-random number generator, we could iterate over a 2D array that represents the heightmap and run the number generator for each cell. This would certainly give us a heightmap, but it would be too random! We would have a terrain that would just have random blocks, one minute being at max height, the next at minimum with no smoothing inbetween. I.e. nothing like the real world, rather a set of lego towers that have been build by different people without looking at the towers. 

Perlin Noise

What we are really looking for is a way to produce coherent pseudo-random noise, this is where perlin noise comes in. It's a method to produce natural looking textures by having different layers of detail and then combining these. A fantastic example of its implementaiton and applications can be found on this post by Herman Tulleken. Another complete explanation can be found here, I really do advise you read the links if you are really interested in using perlin noise in your games.

Implementation

All credit goes to Herman Tulleken for his example code he posted online with his article, it was easy to follow and adapt to C. The link for my C version of his example code can be found here. It should be pretty straigt forward to use and seems to produce some nice randomised textures. 

Image Output

To output the heightmap produced as an image I used a library called LodePNG that can be found here. An example of an image produced using the perlin noise function can be seen below.

Olderperlinc

I plan to use this in map generation and perhaps voxel engine, wait and see!

Chumby, lua and segmentation fault

I've been digging through some of my older projects (read abandoned) and I came across a small lua library I had started writing to interface with the frame buffer on the Chumby, source available here

If I remember correctly the library works and allows you to draw to the screen using lua however it produces a segmentation fault I tracked the error down to the setup method that gets called when the library is first loaded.

I believe it's line 2 that's seg faulting, it's trying to set a meta table for the screen type so that eventually with a few updates to the library it can be used in an object oriented fashion in lua. If anyone has any ideas feel free to comment, and like always code is free to use it might help to give a head start if you were thinking of developing lua apps for the chumby.

 

LÖVE paged list

So, the usual excuse for not posting here busy and all that...although I've been building quite a bit just forgot to upload any of it! 

Anyhow to the point, I've begun rewriting one of my early lua games using the LÖVE game framework called CodeCopy. The original code is pretty rough, and so I've been rewriting to make use of objects and make most of it a lot more reusable. As well as adding level editor and some other nice features.

One of the components I've built during this is a paged list, you give it a function that returns an array of rows and it then displays by default 4 rows on each page and the next/back buttons with each row having a title, as well as an associated image/alternative image. By passing in a callback function you get notice when a row was selected and can do what you want there. Any of the default options can be overridden by passing in overriding values.

The code is here as well as an example of how it can be used and below is a picture of it in CodeCopy being used as a level select screen.

Levelselect

Let me know if there are any issues or questions!

Meggy Jr, Game Of Life

Intro

After my last post with the Meggy Jr displaying the weather, I wanted to write a standalone app for the Meggy Jr that doesn't connect to a PC and does a little more than just display data that's being sent to it. 

So, I decided to implement a version of Conway's Game of Life on the Meggy, there's just enough pixels to make it work well and I think it looks pretty cool. If you haven't heard of the Game of Life, it's not a cheesy gameshow, it's a cellular automaton where we have a grid of cells and each cell is either 'alive' or 'dead'.

Each iteration of the cells follows a set of simple rules:

  • Any live cell with fewer than two live neighbours dies
  • Any live cell with two or three live neighbours lives on to the next iteration
  • Any live cell with more than three live neighbours dies
  • Any dead cell with exactly three live neighbours becomes a live cell

By setting an initial configuration of cells that are alive we can see how patterns evolve, there's huge number of patterns that can be created, some oscilate between different states, some are stable and some 'die' after a certain number of iterations. Check out http://www.conwaylife.com/ if you want to find out more about the different types of patterns that can be created such as 'ships', 'guns' and even self replecating machines.

On the Meggy Jr

I've implemented a simple version of game of life and to get around the limitation of having only an 8by8 grid to play with I've wrapped the edges so that if a pattern goes off the screen it wraps to the other side. I've also put an 'edit' mode in the game so that when it starts up there's a red cursor that can be moved with the directional pads, when the B button is pressed it sets that cell to alive. When A is pressed it continues the game with the cell pattern that has been set. Pressing A again during the game will return it to the 'edit' mode. (Or at least it should most of the time, I really need to put in some debouncing code). The auxilary LEDs at the top display in binary the number of iterations that have passed, this will reset after 255.

You can see it in action below, I create a pattern called a toad that oscilates between two states. 

(download)

As always the code is available here, have fun!