Thread: physics class returning weird numbers

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    physics class returning weird numbers

    I have been trying to make a physics class for to test on an gui, but when I get the output, it's not a # persay. I'm not even sure if the physics are right, and I will be looking into it, but I'm posting here to find out why i didn't get a #. If you see any problems with the physics part of it that would be greatly appreciated too.

    Here is my code
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    vector <float> force;
    
    
    class physics
    {
        public:
        physics(double x, double y, double h, double w, double top_speed, double density, double friction_level);
    
        void apply_force(vector <float> force);
        private:
        vector <double> main_vector;
    };
    physics::physics(double x, double y, double h, double w, double top_speed, double density, double friction_level)
    {
        //set up main vector
        main_vector.push_back(x);
        main_vector.push_back(y);
        main_vector.push_back(h);
        main_vector.push_back(w);
        main_vector.push_back(top_speed);
        main_vector.push_back(density);
        main_vector.push_back(friction_level);
    
        // find volume and mass
        // first we will find volume = h * w
        main_vector.push_back(main_vector[2] * main_vector[3]);
    
        // now lets find mass = V * D
        main_vector.push_back(main_vector[6] * main_vector[8]);
    
        // now we are going to find the gravity of the object using force = mass * 9.8(earth grav)
        main_vector.push_back(main_vector[9] * 9.8);
    
        // find friction
        main_vector.push_back(main_vector[6] * main_vector[10]);
        //lets make some room to put acceleration in there! remember we need one for x and y
        main_vector.push_back(0);
        main_vector.push_back(0);
    
    }
    
    void physics::apply_force(vector <float> force)
    {
        // acceleration = force/ mass    * for both x and y
        // also not we subtract friction from it
        if(force[0]/main_vector[8] > 0)
        {
            main_vector[12] = (force[0] - main_vector[11] )/main_vector[8] ;
        }
        if(force[0]/main_vector[8] <= 0)
        {
            main_vector[12] = (force[0] - main_vector[11]) /main_vector[8] ;
        }
    
        if(force[1]/main_vector[8] > 0)
        {
            main_vector[13] = (force[1] - main_vector[11]) /main_vector[8];
        }
    
        if(force[1]/main_vector[8] <= 0)
        {
            main_vector[13] = (force[1] - main_vector[11]) /main_vector[8];
        }
    
        main_vector[0] += main_vector[12];
        main_vector[1] += main_vector[13];
        cout<< main_vector[0] << ", " << main_vector[1] << "\n";
    }
    
    
    
    
    
    
    
    
    int main()
    {
        force.push_back(10);
        force.push_back(30);
        physics car(5, 5, 25, 25, 20, 40, 0.5);
        car.apply_force(force);
    
    	return 0;
    }
    and my output is:
    1.#INF, 1.#INF


    Thanks for any help,

    Joe

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you dividing by zero?

    [edit]Such that counting from zero you maybe ought to be doing this?
    Code:
        // now lets find mass = V * D
        main_vector.push_back(main_vector[5] * main_vector[7]);  // 8
    (For starters.)

    This use of a vector seems dubious and confusing.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM