| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Gravity Simulator Problem
I cannot figure out what is going on when I run this gravity simulator. I put in the correct numbers for the variables but when I run it it doesn't do what I want to. In the while loops in int main it is supposed display the positions of object 1 and 2 update the positions as many times as the accuracy calls for then repeat it but it only updates the positions once. The next ones are all the same. I am using Dev-C++ on windows vista.
this is the code Code:
#include <iostream>
#include "required.h"
#include <math.h>
//declare needed variables
using namespace std;
double force1;
int time3;
double x1, yone, z1;
double x2, y2, z2;
double Mass1;
double Mass2;
double velocityx1;
double velocityy1;
double velocityz1;
double velocityx2;
double velocityy2;
double velocityz2;
double accuracy;
int range;
int second = 0;
//F = GMm/R²
//calculates gravity with the given parameters
double GravityCalc(double Mass1, double Mass2, double Distance){
//declare variables for order
double top;
double botom;
//G is 1.0 x10^10 times bigger
double G = .667;
double Force;
//calculate gravity
top = G * Mass1 * Mass2;
botom = Distance * Distance;
Force = top / botom;
return Force;
}
//finds the distance in 3 dimensional space between two objects
double finddistance( double x1, double yone, double z1, double x2, double y2, double z2 ){
//declare order variables
double cx, cy, cz;
double distance;
//calculate the distances
cx = x1 - x2;
cy = yone - y2;
cz = z1 - z2;
distance = pow(cx, 2) + pow(cy, 2) + pow(cz, 2);
distance = sqrt(distance);
//return distance always positive
if (distance > 0){
return distance;
}
if (distance < 0){
distance = distance * -1;
return distance;
}
}
int main(int argc, char *argv[]){
double b = 0;
double c = 0;
//receive values of variables
cout << "Enter mass of object 1: ";
cin >> Mass1;
cout << endl << "Enter mass of object 2: ";
cin >> Mass2;
cout << endl << "Enter position of object 1 x, y, and z:" << endl;
cout << "x: ";
cin >> x1;
cout << endl << "y: ";
cin >> yone;
cout << endl << "z: ";
cin >> z1;
cout << endl << "Enter position of object 2 x, y, and z:" << endl;
cout << "x: ";
cin >> x2;
cout << endl << "y: ";
cin >> y2;
cout << endl << "z: ";
cin >> z2;
cout << endl << "Enter the starting velocities of object 1:" << endl;
cout << "x: ";
cin >> velocityx1;
cout << endl << "y: ";
cin >> velocityy1;
cout << endl << "z: ";
cin >> velocityz1;
cout << endl << "Enter the starting velocities of object 2:" << endl;
cout << "x: ";
cin >> velocityx2;
cout << endl << "y: ";
cin >> velocityy2;
cout << endl << "z: ";
cin >> velocityz2;
cout << endl << "Enter the accuracy of the simulation from int between 0 and 1: ";
cin >> accuracy;
cout << endl << "Enter how long you want the sim to run in seconds: ";
cin >> range;
system("pause");
system("cls");
//update the positions according the range of the simulation and the accuracy
while ( b <= range ){
//display the positions
cout << "Object 1 position: " << endl;
cout << "x = " << x1 << " y = " << yone << " z = " << z1 << endl;
cout << " Object 2 position: " << endl;
cout << "x = " << x2 << " y = " << y2 << " z = " << z2 << endl << endl << endl;
//calculate to the given accuracy
while ( c <= 1 / accuracy ){
//positionupdate function coded in required.h
//calculate the force of the gravity between the two objects
force1 = GravityCalc( Mass1, Mass2, finddistance(x1, yone, z1, x2, y2, z2) );
//update positions of x
x1 = x1 + positionupdate(accuracy, force1, vectorx( x1, yone, z1, x2, y2, z2, 1));
x2 = x2 + positionupdate(accuracy, force1, vectorx( x1, yone, z1, x2, y2, z2, 2));
//update positions of y
yone = yone + positionupdate(accuracy, force1, vectory( x1, yone, z1, x2, y2, z2, 1));
y2 = y2 + positionupdate(accuracy, force1, vectory( x1, yone, z1, x2, y2, z2, 2));
//update positons of z
z1 = z1 + positionupdate(accuracy, force1, vectorz( x1, yone, z1, x2, y2, z2, 1));
z2 = z2 + positionupdate(accuracy, force1, vectorz( x1, yone, z1, x2, y2, z2, 2));
//move loop forward
c = c + 1;
}
//move loop forwards
b = b + 1;
}
system("pause");
return EXIT_SUCCESS;
}
Last edited by noey699; October 15th, 2009 at 06:22 PM. |
|
#2
|
|||
|
|||
|
Re: Gravity Simulator Problem
Maybe the c variable needs to be initialized between the first and the second while line.
|
|
#3
|
||||
|
||||
|
Re: Gravity Simulator Problem
why are these global???
Code:
double force1; int time3; double x1, yone, z1; double x2, y2, z2; double Mass1; double Mass2; double velocityx1; double velocityy1; double velocityz1; double velocityx2; double velocityy2; double velocityz2; double accuracy; int range; int second = 0;
__________________
Google is your friend. |
|
#4
|
||||
|
||||
|
Re: Gravity Simulator Problem
Quote:
__________________
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf Premature optimization is the root of all evil --Donald E. Knuth Computer programming requires a great deal of attention to detail and an ability to read and understand abstract instructions. Posting a question to this forum in a way that it will get answered, requires the same, though too far smaller degree. Thus, if you don't get your question answered here, maybe it's time to accept that programming is not your thing. --Yours truly Please read Information on posting before posting, especially the info on using [code] tags. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|