Game Development of .Nettrix: GDI+ and Collision Detection
(Page 1 of 22 )
This chapter introduces the basic concepts of GDI+, the extended library for native graphic operations on Windows systems, and discuss one of the most important aspects of game development: collision detection algorithms. (From the book
Beginning .NET Game Programming in C# by David Weller, et al., Apress, 2004, ISBN: 1590593197.)
In this chapter we introduce you to the basic concepts of GDI+, the extended library for native graphic operations on Windows systems, and discuss one of the most important aspects of game development: collision detection algorithms. Although game developers use GDI+ functions to draw images on screen, collision detection algorithms are responsible for making the drawings interact with each other. This allows a program to know when an image is over another one and to take the appropriate action, such as bouncing a ball when it hits a wall.
To accomplish these goals and illustrate these concepts, we’ll show you how to create a game called .Nettrix. “Hello World” is always the first program that’s written when learning a new programming language. When learning to program games, Tetris is considered to be the best game to try first. In this simple game, you can see many basic concepts at work—for example, basic graphic routines, collision detection, and handling user input.
To begin, you’ll look at the basic GDI+ concepts and examine the idea of collision detection algorithms, so you’ll have the necessary technical background to code the sample game for this chapter (see Figure 1-1).

Figure 1-1. .Nettrix, this chapter's sample game
Basic GDI+ Concepts GDI+ is the new .NET Framework class-based application programming interface (API) for 2-D graphics, imaging, and typography.
With some substantial improvements over the old GDI, including better performance and the capacity to run even on a 64-bit system, GDI+ is worth a look. The new features in GDI+ are discussed in the following sections.
Path Gradients Path gradients allow programs to fill 2-D shapes with gradients with great flexibility, as shown in Figure 1-2.

Figure 1-2. Using path gradients
Alpha Blending GDI+ works with ARGB colors, which means that each color is defined by a combination of red, green, and blue values, plus an alpha value relating to its degree of transparency. You can assign a transparency value from 0 (totally transparent) to 255 (opaque). Values between 0 and 255 make the colors partially transparent to different degrees, showing the background graphics, if any are present.
Figure 1-3 shows a rectangle with different degrees of transparency; if you had an image below it, you could see it, just like looking through glass.

Figure 1-3. Changing the alpha from 0 to 255 in a solid color bitmap
Cardinal Splines Cardinal splines allow the creation of smooth lines joining a given set of points, as shown in Figure 1-4.

Figure 1-4. Creating a smooth curve that joins points with a spline
As you can see, the spline curve has fixed starting and ending points (in Figure 1-4, the points marked 1 and 4), and two extra points that will “attract” the curve, but won’t pass through them (points 2 and 3).
Applying Transformations to Objects Using a 3× 3 Matrix
Applying transformations (rotation, translation, or scale) is especially useful when dealing with a sequence of transformations, as they speed up performance. A sample of some transformations is shown in Figure 1-5.

Figure 1-5. Applying a rotation and scale tranformation over a figure
Antialiasing
Antialiasing is the smoothing of graphics, avoiding a stepped look when, for example, a bitmap is enlarged. An image exemplifying this is shown in Figure 1-6.

Figure 1-6. Applying antialiasing to an image
Note: In this book, we’ll show examples of the first two new GDI+ features: path gradients in this chapter and alpha blending in the next. There are many code examples for the other GDI+ features in the .NET Framework SDK. |
This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Performing Graphic Operations with a Graphics Object >>
More .NET Articles
More By Apress Publishing