Skip to content
Snippets Groups Projects
Commit 2ecb1818 authored by Victor's avatar Victor
Browse files

Generate the connectivity graph

parent b366d019
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,15 @@
using System.Collections.Generic;
using UnityEngine;
public class HandleCollision : MonoBehaviour
public class GraphEdge
{
public MeshTreeNode A, B;
}
public class CollisionHandler : MonoBehaviour
{
public float EdgeStrength;
// Start is called before the first frame update
void Start()
{
......
......@@ -91,6 +91,7 @@ public class Splitter : MonoBehaviour
{
private MeshBuilder mesh;
private MeshTreeNode root;
private List<GraphEdge> graph;
float time = 0;
MeshTreeNode current;
......@@ -105,6 +106,7 @@ public class Splitter : MonoBehaviour
Random.InitState(this.seed);
this.BuildMeshTree();
this.BuildConnectivityGraph();
this.GetComponent<MeshFilter>().mesh = this.mesh.GetMesh();
this.GetComponent<MeshFilter>().mesh.triangles = this.root.triangles;
......@@ -122,6 +124,39 @@ public class Splitter : MonoBehaviour
this.SplitNode(this.root);
}
// Compare fragment bounding boxes to create a connectivity graph
void BuildConnectivityGraph() {
this.graph = new List<GraphEdge>();
// Collect all fragments in a list
List<MeshTreeNode> leaves = new List<MeshTreeNode>();
GetLeaves(this.root, ref leaves);
// Iterate over all leaves/fragments, building a connectivity graph
for (int i = 0; i < leaves.Count; i++) {
Mesh A = new Mesh { triangles = leaves[i].triangles };
for (int j = i + 1; j < leaves.Count; j++) {
Mesh B = new Mesh { triangles = leaves[j].triangles };
// Check if the two AABBs intersect
if (A.bounds.Intersects(B.bounds))
this.graph.Add(new GraphEdge { A = leaves[i], B = leaves[j] });
}
}
}
// Gather all leaves from a given MeshTree recursively
void GetLeaves(MeshTreeNode node, ref List<MeshTreeNode> leaves) {
if (node.left != null)
GetLeaves(node.left, ref leaves);
if (node.right != null)
GetLeaves(node.right, ref leaves);
if (node.left == null && node.right == null)
leaves.Add(node);
}
private void Update()
{
time += Time.deltaTime;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment