Intel Animation for Java* Package TutorialLesson 8: Detecting Sprite CollisionsThis applet demonstrates how to detect when Sprites collide. By clicking the direction buttons for Elvis and Weeble you can initiate a collision. The task of detecting Sprite collisions is accomplished by using the following method provided by the Rectangle class: public boolean intersection(Rectangle r) Each sprite in the applet above is associated with a rectangle, which can then be used with the intersection method. Boolean collision = WeebleRect.intersection(ElvisRect); Further precision with Sprite collisions can be obtained by trimming the sides of the collision rectangles. The output view of each Sprite is the initial size of the collision rectangles.
ELVIS_TRIM and WEEBLE_TRIM, in the code sample below, represent the width is pixels to shave off each side of the collision rectangles.
Finally, the "intersects" method is used to determine if a collision occurred.
This page was last updated on Feb 11th, 1997.
|