Steering Through Chaos: The Power of Flow Fields in ‘Danger Space’
Creating a fluid and dynamic AI can be a challenging task, particularly when your game environment is as chaotic and unpredictable as in Danger Space. One of the major challenges I faced was helping my AI agents navigate around dynamic environmental obstacles smoothly and effectively. Steering behaviors provided the foundation for AI movement, but often, they needed an extra nudge, especially when they found themselves in a tight spot. This is where the implementation of vector fields, specifically, a flow field, made all the difference.
Leveling the Playing Flow Field
In Danger Space, I created a flow field - a specific type of vector field - to act as an additional steering force for the AI agents. This flow field covers the entire level and at each point, provides a vector that the AI can sample to help it steer around obstacles. These vectors are calculated based on the number and location of nearby Collider2Ds, which represent potential obstacles in the game world. Essentially, each vector in the flow field points away from potential collisions, guiding the agents towards the nearest area of open space.
Github Code Section
gh-repo="BIRD-COMMAND/danger-space-public"
gh-branch="feature/portfolio-polish"
gh-file="Assets/Scripts/Systems/FlowField.cs"
gh-start="113" gh-end="134"
Keeping an eye on performance, I designed the system to recalculate the flow field every 0.2 seconds. This frequency provided a good balance between accuracy and efficiency, offering near real-time updates without overloading the system with constant calculations.
Going With the Flow
Importantly, the flow field doesn't automatically affect the agents. The AI for each agent has the flexibility to sample the field at any position – usually their current location – and calculate the average force within a given radius. This force can then be applied as a steering force, giving the agent a helpful push in the right direction.
In this demonstration, an AI agent placed in a confined space is navigating solely by reacting to the flow field around it. Notice that as the large ball gets closer, the flow field around it warps to point away from the ball’s CircleCollider2D. The smaller agent reacts to this change and steers in the new local direction of the flow field.
Conclusion
The introduction of the flow field in Danger Space offered an elegant solution to one of the more complex problems in AI navigation – preventing agents from getting stuck in difficult areas. In fact, the flow field went beyond merely extricating agents from tight spots; it helped them avoid these challenging areas altogether.
Combined with obstacle avoidance and other steering behaviors, the flow field contributed to a robust AI framework capable of fluid, dynamic navigation in a rapidly changing environment. The end result is a rich and engaging game world, where AI agents move with a lifelike quality, skilfully navigating the dangers of space.