3D Projectile - miscalculation on X & Z?

147 views Asked by At

I'm building a small physics engine that launches a projectile with a given velocity, gravity velocity and time interval, and tracks and displays the velocity/position vectors at each time interval.

At the moment, when I run my program, my y coordinates update fine. However my x and z coordinates act the same, and I'm pretty sure my z coordinate calculations are incorrect. (But I could be wrong)

This issue is the same for both position and velocity vectors on the x and z axis.

Here's my code:

include <iostream>

using namespace std;

struct velocityVector {

    float vx = 10.0;
    float vy = 14.14;
    float vz = 10.0;

};

struct gravityVector {

    float gx = 0.0;
    float gy = -9.81;
    float gz = 0.0;

};

struct positionVector {

    float px = 0;
    float py = 0;
    float pz = 0;

};

int main() {

    float deltaT = 0.01;

    positionVector posAccess;   // object for positionVectors
    gravityVector gravAccess;   // object for gravityVectors
    velocityVector velAccess;   // object for velocityVectors

    while (deltaT < 1) {

        deltaT += 0.01;     // increment deltaT

        cout << "Velocity vector = ";
        // Display Velocity x,y,z
        cout << velAccess.vx << "    ";
        cout << velAccess.vy << "    ";
        cout << velAccess.vz << "    ";
        cout << '\n';

        cout << "Position vector = ";
        // Display Position x,y,z
        cout << posAccess.px << "    ";
        cout << posAccess.py << "    ";
        cout << posAccess.pz << "    ";
        cout << '\n' << endl;

        // Update Velocity
        velAccess.vx += deltaT * gravAccess.gx;
        velAccess.vy += deltaT * gravAccess.gy;
        velAccess.vz += deltaT * gravAccess.gz;

        // Update Position
        posAccess.px += deltaT * velAccess.vx;
        posAccess.py += deltaT * velAccess.vy;
        posAccess.pz += deltaT * velAccess.vz;

        getchar();      // so I can go through each interval manually
    }
}

If it helps. Here's my task:

A trajectory in 3D for a projectile that has a launch velocity vector of (10.0,14.14,- 10.0). Time step = 0.01 seconds. The gravity vector is (0.0, -9.81, 0.0).

Show the position vectors and velocity vectors on a console for demonstration purposes.

1

There are 1 answers

2
TChapman500 On

The problem is that you're increasing the deltaT (the amount of time that passes with each physics step). This should stay the same and you should add another variable to keep track of how much time has passed since the start of the simulation. For example:

float time = 0.0;
while (time < 1.0)  // make sure that this STAYS a float
{
    // Physics code here
    time += deltaT; // increment the time AFTER physics is simulated
    getchar();
}

This should get you what you want.

Edit: For even more accuracy, you should adjust the position by the average of the speed at the beginning of the time step and the speed at the end of the time step.