站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Programming Wireless Devices with the Java2 Platform

Programming Wireless Devices with the Java2 Platform

[ directory ] Previous Section Next Section

12.6 Collision Detection

Collision detection is a key feature of the Game API, enabling the developers to quickly and easily determine whether two elements of a game have collided or not.

The collision detection methods are provided in the Sprite class, and they allow the developer to detect a collision between a Sprite and either a TiledLayer, an Image, or another Sprite.

12.6.1 Collision Rectangle

Sprites have a defined collision rectangle that indicates the region to be included for collision detection. By default, this region corresponds to the overall bounds of the Sprite. However, it can be redefined to cover any rectangular region as needed.

A special collision rectangle allows areas of a Sprite to be excluded from the collision detection process. For example, a jet fighter Sprite may include a vapor trail coming from the engines; however, a missile colliding with this part of the Sprite should not be considered as hitting the aircraft. By defining a collision rectangle that includes only aircraft and excludes the vapor trail (see Figure 12.13), selective collision detection can be easily achieved.

Figure 12.13. Setting a collision rectangle

graphics/12fig13.gif

The collision rectangle is automatically updated to reflect any transforms that are applied to the Sprite. Therefore, there is no need to modify the collision rectangle when a transform operation is performed.

12.6.2 Pixel-Level Detection

The collision detection methods allow the developer to specify boundary-level or pixel-level detection. Boundary-level detection checks if the collision rectangles of the two elements are intersecting or not, and it is the simplest and fastest detection scheme. However, many elements are likely to be non rectangular in nature, so boundary-level detection may not be sufficient.

To overcome this problem, the developer may request pixel-level detection. Used in conjunction with transparent image data, this detection scheme checks whether an opaque pixel in the first element is touching an opaque pixel in the second element. Thus, the transparent area around the opaque area is excluded from the detection process, and a collision is detected only when the opaque areas appear to be touching. Any transforms applied to a Sprite are automatically accounted for when checking for collisions.

Pixel-level detection is performed only within the collision rectangles of the two elements. Thus, touching opaque pixels will not result in a collision if either one is outside of the element's collision rectangle.

For platforms that support alpha channel blending, any alpha value less than fully opaque is considered to be transparent for the purposes of collision detection.

Figure 12.14 and Figure 12.15 illustrate the differences between pixel-level and boundary-level collision detection. The gray area around Duke represents transparent pixels. Boundary-level detection indicates a collision whenever the boundaries of the two images are touching; pixel-level detection indicates a collision only when opaque pixels are touching.

Figure 12.14. Boundary-level collision detection

graphics/12fig14.gif

Figure 12.15. Pixel-level collision detection

graphics/12fig15.gif

    [ directory ] Previous Section Next Section