Unity3D Building Damage Tutorial- Part IX - Branches

I decided to try burning some more complex objects, than cylinders and cubes. From Asset store, I've downloaded simple and free Arabian building, seen one the left picture, and than started fire from Alias number. Well, as you can see in the picture on right side of the screen, not everyting went as expected. Why? Becouse aparently mesh was made from several non-connected  object. Thus, fire was not able to spread  properly. In this part of tutorial, I'll try to overcome this problem.

Branches
In tutorial and code I'll use something named branch. Well, in my mind branch will mean a part of mesh not connected to other partsIn picture top right every wooden pole and top branches. We will assign Aliases to branches, but there will be no new class used. This partition will be much teoretical.

Plan
So what we have to do:
 * 1) Divide the mesh to several branches, by assigning a branch number to every Alias.
 * 2) Find inter-branch connections (later known as I-BC)
 * 3) Tell specific Aliases that there are taking part in I-BC and thus should spread fire by them.

FindAliasesBranches method
public MeshManager (ref MeshFilter meshFilter){//constructor Initialise; ParentPosition = meshFilter.transform.position; filter = meshFilter; mesh = meshFilter.mesh; ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight; CalculateNormals; FindAliasesBranches; MakeMeshThick; }

As you see, the function is called in the constructor prior to MakeMeshThick. We will not use any twins. private void FindAliasesBranches{ AliasToBranchArray = new int[Aliases.Count]; for(int i=0; i 0){//only one branch, so we dont need to look for connections for(int i=0; i FinalConnections = FindNumberOfInterBranchConnections(i, 2); foreach(Vector2 vec in FinalConnections){ Aliases[(int)vec.x].AddLinkFromOtherBranch((int)vec.y); Aliases[(int)vec.y].AddLinkFromOtherBranch((int)vec.x); } 			} 		} 	} Okay, first lets look at AliasToBranchArray (leter known as ATBA). It serves similar purpose to VTAA. The lenght of this array is number of original aliases (not twins). Than we will set the number of branch to every Alias 		for(int i=0; i		foreach(Verticle vec in Aliases){//Give AliasToBranchArray values if(AliasToBranchArray[vec.number]==-1){//this alias is not yet set to any branch AliasToBranchArray[vec.number] = CurrentBranchNumber; GetConnectedAliases(vec.number, CurrentBranchNumber); CurrentBranchNumber++; } 		} Okay, this is moderatly intresting. In this loop we will be checking in which branch every Alias will be. if(AliasToBranchArray[vec.number]==-1){//this alias is not yet set to any branch Well, every time the loop will go, there will be more than one Alias assigned (later bout that in a second). Anyway, when code will go to this condition in one of further loops, this ATBA cell may be arleady assigned, so there is no point is counting some things again.

GetConnectedAliases method
private void GetConnectedAliases(int number, int CurrentBranchNumber){ List LinkedAliases = Aliases[number].LinkedAliases; foreach(int i in LinkedAliases){ if(AliasToBranchArray[i]==-1){ AliasToBranchArray[i] = CurrentBranchNumber; GetConnectedAliases(i, ref AliasToBranchArray, CurrentBranchNumber); } 		} 	} Here is intresting code. This method has an aim of partially-filling the ATBA array.

As we know branch is a group of Aliases, which have direct or indirect connection. Becouse of that, all LinkedAlias of alias being in branch 0, are in branch 0 too. In 6th line here we are calling current method for all LinkedAlias (as Alias linked to Alias linked ... to original alias are still in the same branch). This may seem to result in infinite function loop and stack overflow. But we have a  condition here. After some time data of branches (in ATBA array) of all Aliases in branch will be set, so functions will end.

Back to FindAliasesBranches method
if(CurrentBranchNumber > 0){//only one branch, so we dont need to look for connections for(int i=0; i FinalConnections = FindNumberOfInterBranchConnections(i, 2); foreach(Vector2 vec in FinalConnections){ Aliases[(int)vec.x].AddLinkFromOtherBranch((int)vec.y); Aliases[(int)vec.y].AddLinkFromOtherBranch((int)vec.x); } 			} 		} Okay, in this part of code we will find a small number (2 here) of closest connections in each Branch, and than send info of Aliases taking part in close connections that they are taking part in this connection, and effectively that they spread fire using them. The first thing is done is FindNumberOfInterBranchConnections method.

FindNumberOfInterBranchConnections
private List FindNumberOfInterBranchConnections(int BranchNumber, int HowManyConnections){//it searches for This branch Connections (Aliases close to themselves). One Alias can make only One connection! List ConnectionsList = new List; //Vector2.x is the Alais in our branch Vector2.y is the closest Alias in other branch foreach(Verticle vec in Aliases){//here we are trying to get list of closest inter-branch connections of every Alias in breanch if(AliasToBranchArray[vec.number] == BranchNumber){ ConnectionsList.Add ( new Vector2(vec.number, FindCloseAliasNotInTheSameBranch(vec.number, BranchNumber)) ); } 		} 		List FinalConnections = FindNumberOfClosestConnections(ConnectionsList, 2); return FinalConnections; } Method does two things.


 * 1) Make a list of pairs between each Alias in branch and an Alias that is closest to this Alias, but is in other branch (4th line)

And then returns this two pairs.
 * 1) Finds a given number (2 here) of pairs which distance between is smallest (8th line)

FindCloseAliasNotInTheSameBranch method
private int FindCloseAliasNotInTheSameBranch(int number, int BranchNumber){//finds and returns the Alias closest to Alias of given number, not being part of branch int CurrentClosestAlias = -1; float Distance = 1000f; //Just very big number foreach(Verticle vec in Aliases){ if(vec.number!=number){//verticle is not the being checked verticle if(AliasToBranchArray[vec.number]!=BranchNumber){//not from the same branch if(Vector3.Distance(Aliases[number].positionRelative, vec.positionRelative) <	Distance){ CurrentClosestAlias = vec.number; Distance = Vector3.Distance(Aliases[number].positionRelative, vec.positionRelative); } 				} 			} 		} 		return CurrentClosestAlias; } Well, just check every distance between Alias of given number and all Aliases in branch other than given. Than returns the number of Alias that makes the closest pair.

FindNumberOfClosestConnections method
private List FindNumberOfClosestConnections(List InList, int NumberOfClosestConnections){ Dictionary DistanceDictionary = new Dictionary; //Key is the pair, value is the distance between foreach(Vector2 pair in InList){//lets fill the distionary with distances between each pair //Debug.Log("pair x is"+pair.x+" and y is "+pair.y); float Distance = Vector3.Distance(Aliases[(int)pair.x].positionRelative, Aliases[(int)pair.y].positionRelative); DistanceDictionary.Add (pair, Distance); } 		Dictionary<Vector2, float> ClosestConnections = FindANumberOfSmallestElementsInDictionary(DistanceDictionary, NumberOfClosestConnections); List<Vector2> OutList = new List<Vector2>; foreach(KeyValuePair<Vector2, float> pair in ClosestConnections){ OutList.Add (pair.Key); } 		return OutList; } Well, this may seem complicated. At the begining we are making a Dictionary, which will store a pair of Aliases (making close connection) as key and float distance as value. The aim of that is to count all distances, and than find a given number (second parameter) of shortest ones. Finding these is done in FindANumberOfSmallestElementsInDictionary method. Than we have just a simple translation from Dictionary to a list (with losing of distance data).

FindANumberOFSmallestElementsInDictionary method
private Dictionary<Vector2, float> FindANumberOfSmallestElementsInDictionary(Dictionary<Vector2, float> InDictionary, int NumberOFSmallest){//returns a given number of pairs with smallest values; if(NumberOFSmallest>InDictionary.Count){ Debug.Log("Well, the count of given list is smaller than the amount of smallest things you want. That is wrong"); }; Dictionary<Vector2, float> OutDictionary = new Dictionary<Vector2, float>; for(int i=0; i<NumberOFSmallest; i++){//lets add tome default data OutDictionary.Add(new Vector2((float)i,0f), 10000f); } 		foreach(KeyValuePair<Vector2, float> pair in InDictionary){ foreach(KeyValuePair<Vector2, float> pair2 in OutDictionary){ if(pair2.Value> pair.Value){ OutDictionary.Remove(pair2.Key); OutDictionary.Add (pair.Key, pair.Value); break; } 			} 		} 		return OutDictionary; } May look difficult, but is not so complicated. First, you may notice that i am adding some strange data to OutDictionary. I do this, to be able to use foreach statement on OutDictionary that will make the loop the number of times given in second function parameter. Later, there is just somehow tricky finding  and putting to Dictionary pairs with smallest values.

Back to FindAliasesBranches
foreach(Vector2 vec in FinalConnections){ Aliases[(int)vec.x].AddLinkFromOtherBranch((int)vec.y); Aliases[(int)vec.y].AddLinkFromOtherBranch((int)vec.x); } Okay, and here we send info to Aliases about them being in pairs. This is last thing done in method.

Verticle class
... ... ... ... ... ... ... 		public List LinksToOtherBranches; ... ... ... ... 			if(state == VerticleState.Burning){ InflictDamage; TryToFireLinkedAliases; TryToFireLinkedAliasesFromOtherBranches; } ... ... ... ... ... 		public void AddLinkFromOtherBranch(int i){ if(!LinksToOtherBranches.Contains(i)){ LinksToOtherBranches.Add(i); } 			//DebEnlightenThisAlias; //DebEnlightenAAlias(i, Color.magenta); } ... ... ... 		private void TryToFireLinkedAliasesFromOtherBranches{ foreach(int i in LinksToOtherBranches){ OwnerManager.Aliases[i].StartFire; } 		} ... ... ... 		private void Destroy{ //if(number==100){Debug.Log("Linked Alias is "+LinkedAliases[1]);} OwnerManager.MeshWasChanged = true; float time = 0f; time = Time.realtimeSinceStartup; for(int i = 0; i<LinkedAliases.Count; i++){ OwnerManager.Aliases[ LinkedAliases[i]  ].RemoveLink(number); foreach(int l in Triangles){ OwnerManager.Aliases[ LinkedAliases[i]  ].RemoveLink(number); } 				RemoveLink( LinkedAliases[i]  ); } 			RemoveAllLinksToOtherBranches; // INTRENSING HERE if((OwnerManager.IsMeshThick==true)&&IsATwin==false){ OwnerManager.Aliases[number+(OwnerManager.Aliases.Count/2)].Destroy; ProduceTrianglesBetweenWalls; } 			DestroyTriangles; //OwnerManager.UpdateTrianglesList; time = Time.realtimeSinceStartup - time; //Debug.Log("Time made is "+time); OwnerManager.DebDestroyTime += time; OwnerManager.DebDestroyCount++; } I've decided to make a new list LinksToOtherBranches rather than use LinkedAliases. This is becouse for now, if an Alias is in LinkedAlias list, it is making a triangle with current Alias. These new Links are diffrent, so they are in new list. Other method are just copies of ones used with standard LinkedAliases.

Okay, fire now spreads to All elements of mesh. The way is is spreading is unfortunately not perfect, as there are sometimes hovering elements. I'll repair that in next parts of tutorial.

For files, go to, New Github Repo   Version 7. In last one i has some kind of damage, Dyslocated heads, merge issues etc and I am not able to repair it.