
Aircraft Fighter Simulation in C++ – Simulation Example – Bomber vs Fighter
Aircraft Fighter Simulation is a common problem in computer simulation. In this problem we have two entity, one is the bomber and another is the fighter. We need to calculate whether the fighter can catch the bomber.
Problem Statement
There are two entities, the Bomber and the Fighter who are flying in a 2D space with two different jet. The bomber moves randomly to get away from the fighter. On the other case the fighter try to catch the bomber. In the whole operation both moves only in 2D space.
The Fighter will start from (0,0) coordinate. There are given values for position of Bomber (XB, YB) as a function of time. Let’s consider the values in the below table.
Time | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
XB | 80 | 90 | 99 | 108 | 116 | 125 | 133 | 141 | 151 | 160 | 169 | 179 | 180 |
YB | 0 | -15 | -30 | -43 | -58 | -71 | -87 | -92 | -113 | -98 | -81 | -69 | -54 |
Let’s keep all of these value in a text file named ” AircraftSimulationInput.txt”. We will take input the values from this file.
See the above values, those values are not sequential. So, the path of the bomber is not linear. If the path of the bomber would be linear we would apply a linear programming equation to find the path of the fighter.
But in this case the fighter have to move toward the bomber at each point. So, we need to simulate the path at each point.
Moreover, to clearly understand the simulation we will print how the bomber and fighter is moving with a graphics window.
Aircraft Fighter Simulation C++ Code
The above problem can be solved easily if you proceed step by step. In the below program I have solved this problem in very easy method. See the explanation below to understand the program.
#include<bits/stdc++.h> #include<graphics.h> using namespace std; int main(){ int gd = DETECT, gm; initgraph(&gd, &gm, ""); double XZero, YZero, XMin, YMin, XMax, YMax; XZero=150, YZero=200; XMin=0, YMin=0; XMax=800, YMax=600; line(XMin,YZero,XMax,YZero); //X Axis line(XZero,YMin,XZero,YMax); //Y Axis int VF, T; cout<<"Speed of Fighter: "; cin>>VF; delay(3000); int Time[100], XB[100], YB[100]; int XF[100], YF[100]; ifstream myfile; myfile.open("AircraftSimulationInput.txt"); int num, i; string s; i=0; getline(myfile, s); stringstream SSTime(s); while(SSTime >> num){ Time[i]=num; i++; } i=0; getline(myfile, s); stringstream SSXB(s); while(SSXB >> num){ XB[Time[i]]=num; i++; } i=0; getline(myfile, s); stringstream SSYB(s); while(SSYB >> num){ YB[Time[i]]=num; i++; } int Distance, XDistance, YDistance; T = i-1; XF[0]=0, YF[0]=0; for(i=0;i<T;i++){ putpixel( XB[Time[i]]+XZero , YZero-YB[Time[i]], RED ); setfillstyle(SOLID_FILL, RED); circle(XB[Time[i]]+XZero , YZero-YB[Time[i]], 3); floodfill(XB[Time[i]]+XZero , YZero-YB[Time[i]], WHITE); putpixel( XF[Time[i]]+XZero , YZero-YF[Time[i]], BLUE ); setfillstyle(SOLID_FILL, BLUE); circle(XF[Time[i]]+XZero , YZero-YF[Time[i]], 3); floodfill(XF[Time[i]]+XZero , YZero-YF[Time[i]], WHITE); delay(500); XDistance = XB[Time[i]]-XF[Time[i]]; YDistance = YB[Time[i]]-YF[Time[i]]; Distance = sqrt(abs(XDistance*XDistance+YDistance*YDistance)); XF[Time[i+1]] = XF[Time[i]] + VF*XDistance/Distance; YF[Time[i+1]] = YF[Time[i]] + VF*YDistance/Distance; if(Distance<=VF){ cout<<"Hurrah! The Bomber is Caught!"<<endl; setfillstyle(SOLID_FILL, BLUE); circle(XB[Time[i]]+XZero , YZero-YB[Time[i]], 5); floodfill(XB[Time[i]]+XZero , YZero-YB[Time[i]], WHITE); goto End; } } cout<<"Oops! Fighter is Failed to Catch Bomber!"<<endl; End: getch(); closegraph(); return 0; }
AircraftSimulationInput.txt
0 1 2 3 4 5 6 7 8 9 10 11 12
80 90 99 108 116 125 133 141 151 160 169 179 180
0 -15 -30 -43 -58 -71 -87 -92 -113 -98 -81 -69 -54
Sample Input:
Speed of Fighter: 20
Sample Output:
Hurrah! The Bomber is Caught!

Explanation of the Problem and Code
This problem is a bitter long because of some requirement. So, please read carefully to understand the codes below.
Step-1: Arranging the Basic Code
We are going to write a C++ program. So, we need to declare some header file for required function. We need a main function to run program. We need to perform some standard C++ operations. So, we have to declare the using of namespace std. The basic part of the code is the below part.
#include<bits/stdc++.h> #include<graphics.h> using namespace std; int main(){ ......... return 0; }
Step-2: Declaring and Closing Graph Elements
As we are going to display something on graphics window so we need to initiate the graphics window. To initiate the graphics we will write these two lines at starting of main program.
int gd = DETECT, gm; initgraph(&gd, &gm, "");
Here, gd and gm are nothing but two integer variables. But we always use these two variables here. So, they must have a specific meaning.
What is the Full Meaning of GD in C++ Graphics in C++?
GD have the both meaning of the below two:
- GD – Graphics Driver
- GD – Graphics Draw
What is the Full Meaning of GM in Graphics in C++?
GM stands for these two abbreviations below:
- GM – Graphics Module
- GM – Graphics Manager
Whatever, as we have started the graphics we need to end it. Otherwise our program won’t be complete. To close the graphics we need to use the below line at the bottom of main function.
closegraph();
But there is a problem of putting this line only. That is, when your graphics drawing is complete it will close the graphics window without any delay. So, you cannot view or analysis the result properly. To keep this window on let depend the window on a character from console. Use the command below to end with a character.
getch();
We have to write this line just before of closing graph. So, the graphics window will remain as long as we don’t press any key or don’t close it manually.
Step-3: Drawing X Axis and Y Axis in Graph
As, we are going to represent some two dimensional points so we need to consider two axis on the plane. Without axis the values are hard to understand.
One more problem is in C++ graphics the (0,0) point in the graphics window is considered at top-left corner of the screen. So, if you want to put some negative value of X or some positive value of Y they will remain out of the window. So, we need to fix the (0,0) again.
Let’s consider our X axis will start at XMin and end at XMax. Accordingly, Y axis will strat at YMin and end at YMax.
Also, consider the new (0,0) is (XZero, YZero).
So, we have to declare those variables and write the code below to draw the axis.
double XZero, YZero, XMin, YMin, XMax, YMax; XZero=150, YZero=200; XMin=0, YMin=0; XMax=800, YMax=600; line(XMin,YZero,XMax,YZero); //X Axis line(XZero,YMin,XZero,YMax); //Y Axis
So, we found a new coordinate system where (0,0) is located at (XZero, YZero).
Step-4: Take Input the Velocity of Fighter
The velocity is a constant value which means the number of units the fighter will proceed towards the bomber at each time unit. As, the fighter is starting far from the bomber so, the more the speed is the fighter can catch the bomber more quickly. If the speed of the fighter is too low, then the fighter won’t be able to catch the bomber. We will take input velocity of fighter in a variable named VF. Here T is a variable to count the maximum time range which will be used later.
int VF, T; cout<<"Speed of Fighter: "; cin>>VF; delay(3000);
Here, we have used an optional delay so that the graphics simulation starts a bit later. Because, by the meantime we have to switch between console to graphics window.
Step-5: Preparing the File for Input
As, we have the inputs in a file so, we need to open the file and take input from file. In this case we have to perform some file operations. Firstly we need to declare required variables and open the file. In this section I have did this.
int Time[100], XB[100], YB[100]; int XF[100], YF[100]; ifstream myfile; myfile.open("AircraftSimulationInput.txt"); int num, i; string s;
Here, (XB, YB) is the coordinate of Bomber and (XF, YF) is the coordinate of Fighter.
Step-6: Processing File Input
In the file there are three lines for Time, XB and YB respectively. So, we need to take input each line and process the line to keep the values in the respective array.
Let’s start with Time,
i=0; getline(myfile, s); stringstream SSTime(s); while(SSTime >> num){ Time[i]=num; i++; }
Here, i is the incrementor which can be from 0 to length of line – 1.
The getline function extracted a line from the file and String Stream converted it to a stream of variables.
With the while loop we have extracted all vales from the stream with num and put them into Time array.
After reading Time the pointer is at starting of next line. So, when we will run the code below it will get the X coordinate of bomber to the array XB.
i=0; getline(myfile, s); stringstream SSXB(s); while(SSXB >> num){ XB[Time[i]]=num; i++; }
Accordingly, we will extract YB with the code below.
i=0; getline(myfile, s); stringstream SSYB(s); while(SSYB >> num){ YB[Time[i]]=num; i++; }
Step-7: Preparing the Fighter to Proceed
As we have took all input and processed them into array so, we should make the fighter ready now. So, we need to declare those variable and assign values as below:
int Distance, XDistance, YDistance; T = i-1; XF[0]=0, YF[0]=0;
Here, XDistance is the distance of X coordinate of bomber and fighter at any time. YDistance is the distance of Y coordinate of bomber at any time. And Distance is the direct distance (through the tangent).
T is the maximum time found in time array.
(XF[0], YF[0}) is the initial position of the fighter.
After this we have to perform everything in the loop.
Step-8: Calculating the Position of Fighter
In each time unit the fighter will proceed VF unit towards the bomber. So, we need to find the direction from the fighter to bomber.
This direction is known as tangent. So, if we can find the tangent between the bomber and fighter we can derive the equation easily. To find the tangent follow the image below.

In the image the blue point refers to the position of fighter at any time and the red point refers to the position of the bomber at the same time.
So,
Distance in the X axis is b = XB – XF.
Distance in the Y axis is a = YB – YF
According to Pythagoras’s Theorem, the distance is = (a2 + b2)1/2
We have did this in the lines below.
XDistance = XB[Time[i]]-XF[Time[i]]; YDistance = YB[Time[i]]-YF[Time[i]]; Distance = sqrt(abs(XDistance*XDistance+YDistance*YDistance));
Here, we used Time of i’th index instead of i as the array index beacause the time values can be discrete like: 0, 2, 4, 6, 8 or like, 0, 3, 6, 9 etc.
Remember that, we have derived the tangent distance above, but the fighter is not proceeding the whole distance. The fighter is proceeding VF unit at a time. So, we need to find the values in Y axis (sine) and Y axis (cosine) for VF distance towards the bomber (through tangent line).
We know,
sinθ = Perpendicular/Hypotenous
cosθ = Base/Hypotenous
tanθ = Perpendicular/Base
So, in the above case,
sinθ = YDistance/Distance
cosθ = XDistance/Distance
tanθ = YDistance/XDistance
Here, we need the value of θ only. So, in case of X axis we will use cosθ as sinθ = 0 on X axis. As well as we will use sinθ in case of Y axis for same reason.
So, the fighter will move in X direction VF*XDistance/Distance
unit. And in Y direction VF*XDistance/Distance
unit.
So, the next position of the fighter can be derived by the lines below.
XF[Time[i+1]] = XF[Time[i]] + VF*XDistance/Distance; YF[Time[i+1]] = YF[Time[i]] + VF*YDistance/Distance;
Step-9: Drawing the Pixels
As soon as we have found the position of fighter for each step we can put the pixels. We have put the pixels at starting of the loop so that it prints the 0’th pixel. However it tries to omit the final point. But if the bomber is caught that puts a larger point. So, no point is omitted. If the fighter cannot catch the bomber than the final pixel can be omitted.
Below codes draw the graphics. I have used circular forms also to visualize well. You can only use the first line (pupixel) to minimize the code.
putpixel( XB[Time[i]]+XZero , YZero-YB[Time[i]], RED ); setfillstyle(SOLID_FILL, RED); circle(XB[Time[i]]+XZero , YZero-YB[Time[i]], 3); floodfill(XB[Time[i]]+XZero , YZero-YB[Time[i]], WHITE); putpixel( XF[Time[i]]+XZero , YZero-YF[Time[i]], BLUE ); setfillstyle(SOLID_FILL, BLUE); circle(XF[Time[i]]+XZero , YZero-YF[Time[i]], 3); floodfill(XF[Time[i]]+XZero , YZero-YF[Time[i]], WHITE); delay(500);
Here, first part is for drawing the location of bomber in red color. And the second part is to draw the location of fighter.
The below code can replace the above:
putpixel( XB[Time[i]]+XZero , YZero-YB[Time[i]], RED ); putpixel( XF[Time[i]]+XZero , YZero-YF[Time[i]], BLUE );
They are just matter to visualization not mandatory.
Step-10: Finding the Result
To know whether the fighter has been able to catch the bomber we need to compare whether the distance between bomber and fighter is less than VF or not at any time. If the distance is less than VF that means the bomber is caught. See the code below:
if(Distance<=VF){ cout<<"Hurrah! The Bomber is Caught!"<<endl; setfillstyle(SOLID_FILL, BLUE); circle(XB[Time[i]]+XZero , YZero-YB[Time[i]], 5); floodfill(XB[Time[i]]+XZero , YZero-YB[Time[i]], WHITE); goto End; }
If the bomber is caught we will go to a Level named End locating just before of getch.
If the bomber is not caught the loop will be end as usual. After that print the fighter is failed and end.
The below to lines are at the end section:
cout<<"Oops! Fighter is Failed to Catch Bomber!"<<endl; End:
That is the whole thing in the simulation process.
Hope you have understood everything well. If not practice by doing the code once. Best of luck.