How Commodore 128 Sprite Collision Detection Works
This article explains the technical mechanism behind sprite-to-sprite collision detection on the Commodore 128 computer. It focuses on the role of the VIC-IIe video chip, the specific memory registers used to flag collisions, and how programmers can read and reset these flags during software execution. Readers will gain an understanding of the hardware limitations and the necessary steps to implement responsive collision logic in games or graphics applications.
The Role of the VIC-IIe Chip
To understand collision detection on the Commodore 128, one must first identify the active video chip. The Commodore 128 features two main video outputs: the 40-column mode and the 80-column mode. Sprite functionality, including hardware collision detection, is handled exclusively by the VIC-IIe chip, which is active during 40-column native mode and C64 compatibility mode. The VDC chip used for 80-column native mode does not support hardware sprites in the same manner. Therefore, all sprite-to-sprite collision discussions regarding the C128 pertain to the VIC-IIe architecture shared with the Commodore 64.
Collision Registers and Memory Addresses
The VIC-IIe chip manages collision detection through specific input/output registers mapped into the computer’s memory. For sprite-to-sprite collisions, the critical register is located at decimal address 53278, or hexadecimal $D01E. Each bit in this register corresponds to one of the eight available hardware sprites. When two sprites overlap on the screen, the hardware automatically sets the bit corresponding to the sprite with the higher number to 1. For example, if sprite 2 collides with sprite 5, bit 5 will be set in the collision register.
Reading and Clearing Collision Flags
Programming collision detection involves polling the collision register during the main game loop. When the program reads the value at address 53278, it can determine which sprites have interacted by checking which bits are high. However, the hardware latch behavior means that once a collision bit is set, it remains set until the program explicitly clears it. To reset the collision flags, the programmer must write a value of 0 to the register at address 53278. Failing to clear this register will result in the system registering a continuous collision state even after the sprites have moved apart.
Sprite-to-Background Collision
In addition to sprite-to-sprite interactions, the VIC-IIe also handles sprite-to-background collision detection. This data is stored in the adjacent register at decimal address 53279 ($D01F). The mechanics operate similarly to sprite-to-sprite detection, where bits are set when a sprite pixel overlaps with a non-background color pixel in the character matrix. Developers often use both registers to distinguish between an enemy hitting a player versus a player hitting a wall, allowing for more complex game logic without requiring intensive CPU-based pixel checking.
Limitations and Considerations
While hardware collision detection is efficient, it has limitations. The registers only indicate that a collision occurred between the last clear operation and the current read; they do not provide coordinate data or the exact point of impact. Furthermore, because the C128 can switch between video chips, developers must ensure the VIC-IIe is active before relying on these registers. Understanding these hardware constraints is essential for optimizing performance and ensuring accurate gameplay mechanics on the Commodore 128 platform.