Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Obviously, 1016 Newtons would crush you into a pancake, so you must be doing something wrong. The problem is that you’re assuming that the Earth is a point mass that is 1.0 meters away. A better approximation would be to use the radius of the Earth (the center of mass) as the distance, which is 6.38×106 m:

    F = 6.67×10-11 * 70 kg * 5.98×1024 kg / (6.38×106 m)2

      =  685.93 N.

Now that seems more reasonable. As a sanity check, on Earth 1 lb. is equal to 4.45 N, so converting the force to lbs. produces

    685.93 N / (4.45 N / 1 lb.) = 155 lbs.

And this was the starting weight! Anyway, now that you know how to compute the force between two objects you can use this simple model in games. Or course, you don’t have to use the real gravity constant G = 6.67×10-11, you can use whatever you like—remember, you are god. The only thing that’s important is the form of the equation that states that the gravity between two objects is proportional—to a constant times the product of their masses divided by the distance squared between the objects’ centers.

#### Modeling a Gravity Well

By using the formulation explained in the preceding section, you might, say, model a black hole in a space game. For example, you might have a ship that is flying around on the screen near a black hole, and want the ship to get sucked in if it gets too close. Using the gravitational equation is a snap. You would make up a constant G that worked well in the virtual game world (based on screen resolution, frame rate, etc.) and then simply set an arbitrary mass for the ship and one for the black hole that was much larger. Then you would figure out the force and convert the force to acceleration with F=m*a. You would simply vector or fly the ship directly toward the black hole each frame. As the ship got closer the force would increase until the player couldn’t get free!

As an example of a black hole simulation (which is nothing more than two masses, one much larger than another) take a look at `DEMO13_3.CPP|EXE`. It’s a space simulator that allows you to navigate a ship around the screen, but there’s a black hole in the middle that you have to deal with! Use the arrows keys to control the ship. Try to see if you can get into an orbit!

The next use of gravity in games is to simply make things fall from the sky or off buildings at the proper rate. This is really the special case that we talked about before, that is, one object has a mass much greater than the other. However, there’s one more constraint and that is that one object is fixed—the ground. Take a look at Figure 13.14; it depicts the situation that I’m describing.

Figure 13.14 Gravitational attraction.

In this case, there are a number of assumptions that we can make that will make the math work out simpler. The first is that the acceleration due to gravity is constant for the mass that is being dropped, which is equal to 9.8 m/s2 or 32 ft/s2. Of course, this isn’t really true, but is true to about 23 decimal places. If we know that the acceleration of any object is simply 9.8 m/s2 then we can just plug that into our old motion equation for velocity or position. Thus, the formula for velocity as a function of time with Earth gravity is

    V(t) = v0 + 9.8 m/s2*t.

> Tip: I used the velocity to modify the position rather than modifying the position directly with the position formula. This is simpler.

And position is

    y(t) = y0 + v0*t + 1/2 * 9.8m/s2 * t2.

In the case of a ball falling off a building we can let the initial position x0 be equal to 0 and the initial velocity v0 also equal 0. This simplifies the falling object model to:

    y(t) = 1/2 * 9.8m/s2 * t2.

Furthermore, you are free to change the constant 9.8 to anything you like and t represents the frame number (virtual time) in a game. Taking all that into consideration, here’s how you would make a ball fall from the top of the screen:

    int y_pos      = 0, // top of screen

        y_velocity = 0, // initial y velocity

        gravity    = 1, // do want to fall too fast

    // do  gravity loop until object hits

    // bottom of screen at SCREEN_BOTTOM

    while(y_pos < SCREEN_BOTTOM)

         {

         // update position

         y_pos+=y_velocity;

         // update velocity

         y_velocity+=gravity;

         } // end while

You may be asking how to make the object fall with a curved trajectory. This is simple—just move the x position at a constant rate each cycle and the object will seem like it was thrown off rather then just dropped. The code to do this follows:

    int y_pos      = 0, // top of screen

        y_velocity = 0, // initial y velocity

        x_velocity = 2, // constant x velocity

        gravity    = 1, // do want to fall too fast

    // do  gravity loop until object hits

    // bottom of screen at SCREEN_BOTTOM

    while(y_pos < SCREEN_BOTTOM)

         {

         // update position

         x_pos+=x_velocity;

         y_pos+=y_velocity;

         // update velocity

         y_velocity+=gravity;

         } // end while

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.