1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Arrow ballistics

Discussion in 'Development' started by Heisenborger, Jun 17, 2018.

  1. Heisenborger

    Heisenborger Spider

    Messages:
    9
    Given the arrow origin position, direction, force, gravity and drag, how can I calculate the arrow Y at a given X,Z ?
     
  2. Heisenborger

    Heisenborger Spider

    Messages:
    9
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The Projectile movement calculation is implemented here: https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/entity/Entity.php#L1167

    Velocity vector change per movement tick when the arrow is in air:
    Code:
    v.y = v.y * (1 - drag) - gravity
    v.x = v.x * (1 - drag)
    v.z = v.z * (1 - drag)
    
    Given initial velocity vector [v.x(0), v.y(0), v.z(0)], starting at the origin (0, 0, 0), the velocity at time t (after t ticks) can be expressed parametrically:
    Code:
    v.x(t) = v.x(0) * (1 - drag)^t
    v.z(t) = v.z(0) * (1 - drag)^t
    
    where ^ is the exponent operator

    The vertical velocity is a bit more tricky:
    Code:
    y[t] = y[t - 1] * (1 - drag) - gravity
    
    Applying z-transform (or just some tweaks with the sum of geometric sequence), you can derive a general formula for y[t].

    You can derive the t using the v.x(t) or v.z(t) formula (if it is horizontal/vertical, you need to use the other axis; otherwise, either one is OK). Then you can calculate y[t] using the t.
    I am in a hurry so I don't have time to calculate it. Try it yourself :)
     
    Heisenborger and SleepSpace9 like this.
  4. Heisenborger

    Heisenborger Spider

    Messages:
    9
    Code:
    f = (1 - drag)
    v.x(t) = v.x(0) * f^t
    v.z(t) = v.z(0) * f^t
    v.y(t) = v.y(0) * [f - gravity]^t
    
    Ok, this was easy. Now, to calculate the t at a given X/Z, I think this should be ok:

    Code:
    v.x(t) = v.x(0) * f^t
    v.x(t)/v.x(0) = f^t
    t = logf(v.x(t)/v.x(0))
    
    Is this the best way to do it?
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.