- Home /
Creating 'worms'-like terrain
Hi all,
I am fairly new to Unity (only made 2 2d games) and for my next project I want to try and create a terrain generation script to make worms-styled terrain.
My first thoughts was having a 2d array of x and y coordinates with either a 1 or 0 for solid or empty and them somehow using that to render some type of texture with a collider around it. Also, this image doesn't show it but the terrain is destructible too.
My question is, is my idea for a 2d array of x and y coordinates with 1's and 0's a good idea and how could I display that in a textured way like in the image. Or, what is a better approach?
A two-dimensional array would work well for a tiled terrain, but Worms has basically terrain that can be generated and deformed on the pixel level. A better way to do it would probably be to work out shapes by conneceting points that describe the outline, and make a mesh out of it so the inside polygons show up textured.
@cherno I was talking about the array of 1's and 0's being for each pixel (kinda like a bitmap i guess). If i dont do it at this level of detail i would be unable to modify the terrain at the pixel perfect level as you suggested? Also, how would I be able to map a mesh to a pixel perfect terrain.
I see. Using the array like this would work, I guess. I don't know how big the level could be, and there's also the issue of collision which would have to use a custom physics system because Unity's won't be able to handle it.
To map terrain to pixel perfect terrain, I'd imagine that the whole map is covered by large bitmap showing the terrain, and this texture is modified before the game starts so the areas where no terrain is have their pixel colors set to alpha 0, and the same goes for terrain that gets destroyed during runtime.
I'm sure you will be able to find more question and hopefully answers for achieving this in Unity with a thorough search.
@wottf Since I've been playing around a lot with procedural generation I can tell you that a cellular automata will give you worms-like terrain! :)
Here is a good guide to the algorithm.
So just iterate your array of white noise through this algorithm and you will get "worms-like" caves.
Depending on the "resolution" (the size) of your array you should look in to using chunks (its just splitting the terrain into equal bits and using these bits to run updates on), because updating a terrain that is based on an array[1920,1080] would probably freeze up the system for a bit.
You should also take a look on the marching squares algorithm that will help you generate your mesh as well as keep your corners smooth!
Conslusion: Fill your array with random noise ---> Iterate through the cellular automata algorithm ---> process the array through marching squares algorithm to create mesh --> put texture on mesh.
@salmjak thanks for your response. I actually tried exactly what you said yesterday and the marching squares are not 1 pixel per or how can I make it smaller? Also it wasn't smooth. All times are straight or at a 45deg angle. I can't use linear interpolation because all the walls have the same value.
You may want to check this thread in the forum : http://forum.unity3d.com/threads/pixel-destruction-per-pixel-2d-destruction-with-unity.198919/ (it looks like the original code has been removed, but there is a comment with another branch).
Searching 'Unity destructible pixels' also yields results, eg: http://stackoverflow.com/questions/2083390/worms-style-destructible-terrain
@alucardj Thanks for those links, that first one seem especially interesting.
@alucardj I have played around with some ideas you linked but im not very happy with the performance of Texture2D's SetPixel, SetPixels or SetPixels32. $$anonymous$$y Textures are power of 2 and small (one per 'chunk'). Is there an alternative?
Answer by nebur7420 · Nov 29, 2018 at 09:49 AM
I know this is a bit late and you probably already found a way but I post this for the people who are still searching for an awnser.
I have been looking into this for about a day and found a way to do it in 2d.
First you have to make 2 objects, 1 for the terrain 1 for the damage
Give them Composite Colliders. Add all the terrain colliders to the composite collider of the terrain object. And do the same for the explosions when they are created.
Now when a rocket is fired give him the following code:
public Collider2D world;
public Collider2D explosionDamage;
public GameObject explosion;
void Update () {
if(_barrier.OverlapPoint(transform.position) && !_explosionDamage.OverlapPoint(transform.position)) {
GameObject spawnedExplosion = Instantiate(_explosion, transform.position, Quaternion.identity, _explosionDamage.transform);
Destroy(this.gameObject);
}
}
Now add a sprite mask to the explosion objects and say that the world can only be seen outside of the explosion.
I hope this helps and I hope you don't mind my spelling mistakes.
Wouldn't that only create two colliders, one visible for the terrain, and another invisible one for the explosions? I mean how do you prevent the collisions on the terrain collider at the destroyed parts? You can cut parts of the terrain sprites with the sprite mask but you can't cut away parts of a collider with a sprite or another collider, can you?
I would really like to try this but I don't think I understand your solution.