Sign in to follow this  
staticVoid2

Terrain spikes using diamond square

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this