A C++ function to calculate and sample the trajectory of a projectile in 3D space. Physics programming

StackOverflow https://stackoverflow.com//questions/25065676

  •  23-12-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top