Collision Detecting |
Top Previous Next |
Collision detecting is a process in which two sprites, or virtual sprites, are compared to see if any of the pixels in either sprite are overlapping. The detection is done by using screen coordinates and can be either a pixel perfect test or a boundary test. Pixel perfect mode compares the sprites using the transparency mask set with SpriteMaskColor and will do a boundary check first. The transparency color is ignored while performing the test allowing true collision testing based on the contents of the sprites image. Think of a sprite of a creature with tentacles, with pixel perfect testing a sprite of a rock moving towards the creature would only show as collided when it actually hit one of the tentacles. A boundary test on the other hand only compares the boundary rectangles of the sprites. The boundary rectangle is the size of one frame of sprite imagery. Boundary tests are appropriate when the accuracy of collision test is not as important as the speed. A pixel perfect test, while being very fast with the Pro 2D command set, still takes more time to compare pixels then it would just comparing rectangles. The Pro 2D command set has two built in functions for collision detection. SpriteCollided and SpriteCollidedEx. SpriteCollided The sprites positions on the screen are determined by the internal coordinates set by the MoveSprite command. The current image frame is determined by the current frame set by the SpriteFrame command. The sprites must refer to two different sprites. SpriteCollided allows either a pixel perfect test or a boundary test. IF SpriteCollided(goodguy, badguy, TRUE)
SpriteCollidedEx SpriteCollidedEx is a faster version of SpriteCollided and always does a boundary check first and then a pixel perfect test. It requires directly specifying the sprites positions and frames to test. It will also work with a single sprite pointer which can be used for virtual sprite hit testing. By only loading one sprite and drawing it many times on the screen in different positions, you can create virtual sprites. This can be useful when one image contains all of the sprites used in a game or program. IF SpriteCollidedEx(sprite, BALLS[a].x,BALLS[a].y,0,sprite,BALLS[b].x,BALLS[b].y,0) |