Worms-like mesh destruction system

Hi!

I wanted to create a worms-like destructable terrain in 2.5D. I got to the point of “cutting out” a piece of a plane in a certain place. I’d like to extrude the modified plane.
This is my code:

private float holeRadius = 3.0f;

void Update () {
		if (Input.GetMouseButtonDown (0)) {
			Mesh mesh = GetComponent<MeshFilter>().mesh;
			Vector3[] vertices = mesh.vertices;
			List<int> indices = new List<int> (mesh.triangles);
			int count = indices.Count / 3;
			RaycastHit hit;
			if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)){
				Vector3 explosion = hit.point;
				for (int i = count - 1 ; i >= 0; i--)
				{
					Vector3 V1 = vertices[indices[i*3 + 0]] + transform.position;
					Vector3 V2 = vertices[indices[i*3 + 1]] + transform.position;
					Vector3 V3 = vertices[indices[i*3 + 2]] + transform.position;
					if (Vector3.Distance(V1, explosion) < holeRadius && Vector3.Distance(V2, explosion) < holeRadius && Vector3.Distance(V3, explosion) < holeRadius){

						indices.RemoveRange(i*3, 3);

					}
				}


				mesh.triangles = indices.ToArray();
				Destroy(gameObject.GetComponent<MeshCollider>());
				mesh.RecalculateBounds();
				mesh.RecalculateNormals();
				transform.gameObject.AddComponent<MeshCollider>();
				transform.GetComponent<MeshCollider>().sharedMesh = mesh;
			}

		}
	}

Thanks for any help.

what I am wondering is if you can really change the mesh by cutting the worm mesh in two . If so , then what you can try is that when you are slicing the worm you are creating a vector , what is on the left (vertices) is making a part , and what is on the right is making another part . You can determine then left and right using dot product .

I seem to recall that those games use actual boolean operations to cut into meshes. Their physics and collision systems are probably completely different from Unity’s, so for them it might’ve been a trivial task.

However, that’s not necessary to achieve a similar effect. If I were tasked with creating a dynamic 2D terrain, I would want to avoid using true boolean ops because of the edge cases and issues, and also the high overhead of that complex operation. Instead, consider taking a page from voxel concepts:

Build your terrain as a triangular lattice.

Although the tris belong to one mesh (or as few as possible), each tri stands alone; visually they appear to share verts and edges, but in reality they are separate from their neighbors. Imagine you duplicated a triangle to make the lattice, but never used the Remove Doubles (aka Weld Duplicate Verts) command.

Allow “cut” operations to delete tris from the lattice. This could be based on a raycast hit or on an overlapped shape.

The fidelity of the terrain then becomes tied to the density of the lattice, but you could strike a good balance between cost and fidelity, especially since it’s 2D. I would strongly recommend this technique versus true boolean mesh cutting, mostly to eliminate work; both yours and the CPU’s. If you planned ahead for it, it’d even be easy to “add” to the terrain; how about a big “concrete bomb” that creates hills, etc?

If your heart is set on true boolean’d geometry, the best advice I can offer is to search for blog entries, 3D design articles, and academic papers which discuss the concept and attempt to apply what they talk about. Then again, if you can pay ten bucks to get a plugin that does what you need, you might save yourself dozens of hours of work…