- Home /
Procedural object generation, objects overlap each other
Hey there,
I'm posting my code here because I've got to dead end and after rewriting my code twice, I think I'm missing something, so I came for help.
Basically, you call a method which generates the amount of objects (viruses) at the start of the game, but somehow when I want to position them not to overlap each other, the calculation doesn't work.
You call this method like this: GL.InstantiateVirus(int quantity, string Virus) I call this method 3 times with 3 different viruses but it doesn't seem to work. Could someone take a look at it what's wrong with my calculation?
Thank you in advance!
public class GenerateLevel : MonoBehaviour {
public List<StartVirus> VirusList;
public List<StartVirus> VirusListAll;
// Use this for initialization
void Start () {
VirusList = new List<StartVirus> ();
VirusListAll = new List<StartVirus> ();
}
// Update is called once per frame
void Update () {
}
public void InstantiateVirus(int quantity, string virus) {
//Store the positions as Vector3 as ranges where X = width, Y = height, Z = Distance from camera
//from 0-1. 0 is bottom left of camera, 1 is top right of the camera.
VirusList.Clear();
for (int i = 0; i < quantity; i++) {
StartVirus _virus = new StartVirus();
_virus.Pos = new Vector3(Random.Range(1f,100f) / 100f, Random.Range(1f,100f) / 100f, Globals.cameraHeight);
_virus.Virus = Resources.Load ("Virus " + virus.ToString ()) as GameObject;
_virus.Bounds = _virus.Virus.GetComponent<BoxCollider>().size / 100f;
Debug.Log(_virus.Bounds);
//Check if there's no previous position which _virus wants to take
bool positionGood = false;
while (!positionGood) {
for (int j = 0; j < VirusListAll.Count; j++) {
StartVirus _checkVirus = VirusListAll[j];
if (_checkVirus.Pos.x - _checkVirus.Bounds.x > _virus.Pos.x + _virus.Bounds.x &&
_checkVirus.Pos.x + _checkVirus.Bounds.x < _virus.Pos.x - _virus.Bounds.x &&
_checkVirus.Pos.z - _checkVirus.Bounds.z > _virus.Pos.z + _virus.Bounds.z &&
_checkVirus.Pos.z + _checkVirus.Bounds.z < _virus.Pos.z - _virus.Bounds.z) {
continue;
} else {
_virus.Pos = new Vector3(Random.Range(1f,100f) / 100f, Random.Range(1f,100f) / 100f, Globals.cameraHeight);
Debug.Log("Generated New Position");
break;
}
}
positionGood = true;
}
Debug.Log(_virus.Virus);
VirusList.Add (_virus);
VirusListAll.Add(_virus);
}
for (int i = 0; i < quantity; i++) {
GameObject _go = Instantiate (VirusList[i].Virus, Camera.main.ViewportToWorldPoint (VirusList [i].Pos), Quaternion.identity) as GameObject;
_go.name = virus.ToString ();
}
}
[System.Serializable]
public class StartVirus {
[SerializeField]
private Vector3 pos;
public Vector3 Pos { get { return pos; } set { this.pos = value; } }
[SerializeField]
private Vector3 bounds;
public Vector3 Bounds { get { return bounds; } set { this.bounds = value; } }
[SerializeField]
private GameObject virus;
public GameObject Virus { get { return virus; } set { this.virus = value; } }
}
}
Ins$$anonymous$$d of checking bounds, just add all random positions to a list, and when you generate another random position check the list if it exists already?
If I would just do that there would be a possibility of overlapping, since you don't check whether that position already exists or not.
Wouldn't this work? (make sure to change thee x, y ,z randoms as u want):
private List<Vector3> positions = new List<Vector3>();
private Vector3 GetUniqueRandomPosition(float $$anonymous$$Range, float maxRange)
{
var random = new Vector3(UnityEngine.Random.Range($$anonymous$$Range, maxRange), UnityEngine.Random.Range($$anonymous$$Range, maxRange), UnityEngine.Random.Range($$anonymous$$Range, maxRange));
while (positions.Contains(random))
{
random = new Vector3(UnityEngine.Random.Range($$anonymous$$Range, maxRange), UnityEngine.Random.Range($$anonymous$$Range, maxRange), UnityEngine.Random.Range($$anonymous$$Range, maxRange));
}
positions.Add(random);
return random;
}
Answer by Lysander · Jan 05, 2018 at 11:20 PM
Make a single-element array for collision data, then use OverlapBoxNonAlloc or OverlapSphereNonAlloc, which is not only tremendously efficient for exactly this kind of situation, but would avoid pretty much all of the effort you're expending here and trim it down to a single function call. If you use the layermask to ONLY return colliders that are of a certain layer (put viruses on one layer), then returning a single collider result means generate a new random position and try again.