Question

i have already found the math for dealing with projectile motion but the different with mine was the directional vector and time provided.

In my code , my directional vector was -1 to 1 and time was an interval time.

How do I use those vector and time to calculate the new directional vector due to gravity?

Please help

Was it helpful?

Solution

I assume 3D space (with gravity directing in -Y direction)

  1. init

    // player / gun parameters
    const float v0 = ???;             // [m/s] gun projectile escape velocity
    float direction[3] = { ?,?,? };  // [-] your unit direction vector
    float position[3] =  { ?,?,? };  // [m] your player position + gun barrel offset
    
    // projectile parameters
    float a[3]={0,-9.81,0};   // [m/s^2] actual acceleration (just gravity in down direction)
    float v[3]=direction*v0;  // [m/s] actual velocity
    float p[3]=position;      // [m] actual position
    

    if you want to be precise add to v also your players velocity vector. If your projectile is rocket then add to a also its trust acceleration in its direction ...

  2. update in some timer

    const float dt = 0.001; // [s] update 'timer' period
    
    v += a*dt;
    p += v*dt;
    

    here you can add tests like: hit something, hit ground, ... also friction is a good idea ...

    F=c0*|v|^2; for air
    F=c1*|v|^3; for liquid
    

    in direction (-v/|v|) so add to start of update this:

    a={0,-9.81,0}
    a-=v*c0*|v|/m; // or a-=v*c1*|v|^2/m;
    

    where c0,c1 are friction constants (try ~0.01) and m is the projectile mass [kg]

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