CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: c++ physics

  1. #1
    Join Date
    Jan 2009
    Posts
    9

    c++ physics

    hey im new here, so easy lol. um im writing a c++ projectile motion code. What i got doesnt really work, like the values are wrong, and i dont know why. Any help? And this only has 1 function so far, once i get it to work, ill finish it. Also this is to all of u physics programmers, how can i take this a step further and make it solve things like where about will a soccer ball land if given the angle and some stuff? Anyways, just were am i going wrong? Thanks.


    //Richard Chaaya
    //1-25-09
    //kinimatics

    #include <iostream>
    #include <conio.h>
    #include <iomanip>
    #include <string>

    using namespace std;

    int main()
    {
    //declare variables
    double dblVix = 0.0;
    double dblViy = 0.0;
    double dblVfx= 0.0;
    double dblVfy= 0.0;
    double dblAx= 0.0;
    double dblAy= 0.0;
    double dblTime= 0.0;
    double dblDisx= 0.0;
    double dblDisy= 0.0;
    double Disx= 0.0;
    double Disy= 0.0;
    double dblDifference1 =0.0;
    char charAsk;
    char charDisx='y';
    char charTime='y';
    char charVfx=' ';
    char charVix=' ';
    char charAx=' ';
    char charAy=' ';
    char chartime= ' ';
    char charViy= ' ';
    char chrDisx=' ';
    char charDisy= ' ';
    char chrVfx=' ';
    char chrTime=' ';
    char chrVfy= ' ';
    char chrDisy= ' ';
    //Prototypes:
    double Ax (double, double, double, double, double, char, char, char);
    double Ay (double, double, double, double, double, char, char, char);

    //enter input
    cout << "hello, and welcome. This program is useful for projectile motion, it prompts you to enter the givens and the unknowns, and it outputs the answer ";
    cout << "\n\n Is Vi in the x given ?" <<endl;
    cin >> charVix;
    if(charVix=='Y' || charVix=='y')
    {cout << "what is the Initial velocity in the x?" <<endl;
    cin >> dblVix;}
    cout << "\n\n Is Vi in the y given ?" <<endl;
    cin >> charViy;
    if(charViy=='Y' || charViy=='y')
    {cout << "what is the Initial velocity in the y?" <<endl;
    cin >> dblViy;}
    cout << "\n\n Is A in the x given ?" <<endl;
    cin >> charAx;
    if(charAx=='Y' || charAx=='y')
    {cout << "what is the acceleration in the x?" <<endl;
    cin >> dblAx;}
    cout << "\n\n Is A in the y given ?" <<endl;
    cin >> charAy;
    if(charAy=='Y' || charAy=='y')
    {cout << "what is the Initial velocity in the x?" <<endl;
    cin >> dblAy;}
    cout << "\n\n Is Time given ?" <<endl;
    cin >> charTime;
    if(charTime=='Y' || charTime=='y')
    {cout << "what is the time (delta t)?" <<endl;
    cin >>dblTime;}
    cout << "\n\n Is the Displacement in the x given ?" <<endl;
    cin >> charDisx;
    if(charDisx=='Y' || charDisx=='y')
    {cout << "what is the displacement in the x?" <<endl;
    cin >> dblDisx;}
    cout << "\n\n Is the Displacement in the y given ?" <<endl;
    cin >> charDisy;
    if(charDisy=='Y' || charDisy=='y')
    {cout << "what is the Displacement in the y?" <<endl;
    cin >> dblDisy;}



    if(charAx=='Y' || charAx=='y')
    {
    dblAx=Ax(dblVix, dblVfx, dblTime, dblAx, dblDisx, chrDisx, chrTime, chrVfx);
    };

    cout <<dblAx;
    cout << " The acceleration in the x is" << dblAx;



    if(charAy=='Y' || charAy=='y')
    {
    dblAy=Ay(dblViy, dblVfy, dblTime, dblAy, dblDisy, chrDisy, chrTime, chrVfy);
    };

    cout << dblAy;

    cout << " The acceleration in the y is" << dblAy;


    _getch();
    ;
    }








    // This function solves for acceleration in the X direction.
    double Ax(double dblVix, double dblVfx, double dblTime, double dblAx, double dblDisx, char chrDisx, char chrTime, char chrVfx)
    {

    //define local varibles
    int intFunc = 0;

    //calculate
    if(toupper(chrDisx) != 'Y')//they dont have dblDisX
    intFunc = 1;
    if(toupper(chrTime) != 'Y')//they dont have dblTime
    intFunc = 2;
    if(toupper(chrVfx) != 'Y')//they dont have dblVfx
    intFunc = 3;

    switch (intFunc)
    {
    case 1:
    dblAx=(dblVfx - dblVix)/ dblTime ;
    break;

    case 2:
    dblAx=(dblVfx*dblVfx - dblVix*dblVix)/(2*dblDisx);
    break;

    case 3:
    dblAx= 2*(dblDisx - dblVix*dblTime)/(dblTime*dblTime) ;
    break;
    default:
    cout << "error";
    break;
    }
    return dblAx;
    }
    double Ay(double dblViy, double dblVfy, double dblTime, double dblAy, double dblDisy, char chrDisy, char chrTime, char chrVfy)
    {

    //define local varibles
    int intFunc = 0;

    //calculate
    if(toupper(chrDisy) != 'Y')//they dont have dblDisy
    intFunc = 1;
    if(toupper(chrTime) != 'Y')//they dont have dblTime
    intFunc = 2;
    if(toupper(chrVfy) != 'Y')//they dont have dblVfy
    intFunc = 3;

    switch (intFunc)
    {
    case 1:
    dblAy=(dblVfy - dblViy)/ dblTime ;
    break;

    case 2:
    dblAy=(dblVfy*dblVfy - dblViy*dblViy)/(2*dblDisy) ;
    break;

    case 3:
    dblAy=2*(dblDisy - dblViy*dblTime)/(dblTime*dblTime) ;
    break;
    default:
    cout << "error";
    break;
    }
    return dblAy;
    }
    Last edited by richard_chaaya2; January 26th, 2009 at 05:38 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: c++ physics

    Please use code tags. Also, you're declaring your functions ('Ax', 'Ay') inside of main, which is not correct.

    Viggy

  3. #3
    Join Date
    Jan 2009
    Posts
    9

    Re: c++ physics

    what are code tags? and i dont think theres a problem with declaring them like that is there?

  4. #4
    Join Date
    Jan 2009
    Posts
    9

    Re: c++ physics

    any ideas?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: c++ physics

    Quote Originally Posted by richard_chaaya2 View Post
    what are code tags?
    http://www.letmegooglethatforyou.com/?q=code+tags
    Result #3. Or you could just look at the FAQ.....

  6. #6
    Join Date
    Jan 2009
    Posts
    9

    Re: c++ physics

    lol k great to know... any help?

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: c++ physics

    Quote Originally Posted by richard_chaaya2 View Post
    lol k great to know... any help?
    Just waqiting for you to go back an edit your original post to include Code Tags, and follow the other instructions in the "BEFORE you post FAQ". (such as enabling private messaging, etc...)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jan 2009
    Posts
    9

    Re: c++ physics

    k i did, now wat?

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: c++ physics

    Quote Originally Posted by richard_chaaya2 View Post
    k i did, now wat?
    Not you didn't...

    1) You code is not properly tagged an indented as shown below...
    Code:
    class MyClass
    {
        public:
           MyClass();
    }
    2) Private messaging is STILL not enabled.

    3) You apparently did not read the FAQ's carefully because you are still using "IM Speak" instead of properly spelled out words and proper (to the best of your ability) grammer. This is an internaltional forum and many of the members to not speak english as a primary language.

    Please spend some time reading how to be a considerate member of the community. Remember we are all volunteering our time here, freely to help those in need (and show a little consideration).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: c++ physics

    Haha! That's awesome!
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  11. #11
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: c++ physics

    Your program is asking far too many questions, to get a single answer! Assuming that the ball lands at the same height as it was initially kicked from, and assuming gravity at say 9.81ms-2, then you only need to know.

    A) The initial velocity in the x direction and the initial velocity in the y direction.

    or

    B) The angle at which the ball was kicked and the initial velocity of the ball.

    Calculating A is trivial. Ignoring air resistance the velocity in the x direction will remain invariant. Therefore, all you need to do is calculate the time that the ball is in the air based on the initial velocity of the ball in the y direction. You can do this with the following kinematic equation:

    Code:
    v = u + at
    where, v is the final velocity, u is the initial velocity, a is the acceleration (in this case due to gravity) and t is time.

    If you rearrange the above equation to calculate the time (also remembering that acceleration is pushing in the opposite direction to the velocity of the ball, and therefore is negative) then you will be able to calculate how long it takes for the ball to go from the initial velocity to zero velocity in the y direction. i.e.

    Code:
    t = (v-u)/a
    Ignoring air resistance and assuming the ball will land at the same height at which it left the ground, the result given by the rearranged equation, is exactly half the travel time of the ball in flight, multiplying your time result by two, will give you the total flight time.

    You can now use the total flight time, and the constant velocity of the ball in the x direction to calculate how far the ball has travelled. i.e. use

    Code:
    d = s/t
    where d is distance, s is speed, and t is time.

    So, in order to calculate A, you need to write a function that takes two parameters:

    Code:
    float distanceTravelled(float xVelocity, float yVelocity)
    {
    //....
    }
    This function in turn could call two other functions in order to calculate the distance travelled.

    Code:
    float timeInFlight(float yInitialVelocity); //based on t = (v-u)/a times 2
    float distance(float speed, float time); //based on d = s/t
    This means you could write the distance travelled function as:

    Code:
    float distanceTravelled(float xVelocity, float yVelocity)
    {
      return distance(xVelocity, timeInFlight(yVelocity));
    }
    Finally, calculating B is a little harder, but not much. Convert the angle (theta) at which the ball was kicked and the initial velocity (u) of the ball, into initial x velocity and initial y velocity components using first principles. i.e.

    Code:
    
    y = sin(theta)*u
    x = cos(theta)*u
    You can now use the x and y parameters as inputs to the distanceTravelled function.

    I hope that helps, and that I haven't given too much away.

  12. #12
    Join Date
    Jan 2009
    Posts
    9

    Re: c++ physics

    Hey to every1 here who thought that this program wasnt going to work, i got it to work and it was used in a competition, Thanks for all your help everyone. (not that i got any but yea) . Later, long live C++!

  13. #13
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: c++ physics

    [ moderated by Yves ]
    Last edited by Yves M; May 31st, 2009 at 03:50 AM.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)