- Home /
Finding the minimum distance between randomly spawned objects in a 9 x 9 grid.
I have a 9 x 9 set of (x,y) coordinates (81 coordinates total) spaced evenly apart. I am randomly spawning an increasing number of entities in that 9 x 9 grid, e.g., in phase 1 there are 2 entities in the grid, in phase 3 there are 4, in phase 4 there are 7, etc...
Knowing the number of entities I will spawn in that area, I want to figure out how far apart they can be spaced while still allowing for a random position. So I want to know if the random positions are using the space most ineffectively, how far apart would the closest ones be? (this way I can spawn that number of entities and check that they are at least that far away from one another and know that there will still be a spot that it can spawn into).
Thanks and if you have further questions, let me know what I can clarify.
Answer by fred_gds · Nov 29, 2013 at 08:20 PM
Hey,
I'm not quiet sure if understood your problem. As far as I got, was that you are spawning a number of entities, which spawn point is randomly calculated. So you are probably using something like Random.Range to set the position right?
And If I got it right you want to spawn the entites close to each other.
So what I would do is spawn the first entity randomly somewhere. Then I would run something like this to get the coordinates close to it.
var coordinates : Vector3[]; //or vector 2 whatever you are using for those
var spawnPoints : Array = new Array(); var spawnPointsInRange : Array = new Array(); var spawnPointsUnsorted : Array = new Array();
var firstSpawnPoint : int;
var entitesToSpawn : int;
var maxDistanceToFirstEnitity : int;
firstSpwanPoint = Random.Range(0,82);
For(var a : int = 0; a < 82; a++) { if(a != firstSpawnPoint) //Getting all the distances here { spawnPoints.Add(Vector3.Distance(coordinates[firstSpwanPoint], coordinates[a])); spawnPointsUnsorted.Add(Vector3.Distance(coordinates[firstSpwanPoint], coordinates[a])); } }
spawnPoints.Sort(); //seeking closest point
for(var d : int =0 ; d < spawnPoints.length; d++) { if(spawnPoints[d] < maxDistanceToFirstEntity) { spawnPointsInRange.Add(spawnPoints[d]); //adding only the points which are close enough to first entity } }
for(var b : int = 0; b < spawnEntities + 1; b++) //seeking coordinate for each entity to spawn { var random : int = Random.Range(0, spawnPointsInRange.length); //will create a random number for the position inside the range for(var c : int = 0; c < spawnPointsUnsorted.length; c++) { if(spawnPoints[random] == spawnPointsUnsorted[c]) { //Spawn your identity here at coordinats[c]; spawnPointsInRange.RemoveAt(random); // removes the value so that there is only on entity per coordinate } } }
So something like this should work for that. I am not quiet sure if it is what you needed and it's not clean code, but it hopefully contains the basic thought on how to solve the problem.
Thanks for your answer. I'm trying to space the objects out as much as possible knowing that random spawning will not yield the ideal configuration for maximum space between the objects. I want to know the maximum space possible if the space was randomly used most ineffectively. Basically, I want to space as much as possible and still leave room for all the objects.
Your answer
Follow this Question
Related Questions
Random Loot / Weapon Spawning 0 Answers
Grid Letter Box is not expanding, 0 Answers
How to Place and arrange 3D cards using Grid and Bounds 0 Answers
How to name each grid cell in a grid... 0 Answers
Unity2D Grid disappears when zooming out 0 Answers