Hey guys and gals, been a while since my last post.
First of all let me explain what I am trying to achieve: Threaded simulation
I have created a little 10 minutes program that calculates ballistic trajectories. From two user inputted variables, it will return how far the object went, and how long it was in flight.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <cmath>
const double g = 9.81;
double square(double d)
{
return d*d;
}
double DistanceTravelled(double a, double v)
{
double sum = v*std::cos(a); // Multiply velocity by Cosine(Angle)
sum /= g; // Divide total by Gravitational Constant
sum *= (v*std::sin(a)) + std::sqrt(square(v*std::sin(a)) + 2 * g * 0); // Some magic
return sum; // Profit
}
double TimeTaken(double a, double v)
{
double sum = v * std::sin(a); // Multiply velocity by launch angle
sum += std::sqrt(square(v*std::sin(a)) + (2 * g * 0)); // Some magic
sum /= g; // Divide by gravitational constant
return sum; // profit;
}
int main()
{
std::cout << "Angle of Launch: ";
double angle;
std::cin >> angle;
std::cout << "Velocity of Launch: ";
double velocity;
std::cin >> velocity;
double distance = DistanceTravelled(angle, velocity);
std::cout << "Distance travelled: " << distance << " Meters.\n";
std::cout << "Time taken: " << TimeTaken(distance,velocity) << "\n";
return 0;
}
|
First of all are those two functions correct? A 45 Degree angle at 60M/s get's nearly 12 seconds of flight time. At a velocity of 100M/s it only has 5 seconds of air time according to these functions, is that normal? Seems a little strange to me.
Second of all, how would I rearrange this unfied formulae to calculate the objects distance at point (0 < dT < T)? In otherwords how would I simulate this objects movement?
I would like to spawn a thread, and have the thread update the user as trajectory time progresses. So if the trajectorys flight time is 12 seconds, the thread should be alive for those 12 seconds updating the console of the current objects distance away from launch.
I hope my question(s) make sense.
To re-iterate:
- Are those 2 functions correct?
- How do I simulate this motion?
Thanks in advance.