< 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; };
Look at the circle shown in Figure 2.8. Figure 2.8. The graph of (x1) 2 + (y2) 2 = 4.
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
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.
Example 2.8: Sketching a CircleRoughly sketch the circle x 2 + ( y +1) 2 = 9. SolutionYou need to know only two things to sketch a circle: the center and the radius.
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 CircleRoughly sketch the circle x 2 + y 2 = 16. SolutionYou need to know only two things to sketch a circle: the center and the radius.
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 EquationIn 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.
SolutionTo determine an equation, you need the circle's center and radius.
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.
Notice what happens again when the center is the origin, (0,0,0).
Example 2.11: The Center and Radius of a SphereIn 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. SolutionTo determine an equation, you need the sphere's center and radius.
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-AssessmentGive the center and radius of the following circles:
Find the equation of a circle that meets the following criteria:
Give the center and radius of the following spheres:
Find the equation of a sphere that meets the following criteria:
|
< Day Day Up > |