public void makeObjects() { narrowPhase = GetComponent <NarrowPhase> (); // narrowPhase = gameObject.AddComponent<NarrowPhase>(); // narrowPhase.sweepAndPrune = GetComponent<SweepAndPrune> (); // narrowPhase.octTree = GetComponent<OctTreeAlg> (); // narrowPhase.mask = GetComponent<SpatialMasking> (); // narrowPhase.simple = GetComponent<Simple> (); // narrowPhase.boundsThing = GetComponent<Bounds> (); //GameControl.gameControl.createObjects = this; simple = GetComponent <Simple>(); simple.narrowPhase = narrowPhase; mass = GameControl.gameControl.objectMass; speed = GameControl.gameControl.speed; drag = GameControl.gameControl.drag; bounds = GameControl.gameControl.bounds; //space = GameControl.gameControl.spacing; staticRadius = GameControl.gameControl.radius; radiusRange = GameControl.gameControl.radiusRange; //spacing = new Vector3 (space, space, space); dimension = GameControl.gameControl.numObjects; nextUpdate = Time.time; definePlanes(bounds); int count = 0; while (count < dimension) { GameObject particle = GameObject.CreatePrimitive(PrimitiveType.Sphere); float radius = GameControl.gameControl.radius; radius += Random.value * radiusRange; //float radius = radiusRandomizer (); if (radius > maxRadius) { maxRadius = radius; } if (radius < minRadius) { minRadius = radius; } particle.transform.localScale = new Vector3(radius * 2, radius * 2, radius * 2); particle.GetComponent <MeshRenderer> ().material.color = Color.red; particle.name = "Sphere (" + count + ")"; count++; HRigidBody h = particle.AddComponent <HRigidBody> (); if (GameControl.gameControl.massScales) { h.mass = (4f / 3f) * Mathf.PI * Mathf.Pow(radius, 3) * mass; } else { h.mass = mass; } h.velocityExponent = 2.0f; h.drag = drag; h.isStatic = false; h.radius = radius; averageRadius += radius; narrowPhase.bounds = GameControl.gameControl.bounds; int i = distributeObject(h); if (i == 1) { Text msg = GameObject.FindGameObjectWithTag("ErrorText").GetComponent <Text>(); msg.text = "Error: Too many objects for bounding volume: Use fewer objects, enlarge area size, or make radius smaller"; start = false; break; } int[] direction = { -1, 1 }; float speedRange = 0f; h.velocityVector = new Vector3(direction [Random.Range(0, 2)] * (Random.value * speedRange + speed), direction [Random.Range(0, 2)] * (Random.value * speedRange + speed), direction [Random.Range(0, 2)] * (Random.value * speedRange + speed)); } #region arrange objects in grid /* * //static objects in grid * for (int k = 0; k < dimension; k++) { * for (int j = 0; j < dimension; j++) { * for (int i = 0; i < dimension; i++) { * GameObject particle = GameObject.CreatePrimitive (PrimitiveType.Sphere); * float radius = Random.value * radiusRange + staticRadius; * //if (count > dimension * dimension * dimension - 100) { * // radius = 10.0f; * //} else * // radius = 0.05f; * particle.transform.localScale = new Vector3 (radius * 2, radius * 2, radius * 2); * //particle.transform.localScale = new Vector3 (rad * 2, rad * 2, rad * 2); * particle.transform.position = new Vector3 (i * spacing.x + offset, j * spacing.y + offset, k * spacing.z + offset); * particle.GetComponent<Renderer> ().material.color = Color.red; * particle.name = "Sphere (" + count + ")"; * count++; * HRigidBody h = particle.AddComponent<HRigidBody> (); * h.mass = mass; * h.velocityExponent = 2.0f; * h.drag = drag; * h.isStatic = false; * h.radius = radius; * averageRadius += radius; * if (!randomPlacement){ * //h.velocityVector = new Vector3 (-1, 2, -3); * } * if (randomPlacement) { * narrowPhase.bounds = GameControl.gameControl.bounds; * int[] direction = { -1, 1 }; * float speedRange = speed / 5f; * h.velocityVector = new Vector3 (direction [Random.Range (0, 2)] * (Random.value * speedRange + speed), direction [Random.Range (0, 2)] * (Random.value * speedRange + speed), direction [Random.Range (0, 2)] * (Random.value * speedRange + speed)); * bool done = false; * HRigidBody[] tmp = FindObjectsOfType<HRigidBody> (); * int loop = 0; * while (!done) { * particle.transform.position = new Vector3 ((bounds - 4f) * Random.value + 2f, (bounds - 4f) * Random.value + 2f, (bounds - 4f) * Random.value + 2f); * if (tmp.Length == 1) * done = true; * else { * for (int m = 0; m < tmp.Length; m++) { * if (h.name != tmp [m].name) { * bool overlap = CheckBoundingBoxes (h, tmp [m]); * if (!overlap) * done = true; * } * } * } * for (int l = 0; l < planeIndices.Length; l++) { * int result = narrowPhase.CheckGroundPlaneContacts (new CollisionObject (h, planeIndices [l])); * if (result == 2 | result == 1) { * done = false; * } * } * //loop++; * } * h.oldPosition = h.transform.position; * } * } * } * } */ #endregion averageRadius /= count; GameControl.gameControl.avgRadius = averageRadius; GameControl.gameControl.maxRadius = maxRadius; GameControl.gameControl.minRadius = minRadius; HRigidBody[] tmp = FindObjectsOfType <HRigidBody> (); float avgVol = (4f / 3f) * Mathf.PI * (Mathf.Pow(averageRadius, 3f)); #region variance test float percObjVol = (dimension * avgVol) / (bounds * bounds * bounds); GameControl.gameControl.percObjVol = percObjVol; /* * bounds = GameControl.gameControl.bounds = narrowPhase.bounds = (int)Mathf.Pow((avgVol * dimension) / 0.01f, 1f/3f); * definePlanes (bounds); * speed = (float)bounds / 25.6f; * GameControl.gameControl.speed = speed; * foreach (HRigidBody h in FindObjectsOfType<HRigidBody>()) { * distributeObject (h); * int[] direction = { -1, 1 }; * float speedRange = 0f; * h.velocityVector = new Vector3 (direction [Random.Range (0, 2)] * (Random.value * speedRange + speed), direction [Random.Range (0, 2)] * (Random.value * speedRange + speed), direction [Random.Range (0, 2)] * (Random.value * speedRange + speed)); * } * * if (GameControl.gameControl.whichBroad == 1) { * StreamWriter sr; * GameControl.gameControl.fileName = "j" + GameControl.gameControl.testNum + "radiusDist" + ".dat"; * if (!File.Exists ("j" + GameControl.gameControl.testNum + "radiusDist" + ".dat")) { * //sr = File.CreateText ("j" + GameControl.gameControl.testNum + "TimeTest" + whichBroad + ".dat"); * sr = File.CreateText ("j" + GameControl.gameControl.testNum + "radiusDist" + ".dat"); * sr.WriteLine ("# X Y Z"); * } else { * //sr = File.AppendText ("j" + GameControl.gameControl.testNum + "TimeTest" + whichBroad + ".dat"); * sr = File.AppendText ("j" + GameControl.gameControl.testNum + "radiusDist" + ".dat"); * } * sr.WriteLine (perc1); * sr.WriteLine (perc2); * sr.WriteLine (perc3); * sr.WriteLine (perc4); * sr.WriteLine (perc5); * sr.Close (); * } */ #endregion //StartCoroutine (timer ()); if (start) { narrowPhase.StartNarrowPhase(); } }