## 32 Replies - 17268 Views - Last Post: 08 December 2011 - 09:01 PM

### #1

# How do I add gravity to a game?

Posted 06 December 2011 - 10:55 PM

##
**Replies To:** How do I add gravity to a game?

### #2

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 10:58 PM

From a basic algebra perspective, just parameterize x and y both as functions of time. Then use the kinematics equations:

x(t) = x0 + v*t + 1/2 * at^2

y(t) = y0 + v*t - 4.9t^2 (g = -9.8 m/s^2)

### #3

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:08 PM

### #4

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:08 PM

macosxnerd101, on 06 December 2011 - 09:58 PM, said:

From a basic algebra perspective, just parameterize x and y both as functions of time. Then use the kinematics equations:

x(t) = x0 + v*t + 1/2 * at^2

y(t) = y0 + v*t - 4.9t^2 (g = -9.8 m/s^2)

Yeah... I didn't really understand your tutorial. I'm only a sophomore in high school so you would have to explain what every single variable is.

### #5

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:08 PM

@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.

The general form kinematics equation is (where r is position):

r(t) = rInitial + v*t + 1/2 a*t^2

v = velocity

a = acceleration

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?

### #6

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:15 PM

macosxnerd101, on 06 December 2011 - 10:08 PM, said:

@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.

The general form kinematics equation is (where r is position):

r(t) = rInitial + v*t + 1/2 a*t^2

v = velocity

a = acceleration

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?

### #7

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:36 PM

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 (v

_{y}) 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.

Hence why we have the equations:

x(t) = xInit + vX*t

y(t) = yInit + vY*t - 1/2 *a * t^2 (a = g = 9.8 m/s^2 when dealing with gravity)

### #8

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:42 PM

### #9

## Re: How do I add gravity to a game?

Posted 06 December 2011 - 11:53 PM

x(t) = 0 + xVelocity * t

y(t) = 0 + yVelocity * t - 1/2 * a * t^2

At t = 0, x = 0 and y = 0. Let's look at how our x and y values change:

t x y 0.5 5 6.275 1 10 10.1 1.5 15 11.475 2 20 10.4 2.5 25 6.875 3 30 0.9

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).

### #10

## Re: How do I add gravity to a game?

Posted 07 December 2011 - 10:57 AM

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

### #11

## Re: How do I add gravity to a game?

Posted 07 December 2011 - 11:20 AM

### #12

## Re: How do I add gravity to a game?

Posted 07 December 2011 - 11:42 AM

Quote

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.

For a refresher on the quadratic equation, check out this link.

### #13

## 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:

Quote

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.

For a refresher on the quadratic equation, check out this link.

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.

### #14

## Re: How do I add gravity to a game?

Posted 07 December 2011 - 03:09 PM

package me.kennth.gravitysim; import java.awt.BorderLayout; import javax.swing.*; public class Main extends JFrame { private static final long serialVersionUID = 1L; MyPanel panel; private Main(boolean isResizable, String title) { super(title); setLayout(new BorderLayout()); panel = new MyPanel(); add(panel, BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500, 500); setResizable(isResizable); setVisible(true); panel.startSim(); } public static void main(String[] args) { new Main(true, "Gravity Simulator"); } }

package me.kennth.gravitysim; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.math.*; public class MyPanel extends JPanel implements ActionListener { private int x, y, dx, dy, velocity; private final double RATE_OF_ACCELERATION = 9.8; public MyPanel() { x = 100; y = 250; dx = 0; dy = 0; velocity = 15; setDoubleBuffered(true); setPreferredSize(new Dimension(500, 500)); setIgnoreRepaint(true); } public void startSim() { while (true) { } } public void paintComponent(Graphics g) { super.paintComponent(g); } }

### #15

## Re: How do I add gravity to a game?

Posted 07 December 2011 - 04:07 PM

package me.kennth.gravitysim; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyPanel extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private int x, y; private double timeValue, velocity, dx, dy; private final double RATE_OF_ACCELERATION = -9.8; Timer time; public MyPanel() { x = 100; y = 250; dx = 0; dy = 0; velocity = 1; timeValue = 0; time = new Timer(16, this); setDoubleBuffered(true); setPreferredSize(new Dimension(500, 500)); setIgnoreRepaint(true); setBackground(Color.WHITE); } public void startSim() { time.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect(0, 0, 500, 500); g.setColor(Color.BLUE); g.fillOval(x, y, 20, 20); g.dispose(); } public void actionPerformed(ActionEvent e) { timeValue += 0.01; dx = 0 + velocity * timeValue; dy = 0 + velocity * timeValue - (1 / 2) * RATE_OF_ACCELERATION * (Math.pow(timeValue, 2)); x += dx; y += dy; repaint(); } }