1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <iostream> #include <iomanip> #include <thread> #include <chrono> using namespace std; // Function to update aircraft position void updatePosition(float& x, float& y, float throttle, float direction) { x += throttle * cos(direction); // Update x based on throttle and direction y += throttle * sin(direction); // Update y based on throttle and direction } int main() { float x = 0.0f; // Initial x position float y = 0.0f; // Initial y position float throttle = 0.0f; // Throttle (speed) float direction = 0.0f; // Direction in radians char command; cout << "Aircraft Flight Simulation" << endl; cout << "Use 'w' to increase throttle, 's' to decrease throttle" << endl; cout << "'a' to turn left, 'd' to turn right, and 'q' to quit" << endl; while (true) { cout << fixed << setprecision(2); // Set precision for position output cout << "Position: (" << x << ", " << y << ") | Throttle: " << throttle << " | Direction: " << direction << " radians" << endl; cout << "Enter command: "; cin >> command; switch (command) { case 'w': // Increase throttle throttle += 0.1f; break; case 's': // Decrease throttle throttle = max(0.0f, throttle - 0.1f); // Throttle can't be negative break; case 'a': // Turn left direction -= 0.1f; // Turn left by decreasing direction break; case 'd': // Turn right direction += 0.1f; // Turn right by increasing direction break; case 'q': // Quit simulation cout << "Quitting simulation..." << endl; return 0; default: cout << "Invalid command. Please try again." << endl; break; } // Update position based on throttle and direction updatePosition(x, y, throttle, direction); // Sleep to simulate real-time movement this_thread::sleep_for(chrono::milliseconds(100)); } return 0; } |
Explanation
- Headers:
<iostream>
: For input and output operations.<iomanip>
: For formatting the output precision.<thread>
: For usingthis_thread::sleep_for()
to simulate real-time movement.<chrono>
: For time duration used insleep_for()
.
Advertisement - Function Definitions:
void updatePosition(float& x, float& y, float throttle, float direction)
: Updates the position of the aircraft based on the throttle and direction.- Parameters:
x
: The current x position of the aircraft.y
: The current y position of the aircraft.throttle
: The current speed of the aircraft.direction
: The direction in radians.
Advertisement - Uses trigonometric functions (
cos
andsin
) to calculate the new position based on the throttle and direction.
- Parameters:
- Main Function:
- Initialization:
- Initializes the aircraft’s position (
x
andy
), throttle, and direction.
- Initializes the aircraft’s position (
- User Commands:
'w'
: Increases the throttle.'s'
: Decreases the throttle, ensuring it doesn’t go below zero.'a'
: Turns the aircraft left by decreasing the direction angle.'d'
: Turns the aircraft right by increasing the direction angle.'q'
: Quits the simulation.
- Position Update:
- Calls
updatePosition()
to update the aircraft’s position based on current throttle and direction.
- Calls
- Real-time Simulation:
this_thread::sleep_for(chrono::milliseconds(100));
: Adds a delay to simulate real-time movement and control.
- Initialization:
Notes:
- Real-Time Simulation: The
sleep_for()
function introduces a short delay to mimic the real-time nature of flight control. - Direction: The direction is in radians; you might want to convert to degrees for easier understanding in more complex simulations.
- Throttle and Direction Control: The program provides basic controls to change the throttle and direction, simulating movement on a 2D plane.
Advertisement