C/C++ How to Simulate Projectile Motion in C++?

AI Thread Summary
The discussion revolves around a user seeking assistance with a C++ assignment focused on simulating the height of a projectile launched at an angle. The program requires calculating the projectile's position over time based on initial velocity and launch angle, using physics equations that account for gravitational force. Key tasks include updating the projectile's position and velocity in small time intervals, displaying results in a table format or as a character plot, and ensuring user input is within specified ranges for velocity and angle. The conversation emphasizes the need for a structured approach to coding, including the use of libraries like iomanip for formatting output. Sample code is provided to illustrate formatting techniques, and there are hints on how to manage the projectile's position in a two-dimensional array for display purposes. Overall, the discussion highlights the importance of understanding the underlying physics and programming logic to successfully complete the assignment.
riza
Messages
1
Reaction score
0
hi
I am having trouble doing this assignment. I have very little knowledge of c++ and the truth is i don't even know where to begin. Can someone please help me with it. the program I am supposed to be writing is as follows:

compute the height of a projectile that was launched.
You will recall the tutorial problem in which you computed the height of a projectile
that was launched “straight up”. In this case the height after t seconds is given by:
2
2
s u t 1 gt o = −
Where uo is the launch velocity in m/s and the gravitational constant g is 9.8 m/s2.
In the simulation of the motion of the projectile the movement is monitored in very
short time intervals Δt. In a short time interval the velocity v is almost constant and
the distance the projectile moves can be computed as Δs = v Δt. The position can then
be updated by s = s + v * Δt ;
The velocity changes constantly, it is reduced by the gravitational force. In a short
time interval, Δv = -g Δt, and the velocity must be updated as v = v – g * Δt ;
In the new iteration the new velocity is used to update the distance.
However, most projectiles are not shot upright but at an angle to the horizontal. If the
starting velocity has magnitude v and the starting angle of α, then the velocity is a
vector with components vx = v cos α, vy = v sin α.
In the x-direction the velocity does not change. In the y-direction the gravitational
force has an effect. These equations presume there is no air resistance.
Write a program that will perform the simulation of a projectile that has been
launched with an initial velocity and angle. The user is to enter the initial velocity between (between 0 & 100 m/s) and launch angle (between 50 and 850).
The program will present the results in two formats. The user should be able to choose
which format to view.
1. Print the results in a table format on the screen, showing the height (y position)
and range (x position) at each time interval. Update the position every 100
times/second, but print out the position only every full second. Repeat until the
projectile has reached the ground level.
2. Plot the position of the projectile on the screen.
- Use a character display, similar to the sine wave example given earlier.
- The display should plot the x-axis of the projectile on the horizontal
plane of the screen and y-axis on the vertical plane of the screen.
- Hint: You could store the projectile position in a 2 dimensional array
that can represent the projectile positions on the screen. The contents
of the array can then be printed to the screen.
Deleted: ,
Deleted: and the time interval
Deleted: Use the time interval to
u

the reason I am posting the whole thing is beause i got no idea where to even start. thanx
 
Technology news on Phys.org
Start off with a basic outline.

What do you want the user to tell you:

Given the user information, what tools/formulas are at your disposal:

How will you get to the final answer:

For formating your table a useful function is called setw, and it takes an integer parameter.
cout << set(15);
would output 15 white spaces between

You will have to include the library iomanip
#include <iomanip>

Here is a sample code that uses whitespace (it uses Euler's approximation to solve a population ODE):
// file: island.cxx
// author: Andrew Holmgren
// This program uses quantitative techniques to find the populations of foxes
// and geese living together in an environment. The program outputs the
// population numbers every 10 years.

//includes that provide 1) cin & cout 2)EXIT_SUCCESS = 0 and 3)setw()
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

//constants for the program
const double DEATH_RATE = 0.1;
const double CONVERSION_RATE = 0.00001;
const double GROWTH_RATE = 0.4;
const double FOX_ABILITY = 0.0005;
const double CAPACITY = 30000.0;int main ()
{

//variable declarations
int fox_pop; //a variable for storing all the new fox populations
int goose_pop; //a variable for storing all the new goose populations
int temp_fox_pop; //the previous year's fox population
int temp_goose_pop; //the previous year's fox population
int year=0; //to keep track of the years, starting at the year zero

//prompt the user to tell us how many foxes and geese are currently alive
cout << "How large is the current fox population?" << endl;
cin >> fox_pop;
cout << "How large is the current goose population?" << endl;
cin >> goose_pop;

//make an initial build of the table showing the populations
cout << "\n Please wait while the populations are calculated..." << endl;
cout << "---------- Year ---------- Foxes ---------- Geese" << endl;

//here is a while loop that will calculate the fox and goose populations
while (year<=100)
{

//make sure we don't have any negative populations, and set to zero if so
if (fox_pop < 0)
{
fox_pop = 0;
}

//this is the same negative population prevention with the goose population
else if (goose_pop < 0)
{
goose_pop = 0;
}

//if the the year is a factor of ten, then output the current populations
else if ((year%10) == 0)
{
cout << setw(15) << year; //setw(x) makes whitespace with x number of spaces
cout << setw(17) << fox_pop; //have the fox_pop output take up 17 spaces
cout << setw(17) << goose_pop << endl; //print out and make new line
}

//increment the year everytime we enter the loop and calculate the populations
//for that year
++year;

//make temperary populations so that the new population calculations will
//not rewrite the old population values
temp_fox_pop = fox_pop;
temp_goose_pop = goose_pop;

//population formulas
//F_n = (1 - d + bG_(n-1))F_(n-1)
//G_n = (1 + r - rG_(n-1)/k - aF_(n-1))G_(n-1)
fox_pop = int( ((1 - DEATH_RATE) + (CONVERSION_RATE*temp_goose_pop))
*temp_fox_pop );
goose_pop = int( (1 + GROWTH_RATE - (GROWTH_RATE*temp_goose_pop/CAPACITY) -
(FOX_ABILITY*temp_fox_pop))*temp_goose_pop );

}

return EXIT_SUCCESS;
}
 
I just realized I have else-if statements for goose_pop<0 and if modulus of 10 is 10, when they should actually be if statements.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top