Sprite interaction and introspection
The brick engine provides a number of useful mechanisms for sprites to examine the world around them. Sprites also be tested for collision with other sprites, or with the map. Sprites can also test to see if other sprites are nearby or visible by a line-of-sight test, and can also examine the map tiles underneath or directly adjacent to them.
Collision detection
There are two collision modes available for sprites: bounding box and pixel-accurate mask. A sprite can have both a bounding box and a pixel-accurate mask set, and its collision mode can be changed at any time.
The bounding box mode
A sprite bounding box is defined as a rectangle in or around the sprite, where anything within the rectangle will be treated as a collidable region. Bounding box collision detection will run very quickly, but may not be the most accurate form of collision detection if the sprite is intricate or detailed.
The pixel-accurate mask
A pixel-accurate mask is a pixel mask the size of the sprite, with each individual pixel either on or off. Pixel-accurate collision detection runs more slowly, but if the sprite has a lot of detail or an odd shape, this method will allow much greater accuracy in collision detection.
The meaning of a sprite's velocity attribute
The velocity attribute of a sprite provides a way to find out what, if anything, that sprite would hit if it moved according to the velocity, relative to its current position.
Collisions with the map
When collision is tested between a sprite and a map, the sprite is first checked to see if it's already hitting the map. If so, the test returns immediately with that result and no further sprite motion is checked. If not, then the sprite is stepped through its motion, according to the velocity attribute, and at each step collision with the map is tested.
If the sprite hits the map after a certain amount of motion, then it's possible to test to see if the sprite can continue to move in one direction or the other. The amount of extra movement that the sprite can make is determined by the slip factor passed into the collision detection routine. If the slip factor is zero, the sprite motion halts as soon as any collision with the map is detected. If the slip factor is non-zero, the sprite will continue to move up to that many pixels in the non-colliding direction. The one exception to this is if the sprite has moved into a corner, in which case no further sprite motion will be tested.
Collisions with other sprites
Collision among sprites is simpler than collision detection between a sprite and a map. The sprite motion is checked along the entire path set by its velocity, and if any collisions between that sprite and another are detected, the other sprite is added to the list and the direction in which the sprite collided is recorded.
Examining the map
Finding adjacent tiles
If a sprite has collision enabled, either by bounding box or by pixel-accurate mask, the programmer can query at any time, “which tiles are adjacent to this sprite, in any of the eight directions?” This ability comes in handy, for example, when making sprites that walk back and forth across a platform: at each step in the sprite's motion across the platform, the programmer only need check if there is a tile missing at the end of the list of adjacent tiles for that sprite. This ability also comes in handy if a sprite must avoid certain features of the map, such as a tile representing water or sand.
Finding tiles covered by a sprite
Similarly, if a sprite has collision enabled, either by bounding box or by pixel-accurate mask, the programmer can query at any time, “which tiles are covered by this sprite?” This is useful, for example, in altering sprite behavior based on the sorts of tiles it's resting on.
Line-of-sight testing
The programmer can perform a line-of-sight test between two sprites, across a given map, with a single command.
Adjacent sprites
There are two ways to find any sprites near a given sprite:
- find sprites within a given bounding rectangle
- find sprites within a given distance from a point