You are adding angle + distance + distance, so no it is not dimensionally correct.
You might be best finding a physics or maths textbook and looking up "projectile motion" or "2-d motion under gravity".
Get your angle from the direction of the velocity vector (v
x,v
y) with horizontal (x) and vertical (y) components. There is an acceleration g = 9.81 m/s
2 downward due to gravity; put another way, the acceleration vector is (a
x,a
y)=(0,-g)
For velocity and angle:
Horizontally: v
x = u
x
Vertically: v
y = u
y - gt
Then angle is arctan(v
y/v
x) to the horizontal.
For position:
x = u
xt
y = u
yt - gt
2/2
u
x and u
y are the initial velocity components:
u
x = throwspeed * cos(throwangle)
u
y = throwspeed * sin(throwangle)
At the moment, your function seems to be closer to finding a vertical position than anything else.
From a c++ perspective, if you are returning multiple things (two components of position and an angle) then you will either have to return them by reference or put them in a single composite object.
Your question does say return them as a vector, so you could return x,y,angle as the 0,1,2 components of a vector, say; e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <vector>
....
vector<double> javelin( double throwAngle, double throwSpeed, double time )
{
const double GRAVITY = 9.81; // varies from 9.78 to 9.83 m/s2 from equator to poles
const double PI = 3.14159265359; // you will need this for any degrees-to-radians conversion
vector<double> result(3);
double x, y, ux, uy, vx, vy, angle;
ux = ... // formulae as given
uy = ...
x = ...
y = ...
vx = ...
vy = ...
angle = ....
result[0] = x;
result[1] = y;
result[2] = angle;
return result;
}
|