5

I need to write a function in C++ that can model and sample the trajectory curve of an object moving through 3D space.

Problem Statement:

The function will need to take 3 arguments as inputs. The function prototype might look something like this:

void CalculateAndSampleTrajectory(Vec3 direction, float impulse, float mass)
{
  //...
}

The Vec3 direction is a struct. It is essentially 3 floats that behave as a unit vector describing the initial angle of impulse.

The float impulse is a magnitude of how powerful the impulse is.

The float mass describes the mass of the object being projected.

The function would take these 3 arguments and then pre-calculate the trajectory of an object of this mass, given this instantaneous impulse, shot along this vector.

It would do this by taking samples (let's say 200) of the trajectory over the first 4 seconds of the flight. These samples will be Vec3s of the position of the projectile and stored in an array.

All units are given as SI, the acceleration of gravity is 9.81. We will not be factoring in any air resistance.

What I've already learned:

This problem deals with a lot of physics, particularly classical mechanics. This area is not my strength but I do have a basic understanding of what is happening.

I know that I will need the velocity of the object, which I think can be calculated as:

Vec3 velocity = (direction * impulse)/mass;

I think this works because my impulse will be applied instantaneously and so this would be the f=ma re-arranged to find a but as a 3D vector. To be honest I'm not even sure this is correct but I think it is.

Once I have velocity I know I can get into the using the abundant equations that are available online such as the Wikipedia article on Trajectory

I am not very good at interpreting those equations into C++.

After getting the appropriate equation set up, I would then need to take 200 samples over 4 seconds. This could be done in a loop:

for(int i = 0; i < 200; i++)
{
    int t = 0;
    //sample equation with t
    //store resulting vec3 pos in array
    t = t + 0.02;
}

The solution to my equation at t would be given as the coordinates of the position of the object at that time and should have 3 parameters (x,y,z) so it would be stored in a Vec3.

Where I need help:

I don't know how to do the necessary physics programming to get my equation set up properly to be able to sample it. I'm not able to interpret the theoretical discussion of physics on Wikipedia and other sources into a C++ function.

Thank you for any help you can provide.

ekcell
  • 105
  • 2
  • 11
  • 1
    Start with a vec3 for position at (0,0,0), and for each deltaT (=0.02s for your case), and for each x,y,z direction, change the coordinate by v*deltaT, and then update v based on whatever you know (such as y velocity decreasing by 9.81 * deltaT, assuming positive Y is up) – Kevin L Jul 31 '14 at 17:54
  • Also, There is an error in my loop "int t" should be "float t" – ekcell Jul 31 '14 at 18:21
  • 1
    This question appears to be off-topic because it is about *physics*, not programming. – David Rodríguez - dribeas Jul 31 '14 at 18:26
  • As is discussed in the "Where I need help" section I don't know how to convert physics formulas into programming. I don't think a physicist could help me in this area, only a programmer who knows physics. So I believe this is on topic because it is still about programming. – ekcell Jul 31 '14 at 18:35
  • There was another error in my loop. t should not be declared inside. I was sloppy when I wrote that example loop up. – ekcell Jul 31 '14 at 20:15

1 Answers1

3

Basically there is just one equation relevant to your problem, which is

enter image description here

which gives you the position at time t (from a t0).

Now, you've already got s0 and you have got v0, which is the initial velocity, already calculated from the impulse. What you need is the acceleration.

In your case you just have gravity which is constant in time and it's mostly a vec3 pointing down with a prefixed amount (9.8 m/s^2).

You don't need to calculate the values at each step, since you can integrate the value directly, the next step result doesn't depend from the previous.

This is not considering any form of drag force, otherwise you must include it.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • So s(t) is my position at time t s0 is my initial position, v0 is initial velocity and a is gravity. Did I get all those right? – ekcell Jul 31 '14 at 18:13
  • This worked to model my Trajectory. I just needed the correct formula and it turns out I could translate it into C++ after all. Thank you. – ekcell Jul 31 '14 at 19:24