How do I add gravity to a game?

• (3 Pages)
• 1
• 2
• 3

32 Replies - 14800 Views - Last Post: 08 December 2011 - 09:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258839&amp;s=6fecca8d68d371cfedfb66dab702af83&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#16 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:02 PM

What is (1 / 2) when you divide two integers? I'm pretty sure it's 0.
```dy = 0 + velocity * timeValue - (1 / 2) * RATE_OF_ACCELERATION * (Math.pow(timeValue, 2));
```
Edited: Added the line where it occurs to avoid confusion and sound less smug. :\

This post has been edited by Sheph: 07 December 2011 - 06:11 PM

#17 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:08 PM

On another note. I was testing your class. It took me a while to find that mistake actually. You might consider setting it's position to 0 and stopping the timer when it reaches a certain point so it doesn't go off the screen. Maybe a button that resets the ball for your viewing pleasure

#18 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:24 PM

The ball seems to speed up kind of "weirdly". And how would I make it curve?

#19 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:32 PM

The reason it isn't curving is because you eliminate gravity from the equation by multiplying it by (1/2). In java 1 is an integer, and 2 is an integer, and 1 / 2 = 0.5, but it must be converted to an integer, which truncates the decimal value, making it 0 (an integer).

```dy = 0 + velocity * timeValue - (1 / 2) * RATE_OF_ACCELERATION * (Math.pow(timeValue, 2));
//simplified
dy = 1 * timeValue - ( 0 * -9.8 * Math.pow(timeValue,2) ); // this last part equates to 0.
dy = timeValue; // same as your x.
```

So your ball is really just being drawn on the timeline with a slope of -1. To fix this, try changing your 1/2 to 0.5, so it recognizes it at a double, or do

velocity * timeValue - ( ( RATE_OF_ACCELERATION/2 ) * Math.pow(timeValue,2) )

This post has been edited by Sheph: 07 December 2011 - 06:34 PM

#20 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:36 PM

Couldn't you write
```(1D / 2D)
```
?

#21 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 06:45 PM

Okay. I seem to have fixed the curve problem. My next objective is to make it bounce. How do I go about that?

#22 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 07:00 PM

After each movement, you could check if you are above or below the bottom of the screen. If your above - keep going. If your below, you can set the balls position back to the bottom of the screen. At that point you use the same formulas again, but with a reduced velocity. (using the new balls position as 0,0) You would have to add the new balls start position from the left edge of the screen to each new location of the ball in motion.

I'm not sure about walls though - I'd image you just invert the x direction (and reduce velocities if you want to).

I might try my own implementation of this, I have decided after reading this thread to study physics aswell, I haven't taken a physics class myself, so good to learn =)

This post has been edited by Mylo: 07 December 2011 - 07:01 PM

#23 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 08:46 PM

Well bouncing the ball is a little more complicated. First you have to determine where the ball is going to bounce. You can say the bottom of your screen. Then when the ball goes out of the bottom of your screen, you have to set it's position to the bottom of your screen, or maybe even find the displacement from the bottom of the screen and draw it up that many pixels ... ANYWAY

So you reset it's position, meaning that it's initial position (y0) has changed. The time is also reset, because it has started a new falling cycle relative to a new starting position. Lastly, you must change the initial velocity as well. It becomes the vector of the velocity when it hit the ground , but reflected across the x-axis. In other words, you have to derive the velocity from your position formula. That requires some calculus, so let me do that for you:
```y0 + yVelocity*t + (g/2)*t^2
where g = -9.8 m/s^2, derived is:
yVelocity + g*t

Then you have to reflect the value across the x-axis (-y)

-(yVelocity + g*t)
```
This is your new yVelocity. Your new y0 is where you set the ball. Then the formula recalculates the rest for you.

Lastly, if you want to simulate REAL bouncing, you have to account for some friction and elasticity. This involves physics. Meaning, you should reduce the MAGNITUDE (absolute value) of the velocity by an amount each time it bounces. You could probably get away with reducing it by a fixed amount each time, like 20%.

This post has been edited by Sheph: 07 December 2011 - 08:48 PM

#24 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 07 December 2011 - 09:10 PM

Okay... Maybe physics is a little too hard for me right now. What I really want to know how to do is have an image with cubes in it, then, based on the color of the cubes in the image, it will draw different shapes on the screen. Will the physics or the image thing be harder?

#25 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

Re: How do I add gravity to a game?

Posted 08 December 2011 - 02:31 AM

I would like to point out that there are lots of physics libraries written for every language. I haven't studied lots of physics so if I'm ever going to make a game, rather than using my time learning, I'll use an existing library for the physics. This will save my time and the physics will most likely be tested already by many developers so it won't have that many bugs.

e. tons of typos removed

This post has been edited by janne_panne: 09 December 2011 - 01:56 AM

#26 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4827
• Posts: 8,594
• Joined: 08-June 10

Re: How do I add gravity to a game?

Posted 08 December 2011 - 10:32 AM

I 100% agree with janne_panne here. It's one thing to discuss one aspect of physics, like gravity. It's another thing to start writing an entire physics engine in a help thread.

Because now you're not just talking about a simple acceleration applied over time, you're talking about mass, force, elasticity, angles of incidence, etc...It gets exponentially more complicated the more features you're looking for.

I've heard lots of good stuff about Box2D, and there's a java implementation:

http://www.jbox2d.org/

Maybe that'll help.

#27 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11384
• Posts: 42,888
• Joined: 27-December 08

Re: How do I add gravity to a game?

Posted 08 December 2011 - 11:15 AM

Also, while a lot of physics can be abstracted to precalculus and trig, dealing with more physics requires at least a basic understanding of calculus. The more physics you want, the more calculus you'll need. Take a physics class in high school once you get more math under your belt as well. Don't try and re-create a physics engine though.

#28 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 08 December 2011 - 11:37 AM

Lol. Okay. But do any of you know how to make it so you can import an image which is a grid filled with different colored squares and based on those colors you could print images on a JPanel in the same grid arrangment but instead of the colored squares you could have images?

#29 The_Programmer-

• Paranormal Investigator

Reputation: 25
• Posts: 668
• Joined: 24-October 11

Re: How do I add gravity to a game?

Posted 08 December 2011 - 11:48 AM

I'll post what I have to do in math today if that helps for anything.
Spoiler

This post has been edited by Curtis Rutland: 08 December 2011 - 12:22 PM

#30 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11384
• Posts: 42,888
• Joined: 27-December 08

Re: How do I add gravity to a game?

Posted 08 December 2011 - 06:40 PM

The_Programmer-, on 08 December 2011 - 01:37 PM, said:

Lol. Okay. But do any of you know how to make it so you can import an image which is a grid filled with different colored squares and based on those colors you could print images on a JPanel in the same grid arrangment but instead of the colored squares you could have images?

You may want to take a look at BufferedImage for examining each individual pixel. Also, see the GridLayout, JLabel and ImageIcon classes. The JLabel class has a setIcon() method you can use to pass the ImageIcon. Really, for much more sophisticated Image analysis, the JAI API is what you want to use.