Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

1.5d Procedural Generation using Midpoint Displacement

edited January 2014 in Examples Posts: 68

On Codea Community, I just uploaded a midpoint displacement algorithm for anyone that is interested. It could be used, for example, to procedurally generate backgrounds for side-scrolling games.

Something I've noticed: the random number generator seems to have very predictable patterns, as you may be able to see when moving the seed slider. Clearly the algorithm used could be better!

Tagged:

• edited January 2014 Posts: 7,810

@Causeless Interesting program. Setting the passes to 5 and the roughness to .5 and sliding the seed back and forth reminds me of an electrical arc. The random number generator is a pseudo random number generator. The same seed will result in the same string of numbers. You can't write a program to create random numbers.

• Posts: 68

Haha, yeah, I was thinking it could make a lightning effect, too! The actual generation code runs perfectly at 60fps, even with 10 passes, it's just the line drawing that slows it down, btw.

I understand how random number generators work, I just mean, there's a clear and obvious pattern where there shouldn't be. With seed = n and seed = n + 1, the results should be significantly different, but here there is a clear colleration between the seed and the resulting pseudo-random numbers, and is predictable (incrementing the seed results in the random value incrementing too).

• Posts: 580

Speaking of using this algorithm for a lightning effect:

http://twolivesleft.com/Codea/Talk/discussion/1142/a-crappy-plasma-globe • edited January 2014 Posts: 7,810

@toadkick I totally missed that program back in June 2012. Nice effects. I wonder how I missed it.

• Posts: 502

Nice example. You can rewrite it using a mesh to keep the FPS higher.

• Posts: 68

Thanks! I've found in most places now that using any graphical drawing method other than mesh is unbearably slow, haha.

• Posts: 68

@Toadkick It's pretty cool, seeing the differences in implementation (you did a recursive appraoch, i did iterative).

• edited August 2015 Posts: 116

I made something similar, but mine could work faster(i say could, because i dont fully understand your code) :-\$

function setup()
size = WIDTH/2
points = {-0.1,HEIGHT/2,HEIGHT}
parameter.action("next",next)
parameter.integer("scl",1,100,1)
end
function draw()
scale(scl)
strokeWidth(1/scl)
background(40, 40, 50)
for num,pt in ipairs(points) do
if num > 1 then
line(size*(num-2),points[num-1],size*(num-1),pt)
end
n = num
end
end
function next()
for i = 1, n-1 do
table.insert(points, i*2,0)
points[i*2] = rndh(i*2)
end
size = size * 0.5
end
function rndh(number)
lim1 = math.floor(points[number-1])
lim2 = math.ceil(points[number+1])
if lim1 > lim2 then
return math.random(lim2,lim1)+math.random()
elseif lim2 > lim1 then
return math.random(lim1,lim2) +math.random()
else
return math.random(lim2,lim1) +math.random()
end
end