public void ComputeAngle() { var ind1 = Simplex1.Indices; var ind2 = Simplex2.Indices; var ind1_ext = ind2.Except(ind1).ToArray()[0]; var vec1 = Simplex2.GetEdgeByIndex(ind1_ext); var ind2_ext = ind1.Except(ind2).ToArray()[0]; var vec2 = Simplex1.GetEdgeByIndex(ind2_ext); var normal1 = VariousHelpers.GetNormalVector(Simplex1, vec1 - Simplex1.BasePoint); var normal2 = VariousHelpers.GetNormalVector(Simplex2, vec2 - Simplex2.BasePoint); var commonBasePoint = CommonBase.BasePoint; Angle = Math.PI - CommonBase.AmbiantSpace.Angle(commonBasePoint, normal1, normal2); }
// Update is called once per frame void Update() { // Mask to not detect hands when deleting objects in scene int layerMask = 1 << 2; layerMask = ~layerMask; // When to add balls and halos if (OVRInput.GetDown(OVRInput.RawButton.B)) { if (rightHandAnchor != null) { AddBall(rightHandAnchor); } } if (OVRInput.GetDown(OVRInput.RawButton.Y)) { if (leftHandAnchor != null) { AddBall(leftHandAnchor); } } // When to remove balls and halos if (OVRInput.GetDown(OVRInput.RawButton.A)) { if (rightHandAnchor != null) { RemoveBall(rightHandAnchor); } } if (OVRInput.GetDown(OVRInput.RawButton.X)) { if (leftHandAnchor != null) { RemoveBall(leftHandAnchor); } } // When to change radius of halos if (OVRInput.Get(OVRInput.RawButton.RThumbstickUp)) { r += 0.02f; UpdateRadii(); foreach (int cylindex in CList_alive) { if (.05 * r >= CList[cylindex].GetComponent <Simplex1>().length) { CList[cylindex].GetComponent <Simplex1>().is_drawn = true; } } UpdateCylinders(); if (!NoTriangles) { foreach (int triindex in TList_alive) { Simplex2 tri = TList[triindex].GetComponent <Simplex2>(); tri.is_drawn = ( CList[tri.neighbors1[0]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[1]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[2]].GetComponent <Simplex1>().is_drawn); } UpdateTriangles(); } } if (OVRInput.Get(OVRInput.RawButton.RThumbstickDown)) { r -= 0.02f; UpdateRadii(); foreach (int cylindex in CList_alive) { if (.05 * r < CList[cylindex].GetComponent <Simplex1>().length) { CList[cylindex].GetComponent <Simplex1>().is_drawn = false; } } UpdateCylinders(); if (!NoTriangles) { foreach (int triindex in TList_alive) { Simplex2 tri = TList[triindex].GetComponent <Simplex2>(); tri.is_drawn = ( CList[tri.neighbors1[0]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[1]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[2]].GetComponent <Simplex1>().is_drawn); } UpdateTriangles(); } } // When to toggle visibility of halos, cylinders, triangles if (OVRInput.GetDown(OVRInput.RawButton.RThumbstick)) { halo_toggle++; foreach (int ballindex in BList_alive) { BList[ballindex].GetComponent <Simplex0>().is_halo_shown = (halo_toggle % 2 == 0); } UpdateHalos(); } if (OVRInput.GetDown(OVRInput.RawButton.LThumbstick)) { cyl_tri_toggle++; foreach (int cylindex in CList_alive) { CList[cylindex].GetComponent <Simplex1>().is_shown = (cyl_tri_toggle % 3 < 2); } UpdateCylinders(); if (!NoTriangles) { foreach (int triindex in TList_alive) { TList[triindex].GetComponent <Simplex2>().is_shown = (cyl_tri_toggle % 3 < 1); } UpdateTriangles(); } } foreach (int ballindex in BList_alive) { // How user moving balls affects scene if (BList[ballindex].gameObject.transform.hasChanged) { // Adjust neighboring cylinders foreach (int cylindex in BList[ballindex].gameObject.GetComponent <Simplex0>().neighbors1) { GameObject cylinder = CList[cylindex]; GameObject ball0 = BList[cylinder.GetComponent <Simplex1>().neighbors0[0]]; GameObject ball1 = BList[cylinder.GetComponent <Simplex1>().neighbors0[1]]; cylinder.transform.position = (ball0.transform.position + ball1.transform.position) / 2; cylinder.transform.rotation = Quaternion.FromToRotation(Vector3.up, ball0.transform.position - ball1.transform.position); cylinder.transform.localScale = new Vector3(0.015f, Vector3.Distance(ball0.transform.position / 2, ball1.transform.position / 2), 0.015f); cylinder.GetComponent <Simplex1>().length = (ball0.transform.position - ball1.transform.position).magnitude; cylinder.GetComponent <Simplex1>().is_drawn = (.05 * r >= cylinder.GetComponent <Simplex1>().length); UpdateSingle(CList[cylindex]); } if (!NoTriangles) { // Adjust neighboring triangles foreach (int triindex in BList[ballindex].gameObject.GetComponent <Simplex0>().neighbors2) { GameObject triangle = TList[triindex]; temp_list0 = triangle.GetComponent <Simplex2>().neighbors0; temp_list1 = triangle.GetComponent <Simplex2>().neighbors1; Destroy(triangle.GetComponent <Simplex2>()); Simplex2 tri = triangle.AddComponent <Simplex2>(); tri.neighbors0 = temp_list0; tri.neighbors1 = temp_list1; GameObject ball0 = BList[tri.neighbors0[0]]; GameObject ball1 = BList[tri.neighbors0[1]]; GameObject ball2 = BList[tri.neighbors0[2]]; tri.newVertices = new Vector3[3] { .05f * ball0.transform.position, .05f * ball1.transform.position, .05f * ball2.transform.position }; tri.is_drawn = (CList[tri.neighbors1[0]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[1]].GetComponent <Simplex1>().is_drawn & CList[tri.neighbors1[2]].GetComponent <Simplex1>().is_drawn); tri.is_shown = (cyl_tri_toggle % 3 < 1); UpdateSingle(TList[triindex]); } } BList[ballindex].gameObject.transform.hasChanged = false; } } }
// Adds a ball and a halo at the input anchor position void AddBall(GameObject anchor) { // Instantiate ball at anchor position and add to list GameObject clone = (GameObject)Instantiate(ballPrefab, anchor.transform.position, anchor.transform.rotation); clone.name = num0.ToString(); BList.Add(clone); // Instantiate halo at anchor position GameObject halo = (GameObject)Instantiate(haloPrefab, anchor.transform.position, anchor.transform.rotation); halo.name = num0.ToString(); halo.transform.SetParent(clone.transform); HList.Add(halo); Simplex0 s = clone.AddComponent <Simplex0>(); s.is_halo_shown = (halo_toggle % 2 == 0); UpdateSingle(clone); // Instantiate cylinder for every vertex in scene foreach (int b in BList_alive) { GameObject existingball = BList[b]; GameObject cylinder = (GameObject)Instantiate(cylinderPrefab, (anchor.transform.position + existingball.transform.position) / 2, Quaternion.FromToRotation(Vector3.up, anchor.transform.position - existingball.transform.position)); cylinder.transform.localScale = new Vector3(0.015f, Vector3.Distance(existingball.transform.position / 2, anchor.transform.position / 2), 0.015f); cylinder.name = num1.ToString(); CList.Add(cylinder); // Set cylinder attributes Simplex1 c = cylinder.AddComponent <Simplex1>(); Vector3 edger = clone.transform.position - existingball.transform.position; c.length = edger.magnitude; c.is_shown = (cyl_tri_toggle % 3 < 2); c.is_drawn = (.05 * r >= c.length); UpdateSingle(cylinder); // Add edge index to neighbors s.neighbors1.Add(num1); existingball.GetComponent <Simplex0>().neighbors1.Add(num1); // Add vertex indices to neighbors c.neighbors0.Add(num0); existingball.GetComponent <Simplex0>().neighbors0.Add(num0); c.neighbors0.Add(System.Convert.ToInt32(existingball.name)); s.neighbors0.Add(System.Convert.ToInt32(existingball.name)); CList_alive.Add(num1); num1++; } if (!NoTriangles) { // Instantiate triangle for every pair of vertices in scene foreach (int c in CList_alive) { if (!CList[c].GetComponent <Simplex1>().neighbors0.Contains(num0)) { GameObject triangle = (GameObject)Instantiate(trianglePrefab, new Vector3(0, 0, 0), Quaternion.identity); triangle.name = num2.ToString(); TList.Add(triangle); triangle.transform.localScale = new Vector3(20f, 20f, 20f); Simplex2 tri = triangle.AddComponent <Simplex2>(); GameObject ball0 = BList[CList[c].GetComponent <Simplex1>().neighbors0[0]]; GameObject ball1 = BList[CList[c].GetComponent <Simplex1>().neighbors0[1]]; tri.newVertices = new Vector3[3] { .05f * clone.transform.position, .05f * ball0.transform.position, .05f * ball1.transform.position }; // Add triangle index to neighbors s.neighbors2.Add(num2); ball0.GetComponent <Simplex0>().neighbors2.Add(num2); ball1.GetComponent <Simplex0>().neighbors2.Add(num2); CList[c].GetComponent <Simplex1>().neighbors2.Add(num2); GameObject cyl0 = CList[ball0.GetComponent <Simplex0>().neighbors1.Intersect(s.neighbors1).ToList()[0]]; GameObject cyl1 = CList[ball1.GetComponent <Simplex0>().neighbors1.Intersect(s.neighbors1).ToList()[0]]; cyl0.GetComponent <Simplex1>().neighbors2.Add(num2); cyl1.GetComponent <Simplex1>().neighbors2.Add(num2); // Add edge indices to neighbors tri.neighbors1.Add(c); tri.neighbors1.Add(System.Convert.ToInt32(cyl0.name)); tri.neighbors1.Add(System.Convert.ToInt32(cyl1.name)); // Add vertex indices to neighbors tri.neighbors0 = new List <int> { num0, System.Convert.ToInt32(ball0.name), System.Convert.ToInt32(ball1.name) }; // Set triangle attributes tri.is_shown = (cyl_tri_toggle % 3 < 1); tri.is_drawn = (CList[c].GetComponent <Simplex1>().is_drawn & cyl0.GetComponent <Simplex1>().is_drawn & cyl1.GetComponent <Simplex1>().is_drawn); UpdateSingle(triangle); TList_alive.Add(num2); num2++; } } } BList_alive.Add(num0); num0++; }