CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    3

    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&#178;
    //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 05:22 PM.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Gravity Simulator Problem

    Maybe the c variable needs to be initialized between the first and the second while line.

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    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. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Gravity Simulator Problem

    Quote Originally Posted by man sqrt
    DESCRIPTION
    `sqrt' computes the positive square root of the argument. You can modify error handling for this
    function with `matherr'.
    This should help you remove superfluous code
    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


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured