Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
### Summary In this chapter, we discussed several techniques for implementing sprites and sprite animation. We looked at various DirectDraw methods for implementing transparency, and we also took a look at what is required for detecting collisions between two sprites. We also lightly covered some of the more common animation issues facing developers. When writing sprite manipulation code, it is important to keep these points in mind: • The most important graphical elements of any game are those graphics with which the user interacts. Sprites provide the animated elements of a game that make the gaming environment come alive. These can take the form of images that the player directly interacts with, such as a spaceship, or background elements that are non-interactive yet provide animation that makes the environment richer, deeper, and more believable • To produce the illusion of animation, a series of images is required, where each image shows the character or object to be animated in a slightly different position than the image before it, like a filmstrip. When each image is rapidly displayed one after another, the human brain expects to see smooth motion, and any gaps in movement from one frame to another will be automatically filled in. • To produce the illusion of motion, when the application prepares to draw the next frame of animation, simply change the horizontal or vertical coordinate at which the image will be drawn into the destination. If these coordinates keep changing, the animated image will be drawn at different positions. • A few items of information need to be tracked for each sprite. In particular, its current x and y coordinate must be tracked so the application knows where to draw the sprite. A horizontal and vertical velocity could also be stored. These velocities would indicate the number of pixels to move the sprite, and would be added to the x and y coordinates on each iteration through the game loop. It should also track the current frame of animation, the number of frames in the animation, and a pointer to the bitmap or DirectDraw surface containing the sprite images. • The process of standard sprite animation is accomplished by following three distinct steps: erasing the sprite image, updating the sprite position, and drawing the sprite image. • In order to make sprite images more believable, they must contain pixels that are rendered transparently so that the background shows through. This can be accomplished through color keying, or through a more direct approach by drawing each pixel individually and skipping those pixels that are transparent. • When using the Lock method to access surface memory directly, remember to always call the Unlock method before using the surface in other method calls. • All games, at some point, must deal with the situation when one sprite touches another. For some games, this may be when a bullet strikes an alien or when an animated mouth collides with a glowing white pellet to eat it. In most cases, sprite collision can be detected using one of two techniques: bounding boxes and pixel overlap comparisons. • Bounding box collision detection works by defining a rectangular region around the sprite, its “bounding box.” To determine if a collision with another sprite has occurred, the application simply determines if their bounding box rectangles intersect • After determining the rectangular intersection area between two sprites, we can access the sprite image surfaces and compare the pixels that are within this intersection rectangle. If any non-transparent pixels overlap, we have a pixel overlap collision. This method is very accurate, but tends to be slow. • Sprite animation should be regulated to make it look more believable. We can accomplish this by incrementing the current frame number only at specific intervals. • Games with multiple layers of sprites must take into account the sprites’ z-order. Sprites should be sorted by z-order and drawn starting with the farthest sprites. This will result in closer sprites obscuring sprites that are farther from the viewer, giving an illusion of depth.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.