Circles and Spheres

 < Day Day Up > 

It's very important to be familiar with the equation of a circle. You might want to draw a circle on the screen, which means graphing the equation. You might want to trace the motion of an object on a circular path . You might even want to use a bounding circle on your object for collision detection. In any of these cases, you need to manipulate the equation of a circle. Remember that in a flat plane (such as the screen), a circle is the set of all points at a given distance, called the radius , from a given fixed point, the center . Therefore, just those two elements, the center and the radius, can determine an equation for the circle. Which equation gives you the distance between the center and a point on the circle? That's rightthe Pythagorean theorem. Notice the similarity between the Pythagorean theorem and the equation of a circle.

In code, we can define circles or spheres in the same way as math: by storing the center and the radius. The following two structures are an example of a circle and sphere as defined in code:

 struct circle {     float center[2];     float radius; }; struct sphere {     float center[3];     float radius; }; 

The Equation of a Circle

( x h ) 2 + ( y k ) 2 = r 2

where the center is ( h , k ) and the radius is r .


Look at the circle shown in Figure 2.8.

Figure 2.8. The graph of (x1) 2 + (y2) 2 = 4.

graphics/02fig08.gif

It's centered at point (1, 2) and has a radius of 2. Now look up the distance formula from the beginning of this chapter. For each point ( x, y ) on the circle, you have

( x 1) 2 + ( y 2) 2 = 2 2

or

( x 1) 2 + ( y 2) 2 = 4

You might recognize the Pythagorean theorem more easily when the circle is centered at the origin. If you plug in (0,0) for ( h , k ), look at what happens to the circle equation.

Equation of a Circle Centered at the Origin

x 2 + y 2 = r 2

where the center is (0,0) and the radius is r .


Example 2.8: Sketching a Circle

Roughly sketch the circle x 2 + ( y +1) 2 = 9.

Solution

You need to know only two things to sketch a circle: the center and the radius.

  1. To find the center pattern, match back to the general form, which is ( x h ) 2 + ( y k ) 2 = r 2 . The center has coordinates ( h , k ). Looking at the equation, x 2 + ( y +1) 2 = 9, h =0 and k =1. Therefore, the center of this circle is (0,1).

  2. The other thing you need is the radius. This one is easy; just look at the number by itself on the right side. That number is r 2 , so just take the square root, and you have the radius. In this case, r 2 =9, so r=3.

  3. Now that you have the center (0,1) and the radius of 3, you can sketch the circle. This is done for you in Figure 2.9.

    Figure 2.9. The graph of x 2 + (y+1) 2 = 9.

    graphics/02fig09.gif

NOTE

Be careful with the plus and minus signs when looking for the center. Notice in the general form that minus signs are inside the parentheses. This means that if there's a plus sign, it's really minus a negative, so your h or k is actually a negative number, like the k in Example 2.8.


Example 2.9: Sketching Another Circle

Roughly sketch the circle x 2 + y 2 = 16.

Solution

You need to know only two things to sketch a circle: the center and the radius.

  1. To find the center pattern, match back to the general form, which is ( x h ) 2 + ( y k ) 2 = r 2 . The center has coordinates ( h , k ). Looking at the equation, x 2 + y 2 = 16, you can see that there are no parentheses and no h and k . This just means that h and k are 0, so the center of this circle is the origin (0,0).

  2. The other thing you need is the radius. Don't forget that the 16 is r 2 , so just take the square root, and you have the radius, which in this case is 4.

  3. Now that you have the center (0,0) and the radius of 4, you can sketch the circle. This is done for you in Figure 2.10.

    Figure 2.10. The graph of x 2 + y 2 = 16.

    graphics/02fig10.gif

Now that you can visualize a circle based on its equation, let's go the other way. Suppose you want to generate an equation for a circle based on a situation in your game.

Example 2.10: Finding a Circle Equation

In your game, you have decided to use a bounding circle for the collision detection on your car as it drives up an incline. The car's center point is (20,50), and the farthest vertex has coordinates (60,80), as shown in Figure 2.11. Find an equation to represent the bounding circle.

Figure 2.11. A bounding circle on a car.

graphics/02fig11.gif

Solution

To determine an equation, you need the circle's center and radius.

  1. The center of your object has been given: (20,50). This means that h =20 and k =50. You can substitute those values into the general equation for a circle, which gives you

    ( x 20) 2 + ( y 50) 2 = r 2

  2. Now you just need to find the radius. Remember that the radius is defined as the distance from the center to any point on the circle, so all you have to do is use the distance formula for the center point (20,50) and the point on the circle (60,80):

    graphics/02equ13.gif


  3. Now that you have the radius of 50, you can plug it into the equation, which gives you

    ( x 20) 2 + ( y 50) 2 = 2500

Although it makes sense to discuss circles in 2D, you need to address spheres for three dimensions. A sphere is what you get when a circle revolves about its center point. Picture a basketball or a tennis ball; they are examples of spheres. Spheres can be used to define objects such as balls, or they can be used as bounding geometry for complex-shaped objects such as cars or spaceships. Quite often, game developers use bounding spheres to simplify collision detection between two objects that don't require precise collision. Just like a circle, the sphere is defined by a center and a radius. The only difference is that the center point has three coordinates rather than two.

Equation of a Sphere

( x h ) 2 + ( y k ) 2 + ( z l ) 2 = r 2

where the center is ( h , k , l ) and the radius is r .


Notice what happens again when the center is the origin, (0,0,0).

Equation of a Sphere Centered at the Origin

x 2 + y 2 + z 2 = r 2

where the center is (0,0,0) and the radius is r .


Example 2.11: The Center and Radius of a Sphere

In your game, you have decided to use a bounding sphere for the collision detection. The center point of your object is (20,50,30), and the farthest vertex has coordinates (60,80,20). Find an equation to represent the bounding sphere.

Solution

To determine an equation, you need the sphere's center and radius.

  1. The center of your object has been given: (20,50,30). This means that h =20, k =50, and l =30. You can substitute these values into the general equation for a sphere, which gives you

    ( x 20) 2 + ( y 50) 2 + ( z +30) 2 = r 2

  2. Now you just need to find the radius. Remember that the radius is defined as the distance from the center to any point on the sphere, so all you have to do is use the distance formula with the center point (20,50,30) and the point on the sphere (60,80,20):

    graphics/02equ14.gif


  3. Now that you have the radius of graphics/02inl01.gif , you can plug that into the equation, which gives you

    ( x 20) 2 + ( y 50) 2 + ( z +30) 2 = 2600

NOTE

Just like with the circle, be careful with the plus and minus signs when plugging in the center's coordinates.


Now that we've discussed the equation of a circle and the equation of a sphere, you can use them to define round objects in your game, such as a ball. You can use them to define a circular path for an object. You can also use them as bounding geometry for collision detection, which is addressed in the next section.

Self-Assessment

Give the center and radius of the following circles:

1.

( x 30) 2 + ( y 10) 2 = 400

2.

( x +20) 2 + ( y 90) 2 = 100

3.

( x +50) 2 + y 2 = 625


Find the equation of a circle that meets the following criteria:

4.

center = (40,25), radius = 30

5.

center = (0,0), radius = 15

6.

center = (10,40), circle goes through the point (20,50)


Give the center and radius of the following spheres:

7.

( x 10) 2 + ( y 30) 2 + ( z 50) 2 = 1600

8.

x 2 + y 2 + z 2 = 100

9.

( x 50) 2 + y 2 + ( z +40) 2 = 1


Find the equation of a sphere that meets the following criteria:

10.

center = (40,25,30), radius = 10

11.

center = (0,0,0), radius = 22

12.

center = (10,0,60), circle goes through the point (10,50,30)


 < Day Day Up > 


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net