Log in Page Discussion History Go to the site toolbox

XNA Gravity

From BluWiki

Return to the XNA Tutorial Menu Page.

Introduction

(Size)px

Games have featured gravity for over 45 years. In Spacewar! (1961) ships would fly around a star and be affected by the star's gravity.

With the creation of platformer games such as Donkey Kong in 1981, gravity became very important. Players would move between platforms and avoid obstacles by jumping. Jumping can only be done if gravity is there to bring the player back down.

This brief tutorial will show you how to add gravity to the example shown in the MSDN tutorial Your First Game: Microsoft XNA Game Studio in 2D.

1. Find the Update Function

2. Add Gravity

3. Add Friction

1. Find the Update Function

The update function is where everything in your game should be updated. It is one half of the game loop with the draw function composing the other half. Because gravity is updating an object's speed, the function for it should be in the update function.

2. Add Gravity

Gravity is a constant force pulling down. It only affects vertical movement. To represent this our function should only affect the Y speed.

At the end of the update function add this line:

 spriteSpeed.Y = spriteSpeed.Y + 16; 

This line will give gravity to the sprite. It does not matter which direction the sprite is moving, its downward speed will constantly be increasing, just as in a real life environment. The larger the number the more the gravity and vice-versa.


3. Add Friction

Your object should now fall like it is being affected by gravity and then bounce back up. However, you may have noticed that it keeps bouncing to the same height instead of getting lower every time. This is due to a simple fact of physics: almost all objects do not bounce and maintain perfect momentum.

When you drop a ball, it hits the ground at once speed and then bounces up slightly slower than it was going down. This is because some of its momentum was transferred to the surface it bounced off of instead of staying with the ball. To simulate this, we must change the way the ball bounces off the bottom of the screen.

Under the if statement that checks if the sprite hits the floor:

 if (spritePosition.Y > MaxY)
 {
     spriteSpeed.Y *= -1f;
     spritePosition.Y = MaxY;
 }

The -1 preserves full momentum. Instead, change the spriteSpeed to look like this:

 if (spritePosition.Y > MaxY)
 {
     spriteSpeed.Y *= -.9f;
     spritePosition.Y = MaxY;
 }

This provides some friction when the sprite hits the floor. If this value was kept at -1 the sprite would bounce at the same height every time. Instead, the .9 means the sprite only maintains 90% of its speed. The smaller the number the less bouncy the sprite will seem and vice-versa.

We are not done yet! Try running your program now and see what happens. Hopefully your ball is bouncing lower and lower each time, but does it completely stop?

You probably see now that your sprite continues sliding from side to side across the screen even after it has stopped bouncing up and down. We now need to add horizontal friction to simulate the real friction of an object sliding along the ground.

fine the same if statement from before:

 if (spritePosition.Y > MaxY)
 {
     spriteSpeed.Y *= -.9f;
     spritePosition.Y = MaxY;
 }

Now add a line to reduce horizontal speed every time the sprite hits the ground:

 if (spritePosition.Y > MaxY)
 {
     spriteSpeed.X *= .9f;
     spriteSpeed.Y *= -.9f;
     spritePosition.Y = MaxY;
 }

Now you are done! Your sprite is constantly being pulled down just as gravity does. It has friction to prevent it from preserving its full momentum as it bounces back up, and friction to slow its horizontal speed every time it hits the ground.

Further Study

This simple tutorial shows you how to make your objects appear to have gravity. But what would you do if you wanted to actually simulate real Earth gravity? The moons gravity?

First you would have to calculate how many pixels equals a standard unit of distance, i.e. a foot or a yard. Then you would multiply the gravity constant you are using (Earth's is 32.174 ft/s^2) by the number of pixels you decided on.

Ex. If a foot is equal to 10 pixels in your game, then your speed update function would look like:

   spriteSpeed.Y = spriteSpeed.Y(10*32.174);  //10 pixels per ft. * 32.175 ft/s^2

You can then adjust pixels per distance or the gravitational constant to change how gravity affects your objects. You can also adjust the friction numbers to try and simulate the actual friction of your object hitting the ground. Have fun experimenting!

Return to the XNA Tutorials Menu Page.

References

History of Video Games, http://en.wikipedia.org/wiki/History_of_video_games

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 10 September 2009, at 19:25.
Disclaimers - About BluWiki