Set player velocity to launch them from point A to point B in an arc with bukkit

Viewed 102

I'm trying to code a plugin for a server and I want to launch a player from point A to point B.

I've tried this but a lot of the time it is inaccurate and misses the block by a few blocks.

 private  Vector calculateVelocity(Location fromLoc, Location toLoc, int heightGain) {
    Vector from = fromLoc.toVector();
    Vector to = toLoc.toVector();

    //Block locations
    int endGain = to.getBlockY() - from.getBlockY();
    double horizDist = fromLoc.distance(toLoc);

    //Height gain
    int gain = heightGain;
    double maxGain = (gain > (endGain + gain) ? gain : (endGain + gain));

    //Solve quadratic equation for velocity
    double a = -horizDist * horizDist / (4 * maxGain);
    double b = horizDist;
    double c = -endGain;
    double slope = -b / (2 * a) - Math.sqrt(b * b - 4 * a * c) / (2 * a);

    //Vertical velocity
    double veloY = Math.sqrt(maxGain * 0.115);

    //Horizontal velocity
    double vH = veloY / slope;

    //Calculate horizontal direction
    int distX = to.getBlockX() - from.getBlockX();
    int distZ = to.getBlockZ() - from.getBlockZ();
    double mag = Math.sqrt(distX * distX + distZ * distZ);
    double dirX = distX / mag;
    double dirZ = distZ / mag;

    //Horizontal velocity components
    double veloX = vH * dirX;
    double veloZ = vH * dirZ;

    //Actual velocity
    Vector velocity = new Vector(veloX, veloY, veloZ);

    return velocity;
}

Any suggetions on what I should do to get it accurate 100% of the time?

0 Answers
Related