2D voxels! ~Almost there

Today I continued working on the “Worms” project talked about here. I was able to get mesh generation with randomness placed in to help make each generation unique, sadly it is currently just noise being generated. Last time when I talked about this, I proposed how I would accomplish the mesh generation of the world, I planned to have all vertices generated from a map of values in which I would connect surrounding values together, and this is how I was able to accomplish it.

I began with creating a struct called Vector2i (since unity had a struct called Vector2) that is just:

 public struct Vector2i
     public int x, y;

I created a dictionary with a key of Vector2i and a value of int, this would be used to store the location of the indices of the mesh (I’ll explain soon) and a list of Vector3’s. Going through all points in the data table created before (in previous blog), I created a Vector3 for that location if the value was greater than 0, after recording the amount of Vectors I had in the list. The recorded value was added to the dictionary with the Vector2i equivalent of the grid position (storing the location of the indices).

I then moved onto creating the triangles of the mesh. Knowing that this mesh is a 2D plane and each vertex location corresponds to a point in the data table, I was able to create the triangles by going through each grid point, starting at (1,1) instead of (0,0) and then checking at that point the above left, above, and left grid points if they are valid and generating a maximum of 2 triangles. This originally came out backwards as I mistook the mesh as coming vertically down (I thought the grid points descend as you go advance through the data table). This turned out as I expected:
Generated level 1.pngGenerated level 2.png

This result isn’t what will be the final result, I intend to put in the use of float values(the table is currently using floats, but the generating of the mesh makes them behave like booleans) to create varying heights in the mesh. I also intend to introduce distance fields into this project to allow for terrain-like mesh, something is currently lacks.


2 thoughts on “2D voxels! ~Almost there

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s