I'm relatively new to games development and I have a 2D game where the playing area is an enclosed rectangle with width w and infinite height. In the playing area there is a ball. A gravity force of strength G acts on the ball. The ball starts at position p and has an initial velocity of v.

How would I calculate the position the ball will be at when it reaches the specified height h. I have a function setup using the following parameters:

bool CalculateXPositionAtHeight(float height, Vector2 position, Vector2 velocity, float Gravity, float width, float& xPosition);
  • The playing area doesn’t have any boundary at the top, so no collision ever happens with it.
  • The ball can be considered as a point.
  • There are no precision issues and everything can easily fit in a float.
  • There is no air resistance.
  • All collisions are perfect without any energy loss.
  • If the ball hits the bottom it won’t bounce.
  • 1
    \$\begingroup\$ How can your question be about C# and C++ at the same time? By the look of your code this looks like C#, but the last argument makes me wonder... \$\endgroup\$ Commented Jun 16, 2015 at 19:06
  • \$\begingroup\$ I'm using C++ but I am more interested in a pseudo code based solution therefore I put both C# and C++ to increase the number of people who may see the post. \$\endgroup\$
    – Nexuz
    Commented Jun 16, 2015 at 19:09
  • 2
    \$\begingroup\$ There will be plenty of people who will see your post, but adding C# to a C++ post is just misleading. I would remove it if I were you. \$\endgroup\$ Commented Jun 16, 2015 at 19:18
  • \$\begingroup\$ The 1-D kinetic equations as noted here (gamedev.stackexchange.com/questions/54732/…) can be applied in the horizontal and vertical directions independently. Additional links: gamedev.stackexchange.com/questions/71900/… and gamedev.stackexchange.com/questions/71900/… \$\endgroup\$ Commented Jun 16, 2015 at 22:41

2 Answers 2


Like Pieter mentioned, you can use 1D kinetic equation for height, which in your case will come to:

\$h = p + vt + 0.5at^2\$

where a is the acceleration due to gravity. You can then use the quadratic equation to solve for t. Once you have t, you can use the kinetic equations again to calculate how far the ball has traveled width ways. This would tell you where the ball was if there were no walls. Assuming the the walls have no friction, it should be pretty obvious how to figure out where the ball would be between the walls.

If the walls do not just directly reflect the velocity, then you need to calculate the time at which the ball hits the wall, and then repeat with the new velocity until the ball reaches the ground.

Assuming gravity is a negative number, the following code should work:

bool CalculateXPositionAtHeight(float height, Vector2 position, Vector2 velocity, float Gravity, float width, float& xPosition)
    // kinetic equation for y is: y = position.y + velocity.y * t + 0.5 * Gravity * t^2
    // we want to find when y = height, and have the equation in the form at^2 + bt + c = 0
    // which gives the following:
    float a = Gravity * 0.5f;
    float b = velocity.y;
    float c = position.y - height;
    // the quadratic formula can then be used to solve for t
    // we know that gravity will always be negative and time is positive, so only care about that solution
    float t = (sqrtf(b * b - 4.0f * a * c) - b) / (2.0f * a);
    // now that we have t, we can solve the kinetic equation for x
    // there is no acceleration in the x direction, so this just becomes:
    float x = position.x + t * velocity.x;
    // the following code handles the ball bouncing off the walls
    x = fmodf(x, 2.0f * width);
    if (x > width)
        x = 2.0f * width - x;
    xPosition = x;
    return true;
  • \$\begingroup\$ Could you show me roughly how that would translate into a code sample which I can work from as I don't have experience with quadratic equations. \$\endgroup\$
    – Nexuz
    Commented Jun 17, 2015 at 17:41
  • 1
    \$\begingroup\$ I've edited the post to include code. You can simplify this a bit, but I've tried to make it clear what formulas are being used. \$\endgroup\$ Commented Jun 17, 2015 at 22:07
  • \$\begingroup\$ Thanks for that Richard, I will go over this and see if it's what I'm looking for. Would you be able to comment each line so I can more easily understand what the function is doing? \$\endgroup\$
    – Nexuz
    Commented Jun 18, 2015 at 9:27
  • 1
    \$\begingroup\$ Jinbom's answer below reminded me that I've made an assumption I didn't explicitly state. My code assumes that height is always below your current position. If that's not the case, then the ball may never reach that height. For that case, b^2 - 4ac would be negative. If it's not negative, then you would want to use the - half of the +/- in the quadratic formula, or compute both answers and take the smallest positive t value. \$\endgroup\$ Commented Jun 19, 2015 at 13:27

@Richard Byron explained well and gave a well-made sample code.

I think it's better to add additional information about the basic math.
I leaved out the wall collision.

Sorry for bad hand writing an drawing.

physics 2d fomular equation


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .