Thread: Cannonball trajectory issue, I'm completely stumped!

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Cannonball trajectory issue, I'm completely stumped!

    I'm currently working on a program that, when a user inputs an angle and muzzle velocity, it outputs the trajectory the cannonball will go (This code does not take in to account wind, only gravity itself).

    For some odd reason it just won't seem to work. Seeing as I'm using a different graphics class than most, I should probably mention the coordinates on my graph. X maximum is 2100, Y maximum is 2050, and the cannon is at 0,0 itself.

    The big concern is I'm trying to only output points at every one second intervals, and doing the calculation 100 times per second to determine where that point goes. Unfortunately I'll only sometimes end up with no points other than (0,0), or the end point, nothing in between!

    If you guys wouldn't mind giving me a hand that would be awesome:

    for reference, dT is my delta time, which is 0.01.
    Code:
    
    #include "ccc_win.h"
    #include <cmath>
    
    int ccc_win_main()
    {
        const double G = 9.81;
        const double dT = 0.01;
        double anglealpha = 0;
        double velocity = 0;
        double Vx = 0;
        double Vy = 0;
        int x = 0;
        double Px = 0;
        double Py = 0;
        double Pxnew = 0;
        double Pynew = 0;
        double Vynew = 0;
    
        cwin.coord(0, 2050, 2100, 0);
    
        anglealpha = cwin.get_double("Please enter your angle");
    
        velocity = cwin.get_double("Please enter your initial muzzle velocity");
    
        anglealpha = anglealpha * (M_PI / 180);
    
        Vx = velocity * cos(anglealpha);
        Vy = velocity * sin(anglealpha);
    
    
        cwin << Point(Px, Py);
    
        while(Py > 0)
        {
            x = 0;
    
            while(x <= 100)
            {
                Vynew = Vy - G * dT;
                Vy = Vynew;
                Pxnew = Px + Vx + dT;
                Px = Pxnew;
                Pynew = Py + Vynew * dT;
                Py = Pynew;
                x++;
    
            }
    
    cwin << Point(Pxnew, Pynew);
    
        }
    
        return 0;
    }
    Last edited by Kyler45; 10-11-2014 at 11:01 AM.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    I have found one of my errors, it turns out Pxnew should be = Px+vx*dT, not +dT. However now I have a problem where my code will not output the very final point, which is saddening! It will do all other points perfectly (At least, I think so), and then not output the final point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stumped on char* issue
    By refact0r in forum C Programming
    Replies: 11
    Last Post: 11-08-2013, 04:30 AM
  2. Replies: 8
    Last Post: 10-19-2011, 07:10 PM
  3. C++ Cannonball Pyramid Calculator (tetrahedron)
    By Zedaine in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 04:52 AM
  4. Angle of Trajectory?
    By napkin111 in forum Game Programming
    Replies: 3
    Last Post: 12-18-2003, 01:09 PM
  5. Physics: Kind of like a cannonball...
    By Cheeze-It in forum Game Programming
    Replies: 11
    Last Post: 09-12-2001, 01:53 PM