Ben Tanos
Ben Tanos

Reputation: 51

2D projectile - Velocity and Position Vectors

I'm building a small physics engine that launches a projectile with a given angle and velocity, and tracks and displays the velocity/position vectors at each time interval, but I'm having some issues with my program.

When I run my program, it doesn't loop, and the postion variable stays at 0. I know I've got the maths wrong somewhere, just don't know where.

Here's my program:

#include <iostream>

using namespace std;

#define PI 3.14159265359

struct vecVariables {

    float v = 0, a = -9.81;
    float posNew = 0, posOld = 0;
    float x, y;
    float theta = 45;   // our start angle is 45
    float u = 20;       // our start velocity is 20
};

int main() {

    int deltaT = 0.01;
    int test;

    vecVariables vars;      // creates an object for Variables to be used

    while (deltaT <= 1) {

        deltaT += 0.01;

        vars.v = vars.u + vars.a * deltaT;  // gets the velocity V
        vars.posNew = vars.posOld + vars.v * deltaT;    // gets position D

        vars.x = vars.u * cos(vars.theta *  PI / 180);
        vars.y = vars.u * sin(vars.theta*  PI / 180);

        cout << "velocity vec = [" << vars.x << " , " << vars.y << "]" << endl;  // velocity on x, y

        cout << "pos = "<< vars.posNew << endl;  // display position

        vars.posOld = vars.posNew; 

        getchar();
    }
}

Upvotes: 1

Views: 1084

Answers (3)

cmourglia
cmourglia

Reputation: 2534

Apart from the int deltaT stuff already cited, there are some mistakes in the code. First of all, the way you compute the velocity if not correct. Indeed, here your vars.u is constant, hence the velocity vars.v will always be the same.

vars.v = vars.u + vars.a * deltaT;  // gets the velocity V

You can do two things to fix that :

  • Remove the vars.u and only use vars.v (which you initialize at 20 if you want), which changes the code to be something like vars.v += deltaT * a;

  • Do the same stuff you did for the position : vars.v = vars.u + deltaT * a; vars.u = vars.v;

Your computation of vars.x and vars.y will still be wrong though, theta should be updated every frame too.

I'm not really sure why you took this direction to solve your problem, but I would suggest to solve everything along x and y axis, which would look something like this (this should compile, but I did not try it, might have typos in it)

#include <iostream>
#include <cmath>

using namespace std;

#define PI 3.14159265359

struct vecVariables {

    float startAngle = 45;
    float startVelocity = 20;

    float vx = 0;
    float vy = 0;

    float px = 0;
    float py = 0;

    float ax = 0;
    float ay = -9.81;
};

int main() {

    float deltaT = 0.01;
    vecVariables vars;      // creates an object for Variables to be used

    vars.vx = vars.startVelocity * cos(vars.startAngle * PI / 180);
    vars.vy = vars.startVelocity * sin(vars.startAngle * PI / 180);

    while (deltaT <= 1) {

        deltaT += 0.01;

        // Update velocity
        vars.vx += deltaT * vars.ax;
        vars.vy += deltaT * vars.ay;

        // Update position
        vars.px += deltaT * vars.vx;
        vars.py += deltaT * vars.vy;

        cout << "velocity vec = [" << vars.vx << " , " << vars.vy << "]" << endl;  // velocity
        cout << "position vec = [" << vars.px << " , " << vars.py << "]" << endl;  // position
        cout << endl;
    }

    return 0;
}

Upvotes: 0

JimK
JimK

Reputation: 53

You have declared delta T as an int then set it to 0.01, which is converted to 0. Change the declaration of delta T to a float.

Upvotes: 1

TChapman500
TChapman500

Reputation: 137

Change int deltaT = 0.01; to float deltaT = 0.01f; or double deltaT = 0.01;. What's happening is that your ".01" is being chopped-off when it's converted to an integer. You want a float or double because those tell the compiler to expect a non-integer value. Right now, you're telling it to expect an integer value and because it's not getting an integer, it's removing everything past the decimal place to force it to be an integer.

Upvotes: 3

Related Questions