Swarm Battle

This project is about an air defence scenario with swarm robots. Two teams attack each other with rockets and their swarm robots try to intercept rockets. This project has written in C++. In the project, Box2D is used as the physics engine and SFML is used for displaying entities. Github repostitory is here.

The scenario of this simulation includes Swarm robots try to intercept dangerous rockets incoming by estimating their trajectories. Boids algorithm has been extended in this work. Here is an example;

interception example

In this simulation we have classes like;

  • Object: All entities in the simulation except SensorContactListener and Engine derives from Object class. Object class implements simple box2d and sfml implementations. And with inheritence other classes use these constructors and easily wrap sfml and box2d.
  • DynamicObject: They derive from Object class and their bodies are implemented as dynamic objects in box2d.
  • StaticObject: They derive from Object class and their bodies are implemented as static objects in box2d.
  • Base: Base derives from StaticObject class fires Rockets and has a certain hp(default 1000). It’s box2d and sfml shapes and rocket firing functions are implemented.
  • Particle: Particle class derives from Dynamic Object and they are produced when a certain entity dies. It creates an explosion effect and applies external forces to nearby dynamic entities too.
  • Robot: Robots are derived from Dynamic Object and they have 500 hp and 100 fuel. Their patrol area is between 220-330 meters. They have to sensor fixtures implemented with box2d. first one senses nearby robots and the second one senses incoming robots. In this class, Interception, Swarm, Rocket chasing algorithms are applied using forces with box2d physics engine.
  • Rocket: Rockets are Dynamic Objects which are fired from bases and they deal 100 damage on impact to entities and explode. Rocket class also has a function called handleDrag() which introduces drag forces on it to make its move more realistic.
  • Engine: Engine is the class which opens up main SFML window and runs the simulation in a loop. Every entities act() function is called each timeStep. Engine also reads mainSettings.config file for initial configurations like window size or scale. It also handles dead entities at the end of each time step for memory efficiency.
  • ObjectFactory: Object factory class is implemented for producing simulation entities faster and easier.
  • SensorContactListener: This class is derived from b2ContactListener class in box2d library. Main purpose of this class is dealing with user defined collisions and fire up events. At the simulation, every damage dealing and sensor sensing operations are handled in this class.

Implementation Details

During this project, I was working on an air defence simulation with swarm robots. According to the scenario, there are two teams(blue and red) which try to destroy eachother’s bases by sending rockets. Team 1(blue) has a robot swarm which patrols nearby its base and protects it from incoming rockets by intercepting them which has high potential threat. Robots try to mimic the behavior of birds.

I tried to make physical movements as realistic as possible by making physical calculations on every move. For that, BoX2D became very useful.

During my implementation I found Craig Reynold’s paper(1987) which introduces boids algorithm for my swarm robot implementation. That study explains aggregate motion of flock birds or school of fish. Boids algorithm simulates flocking behavior of birds. In this algorithm, velocities of swarm members depend on various rules. Simples rules are separation for avoiding local flockmates, alignment for steering towards the average heading of local flockmates and cohesion for moving towards the average position of local flockmates.

I implemented Robot, Rocket and Base as my entities in this study. And then, I implemented behavior of robots. Robots have bunch of choices during simulation, they can move as a swarm, chase a robot which has dangerous estimated trajectory, or refuel if they have little fuel left.

Robots are hexagonal shapes and their center of mass is at the middle of the shape. During the simulation, gravity is being applied to robots as a constant external force. At the same time, if robots collide with rockets from enemy team or particles coming from exploded entities, they also result as an external force on robots. In my implementation, Robots only collide with enemy Rockets and Particles. External forces are likely to disturb the balance of robots, causing them spin because of the torque created by external forces. In each
time step of the simulation, robots execute orientationControl() and gravityNeutralizer steps in order to ensure balance during operations.

Robots have two sensors called rocketSensor and rocketBSensor. rocketSensor is a circle shaped sensor with its center is aligned with robot’s center of mass which senses friendly robots and saves their their position and velocities. This information is later used for swarm behavior with boids algorithm. rocketBSensor is a bigger version of rocketSensor and it senses up to 700m distance for incoming rockets. When this sensor sees an incoming rocket, it saves its velocity. This velocity is used by the robot to estimate its trajectory.

I added 5 states to robot which are Patrolling,Steady,Refueling,Chasing and Returning. In P atrolling state, robot patrols nearby its base and tries to group up with robots it sees and forms a swarm. In Steady state, robots stand still, in Refueling state, robots go to base and refuel, in Chasing state, robot chases rocket it decided to intercept after various calculations, and in Returning state, robots return back to base after successful interception.

When robotBSensor senses rockets and checks their trajectories. After implementing robots, I implemented rocket shape, Rockets are fired from guns mounted on top of bases. Base shape is shown below.

Conclusion

At the beginning of my work, I learned the basics of SFML and Box2D physics engine. With entity and factory classes I implemented, I merged SFML and Box2D shapes so this project can also act as a SFML,Box2D wrap library for simplicity. I started implementing the shapes and base classes for my implementation, in the middle of the term, I managed to implement pure boids algorithm to my robots but my project was lacking complete air defence scenario. After that, I started extending the boids algorithm with air defence specific goals and completed. In my github repository, I also added an interception example
taken from my simulation. In the future, I can turn this project into a game and add new ai features like swarm attack etc. Finally, on youtube(here is the link), I posted a demonstration of my project working. In the video it is seen that robots determine dangerous rockets and try to intercept them. If there are more than 3 robots are chasing a rocket, then others do not try to chase it. And robots also do not chase the
robots which won’t hit the base.

Leave a comment

Design a site like this with WordPress.com
Get started