Archived

This topic is now archived and is closed to further replies.

Elfs1der

Help with my physics project!

Recommended Posts

I''m using C++ to make a program for my physics class project. What it does is you enter in the time and angle of the trajectory of a spring that is going to be launched and it will take that information and find the muzzle velocity, then use that and split it into components to find the height and distance of the spring and where its gonna go, here is the source but i think the equations are wrong.
/*

Pragram: Physics project

*/

#include <iostream>
#include <cmath>
using namespace std;

//// CONSTANT VARS

const double pi = 3.14159;
const double gravity = 9.8;

//// VARIABLES

float theta = 1;
float time = 1;
float v1;
float dx;
float dy;

bool done = false;

//// FUNCTION PROTOTYPES

float		calcMuzzleVel(const float f_Gravity, const float f_Time);
float		calcDistance(const float f_V1x, const float f_Time, const float f_Theta);
float		calcMaxHeight(const float f_V1y, const float f_Time, const float f_Gravity, const float f_Theta);

void main()
{
	while(done == false)
	{
		cout << "To exit type a value < 0" << endl << endl;
		cout << "Please enter in theta: ";
		cin >> theta;

		if(theta < 0)
		{
			done = true;
			break;
		}// end if


		cout << "Please enter in the time: ";
		cin >> time;
		
		if(time < 0)
		{
			done = true;
			break;
		}// end if


		// Do calculations here

		v1 = calcMuzzleVel(gravity, time);
		dx = calcDistance(v1, time, theta);
		dy = calcMaxHeight(v1, time, gravity, theta);

		cout << "The Muzzle velocity is: " << v1 << "m/s" << endl;
		cout << "The distance is: " << dx << "m" << endl;
		cout << "The maximum height is: " << dy << "m" << endl;
	}// end while

}

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//


float calcMuzzleVel(const float f_Gravity, const float f_Time)
{
	float f_V1;

	f_V1 = (f_Gravity / 2) * f_Time;
	return f_V1;
}

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//


float calcDistance(const float f_V1x, const float f_Time, const float f_Theta)
{
	float f_Dx;

	f_Dx = (f_V1x * cos(f_Theta * (pi / 180))) * f_Time;
	return f_Dx;
}

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//


float calcMaxHeight(const float f_V1y, const float f_Time, const float f_Gravity, const float f_Theta)
{
	float f_Dy;

	f_Dy = ((f_V1y * sin(f_Theta * (pi / 180))) * f_Time) + ((f_Gravity / 2) * (f_Time * f_Time));
	return f_Dy;
}



I''m pretty sure that i got the conversions from degrees to radians right, but if i enter in an angle of 0 degrees it says it has a distance but it should be 0, can someone PLEASE help me out, this is due on friday too hehe..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if the angle with the ground is zero, the trajectory will still have a distance, it just won''t have a height. you''re just shooting something straight across a surface.

Share this post


Link to post
Share on other sites
wish my physics class let me do a C++ program for my homework... well when i was in school that is.



RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

Share this post


Link to post
Share on other sites
I did something really close to that for Physics too, but mine only found solutions for simple kinematics... whatever.

We did the same experiment in class, and I'm not sure you have the right equation for finding the muzzle velocity (U)..?


X: Y:
Ax=0 Ay=-9.8
Sx= Sy=0
Vx= Vy=
Ux= Uy=
T=T

Therefore:

Uy = (Sy - .5 * Ay * T^2)/T

Vy = Uy + Ay * T

= (Sy - .5 * Ay * T^2)/T + Ay * T

Then we can use trig to find the initial (muzzle) velocity on the x-axis:

Ux = Uy / tan Theta

And the muzzle velocity = sqr(Ux^2 + Uy^2) (pythagorean)

I think your other equations are right though.

-------



[edited by - Mushu on October 15, 2003 12:44:30 AM]

Share this post


Link to post
Share on other sites