#include <iostream>
#include <cmath>
#include <iomanip>
const double GRAVITY = 9.81; // Acceleration due to gravity (m/s^2)
// Function to simulate projectile motion
void simulateProjectile(double velocity, double angle, double timeStep, int numSteps) {
double angleRad = angle * M_PI / 180.0; // Convert angle to radians
double initialX = 0.0; // Initial x position
double initialY = 0.0; // Initial y position
double velocityX = velocity * cos(angleRad); // Initial x velocity
double velocityY = velocity * sin(angleRad); // Initial y velocity
std::cout << "Time (s)\tX (m)\tY (m)\n";
std::cout << std::fixed << std::setprecision(2);
for (int step = 0; step <= numSteps; ++step) {
double time = step * timeStep;
double x = initialX + velocityX * time;
double y = initialY + velocityY * time - 0.5 * GRAVITY * time * time;
// Check if the projectile has hit the ground
if (y < 0) {
y = 0;
std::cout << time << "\t" << x << "\t" << y << " (Ground)\n";
break;
}
std::cout << time << "\t" << x << "\t" << y << "\n";
}
}
int main() {
double velocity, angle, timeStep;
int numSteps;
std::cout << "Enter the initial velocity (m/s): ";
std::cin >> velocity;
std::cout << "Enter the launch angle (degrees): ";
std::cin >> angle;
std::cout << "Enter the time step (s): ";
std::cin >> timeStep;
std::cout << "Enter the number of steps: ";
std::cin >> numSteps;
std::cout << "Projectile Motion Simulation\n";
simulateProjectile(velocity, angle, timeStep, numSteps);
return 0;
}