Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Gravity on Java object?

I'm trying to make a 'Physics' class that all my sprites will extend to, which I want to control all movement with. How can I make all objects in the air accelerate towards the ground? (and look realistic, using 9.81m/s^2)

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    First, use a decomposed motion vector to control motion...

    Make sure your sprite includes the following code in each frame.

    sprite.x += sprite.dx;

    sprite.y += sprite.dy;

    Change dx and dy to change the speed and direction of the sprite's travel.

    If you're doing gravity near a large planet (like platform games) you can often get away with a very simple expedient

    Add a small amount to dy each frame to simulate gravity, so now you're main game loop looks like this:

    sprite.dy += ddy;

    sprite.x += sprite.dx;

    sprite.y += sprite.dy;

    Set ddy to be you're gravitational constant. ddy will actually be in pixels per frame, so you can use ratios to determine how to get from 9.8 meters/second to the pixel/frame value. Typically I just get the scale of the game right, and adjust ddy until it looks about right, and then use a ratio to determine how to get from 9.8 m/s to that frame rate.

    If you want to simulate orbital gravity, you need to do a bit more work.

    Use Newton's law of universal gravitation:

    force = (m1 * m2 * g) / (d^2); to calculate the force vector pulling the two bodies together. Calculate the distance using the Pythagorean Theorem:

    If the centers are (x1, y1) and (x2, y2), the distance is

    d = sqrt((x2 - x1)^2 + (y2-y1)^2)

    The easiest way to figure the direction is to normalize the distance vector by dividing its components by the distance:

    difX = (x2 - x1)/d

    difY = (y2 -y1)/d

    now just multiply the force by both the difference vectors.

    gravX = difX * force

    gravY = difY * force

    Finally add the new gravity vector to the sprite's position vector:

    sprite.x += gravX;

    sprite .y += gravY;

    The gravitational constant G is again best fudged with to get the results you want. Generally in orbital calculations you'll want to adjust both time and gravity so it doesn't take months to see how an orbit transfer works, so getting the spirit of the formulas is more important than absolute exactness.

    Good luck!

    I've got a complete example (in Flash) at

    http://www.cs.iupui.edu/~aharris/flash/fg2r12/plan...

    and another in Python at

    http://www.cs.iupui.edu/~aharris/pygame/ch09/orbit...

    I actually haven't done it recently in Java, but one of my students did this last semester, and it worked out pretty well.

    Source(s): Flash Game Programming for Dummies (author) Game Programming - The L Line (author)
  • Imma M
    Lv 5
    1 decade ago

    to do a true to life physics you will also need to calculate the movement vectors and escape velocities. simulated realism can be achieved by comparing the directions, and applying the gravitation using a sin/cos formula to calculate angular changes in the movement to incorporate along with velocity changes.

    Think of it in terms of triangles. In a 2D application you would use your angle of motion as the hypotenuse, a line from your origin parallel to the ground as your adjacent side, and the next location in a linear movement would be your point to connect your hypotenuse to your adjacent side to form the opposite side. Then using trigonometry alter the angle of motion and speed according to the physics of the situation.

Still have questions? Get your answers by asking now.