# How can I calculate the future position of my moving object?

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.
• 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... – Alexandre Desbiens Jun 16 '15 at 19:06
• 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. – Nexuz Jun 16 '15 at 19:09
• 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. – Alexandre Desbiens Jun 16 '15 at 19:18
• 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/… – Pieter Geerkens Jun 16 '15 at 22:41

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 travelled 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;
}

• 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. – Nexuz Jun 17 '15 at 17:41
• 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. – Richard Byron Jun 17 '15 at 22:07
• 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? – Nexuz Jun 18 '15 at 9:27
• 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. – Richard Byron Jun 19 '15 at 13:27

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