Iteration. Has anyone ever made fractals within a game?

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Iteration. Has anyone ever made fractals within a game?

Post by sheamkennedy »

Just wondering if anyone has ever accomplished any sort of fractal programming within their game/program? I've been contemplating if it's worth trying myself and wanted to see if/how someone else has approached this.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Kinda... Not really sure if it exactly counts as fractal programming, but I've managed to get a decent diamond square thing going for random terrain generation. Not sure if that's what you're looking for though. What/How/Where are you planning to use fractals?
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Re: Iteration. Has anyone ever made fractals within a game?

Post by Bob the Hamster »

sheamkennedy wrote:Just wondering if anyone has ever accomplished any sort of fractal programming within their game/program? I've been contemplating if it's worth trying myself and wanted to see if/how someone else has approached this.
Depends on what you mean by fractal.

If you mean like rendering a mandelbrot set, i know I tried once, but quickly got bogged down and gave it up.

If you mean something like perlin noise terrain, I am pretty sure someone did it successfully (BMR, I think?)

EDIT: Oops! haha! BMR posted before I submitted. Diamond-square and perlin-noise are pretty similar.
Last edited by Bob the Hamster on Fri Nov 15, 2013 5:35 pm, edited 1 time in total.
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Re: Iteration. Has anyone ever made fractals within a game?

Post by BMR »

Bob the Hamster wrote:Diamond-square and perlin-noise are pretty similar.
But despite that, BMR has yet to figure out a good way to script Perlin noise, hehe. One day he shall, perhaps on yet another late-night scripting spree =p
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Re: Iteration. Has anyone ever made fractals within a game?

Post by sheamkennedy »

BMR wrote:
Bob the Hamster wrote:Diamond-square and perlin-noise are pretty similar.
But despite that, BMR has yet to figure out a good way to script Perlin noise, hehe. One day he shall, perhaps on yet another late-night scripting spree =p[/quote

Yeah I consider all of what has been mentioned as fractal related stuff. I'm not asking about it for any particular reason, I just wanted to know if something like that has been done and if there is any examples of it out of curiosity. I was originally intrigued by Julia sets and considered that someone in the community may have taken something like that on as a challenge. I was thinking of maybe trying to make a tree thing in which the branches multiply shorten segment by segment until a random shaped tree is formed.

If you have a program that shows off the Diamond-square terrain mapping, I'd love to see it. Where can I find it?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Perhaps I could make a simple tree in which a "branch" slice spawns 2 more smaller slices at its tip and at random angles, then those branches further spawn 2 more branches each, and so forth. Seems like it may be doable with just parent and child slices. Not sure how i'd do angled branches... might have to draw each angle of branch from scratch but I'm not sure.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

I don't have a game up, sadly. I do have a forum thread where I posted results and a few details about what I was working on. If you're interested, I could PM you the scripts. That is, if you're willing to sift through a bunch of messy code, heh.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

BMR wrote:I don't have a game up, sadly. I do have a forum thread where I posted results and a few details about what I was working on. If you're interested, I could PM you the scripts. That is, if you're willing to sift through a bunch of messy code, heh.
Yeah I'd love to take a peek. I'm sure it'll give me some insight and maybe inspire some ideas.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Here you go:

Code: Select all

#====================================================================================================
#This script generates a height map for everything using a very -very- crude diamond-square algorithm
#====================================================================================================
script, generate height, begin

	#Variable Declarations
	#---------------------
	variable(		
		minX #The smallest possible X value
		maxX #The largest possible X value
		minY #The smallest possible Y value
		maxY #The largest possible Y value
		
		bs   #Block Size, the size of the current block being worked
		
		curX #The current X value
		curY #The current Y value
		
		x1
		x2
		y1
		y2
		
		va   #Variance, this is how much the surrounding terrain affects the code.  Larger values make for rougher terrain.
		rgh  #Roughness, this is how much the random value ranges.  Higher numbers make for rougher terrain. (2-16)
		
		tp   #Temporary Passes, used for debugging
		
		ctr  #A counter
	)
	#---------------------






	#Prime the variables
	#-------------------
	minX      := 0
	maxX      := map width
	
	minY      := 0
	maxY      := map height
	
	bs := map width   #This assumes that the map is square
	
	va  := 2          #Only 2% of the generated value is random, the other 98% is based on surrounding terrain
	rgh := 10         #The random numbers are relatively low.
	#-------------------




	tp := 1

	while(bs >= 1) do( #This block will repeat until bs reaches its minimum
	
	
		for(curX, minX, maxX + 1, bs) do(
			for(curY, minY, maxY + 1, bs) do(
			
				x1 := curX
				y1 := curY
				x2 := curX + bs
				y2 := curY + bs
			
				if(x2 >> maxX) then(x2 := maxX)
				if(y2 >> maxY) then(y2 := maxY)
			
				
				diamond pass(x1, y1, x2, y2, va, rgh)
				square pass(x1, y1, x2, y2, va, rgh)
			
			
			)
		)
		
		bs := bs / 2  #This halves the size of the block the code works with
		tp += 1

	)


end
#====================================================================================================
#====================================================================================================








#========================================================================================================
#These two functions are the main block used.
#========================================================================================================

#-----------------------------------------------------------
#This script runs the diamond pass of the generation process
#-----------------------------------------------------------
script, diamond pass, x1, y1, x2, y2, va, rgh, begin

	#Variable Declarations
	#---------------------
	variable(
		x3   #This is the X-cooridinate of the newly generated value
		y3   #This is the Y-cooridinate of the newly generated value
		
		p1v  #The height value of point 1, this is known
		p2v  #The height value of point 2, this is known
		p3v  #The height value of point 3, this is known
		p4v  #The height value of point 4, this is known
		p5v  #The height value of point 5, this will be generated
		
		ap   #Average Points, the average of the four known points
		vp   #Variance Percent, a fraction of the above average as determined by variance
		rp   #Roandom Percent, a random value from 0 to 15, how much is applied is determined by variance
		
		rl   #Roughness Low, the lowest roughness value
		rh   #Roughness High, the highest roughness value
		rf   #Roughness Final, the final roughness value
		
		pv   #The point value of a coordinate
		tv   #The temporary working value for the new point
		nv   #The new value of a coordinate

		crX  #Current X
		crY  #Current Y
	)
	#---------------------
	
	
	#Prime the variables
	#-------------------
	p1v := read map block(x1, y1, 0)
	p2v := read map block(x2, y1, 0)
	p3v := read map block(x1, y2, 0)
	p4v := read map block(x2, y2, 0)
	
	x3  := (x2 + x1) / 2
	y3  := (y2 + y1) / 2
	
	ap  := (p1v + p2v + p3v + p4v) / 4
	vp  := (ap * (100 -- va)) / 100
	rp  := (random(0, 15) * va) / 100
	
	rl  := rgh * -1
	rh  := rgh
	rf  := random(rl, rh)
	#-------------------
	
	tv := (vp + rp) + rf          #Change the temporary value to a new randomly generated one
	
	if&#40;tv <<0>> 127&#41; then&#40;tv &#58;= 127&#41; #Make sure the value is not higher than 127
	
	p5v &#58;= tv
	
	nv  &#58;= &#40;p1v + p2v + p3v + p4v + p5v&#41; / 5
	
	write map block&#40;x3, y3, p5v, 0&#41; #Write the tiles
end
#-----------------------------------------------------------
#-----------------------------------------------------------




#----------------------------------------------------------
#This script runs the square pass of the generation process
#----------------------------------------------------------
script, square pass, x1, y1, x2, y2, va, rgh, begin

	#Variable Declarations
	#---------------------
	variable&#40;
		x3   #This is the X-cooridinate of the newly generated value
		y3   #This is the Y-cooridinate of the newly generated value
		
		p1v  #The height value of point 1, this is known
		p2v  #The height value of point 2, this is known
		p3v  #The height value of point 3, this is known
		p4v  #The height value of point 4, this is known
		p5v  #The height value of point 5, this is known after the diamond pass
		
		ap1  #The average of three points, for use with the top point
		ap2  #The average of three points, for use with the right point
		ap3  #The average of three points, for use with the bottom point
		ap4  #The average of three points, for use with the left point
		
		vp1  #A fraction of the above average as determined by va
		vp2  #A fraction of the above average as determined by va
		vp3  #A fraction of the above average as determined by va
		vp4  #A fraction of the above average as determined by va
		
		rp1  #A random value from 0 to 15, how much is applied is determined by va for the top point
		rp2  #A random value from 0 to 15, how much is applied is determined by va for the right point
		rp3  #A random value from 0 to 15, how much is applied is determined by va for the bottom point
		rp4  #A random value from 0 to 15, how much is applied is determined by va for the left point
				
		rl  #The lowest rgh value
		rh  #The highest rgh value
		
		rf1 #The final rgh value for the top point
		rf2 #The final rgh value for the right point
		rf3 #The final rgh value for the bototm point
		rf4 #The final rgh value for the left point
		
		tv1 #The temporary working value for the top point
		tv2 #The temporary working value for the right point
		tv3 #The temporary working value for the bottom point
		tv4 #The temporary working value for the left point
	&#41;
	#---------------------
	
	
	
	#Prime the variables
	#-------------------
	x3  &#58;= &#40;x2 + x1&#41; / 2
	y3  &#58;= &#40;y2 + y1&#41; / 2
	
	p1v &#58;= read map block&#40;x1, y1, 0&#41;
	p2v &#58;= read map block&#40;x2, y1, 0&#41;
	p3v &#58;= read map block&#40;x1, y2, 0&#41;
	p4v &#58;= read map block&#40;x2, y2, 0&#41;
	p5v &#58;= read map block&#40;x3, y3, 0&#41;
	
	ap1 &#58;= &#40;p1v + p2v + p5v&#41; / 3
	ap2 &#58;= &#40;p2v + p4v + p5v&#41; / 3
	ap3 &#58;= &#40;p3v + p4v + p5v&#41; / 3
	ap4 &#58;= &#40;p1v + p3v + p5v&#41; / 3
	
	vp1 &#58;= &#40;ap1 * &#40;100 -- va&#41;&#41; / 100
	vp2 &#58;= &#40;ap2 * &#40;100 -- va&#41;&#41; / 100
	vp3 &#58;= &#40;ap3 * &#40;100 -- va&#41;&#41; / 100
	vp4 &#58;= &#40;ap4 * &#40;100 -- va&#41;&#41; / 100
	
	rp1 &#58;= &#40;random&#40;0, 15&#41; * va&#41; / 100
	rp2 &#58;= &#40;random&#40;0, 15&#41; * va&#41; / 100
	rp3 &#58;= &#40;random&#40;0, 15&#41; * va&#41; / 100
	rp4 &#58;= &#40;random&#40;0, 15&#41; * va&#41; / 100
	
	rl  &#58;= rgh * -1
	rh  &#58;= rgh
	
	rf1 &#58;= random&#40;rl, rh&#41;
	rf2 &#58;= random&#40;rl, rh&#41;
	rf3 &#58;= random&#40;rl, rh&#41;
	rf4 &#58;= random&#40;rl, rh&#41;
	#-------------------
	
	
	tv1 &#58;= &#40;vp1 + rp1&#41; + rf1 #\
	tv2 &#58;= &#40;vp2 + rp2&#41; + rf2 # \Change the new random value for
	tv3 &#58;= &#40;vp3 + rp3&#41; + rf3 # /the temporary points
	tv4 &#58;= &#40;vp4 + rp4&#41; + rf4 #/
	
	if&#40;tv1 << 0&#41;  then&#40;tv1 &#58;= 0&#41;  #\
	if&#40;tv2 << 0&#41;  then&#40;tv2 &#58;= 0&#41;  # \Make sure the value
	if&#40;tv3 << 0&#41;  then&#40;tv3 &#58;= 0&#41;  # /is not lower than 0
	if&#40;tv4 <<0>> 127&#41; then&#40;tv1 &#58;= 127&#41; #\
	if&#40;tv2 >> 127&#41; then&#40;tv2 &#58;= 127&#41; # \Make sure the value is
	if&#40;tv3 >> 127&#41; then&#40;tv3 &#58;= 127&#41; # /not higher than 127
	if&#40;tv4 >> 127&#41; then&#40;tv4 &#58;= 127&#41; #/
	

	tv1 &#58;= &#40;tv1 + read map block&#40;x3, y1, 0&#41;&#41; / 2 #This is the top point
	tv2 &#58;= &#40;tv2 + read map block&#40;x2, y3, 0&#41;&#41; / 2 #This is the right point
	tv3 &#58;= &#40;tv3 + read map block&#40;x3, y2, 0&#41;&#41; / 2 #This is the bottom point
	tv4 &#58;= &#40;tv4 + read map block&#40;x1, y3, 0&#41;&#41; / 2 #This is the left point
	
	
	write map block&#40;x3, y1, tv1, 0&#41; #Write this to the top point
	write map block&#40;x2, y3, tv2, 0&#41; #Write this to the right point
	write map block&#40;x3, y2, tv3, 0&#41; #Write this to the bottom point
	write map block&#40;x1, y3, tv4, 0&#41; #Write this to the left point
	
	
end
#----------------------------------------------------------
#----------------------------------------------------------


#========================================================================================================
#End of block
#========================================================================================================


I'll post it here rather than PMing it so anyone can see the code. It's not the entire terrain generation thing I use, as this code doesn't include river generation, coastlines, etc... I haven't tested this in a while, so I'm not sure if it'll work well by itself (i.e. without the other parts) or even at all. If you have any questions (or comments, or suggestions, or disparaging remarks) feel free to get in touch.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Meowskivich
Blubber Bloat
Posts: 2199
Joined: Tue Mar 06, 2012 12:38 am
Location: Earth
Contact:

Post by Meowskivich »

Image

I can't stop thinking about this when looking at the thread title.
dOn'T MiNd mE! i'M jUsT CoNtAgIoUs!!!
Play Orbs CCG: http://orbsccg.com/r/4r6x :V
User avatar
Sparoku
Metal Slime
Posts: 308
Joined: Tue Jun 18, 2013 3:19 pm
Location: Dairy Queen
Contact:

Post by Sparoku »

Meowskivich wrote:Image

I can't stop thinking about this when looking at the thread title.
I wasn't the only one! :v:
"One can never improve enough nor should one stop trying to improve."
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Thanks
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

This is one reason I want to let you draw pixels to sprite slices :)

Note that the forum ate part of those scripts (due to the angle brackets). You should check "Disable HTML in this post" when posting scripts.
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Yes! Read/Write pixels would indeed be amazing! All the amazing stuff you'd be able to do :D

And I shall fix the script when I get back to my main computer as I do not have any of my OHRRPGCE stuff on this machine.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Mystic
Metal Slime
Posts: 322
Joined: Wed Jul 23, 2008 4:32 am

Post by Mystic »

1x1 rectangles are pretty annoying, but work if you really need it,
Post Reply