public void SetBall(BallType type, PancakePhysicsBall physicsBall) { if (balls == null) { balls_ = new List <PancakePhysicsBall>(); balls = new Dictionary <BallType, PancakePhysicsBall>(); ballDistance = new Dictionary <BallType, float>(); } balls_.Add(physicsBall); if (balls.ContainsKey(type)) { balls[type] = physicsBall; ballDistance[type] = Vector3.Distance(transform.position, physicsBall.transform.position); } else { balls.Add(type, physicsBall); ballDistance.Add(type, Vector3.Distance(transform.position, physicsBall.transform.position)); } if (type == BallType.left) { l = physicsBall; } else if (type == BallType.right) { r = physicsBall; } else { c = physicsBall; } }
public VerticeGroup(int vertID, float xAxis, float zAxis, PancakePhysicsBall pb) { x = xAxis; z = zAxis; physicsBall = pb; verticesIDs = new List <int>(); verticesIDs.Add(vertID); isCenter = false; }
int centerId = -1; //so we can set it on physics ball later void Start() { mesh = GetComponent <MeshFilter>().mesh; vertices = mesh.vertices; verticeGroups = new List <VerticeGroup>(); bool found = false; /* * group all vert's, so anythink that has the same x, z value can be moved together. * we are also going to put a point (or sphere) on each group which will be used for are physics * we want the two faces of the cylinder (or pancake) to move together. */ // loop all vert's for (int i = 0; i < vertices.Length; i++) { found = false; // find if there is an existing group for (int j = 0; j < verticeGroups.Count; j++) { if (verticeGroups[j].Compare(vertices[i].x, vertices[i].z)) { verticeGroups[j].AddVertID(i); found = true; break; } } if (!found) // add spwan physics ball and add new group. { PancakePhysicsBall pb = Instantiate(physicsBall, transform); verticeGroups.Add(new VerticeGroup(i, vertices[i].x, vertices[i].z, pb)); pb.transform.localPosition = verticeGroups[verticeGroups.Count - 1].GetAsVector3(); // update center id once it has been found (it should always be 0,0) if (centerId == -1 && vertices[i].x == 0 && vertices[i].z == 0) { centerId = verticeGroups.Count - 1; } } } verticeGroups[centerId].physicsBall.SetIsCenter(true); // now we have all our groups we can, set the center and two cloest physics balls to each physics ball for (int i = 0; i < verticeGroups.Count; i++) { verticeGroups[i].physicsBall.SetBall(PancakePhysicsBall.BallType.center, verticeGroups[centerId].physicsBall); // find the two closes balls that are not the center. List <int> cloestId = new List <int>(2); cloestId.AddRange(new int[2] { 0, 0 }); List <float> dist = new List <float>(2); dist.AddRange(new float[2] { 100f, 100f }); verticeGroups[i].physicsBall.name = i + ""; print(i + " center dist: " + Vector3.Distance(verticeGroups[i].GetAsVector3(), verticeGroups[centerId].GetAsVector3())); for (int j = 0; j < verticeGroups.Count; j++) { if (verticeGroups[j].GetAsVector3() == Vector3.zero || i == j) // skip center and self { continue; } float tempDist = Vector3.Distance(verticeGroups[i].GetAsVector3(), verticeGroups[j].GetAsVector3()); if (j == 0 || tempDist < dist[0]) { cloestId.Insert(0, j); dist.Insert(0, tempDist); } else if (j == 1 || tempDist < dist[1]) { cloestId.Insert(1, j); dist.Insert(1, tempDist); } } // set the two balls now we have found the cloest two verticeGroups[i].physicsBall.SetBall(PancakePhysicsBall.BallType.left, verticeGroups[cloestId[0]].physicsBall); verticeGroups[i].physicsBall.SetBall(PancakePhysicsBall.BallType.right, verticeGroups[cloestId[1]].physicsBall); verticeGroups[i].physicsBall.Init(); } }