How do I add gravity to a game?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 10142 Views - Last Post: 08 December 2011 - 09:01 PM Rate Topic: -----

#16 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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

Was This Post Helpful? 0
  • +
  • -

#17 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#18 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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?
Was This Post Helpful? 0
  • +
  • -

#19 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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

Was This Post Helpful? 0
  • +
  • -

#20 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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)
?
Was This Post Helpful? 0
  • +
  • -

#21 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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?
Was This Post Helpful? 0
  • +
  • -

#22 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#23 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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

Was This Post Helpful? 0
  • +
  • -

#24 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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?
Was This Post Helpful? 0
  • +
  • -

#25 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#26 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • 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.
Was This Post Helpful? 1
  • +
  • -

#27 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,628
  • 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.
Was This Post Helpful? 0
  • +
  • -

#28 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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?
Was This Post Helpful? 0
  • +
  • -

#29 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • 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
Reason for edit:: added spoiler

Was This Post Helpful? 0
  • +
  • -

#30 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,628
  • Joined: 27-December 08

Re: How do I add gravity to a game?

Posted 08 December 2011 - 06:40 PM

View PostThe_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.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3