Home Coding, Scripting, Shaders

Island Generator (WIP in C++)

greentooth
Offline / Send Message
Finnn greentooth
Island Generator - procedurally generating a map of islands

Today I started my C++ journey!
My Goal is to create my own game in UE4, but first I want to get used to native C++

The Island Generator will work like this:

- Generate a Grid (static 2-dimensional Array) that is filled with 0`s
- Place random Island Centers
- Recursively grow from island centers (remember to save center coordinates)
- from Grid to PNG (libPNG)
- eventually create GUI and wrap it up as a small, functional program

Lessons I learned:
- rand() is pseudo random. Use srand(time(NULL)) for a seed based on runtime
- deallocation
- Constants
- Array initializer uses constants as parameters

Current State



Replies

  • Finnn
    Offline / Send Message
    Finnn greentooth
    My current state is a grid of 0`s (black)
    With Island Center Coordinates that are placed pseudo randomly all over the grid.
    Later on I will recursively expand from those island centers to generate naturally looking islands.
    Afterwards I will use a png library to generate actual images that can be used as masks.


  • Finnn
  • Klunk
    Offline / Send Message
    Klunk ngon master
  • Finnn
    Offline / Send Message
    Finnn greentooth
    Thanks for your suggestion.
    I've been looking into Perlin's documentation but as I want to create only black or white masks I will stick with my simpler approach.
    Although as far as I can tell, Perlin is also using rand()
    #define random()	rand<span>()</span>

    also, it would be nice if you put the source code into spoilers so the thread stays readable!
  • Klunk
    Offline / Send Message
    Klunk ngon master
    you can create black and white masks with the above method, here's one I made earlier

    it's just a matter of setting an upper and lower "threshold" level with the difference less than one over 255 but not zero. the lower  the values the larger the islands or more white if you like. Yes perlin noise use's rand() as a basis but the Gaussian distribution your going to get using it like you are is more tricky to "coerce". As for the code format thing you need to talk to polycount as thats theirs
  • Finnn
    Offline / Send Message
    Finnn greentooth
    Thanks for pointing that out.
    I am going to continue like I started, because this project is mostly about getting used to C++
    But at some point I want to add Interpolation

    Would be nice if you put your first post in spoiler so its not taking that much space. Was not talking about formatting the code :)
  • Finnn
    Offline / Send Message
    Finnn greentooth
    Also, someone on a Programming Discord pointed me towards the <random> header, which should produce better random values.
    Will try that out aswell
  • Finnn
    Offline / Send Message
    Finnn greentooth
    I literally programmed 10 hours today and didnt get it done :disappointed:
    Atleast I made some progress on the grow algorithm. The islands grow based on their centers into each direction.
    They randomly pick some left or right distortion. The growth can randomly stop, because before each turn an if clause based on a random number decides whether or not to progress. Later on I will use Constants to make these variables changeable from the outside.
    In extreme cases of reaching the grid border. The algorithm just continues on the bottom of the grid and vice versa!
    Now the Print Method has it's own function scope so I can easily reuse it and keep my main() clear.
    I am not very happy with my code structure though. I have alot of redundant code as I do the calculati0on for each direction. There might be a better way but thats how it is for now :D

    Lessons learned today:
    the rand() can give inaccurate results, because in very high numbers the probability can change. In my case it doesnt matter, but for Algorithms in crypto it can have a big impact.
    Recursion becomes very complex when it comes to handle stuff simultaneously.
    How the Debugger in Visual Studio works
    Working with Multidimensional Arrays, if else clauses, for-loops, while loops, <random> header, how to implement probability

    I cant show any result now, because I broke the current program. :dizzy:
    Will continue tomorrow.



    @Klunk Would be awesome if you put your message into spoilers so my thread stays readable. many thanks !
  • Finnn
    Offline / Send Message
    Finnn greentooth
    I get some really weird spikes in my values, cannot figure out why...
    Everything should work correctly, as I pick my values from a range in between 0-30
    I hope I get this one figured out.

  • ProperSquid
    Offline / Send Message
    ProperSquid polycounter lvl 13
    My guess is one of two things. Integer overflow or underflow (where a number is bigger or smaller than the maximum or minimum number for the int type), or you're accessing values outside of your array bounds. I'd have to look at your full source to understand what's going on to be sure.
  • Finnn
    Offline / Send Message
    Finnn greentooth
    @ProperSquid I got it fixed. It was an issue that you guessed. I didnt check the extreme cases properly and got an Access Violation because it tried to access my grid out of bounds.

    This is a way better way of checking for the extreme cases. It is called function wrap. So whenever I need to check the variables, I just wrap the variable with my wrapper function and check it.



  • Finnn
    Offline / Send Message
    Finnn greentooth
    This is my current output! It is not exactly what I want, but everything works for now.
    I will now try to let the cells grow with more spread. As I want to translate my grad to a PNG pixel per pixel, it has to have a much wider spread.


  • Finnn
    Offline / Send Message
    Finnn greentooth
    Found good values. Set up constants for randomness.


  • ProperSquid
    Offline / Send Message
    ProperSquid polycounter lvl 13
    Looking good. Feel free to ping me if you have any C++ questions. I'm not an expert, but I know my way around the language.
  • Finnn
    Offline / Send Message
    Finnn greentooth
    Looking good. Feel free to ping me if you have any C++ questions. I'm not an expert, but I know my way around the language.
    Thanks alot! @ProperSquid
    I tried to include libpng as well as Magick++ (both external libraries with API's capable of writing images)
    but I couldnt get it to work. I cant even compile the demo projects. Libs are tricky !

  • Finnn
    Offline / Send Message
    Finnn greentooth
    !!!!!!!!!!!!!!!!!!!!

  • Finnn
  • Finnn
    Offline / Send Message
    Finnn greentooth
    I saw a great movie today : Capernaum Trailer
    No time for art or programming today, but tomorrow is a new day!

    Anyway, I want to finish my first program with a small GUI
    After some research I choose QT as a framework. It looks modern, isnt too complicated and was relatively easy to install.

    This is the basic layout !




    There will be more sliders for options like randonmness, change in direction etc. I set up all important numbers with constants so it shouldnt be difficult to implement. When I get a new value, it will be changed throughout the whole logic. Although I have no idea yet how to connect the widgets with my logic yet, I think it will be a masterable challenge.
    I also want everything to be nicely scalable (dynamic sizing)
    The image in the middle of course will be displaying the generated image. I will probably run into troubles with my current file format .ppm, I hope it can be displayed.
    The process of saving the generated image will include a classic "choose directory" menu.
  • cupsster
    Offline / Send Message
    cupsster polycounter lvl 11
    What you should do is generate in passes and increment selected positions in each phase by one bit up, that way you will end up with grayscale map after conversion which is more interesting just plain masked 01 image..

  • Finnn
    Offline / Send Message
    Finnn greentooth
    cupsster said:
    What you should do is generate in passes and increment selected positions in each phase by one bit up, that way you will end up with grayscale map after conversion which is more interesting just plain masked 01 image..

    Hey thanks for your suggestion, but I will leave it as 01. But I will add more passes like mountains, jungle, beach and each layer with their own color.

    I've been making some progress, although I had to remake the whole GUI several times.
    And my code structure isnt the best either. But I already learned alot about C++
    For example about  object visibility and lifetime, Heap & Stack Allocation which caused me alot of problems that I fixed now.

    Will post an update soon !
Sign In or Register to comment.