Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Projectile Motion in 3D space (Writing an Aimbot)

  1. Dec 25, 2012 #1
    Alright, so, I am writing an aimbot for a popular game, and I need a bit of help with calculating the correct pitch angle for the player. The weapon is a bow, so the arrows are affected by gravity. I am trying to find the pitch angle that accounts for gravity and distance from the Entity you are firing at.

    My current code doesn't work, but I will post it anyways. I have researched the correct algorithm (I found it at http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29) and have attempted to implement it. However, when the aimbot is being used, for some reason, it stays in the middle of my screen, regardless of the entity's Y value or the player's Y value. I have the velocity correct, and the gravity constant correct, and that is all I know.
    Anyways, enough rambling, here is my code:
    Code (Text):

        private float getBowPitchFromEntity(Entity e) {
            double x = getX(e);
            double y = getY(e);
            double z = getZ(e);
            double velocity = getVelocity();
            double posX = Math.pow(x, 2) + Math.pow(z, 2);
            double height = y - getY(getPlayer());
            return (float)getTrajectory(posX, z, velocity, height);
        private double getTrajectory(double x, double z, double velocity, double height) {
            double vsquared = Math.pow(velocity, 2);
            double vquad = Math.pow(velocity, 4);
            double gxsquared = Math.pow(x * gravityConstant, 2);
            double gx = x * gravityConstant;
            double twoyvsquared = 2 * Math.pow(height * velocity, 2);
            double add = Math.atan2(vsquared + Math.sqrt(vquad - (gravityConstant * (gxsquared + twoyvsquared))), gx);
            double sub = Math.atan2(vsquared - Math.sqrt(vquad - (gravityConstant * (gxsquared + twoyvsquared))), gx);
            return Math.min(add, sub);
    The gravity constant is -0.05D, and the velocity doesn't matter, as I have compared it to the in-game arrow velocity and it matches.

    If anyone can help, I would appreciate it.
  2. jcsd
  3. Dec 25, 2012 #2


    Staff: Mentor

    could you have a degrees vs radians issue? I noticed the last eqns using Math.atan2()

    I also noticed that the wiki eqn says g*x^2 but you've used (g*x)^2 for gxsquared

    Lastly, z doesn't appear to be used in the second function.
    Last edited: Dec 25, 2012
  4. Dec 25, 2012 #3
    That might possibly be my problem. I got mad and deleted the module last night, so I'll rewrite it and try again. Thank you very much for your help!
  5. Dec 25, 2012 #4


    Staff: Mentor

    Note to self: Never get mad at code and delete it instead comment it out in a more gentle deprecational mode.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook