<<

Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

Group Simulation of Three Agents using Unity3D Vincent Wong Max Turpeinen [email protected] [email protected]

KTH Royal Institute of Technology | Supervisor: Christopher Peters

Link to our blog: http://www.crowdsimulationkth.blogspot.se/

Figure 1: Screen captures from our scene with the latest implementation of our model.

Abstract

Crowd simulation refers to simulating a large number of agents trying to replicate collective behavior in 3D . For this, we have been using the 3D. In our work we are mainly focusing on group formations consisting of 3 agents. Each group is assigned a leader which keeps track of formations and evading collisions. The groups can take on four different formations, working like a finite state machine. In our general scenario, two groups could face, making it useful to adapt the formations and movement direction to avoid each other.

We have been implementing our own model after having looked at different major concept already existing in the world of . You could define our model as a hybrid rule-based one, seeing that we have a smaller group working in a way like a single entity, making its judgment dependent on checks with corresponding rules.

Virtual cinematography reflecting for most parts real human behavior in one or several 1. Introduction groups interacting. The major industry lays in Crowd simulation is all about simulating a making films and games using 3D computer large number of entities, also known as agents graphics but is also used for public safety as a greater group, instead of individuals. The planning. During e.g. sporting and general general technique is needed when the number spectator occasions crowd controlling of characters exceeds the given tolerated strategies can be made beforehand using computer performance. The idea is to make crowd simulation. This is done to prevent the cost efficient which in many accidents during emergencies. Generally, cases requested. The concept is applied in crowd simulations are based off of two Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology different categories; microscopic models and 2.1. Microscopic macroscopic models (discussed in the section The microscopic [4] approach focuses on the “2. Related work”). There are several software issues on an agent , not taking the group of crowd simulation, but none (what we could as a whole in to account. Usual implemented find) so far specified on three agents. That is aspects for the pedestrian in such approach is what we have been focusing on with our own psychological and social behavior. The rule-based algorithm (which can be seen in “3. Individual agents thereby make their own Implementation”). We have also been decisions as well as sending information in discussing the different techniques as well as between each other. A distinguishing feature our own implementation (in section “4. Result for a microscopic model is its accuracy and its & Discussion”) to finally reach our conclusion high resolution, since it describes how each (“5. Conclusion”). pedestrian behaves in every single step. However, as a result, the cost in computation complexity is very high. Increasing the scale 2. Related work of the crowd, the approach can no longer Throughout the years several different sustain its efficiency and smoothness. techniques to model agents forming crowds have been proposed. Especially now seeing the There are lots of approaches within this immense growth of interest in areas such as category; Cellular Automata, agent-based, movies and computer games. The concept is to particle system, social force and rule based etc. compute a large number of agent’s paths towards shared or different goals, while 2.1.1. Cellular Automata simultaneously avoiding collision in between Also known as “CA” [5] is one of the earlier each other as well as other obstacles. Not to approaches doing crowd simulations. This forget, replicating the human behavior. The approach can be seen as having a navigation problem in itself can be seen in a broader scale mesh divided into identical cells using a using global planning as well as local discrete function and having a state for each behavior. It all can be traced way back to the cell. The state of each cell is based on a set of works of Reynolds [2] who proposed “Boids” local rules. The state of a grid cell changes which is implementing simple rules commonly between zero and one indicating whether or as forces to avoid collision while preventing not a cell is occupied by an agent or not. The flock cohesion. Methods such as seek flee and agents then move towards its goal only able to pursue were later added. The rule-based move in between adjacent grid cells. approach became very popular and created the 2.1.2. Particle Systems basis of what today is known as crowd Particle systems [6] were initially made to simulation. render fuzzy objects like smoke and fire, The subject has also been looked at from many though the idea was also applicable to the area aspects and viewpoint whereas in general, the of crowd simulation. Here, the pedestrian are different methods can be divided in to two defined in a more vague sense seeing them as groups; microscopic & macroscopic. The two mere objects. Like in other microscopic categories focus on crowd simulation on two models the behavior is rule based, but can be different levels, which results in different based off of various algorithms that define the detail-of-level which also cause different behavior or the necessary path calculation. features. 2.2. Macroscopic On the contrary to the microscopic approach, the macroscopic [4] approach cannot bring the same high-resolution result for every Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology pedestrian, nor does it use the social and just a plane of agents moving by psychological elements of each pedestrian. In random numbers generated. other words, the approach contains overall less information, which makes it more durable for 2.3. Hybrids large-scale crowds and highly concentrated Hybrids, as well in this case as in many others, populations. This is due to the fact that it takes refer to a combination of two commonly used the crowd as a whole and looks more on how implementations. When talking about crowd the environment may affect the behavior of simulation, the combination is about the crowd. Local interaction in between microscopic- and the macroscopic approach. pedestrians in the crowd is not taken in to Many different combinations of the two account when it comes to macroscopic models have been proposed trying to unite the approach. One of the more known models of advantages of each with various results. this type is the flow-based model as well as the An example of a hybrid technique is the one fluid model. A general way of looking at implemented by Narain R. et al. 2009 [3], crowd in the macroscopic sense is using partial where they use a dual representation of the differential equations to describe the change in microscopic- and the macroscopic model. time for the relationship between density flow Here the idea is to use discrete agents along and velocity of a crowd. with a single continuous system where the 2.2.1. Fluids continuous setting steers the large-scale As mentioned earlier, one of the greater behavior of the crowd as well as avoidance approaches in the macroscopic category is the between the agents in dense scenarios. fluid [7] one. This approach has the idea of assigning the large group into different areas, 2.4. Pedestrian also known as fields. Depending on the In a crowd simulation the pedestrians also play specific approach we can have several fields a part. What is the most common thought is with the corresponding aspect, such as density that the pedestrian should fit your fields, velocity fields, and dynamic fields environment, though what is also worth where e.g. dynamic fields are used to handle mentioning is the detail of it. The cost between obstacles. Since the crowd is seen either as a simulating a group of high resolution 3D fluid or a gas, physical laws of fluids dynamics characters and a group of low resolution 3D are of great value doing the modelling. characters can be of great importance. Important to note is that the group should A solution to this problem is to use imposters share the same goal. [9]. Imposters is a way of rendering a 3D 2.2.2. Chaos Models character in 2D and placing it in front of the The chaos model refers to a crowd with a real 3D one, which takes less time than chaotic behavior known to be implemented by actually rendering the real 3D one. This is Saiwaki et al., 1997 [8]. The agents work on commonly rendering background objects, but their own, but what makes them a uniform can also be used in the case of crowd group is that each agent is share the same set simulation, implementing it on pedestrians of parameters and rules. This might be seen as either meant to be far away or not placed in a microscopic approach but the focus lies in center (not meant to have focus). the group, and not the individual agent. By changing the few existing parameters and rules for all of them, very different and interesting 3. Implementation (model) behavior can be observed. This make the In general, every crowd simulation consists of model and its agents more controllable, and a model of an agent, as well as an implementation of an algorithm. In this Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology project the main focus has been on the group rotates towards the goal while approach of controlling and steering crowds, translating, creating a nice and beautiful both on group and individual level, i.e. the spline. There is also a fixed tolerated radius, if hybrid model. either one of the agents or the leader reaches the inside of the area, the group disappears 3.1. Formations since they have reached their goal. A small group of pedestrians usually strive to walk in a line formation, if possible (a way of socializing). The formation however changes or in so cases splits up to avoid obstacles [10], the choice of avoidance depends on the obstacle and the size of the group, the bigger the group is, the more likely of a split.

To begin with a simple correlation is needed between the agents in a group. By taking in consideration of [10] this project mainly focuses on groups consisting of three agents with 4 different formations to change between. The implemented approach was having a leader placed in the center and the agents adjacently positioned on each side of the Figure 3. The direction is towards the goal but leader. This forms our standard formation, also the is time dependent, giving the known as line formation. There are also three group a more natural way of moving. other formations which can be seen in Figure 2. Each formation is predefined and static with 3.3. Position Holding a list of 3D vectors. The leader’s formation position is always at (0,0,0) with the followers 3.3.1. Distance to Position position adjusted according to the leader. The agents in each group are dependent on the leader in such way that the leader generates a formation position for each agent. This position is for e.g. the line formation two adjacently placed 3D-coordinate. Agents will always try to hold the formation and in case an agent should fractionate it will adjust its speed to maintain the given formation. This happens e.g. when followers move to fast/slow compared to the leader (Figure 4.).

Figure 2. a. Line Formation, b. Left Formation, c. Straight Formation, d. Right Formation.

3.2. Goal Either by generating a goal position manually or randomly, a 3D-coordinat position is placed Figure 4. If d is greater or less than x the for a group and its leader. The goal point can i agent will have to decrease or increase the be seen as the leaders target position, the speed accordingly. Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

3.3.2. Rotation of Group mathematical model, conceived as an abstract machine that can be in only one of a finite number of states at a time. It changes from one state to another state when triggered. There is a number rays for each set of group which can be seen in Figure 7. These rays check the area in front of the group and depending on the combinations of hit the state changes accordingly.

Figure 5. The solid agents are where the agents are supposed to be.

One problem that occurs while holding the formation is when the group is supposed to rotate as seen above (Figure 5). In order to keep the formation during rotation the scalar product of the direction from the leader towards the goal VG and the adjacent right VR of the leader will return a positive value if the rotation is to the right, and if we get a negative outcome, the rotation is to the left. We now know which agent needs to slow down and which to speed up in order to maintain the formation. The speed of each agent is randomized from an adapted allowed interval, Figure 7. The solid ones checks for collision which gives a realistic looking behavior to the with obstacles and agents, whereas the dotted group. one only detects agents.

3.4.1. Avoidance of Obstacles There are in total five rays that are used to detect obstacles as can be seen on Figure 7. These rays follow the group and always hold the standard formation i.e. they will not change position even if the group changes formation. One can say that it is static to the group. The left-, right-, up- and down-rays (basic rays) holds up to three different states; these states depend on the combination of hits. Whereas the mid-ray with double the length to the side-Rays, creates temporary rays on each

Figure 6. VG is the direction to the goal; VR is side of mid-ray with θ (15°) shifting. the leader’s right vector. Whenever one of these temporary rays does not detect any obstacle, that point will be the 3.4. Avoidance temporary goal position and if detection To avoid collisions and other hindrance’s e.g. occurs, another 15° will be added. The other group of agent or obstacles; the FSM algorithm can be seen below (Algorithm 1.). [11] (Finite state machine) model is implemented. The finite-state machine is a Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

Algorithm 1. Avoids obstacles

Algorithm 2. Avoids agents.

Figure 8. In this scenario, the group will rotate and pass the obstacle to the left.

3.4.2. Avoidance of Agents Figure 9. A representation of the so called The same concept used for obstacle avoidance “right rule” to avoid collision. is applied for agents as well. With the basic rays doing the same thing for agents as it does With the addition the 45-ray collision from the for obstacles. However for agents, the Mid- right side will be avoided if the detected group Ray does cast two new temporary rays upon is moving towards the detectors with 90° detection, instead it checks for the other (Figure 10.) the detectors will slow down. group’s direction. If the other group is moving towards the group, both group will turns to the right (similar to Swedish pedestrian traffic). Then there are cases when the other group in front is moving with lower speed, the group behind will then adjust its speed to the same as the group in front. Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

at all since this project main focus was on groups of three’s behavior. But there are a few methods to solve this problem, one solution would be to use navigation mesh, a mesh where the pedestrians are allowed to walk, this does not only solve the rotation towards goal problem but also translation in 3D. A good example of a navigation mesh solution with pathfinding would be A* Pathfinding algorithm [13].

Another major issue is that in most cases you require a human crowd simulated in real time with preferably higher level of detail and an accurate realism in behavior. This obviously Figure 10. A 45° check to avoid following points out the correlation between the scenario. accuracy of realism in the behavior and the computational cost of the simulation. To satisfy both at the same time is in particular a 4. Result & Discussion challenge of major importance. The state of the art is to reduce the computational cost 4.1. In General while maintaining the high level detail of Typical approaches either resemble particle simulation. simulations where agents usually lack their own control of orientation, or as a more 4.2. Our Implementation conservative approach, having agents lack the Seeing all the related works there several psychological state, not being able to be possible approaches to be implemented, with pushed. Others as social-force models tend to the choice of different hybrid combinations. In look more like particle simulations than our work we have implemented our own realistic human motion. Cellular Automata- algorithm mainly based off of the rule-base models limit agent spatial movements and tend model. To note, is that ours differs from to fail when dealing with larger dense crowds. previous ones since we are focusing on three Therefore it is also often an excluded approach agents for each group. in computer games. Finally, rule-based Since we have built our own model and models, like the one we have implemented, algorithm for groups of three, we have had to either do not consider collision detection at all confine the ways possible for a group to act. In or malfunctions when trying to adapt to other words, our model cannot take every slowing down and waiting rules, which works exception in to account. Especially when fine, until you reach the state of having a high- seeing all the other models in related work density crowd. being far from optimal. Human usually have a great sense of direction To begin with, we added the object oriented and knows where they can walk and where not essence with each agent having a physical to walk e.g. walking along a wall before state. This comprises of attributes as position proceeding to rotate towards the goal. This is direction, speed, (size). This felt like the most where our implementation fails, it only moves intuitive approach doing it all from scratch. To in two and it always tries to rotate have a macroscopic approach like a fluid one towards the goal. We simply did not focus on for only three agents each felt excessive. Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

Instead, we let the actual leader be a leader, Instead, we went with the scalar product having the other two agents following him and approach (as can be read on 3.3.2 Rotation of his lead. The leader alongside the implemented Group) algorithm is in other words the one calling the shots. The attached FSM and the leader then keep track of the current formation, and eventual upcoming changes. Our approach can in a sense be seen as a hybrid, not fully have separated agents walking on their own, but nor having the leader fully calling the shots with just two agents attached.

When walking in groups of three, the most Figure 11. Analytic solution to 3.3.2 common formation to form is on a straight horizontal line. Therefore we have made the In some cases, like the one discussed by Akker line formation our standard one. For most et al. 2010 [12], some approaches can be seen parts the person in the middle might be placed as NP-hard when dealing with a dynamic a step behind the two others, making it multi-commodity flow problem. This means possible to create eye contact with each and we have to find better solutions for the crowd everyone in the group. In our case, we have simulation problem, where Moore’s Law can named the middle agent “the leader”, and only help us so much. therefor do not place him behind the other, will instead, seeing it as the others following A thing to notice in crowd simulations is often the leader. This becomes much more clear in a common way for groups to be repetitive. the straight formation were the leader leads the Like in our algorithm, we simplified the group. execution for our group when interfering with other groups of agents. When facing a total When deciding on the different formations, the collision our group always turns to the right to simpler and more natural ones come to mind. find an opening. This recurrent behavior can When facing obstacles such as agents or be seen in other approaches as well, especially blocks, what is the most realistic move to the rule-based ones. Adding a stochastic factor make? Walking on a horizontal line, as well as in situations like total collision gives the having someone move to the back when model a more human-like behavior. The interfering with something on either side, possibility to check on side of the obstacle makes the most sense. Also, the group should would also work, thinking that there is an be able to pass a narrow road, with the only optimal way for humans to walk, but what possibility to form a straight vertical line. This happens if both sides are equally long? gives us the few most useful formations. 4.3. Final Scenarios To solely walk in a straight line forward does The final obtained result proved to be better not take much, but when initially than expected, with relatively high polygon implementing the rotation we tried to solve it 3D characters and multiple groups checking analytically as can be seen in Figure 11. The for avoidance simultaneously. The simulation problem is that the angle is very small for one did not lag and did run quite well regardless frame which makes it almost negligible. We tried to compensate for the small magnitude of of the number of entities. the angle by adding a scalar factor, but zero times a million is still zero. We are also always one step behind with this approach. Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

conditions could be implemented to solve all problems that may occur. Needless to say, theory and practice does not always reflect each other. Instead, the focus lies on trying to optimize the algorithm, only including the most crucial checks allowing the majority of scenarios to be handled in a realistic way. What is not to be forgotten is that simulations are meant to be a cheaper alternative to reality, Figure 12. Final scene, avoiding obstacles. making its realism the key component. The algorithm did works well in scenarios with low density, it avoids both obstacle and 6. References agents by changing formations (Figure 12), [1] Rojas F., Tarnogol F., Yang H.S.: Dynamic adjusting speed or changing directions. social formations of pedestrian groups However when the density increases by a third navigating and using public transportation in a errors starts to occur as can be seen in Figure virtual city. KAIST A.I. & Media Lab., 13, with an agent walking into the wall. Daejeon, Republic of Korea. PsyTech LLC, Buenos Aires, Argentina (2015)

[2] REYNOLDS C.: Flocks, herds, and schools: A distributed behavior model. In Proceedings of ACM SIGGRAPH 87, Annual Conference Series. (1987)

[3] Narain R., Golas A., Curtis S., Lin M.C.: Aggregate Dynamics for Dense Crowd Figure 13. An agent stuck in the wall marked Simulation. Novel, UNC, North Carolina, with red. United States (2009)

[4] Saad A., Nishino K., Manocha D., Shah 5. Conclusion M.: Modeling, Simulation and Visual Analysis In general, which we have seen throughout the of Crowds: A Multidisciplinary Perspective, project (and which we have also discussed in chapter 3, p. 44 (2013) the “Result & Discussion”-section), are that [5] Tasse F. P.: Crowd simulation of some models and approaches are better than pedestrians in a virtual city, Bachelor’s thesis, others at doing different things. When wishing RU, South Africa (2008) for high resolution you might not be able to have as many agents or when having a huge [6] Brogan D., Hodgins J.: Group Behaviours number of agents it might not be possible for for Systems with Significant Dynamics. them to act like individuals. If you decide Autonomous Robots, 4. p. 137-153. (1997) upon either having the highest possible resolution for a single agent or having as many [7] Ristic R., Berglund J.: High-density Real- moderate agents as possible, a suited approach time Virtual Crowds via Unilaterally can be given to you, but having them both is Incompressible Fluid Simulation. Degree what we would call the state of the art. Project, KTH, Stockholm, Sweden (2015)

For future work regarding our current rule- based model, in theory unlimited checks and Bachelor’s Project in simulation and virtual design KTH Royal Institute of Technology

[8] Saiwaki N., Komatsu T., Yoshida T., and Nishida S.: Automatic generation of moving crowd using chaos model. Proceedings of the IEEE International Conference on System, Man and Cybernetics (1997)

[9] Ashley D.: Dynamic 2D Imposters: A Simple, Efficient DirectX 9 Implementation. Online article (2006)

[11] Harmeet Singha, Robyn Arterb, Louise Doddb, Paul Langstonb, Edward Lesterb, John Druryc.: Modelling Subgroup Behaviour in Crowd Dynamics DEM Simulation: Applied Mathematical Modelling, VOL 33 NO.12, p. 4408–4423. (2009)

[11] Saad A., Nishino K., Manocha D., Shah M.: Modeling, Simulation and Visual Analysis of Crowds: A Multidisciplinary Perspective, chapter 8, 187 (2013)

[12] Akker M., Gerearts R., Hoogeveen H., Prins C.: Path Planning for Groups using Column Generation, Technical Report UU- CS-2010-019, Utrecht University, Utrecht, The Netherlands (2010)

[13] Cui X., Shi H.: A*-based Pathfinding in Modern Computer Games. International Journal of Computer Science and Network Security, VOL.11 No.1 (2011)

7. Acknowledgements We would especially like to thank our supervisor Christopher Peters who has been with us throughout this project, scheduling meetings and giving us useful guidance. Other thanks go to our fellow students Staffan Sandberg and Martin Funkquist who have been collaborating with us during the project.