Terrain spikes using diamond square

Started by
0 comments, last by Vilem Otte 13 years, 4 months ago
hi, I've implemented a terrain generation program using the diamond square algorithm and I've noticed that it forms spikes at randomly distributed points around the heightmap.



I'm pretty sure why this is happening at the edges: because when I average 3 points instead of 4 (in the Square routine) I find the centre of that triangle rather than the centre destination point that lies on the edge. I've tried changing this so that it just averages the edge points when on an edge but it still generates these spikes. I have a feeling its either something to do with floating point precision or my Random() function:

(rand()/(float)RAND_MAX) * 2.0f - 1.0f;

I got the algorithm from here: http://www.javaworld.com/javaworld/jw-08-1998/jw-08-step.html?page=2

and I've noticed some of the images of the terrain on that site have also the same artifacts. Is this just a general problem with the algorithm or is it an implementation fault?
Advertisement
I havent tried the algorithm (so it may be a problem with it), but ...

(rand()/(float)RAND_MAX) * 2.0f - 1.0f;

rand() procedure returns int, so to have it correctly written
((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f;

But that is a detail, that shouldn't be needed (as compiler will do that on his own).

I'd test application if you're not dividing by zero ... or get rid of them after I generate fractal (some kind of filtering).

Maybe clever thing how to avoid this could be to generate 2 units wider and longer (e.g. have one more unit on each edge of generated "height map") and not to use edges, but just the inside.
E.g. you generate 514x514 fractal and use <1 - 513> x <1 - 513> intervals from it.

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

This topic is closed to new replies.

Advertisement