Previous Section  < Day Day Up >  Next Section

Converting Units Between Systems

Converting between different systems of units is a little more complicated than staying within metrics. That's why I suggest estimating distances in meters and estimating speeds in kilometers per hour. Unfortunately, if you grew up in the United States, it's a lot more natural to estimate quantities in English units, such as speeds in miles per hour.

In game programming, you'll mostly be dealing with length and time quantities, so we'll focus on those. There will be times when you'll need to convert within the English system, and there will be times when you'll need to convert between the English and metric systems. In either case, the process is the same.

When you are converting units, it is very important to keep your work organized. The premise behind this procedure is that when you multiply a number by 1, it stays the same. So if you start with the quantity you want to convert and multiply it by 1 several times, it will not change the quantity. Remember that any fraction whose numerator is equal to the denominator equals 1. The fraction graphics/07inl01.gif equals 1. The fraction (60 seconds/1 minute) equals 1. All you want to do is multiply the original quantity by a bunch of conversion factors (or ratios), all of which are fractions equal to 1, fractions whose numerator is equal to the denominator. An example of a conversion factor is (60min/1hr). We all know that 60 minutes equals 1 hour, so that fraction equals 1.

It is also a conversion factor, because it shows an equality between two different units. You can use that particular conversion factor to convert from hours to minutes. For example, to convert 3 hours to minutes, just multiply by (60min/1hr):

graphics/07equ01.gif


Take a close look at the conversion factor (60min/1hr). If it were flipped, it would still be a conversion factor, but it would convert minutes to hours instead. Whenever you're not sure which way to flip, use the units as a guide. When you multiply, you want the old units to cancel out and the new units to stay. Always treat the original quantity as a number over 1 so that you know which way to flip the conversion factor for the units to line up and cancel the way you want. Table 7.1 lists all the conversion factors you should need.

Table 7.1. List of Conversion Factors

Length

Time

1m = 39.37in = 3.281ft

1s = 1000ms

1in = 2.54cm

1min = 60s

1km = 0.621mi

1hr = 3600s

1mi = 5,280ft = 1.609km

1 year = 365.242 days

When coding the solutions to these problems, it's a wise idea to make a few constants that can be referenced quickly in code. For example:


#define KM_TO_MILES 1.609

#define KM_TO_METERS 1000

#define DAYS_TO_WEEK 7

#define HOURS_TO_DAY 24

#define HOURS_TO_MIN 60

#define MIN_TO_SEC 60

These values are easy enough to determine from looking at Table 7.1 or other sources. Using a constant can make the actual calculation of the values in the game a little easier to read. We also gain the benefit of making wholesale changes throughout the application should one of these values need to be changed. On Earth that might be unlikely, but then again many games don't take place on Earth. A single day on Saturn is a little more than 10.5 Earth hours.

The best way to solidify this concept is to work through a few examples. Let's start with a simple time conversion.

Example 7.3: Time Conversion

How many seconds are in 2 weeks?

Solution
  1. Start with the original quantity, 2 weeks.

  2. Stack to the right a series of conversion factors that will convert weeks to seconds. Try to keep the fractions lined up so that you're less likely to make a mistake when you go back to multiply:

    graphics/07equ02.gif


  3. Make sure that the fractions are flipped in such a way that the units cancel the way you want.

  4. Multiply all the fractions:

    graphics/07equ03.gif


As mentioned earlier, quite often you'll estimate quantities in English units but then program in metric units. Let's look at an example of that process.

Here's a quick function that will take a number of days and convert it to seconds. See if you can make your own derivatives of this function to approximate time conversion:


float convertDaysToSeconds(float days)

    {

      float seconds;

      seconds = days*HOURS_TO_DAY*HOURS_TO_MIN*MIN_TO_SEC;

      return seconds;

    }

Take note of how the constants are used here to make the code clean. This is a good technique for making code easy to read.

Example 7.4: Distance Conversion

Suppose you estimate that the character in your game has run 5 miles. Unfortunately, your team has decided to program in metric units. How many meters has he run?

Solution
  1. Start with the original quantity, 5 miles.

  2. Stack to the right a series of conversion factors that will convert miles to meters. Try to keep the fractions lined up so that you're less likely to make a mistake when you go back to multiply:

    graphics/07equ04.gif


  3. Make sure that the fractions are flipped in such a way that the units cancel the way you want.

  4. Multiply all the fractions:

    graphics/07equ05.gif


Coding this out is also pretty straightforward:


float convertMilesToMeters(float miles)

    {

      float meters;

      meters = miles*KM_TO_MILES*KM_TO_METERS;

      return meters;

    }

NOTE

Most books use "mi" for miles and "m" for meters when labeling units. This book follows the same standard.


When dealing with quantities that have a fraction in the unit, this process becomes slightly more complicated. For example, speeds can be measured in miles per hour (mi/hr). Many errors are made when dealing with the hours on the bottom of the fraction. One way to avoid errors is to write a quantity such as 55mi/hr as 55mi/1hr instead. This might help you keep the conversion factors lined up the way you want. Example 7.5 demonstrates this process.

Example 7.5: Speed Conversion

Suppose you estimate that the car in your racing game is currently going 180mi/hr. Your team has decided to program in metric units. What is its speed in m/s?

Solution
  1. Start with the original quantity, 180mi/hr. Write it as 180mi/1hr.

  2. Stack to the right a series of conversion factors that will perform two separate conversions: miles to meters in the numerator and then hours to seconds in the denominator. Try to keep the fractions lined up so that you're less likely to make a mistake when you go back to multiply:

    graphics/07equ06.gif


  3. Make sure that the fractions are flipped in such a way that the units cancel the way you want. Notice that by keeping the 1 hour on the bottom of the fraction, it is easier to determine which way to flip the last two conversion factors.

  4. Multiply all the fractions:

    graphics/07equ07.gif


This conversion can also be coded out in a pretty straightforward manner:


float convertSpeed(float miles)

    {

      float meters;



      meters =

       (miles*KM_TO_MILES*KM_TO_METERS)/(HOURS_TO_MIN*MIN_TO_SEC);



      return meters;

    }

The last complication arises when you have a unit that is squared. For example, acceleration always has a unit of length divided by a unit of time squared, such as m/s2. Remember that s2 is s*s, not just seconds by itself. This means that when you convert, both seconds need to be converted. Let's look at an example to demonstrate this process.

Example 7.6: Acceleration Conversion

Suppose you estimate that the car in your racing game is currently accelerating at a rate of 20,000mi/hr2. Unfortunately, your team has decided to program in metric units. What is the acceleration in m/s2?

Solution
  1. Start with the original quantity, 20,000mi/hr2. Write it as 20,000mi/1hr2 so that the time squared stays on the bottom of the fraction.

  2. Stack to the right a series of conversion factors that will convert miles to meters and hours squared to seconds squared. Make sure you convert from hours to seconds twice, because the unit is hr2, not just hours. Also, make sure the fractions are flipped in such a way that the units cancel the way you want. Try to keep them lined up so that you're less likely to make a mistake when you go back to multiply:

    graphics/07equ08.gif


  3. Multiply all the fractions:

    graphics/07equ09.gif


No matter what type of units you need to convert, the process is always the same: You multiply the original quantity by a series of fractions equal to 1 (conversion factors) that eventually bring you to the desired units. Be careful when doing this by hand. Try to keep the fractions lined up so that you don't accidentally divide when you meant to multiply. Also, keep in mind what units you're trying to reach. It's easy to start stacking conversion factors and forget where you were originally going with them. Most importantly, when you're working on a development team, always remember to establish a common set of units before you start programming anything. Conflicting units can lead to disastrous results—just ask NASA!

Let's take a look at how to code this conversion. Here is a function that will convert acceleration from MPH2 to m/s2:


float convertAccelMilesToMeters(float miles){

      float accelMeters;

      accelMeters = (miles*KM_TO_MILES*KM_TO_METERS) /

(HOURS_TO_MIN*MIN_TO_SEC*HOURS_TO_MIN*MIN_TO_SEC);

      return accelMeters;

     }

Note again the usage of defines in this formula. When the more commonly used conversions are known they are usually computed ahead. For example, instead of converting hours to minutes, then minutes to seconds, we could have just made the quicker jump from hours to seconds. The example is provided here to map with the previous explanation more than to identify the fastest way to handle the problem. Programming physics in a game that responds quickly is a big challenge that needs to reduce the processing as much as possible. Look for such opportunities in your own code.

Self-Assessment

1.

How many minutes are in 3 days?

2.

How many centimeters are in 10 inches?

3.

Suppose you estimate that the car in your racing game has gone 12 miles. Unfortunately, your team has decided to program in metric units. How many meters has it traveled?

4.

Suppose you estimate that the car in your racing game is currently going 225mi/hr. Your team has decided to program in metric units. What is the car's speed in km/hr?

5.

After you convert the speed in question 4 to km/hr, you find out that the rest of your team is using meters and seconds. What is 225mi/hr in m/s?

6.

What is 9.8m/s2 in mi/hr2?


    Previous Section  < Day Day Up >  Next Section