# Realistic Multi-Agent Formation Using Discretionary Group Behavior (DGB)
## 3 Algorithm: Discretionary Group Behavior
### Maintaining in the Right Position  
This section is for a situation when the calculated density is zero and the agents are forced to leave their proper area across the group to avoid other agents and obstacles from colliding. Having the right location for the agent, two half-planes will be added by rotating the vector calculated from the relative position between the agent and the right spot, with 45 degrees in two directions (Figure 3). Relative position (RP) calculates by the subtraction of the agent's position from the proper position. The rotation of RP in two-dimensional space can simply calculate by the two matrixes for clockwise (Equation (3)) and counterclockwise (Equation (4)) rotations. For determining each half-planes, it is required to have three main vectors--the agent's unit velocity, the normal vector of the half-plane toward the desired direction and a unit vector in one of the left or right directions of the moving path. Since in our case, the rotation degree (\(\theta\)) is \(45^{\circ}\), there is no need to calculate normal vectors because the two half-planes are perpendicular to each other. If the rotation degree changes to other values, the calculation of normal vectors is necessary.  
\[R(\theta)=\begin{bmatrix}\cos\theta&-\sin\theta\\ \sin\theta&\cos\theta\end{bmatrix}, \tag{3}\]  
\[R(-\theta)=\begin{bmatrix}\cos\theta&\sin\theta\\ -\sin\theta&\cos\theta\end{bmatrix}. \tag{4}\]  
Figure 2: An approximate sample of position production for a uniform formation. The first line will be computed directly from the leader’s unit velocity and other positions by the weighted sum of the velocities related to the leader (\(V_{L}\)) and the agent (\(V_{A}\)) in the upper line of the formation.  
Let \(n_{1}\) and \(n_{2}\) be normals of the rotated vectors (our half-planes) from RP and let \(u^{\prime}\) be the unit vector in the left or right directions considering the moving side. For a better understanding of how \(u^{\prime}\) calculates, let's consider vector RP (\(RP=pos_{valid}-pos_{agent}\)) as the desired moving direction for agent A (Figure 4). So according to this direction, \(u^{\prime}\) will be equal to \((1,0)\). This tactic helps the starting point of all half-planes including collision avoidance [2] and the two other half-planes created in this section be so close to each other. Therefore, it facilitates solving linear programs during the algorithm.  
Hence, \(ORCA(A\to pos)\) is a half-plane defining a set of allowed velocities leading agent A toward its proper position. This half-plane is pointing in the direction of \(n_{1}\) or \(n_{2}\) starting at the point of \(v_{A}+u^{\prime}\) (Equations (5) and (6)).  
\[ORCA_{A\to pos}^{1}=\{v|(v-(v_{A}+u^{\prime}))\cdot n_{1}\}, \tag{5}\]  
\[ORCA_{A\to pos}^{2}=\{v|(v-(v_{A}+u^{\prime}))\cdot n_{2}\}. \tag{6}\]  
The intersection of all half-planes leads the agent to move in a direction that provides both collision-free motions for all members and a well-constructed formation for the group during the movement. After calculating the two half-planes in the direction of the accurate spot in the group, the agent that is far away from its position because of the avoidance function against some contrary components can adjust (increase) its speed for being able to get back in the proper location in the group. Once the agent gets to the right place, it will change the speed to be the same as the leader. This important feature of agents that can change their speed depending on the situation, has been  
Figure 4: A simple method for calculating \(u^{\prime}\) that has been used for defining the points of the half-planes. In this special case, the moving direction of the group considers in \(Y\) coordinate.  
Figure 3: Producing additional half-planes in the direction of the proper location, in each time step of the algorithm.  
used in a few methods [20; 23] and is missing in some other works for simulating large groups or small groups.