[color=black]2d Landscape Generation with Midpoint Displacement[/color]hey everyone… I’m working on implementing the simple 2d algo. described here:[color=#800080]http://www.gameprogrammer.com/fractal.html[/color] in actionscript.
It’s a simple midpoint displacement scheme, and it’s almost working correctly. The problem i’m having is that the array i’m generating isn’t sorting correctly. I need to sort it so that the drawing loop cycles through from left to right (on the x axis)… For some reason, it’s not sorting points that are between [0] and [1] in my array. because the algo. is iterative, the problem snowballs the more iterations you run it for.
here’s the code i’m working on. it’s in 3 frames, and init frame to get it going, and then a loop in frames 2 and three (so that i can see the iterations as they’re drawn out):
//Begin Init Frame:
pointsxy = new Array ();
//set up my starting points
pointsxy.push ([0,200])
pointsxy.push ([500,200])
//set up random factor
randomness = 500
//set up my number of iterations (be careful here!)
count = 0
target = 5
//set to refresh
redraw = 1
//create my empty movie clip for drawing
createEmptyMovieClip(“test”,1)
//End Init Frame
//Begin Loop Frame:
if (redraw == 1) {
// here’s my sorting place… i’ve tried this everywhere to no avail.
pointsxy.sort(16);
// basic AS drawing routine
test.clear();
test.lineStyle(2, 0, 100);
test.moveTo(pointsxy[0][0], pointsxy[0][1]);
for (i=0; i<pointsxy.length; i++) {
test.lineTo(pointsxy*[0], pointsxy*[1]);
}
// main iterative loop. i know it’s messy.
if (count<target) {
oldlength = pointsxy.length;
for (i=1; i<oldlength; i++) {
dx = (pointsxy*[0]-pointsxy[i-1][0]);
dy = (pointsxy*[1]-pointsxy[i-1][1]);
finaldx = dx/2;
randomy = (random(randomness))-(random(randomness));
finaldy = (randomy*.2)+(dy/2);
currentpointx = pointsxy[i-1][0];
currentpointy = pointsxy[i-1][1];
pointsxy.push([currentpointx+finaldx, currentpointy+finaldy]);
randomness = randomness*.9;
}
count++;
} else {
redraw = 0;
}
}
//End Loop Frame
any ideas would be greatly appreciated!
thanks,
roland