How do I add gravity to a game?

Posted 06 December 2011 - 10:55 PM

I'm in High School Honors Geometry and I am half way through the year so I might not understand everything. Could someone link me to a tutorial where you can just make a ball drop and it applies gravity and the ball bounces?

Re: How do I add gravity to a game?

@The_Programmer-: Let's start with a simple x-y graph. Are you familiar with Cartesian graphs of functions? They relate x and y, and generally y is a function of x. You may have seen f(x) = y = 3x. So what we are doing is treating x and y independently. The x and y variables are related to time. So x(t) = 3t, and y(t) = someInitialYPosition + yVelocity*t - 4.9t^2.

Gravity accelerates the particle. Since gravity points downwards and the positive y direction is upwards, you subtract the acceleration due to gravity from the position. So:
y(t) = yInit + v*t - 4.9t^2

Then you plot the object at the x and y coordinates for the given time value.

Re: How do I add gravity to a game?

@The_Programmer-: Let's start with a simple x-y graph. Are you familiar with Cartesian graphs of functions? They relate x and y, and generally y is a function of x. You may have seen f(x) = y = 3x. So what we are doing is treating x and y independently. The x and y variables are related to time. So x(t) = 3t, and y(t) = someInitialYPosition + yVelocity*t - 4.9t^2.

Gravity accelerates the particle. Since gravity points downwards and the positive y direction is upwards, you subtract the acceleration due to gravity from the position. So:
y(t) = yInit + v*t - 4.9t^2

Then you plot the object at the x and y coordinates for the given time value.

Does that make more sense?

Yeah, it makes more sense but I'm not completely sure. I know this might be asking a lot or you might not be able to do it because it would be giving away code, but could you write some example class that I could look at and see what everything does?

Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:36 PM

Honestly, I think a graphic might be better than a code sample here. If you can understand how it works on a Cartesian graph, then programming it is the easy part. A lot of this math you will get in trig/pre-calculus, physics, and calculus. Really, the trig and pre-calc is good for abstracting a lot of it.

The idea is to treat the x and y coordinates separately. When you calculate the slope of a line, you divide the change in y over the change in x: m = (y2 - y1)/(x2-x1). So what you want is to look at deltaX = (x2-x1) and deltaY = (y2-y1) separately. The object's movement in the x-direction is not affected by gravity. Only the y-direction is affected by gravity.

I've put together a graph below, which describes the motion of gravity. Notice it is a curve pointing downwards. In order for an object to move in the positive y-direction, it's y-velocity (vy) has to be greater than 0. This is what you want to deal with in your program- when an object moves upwards, it's y-velocity is positive. Now, gravity pulls down on an object, so it is a negative quantity. However, gravity is acceleration, not velocity. Remember that acceleration says how much the velocity is changing.

There will come a point on the graph where the y-velocity = 0. This is the point at the maximum height. However, since gravity is still acting on the object, the acceleration is still negative, thus causing the object to fall. Think about it that way.

Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:42 PM

Ok. I think I'm understanding it now. I'm just not sure on the equations you have but I understand everything else. I have to go so I will post in the morning. I will also try out a program in the morning.

Notice how the y increases, then decreases. At some point between t = 1.5 and t = 2, the y-velocity = 0 and the object is at its highest point. Then somewhere right after t = 3, the y-velocity = 0 and the ball touches the ground. You can get the exact time values by solving the quadratic equations (which you may not have covered at this point in your math career).

Re: How do I add gravity to a game?

Posted 07 December 2011 - 10:57 AM

OK. I understand everything now. But why do you add time squared to the y velocity? And is time in milliseconds? seconds?
Edit: And I guess I don't understand everything because does x(t) mean x(at a certain time)?

This post has been edited by The_Programmer-: 07 December 2011 - 11:13 AM

Re: How do I add gravity to a game?

Posted 07 December 2011 - 11:42 AM

I add 1/2 * a * t^2 to the y position. There is some Calculus involved in deriving this, but the conceptual explanation is that acceleration determines how much the velocity changes. So acceleration/2 * t^2 + v*t gives the new position at time t, assuming constant acceleration.

Quote

And I guess I don't understand everything because does x(t) mean x(at a certain time)?

This is just like a function f(x). When you see x(t), we are referring to elapsed time. So we start at t = 0.

Re: How do I add gravity to a game?

Posted 07 December 2011 - 11:56 AM

macosxnerd101, on 07 December 2011 - 10:42 AM, said:

I add 1/2 * a * t^2 to the y position. There is some Calculus involved in deriving this, but the conceptual explanation is that acceleration determines how much the velocity changes. So acceleration/2 * t^2 + v*t gives the new position at time t, assuming constant acceleration.

Quote

And I guess I don't understand everything because does x(t) mean x(at a certain time)?

This is just like a function f(x). When you see x(t), we are referring to elapsed time. So we start at t = 0.

Thanks for everything. Using all of this, I will try to make a simple program that drops a ball or throws it or something. My next step would be to add friction and stuff so the ball will bounce around the screen.