[Script] Random hill map generation using interpolated noise |
Firestonnne « Citoyen » 1410306660000
| 0 | ||
Here is a very cool (but kinda useless) script I made that generates a map made of hills based off a seed. Here is the script, I will explain how it works / how to use it below. The map will generate as soon as you click submit, and you will need to click submit each time you want a new map. Here are some values you can change in the script (all them HAVE to be numbers): SEED (default nil) The number that will be used to seed the lua random number generator. A value of nil will make the seed equal to the current time (default). Any other value will generate the terrain based off of it "randomly", but will allow you to generate the same thing again if you use the same seed. PERIOD (default 40) The period is the distance the points are from each other (in 10px increments). A lower value will be tighter, and will look choppy. A higher value will be smoother. I would just keep it at the default for most instances. NUMBER_OF_VERTICES (default 4) How many random numbers will be chosen. A higher number will make the generation space longer, however, unless you lower the period, the effect will be off screen and unnoticeable (besides a longer load time). Once again, I wouldn't mess with this number. MAX_HEIGHT (default 400) This is how high the generator will build too (in pixels). Use higher numbers for higher mountains and more variance, but anything higher than 400 will be off screen. INTERPOLATION_TYPE (default 1) Now this is the main feature of this script. This specifies how you want to generate the space in between each vertex. Basically, this script will generate some number of vertices (default 4) with random y-coordinates / heights. This random assignment is known as noise, specifically white noise in this case. The interpolation type will link a function written in the code that will assign a y-value to the space between the vertices, specified by the period. All pictures that I've included of these in action use the seed 1410299446696. Type 0 is linear interpolation. This looks very simple and plain. It just draws straight lines between the vertices. This is represented by the code value = ((y2 - y1) / (x2 - x1)) * (x - x1) + y2. In this code (and all other ones in my script), x represents the horizontal position to the nearest 10px mark, (x1, y1) is the point (in 1 px increments) of the lower vertex, and (x2, y2) is the upper vertex. If you ever took an algebra class , this formula should look very familiar to y = mx + b and the slope formula. So this code just calculates the slope between the two vertices and returns the y-value of the point plus the height of the vertex. Type 1 (the default) is sine interpolation. The code for it is value = -(0.5 * (y2 - y1)) * math.sin(math.pi/(x2 - x1)*(x - x1) + 4.72)+(y1 + 0.5 * (y2 - y1)). It uses the sine trigonometric function, and but with certain transformations associated with it to create half a sine wave with an amplitude between the two vertices' y-positions and a width of the period, and moved so that the trough of the wave is at the beginning. This formula was a huge pain to come up with, and it required an hour or two with a graphing calculator. If you are that kind of person who is interested in it, this is a picture of the base function that I came up with in Desmos to write up with the code. w = width between points, h = the height difference, and s = the starting height. Any other type will get you the default one (which features absolutely no interpolation) or any functions that you write and put in. Commands I only put in one command, and that is !seed to display the current seed for the terrain if you want to keep it. That's basically it. It was pretty fun to code, and if you want to edit or modify anything feel free. |
Grimrice « Citoyen » 1410902760000
| 0 | ||
This looks epic but confusing, maths is so hard. However the script does not work Dernière modification le 1410902820000 |
0 | ||
Grimrice a dit : /lua has not been working lately, it should work now This is interesting. I set period to 50 and vertices to 10, and then changed the map length to 5000. Perhaps someone could make a small minigame using this generator. |
Cemoscuk « Citoyen » 1412020080000
| 0 | ||
Its good bro |
Itchyboy « Citoyen » 1412296020000
| 0 | ||
I don't know if you'll mind that I edited your code, but all I did was add a new command: !newmap. All it does is it changes the map to a random hill map. I was thinking of making a random generated map like this, but obviously now I don't have to :P Here's the code: http://pastebin.com/SNTtPuJD I added a new command: !setseed <seed>/random If you say random instead of putting a seed it sets the seed back to nil. Dernière modification le 1412385840000 |
Itchyboy 1412381700000
| | ||
[Modéré par Shamousey, raison : Double post. You can edit your posts, just click on your username in your last post and then "edit".] |
Firestonnne « Citoyen » 1412393880000
| 0 | ||
Nice one Itchyboy. I don't mind if you edit the code since I know that I left it incomplete (there are still tons of things that could be added and it really just is an empty map), not to mention the many types of interpolation that could be put in that I didn't (polynomial interpolation would be cool). |
0 | ||
It's amazing but I can't keep the seed it doesn't appear :( |
0 | ||
This is actually really amazing |
Thewildnes « Citoyen » 1413623520000
| 0 | ||
Good thanks |
Idontexistlmao « Citoyen » 1427499720000
| 0 | ||
i love this. i've tried numerous forms of waves and they all look amazing. i wonder how pure noise looks, though? |
Firestonnne « Citoyen » 1427584500000
| 0 | ||
uhmskghwehna a dit : Noise just means random (sorta). Interpolation is just "connecting the dots" between the random numbers, and there are many ways to do this, each one giving a slightly different appearance. The sine interpolation just connects it using a sine wave, which gives it the appearance of a smooth bump or hill. If you are still curious you could read this: http://en.wikipedia.org/wiki/Interpolation |
Papero « Citoyen » 1429764840000
| 0 | ||
Are you using Lagrange interpolation? Never mind read through the formulaes http://en.m.wikipedia.org/wiki/Lagrange_polynomial Why not try this .0. Dernière modification le 1429765140000 |