Previous Section  < Day Day Up >  Next Section

Forces

When you're getting ready to start the physics simulation portion of the code, the first step is to brainstorm all the forces that are acting on the object you want to move. The sum (or total) of all those forces determines how the object moves. Some of the forces are obvious: an enemy punches or kicks the player, the player pushes a large object out of the way, a ball is thrown, or a missile is launched. However, some less-obvious forces often get overlooked: gravity, friction, wind resistance (if it's significant), or the normal force. Let's stop and take a closer look at how each of these forces works, starting with gravity.

Earlier, we discussed acceleration due to gravity, which is represented in most formulas as the constant g.

Whenever we looked at the vertical components of the motion, we used –g or –9.8m/s2 for the acceleration. Now you can use that gravitational constant to calculate the force due to gravity, also known as weight. Weight is actually a force, which means it's a vector, and its direction is always down toward the center of the earth. The magnitude of weight is actually the object's mass times the acceleration due to gravity.

Weight

w = mg

where m = mass and g = acceleration due to gravity

(–9.8m/s2 on Earth).


While most games won't need to differentiate between mass and weight, in extremely detailed games (or any simulation) such a distinction is needed. An example of some instances where this would be true is rag-doll physics or any form of driving simulation where the amount that the tires skid on turns needs to be calculated. The following function calculates weight. Notice that the return is not simply a number, but an array of three numbers. Why? Because weight is a vector which acts upon the object, not simply a scalar value.


// purpose: to calculate the weight of an object based on its mass

// input:   mass - the mass of the object

//          grav - the amount of constant acceleration due to gravity

// output:  an array of 3 floats representing the vector of weight

float *calcWeight3D(float mass, float grav)

{

    // This will hold the weight of the object until it is returned

    // The value in [1] will be the only number changed, since gravity

    // is only applied along the Y axis

    float weight[3] = { 0, };



    // Calculate the weight, it is assumed that grav is a negative number

    weight[1] = mass * grav;



    // Return our answer

    return weight;

}

NOTE

An object's weight can change if the acceleration due to gravity changes. For example, gravity on the moon is 1/6 as strong as it is on Earth, so an object's weight would be 1/6 of what it is on Earth. However, the mass always stays the same no matter where the object is.


Let's look a little closer at mass and weight. The biggest difference between the two is that mass is a scalar quantity, and weight is a vector quantity. Even though we tend to use these two terms interchangeably in everyday language, in physics they are two very different quantities. The units are often a dead giveaway as to which quantity you are working with. Mass often is measured in either grams or kilograms, whereas weight is measured in pounds or newtons.

You might not be familiar with newtons, so let's define this new unit. If mass is measured in kilograms, when you multiply it by the acceleration due to gravity (g = –9.8m/s2) to calculate the object's weight on Earth, you end up with the unit of kg*m/s2. This unit is quite awkward to say, so it has been named the Newton (written N) after Sir Isaac Newton. Unfortunately, you might be accustomed to approximating weights in pounds, so you might need to convert a weight from pounds to newtons. The conversion factor is 1N = 0.2248lbs.

Newtons

1N = 1kg*m/s2

1N = 0.2248lbs


NOTE

Most forces are also measured in newtons, so this is just another reason why metric units are the easiest to work with.


Example 11.1: Calculating Weight

If an object in your game has a mass of 50kg, what is its weight in pounds?

Solution
  1. Find the weight in newtons:

    w = mg = 50kg(–9.8m/s2) = –490kg*m/s2 = –490N

  2. Convert from newtons to pounds:

    graphics/11equ01.gif


More often in game programming, you'll find that you estimate an object's weight in pounds out of habit, and then you can convert it to newtons or use it to find the mass.

Example 11.2: Calculating Mass

Suppose you estimate a character's weight (on Earth) to be 200lbs. What is its mass?

Solution
  1. Convert from pounds to newtons:

    graphics/11equ02.gif


  2. Use the weight in newtons to calculate the mass in kilograms:

    w = mg

    –890N = m(–9.8m/s2)

    m = –890N/–9.8m/s2 = 90.78kg

Have you ever wondered why, if gravity (or your weight) is always pulling you down, you don't perpetually fall? Something is stopping you from falling all the way to the Earth's core. That's right—the ground. The ground actually exerts a force on you as well; it's called the normal force. The normal force is the force of the surface your object is sitting on that counteracts gravity and keeps it from falling any farther. If the surface is suddenly removed, the object falls until it hits another surface with a normal force that counteracts gravity. It's called the normal force because the term "normal" indicates perpendicular, and this force is always perpendicular to the surface the object is on. Chapter 4, "Vector Operations," discussed how to find the surface normal by taking the cross-product of the two vectors that define the surface.

Therefore, if you are simply standing on the ground, gravity is pulling you down, but the ground is exerting a force straight up (perpendicular to the ground) to keep you from falling through.

Example 11.3: Normal Force on a Flat Surface

Suppose you estimate a vehicle's weight (on Earth) to be 1,000lbs. What must the normal force of the road be to keep the car from sinking?

Solution
  1. Convert the weight from pounds to newtons:

    graphics/11equ03.gif


The normal force needs to cancel out the weight of the car so that it doesn't float up in the air or sink through the road. Therefore, the normal force must be 4448.4N.

NOTE

Weight is always a negative value, because the direction is down toward the center of the Earth. That is why the normal force must always have a positive value.


Example 11.4: Normal Force on an Inclined Plane

Suppose a ball that weighs 0.25lbs is rolling down a ramp, which is positioned at a 30° incline. What is the normal force of the ramp?

Solution
  1. Convert the weight from pounds to newtons:

    graphics/11equ04.gif


  2. Remember that the normal force must be perpendicular to the surface of the ramp, so it's not simply the opposite of the weight this time. You want the opposite of the component of the weight that is perpendicular to the ramp, as shown in Figure 11.1.

    Figure 11.1. Normal force on an inclined plane.

    graphics/11fig01.gif

  3. Notice that the component of the weight that's perpendicular to the ramp's surface is wcos30° = –1.112N(cos30°) = –0.963N. Therefore, the normal force must be 0.963N.

NOTE

This normal force accounts for the ball's slower acceleration downward compared to freefall if there were no surface.


Another type of force that often gets ignored is the force due to friction. In gaming it might be a good idea to ignore friction, but that's entirely up to the programmer. If the force of friction is relatively insignificant, and the player won't even notice it, why waste precious processing power on it? However, if you are attempting to code a perfectly realistic simulator, it might be extremely important to spend the extra clock cycles calculating friction. It all depends on how you want to balance speed against realism.

NOTE

Friction winds up playing a pretty prevalent part in many games. Cars in GTA: Vice City and Gran Turismo 3 skid more easily on wet pavement, and Mario slides all over the ice in Mario 64. Not every game accommodates it, but several do incorporate friction as a core game element.


There are actually two types of friction: static and kinetic. Static friction is the force that keeps an object from moving initially, and kinetic friction is the force that slows down an object after it gets moving.

Always calculate static friction first. If all the other forces added up are less than the static friction, the object will not move. As soon as the other forces become greater than the static friction, the object starts to move, and kinetic friction takes over. Both types of friction are completely dependent on the two surfaces coming into contact with each other. The smoother the surfaces, the less friction there is. This means that a metal object on ice has significantly less friction than rubber tires on dry pavement. When you're ready to calculate friction, you need a table of coefficients to use as a guide. Table 11.1 lists some common surfaces to give you an idea of what some coefficients are. Just remember that the smoother the surfaces, the lower the coefficient.

NOTE

The Greek letter m (mu) is the standard symbol for the coefficient of friction.


Table 11.1. Coefficient of Friction

Surface Friction (mK)

Static Friction (mS)

Kinetic

Steel on steel (dry)

0.6

0.4

Steel on steel (greasy)

0.1

0.05

Teflon on steel

0.041

0.04

Brake lining on cast iron

0.4

0.3

Rubber on concrete (dry)

1.0

0.9

Rubber on concrete (wet)

0.30

0.25

Metal on ice

0.022

0.02

Steel on steel

0.74

0.57

Aluminum on steel

0.61

0.47

Copper on steel

0.53

0.36

Nickel on nickel

1.1

0.53

Glass on glass

0.94

0.40

Copper on glass

0.68

0.53

Oak on oak (parallel to grain)

0.62

0.48

Oak on oak (perpendicular to grain)

0.54

0.32

Now you can use these coefficients to calculate both types of friction.

Static friction:

FS = –mSN

where N is the normal force.

Note that the static frictional force is always the opposite direction of the normal force of the surface.

Kinetic friction:

FK = –mKN

where N is the normal force.

Example 11.5: Calculating Friction

Suppose you're coding a racing game, and the car weighs approximately 1,500lbs. What values might you use for the static and kinetic friction of the rubber tires on a dry road?

Solution
  1. Convert the weight from pounds to newtons:

    graphics/11equ05.gif


  2. Find the normal force. If the road is flat, the normal force must be N = 6672.6N.

  3. To find the static friction, look up the coefficient of static friction between rubber and dry concrete in Table 11.1. In this case mS = 1.0, so the static friction is

    FS = –mSN = –1.0(6672.6N) = –6672.6N

  4. To find the kinetic friction, look up the coefficient of kinetic friction between rubber and dry concrete in Table 11.1. In this case mK = 0.9, so the kinetic friction is

    FK = –mKN = –0.9(6672.6N) = –6005.3N

So now it's time to combine everything we've learned about normals and friction into some functions which may have an applicable use in a game. Suppose we are presented with the problem of an object—for instance a box, which is sitting at rest on an incline that is slowly increasing in angle. For a realistic simulation, at some point the box must begin to slide down the ramp, so how can we figure this out? First and foremost, we must be able to calculate the normal force applied to the object from the ramp, and secondly, the opposing perpendicular force which is trying to pull the box down the ramp. Using the normal force and the coefficient of friction between the two surfaces, we can then determine the amount of force needed for the object to begin moving. Once the force perpendicular to the normal exceeds that force, the box will begin to slide. Finally, note that weight has been simplified to a scalar quantity here, for ease of use.


// purpose: to determine whether an object on an incline will slide

// input:   angle - the current angle of the incline

//          weight - the weight of the object

//          fric_coeff - the coefficient of static friction between surfaces

// output:  true if the object should slide, else false

bool checkForMotion(float angle, float weight, float fric_coeff)

{

    // Calculate the normal force being exerted by the ramp

    float normal = weight * cosf(angle * PI / 180);

    // Calculate the force perpendicular to the normal

    float perpForce = weight * sinf(angle * PI / 180);

    // Calculate the amount of static friction keeping the object at rest

    float stat_friction = fric_coeff * normal;

    // Return true if the object should slide, else false

    return perpForce > stat_friction;

}

Let's take this problem one step further and calculate just how fast the box should be accelerating. If we subtract the amount of the kinetic friction (the force which is trying the stop the object) from the amount of force which is perpendicular to the normal (the force which is trying to move the object), we will end up with the amount of force which is actually being applied to the object. Knowing that F = ma and subsequently that a = F / m, we can easily figure out the acceleration and return it. Note that this function call should only be allowed if the checkForMotion() function call returns true, as otherwise it would return a negative acceleration and the box would actually slide up the ramp.


// purpose: to determine whether an object on an incline will slide

// input:   angle - the current angle of the incline

//          weight - the weight of the object

//          fric_coeff - the coefficient of kinetic friction between surfaces

//          mass - the mass of the object

// output:  the acceleration of the object

float calcAccel(float angle, float weight, float fric_coeff, float mass)

{

    // Calculate the normal force being exerted by the ramp

    float normal = weight * cosf(angle * PI / 180);

    // Calculate the force perpendicular to the normal

    float perpForce = weight * sinf(angle * PI / 180);

    // Calculate the amount of static friction keeping the object at rest

    float kin_friction = fric_coeff * normal;

    // Calculate the sum of forces acting upon the object

    float total_force = perpForce – kin_friction;

    // return the acceleration of the object

    return total_force / mass;

}

After you've brainstormed all the significant forces acting on an object, the next step is to add them all up to find the net or total force, which is often written
Previous Section  < Day Day Up >  Next Section