Arrange objects in a box formation?

Hello. I’m making a top-down game where “minions” can be spawned around and moved with the player. No matter what I try, though, I’m stuck when attempting to arrange said minions in a thickening box formation around the player. I’ve attached some code to show how I perform a “rank and file” formation in front of the player. I already have ways to change the formation, but I can’t figure out how to calculate the minion’s coordinates offset from the player for the box formation. Is there any way to do this?

EDIT: Here is a rough idea of how I imagined the minion order (imagine they’re in a grid). I’m not sure if the third layer is a good pattern, so I’m open to modifications.

3720-arrangement.png

public int myID;
public float x = 0;
public float z = 0;

void AssignID (int minionID) { // called from another class
	myID = minionID; // starts at zero, and is incremented for each new minion
}

void FormUp () {
	if (myID % 2 == 0) { //go left
		x = 0.5f * (1 + myID % 10);//rank and file formation
		z = 1 * (1 + myID / 10);
	} else { //go right
		x = 0.5f * (myID % 10);//rank and file formation
		z = 1 * (1 + myID / 10);
	}
}

void Update {

	if (myID % 2 == 0) { // go left
		// offset the position of the minion from the player's position
		// "movement" is a class that deals with the player's movement and... 
		// ...position on the screen
		placement = new Vector3(movement.myX - x, 0, movement.myZ + z);
		iTween.MoveUpdate(gameObject, placement, 0.4f);
	} else { // go right
		placement = new Vector3(movement.myX + x, 0, movement.myZ + z);
		iTween.MoveUpdate(gameObject, placement, 0.4f);
	}
}

that’s not particularly easy.

I’ll tell you the simplest idea to get this done and move on…

To be clear you only need UP TO THREE LAYERS, correct?

Ok here’s what to do. LOOK at any one number. Let’s say 23.

You have to write down the “MOVE” for 23.

For 22, the “MOVE” is in fact -2,+3 . you get it?

For 7 the MOVE is +1, -1

ok?

Now do this.

private var moves:Vector2[] =
		[
		Vector2( 2,-1 ),		// 0
		Vector2( 2,2 ),			// 1
		Vector2( 0,-1 )	,		// 2
		.. etc .. do all of them up to 47 ...
		.. etc .. do all of them up to 47 ...
		.. etc .. do all of them up to 47 ...
		];

You understand how to do the rest right?

Just make sure you have the local forward. Whatever your gap between them is (say 0.4 or whatever), just multiply by that. So it’s like …

for say number 15 …

goThisFarRight = moves[15].x * 0.40;
goThisFarUp = moves[15].y * 0.40;

So those two are how far you move “15” from the “H” hero.

You could easily write a logical procedure that gives them to you, but it would be pointless because I guess you want the flexibility of knowing the layout your are using (ie, the one you drew)

I hope this does the trick ! Cheers! K.I.S.S. … very important!


BTW

If you TRULY want a random position on layer N, it’s just this

function randomOnLayer(n:int ):Vector2
  {
  if (Random.Range(0,2)==0)
  return Vector2(Random.Range(-n,n+1),n*(Random.Range(1,3)*2-3));
  else
  return Vector2(n*(Random.Range(1,3)*2-3),Random.Range(-n,n+1));
  }

but really I recommend the “moves array” above.