Coordinate system of Digger as its major fault
From my previous post you know already that I have thought of characters forming the maze âgridâ, and sprites that can move freely in it. C64 can display 8 sprites at a time (if you need more you need to implement something called âsprite multiplexingâ yourself). There are 16 registers (0xD000 - 0xD00F) to store X and Y coords of each sprite, and 1 extra register 0xD010 whose bit i is 1 if sprite iâs X coord > 255. So to move sprites around and at the same time to know in which maze tile they are I invented the following coordinate system:
Each object has X and Y coordinates that correspond to X and Y indices of a tile where itâs located, and also X_OFFSET and Y_OFFSET that store offset from a tile top left corner, in pixels, measured between -15 and 15.
When Digger moves right, its X_OFFSET increases. When it becomes 16, its X increments and X_OFFSET becomes 0. Correspondingly when Digger moves left, its X_OFFSET decreases. When it becomes -16, its X decrements and X_OFFSET becomes 0.
This turned out to become a fundamental design mistake. Problem is, such coordinate system allows storing the same âphysicalâ position of sprite (in screen pixels) in multiple possible ways. Assume on the picture above that X_OFFSET = 9, Y_OFFSET = 2. Then all following positions are valid and represent the same position of Digger on screen:
X=1, Y=1, X_OFFSET=9, Y_OFFSET=2
X=2, Y=1, X_OFFSET=-7, Y_OFFSET=2
X=1, Y=2, X_OFFSET=9, Y_OFFSET=-14
So when I need to check for collisions (e.g. whether a falling gold bag has hit the Digger) I have to check all possible combinations of logical coordinates instead of simply checking the physical pixel coordinates!
Instead I could for example just store Diggerâs X and Y, and use smth like X>>4 to determine a horizontal tile index. I have even tried to implement this but realized itâs too late - the project has been close enough to release. But surprisingly enough, the changes I started to make didnât look any simpler or faster - just the accuracy of collision detection would have benefited from this change, so not a big deal after all :)