I've been able to demonstrate that the sprite offset for the cars both ends up larger than the number of sprites and below zero, but I haven't been able to find the cause.
It might be the result of some completely unrelated memory corruption.
The only thing that seems to be a trigger is when the game is struggling, where all 4 players are alive and some frames take longer than 1 frame time to compute, but other than that I can't seem to figure it out.
Edit: I've simplified my code for setting the car sprite offset from the car angle and if I start a race with 2 players and 2 bots and immediately let one player die it's a lot more stable, with only rare bugs. I still end up with invalid sprite offset sometimes which I do not understand.
One thing I've been considering is this C macro:
Code: Select all
#define SPRITE_BLOCKHI(addr) (addr >> 13)
This is for setting the Hi address value of a sprite register and I'm not sure what effect the statement will have when "addr" is a #define larger than 16bits with a 16bit int offset added to it?
Edit: The thing that concerns me is whether bit 16 is lost in the process and if that would have adverse effects?
Edit2: I've tried hardcoding the values for Hi and Lo and then just adding the shiftet sprite offset and it doesn't seem to make a difference.
Regarding the problem with running the game with all 4 players alive, the culprit seems to be my car collision code. The 6502 simply can't handle this code when all 4 players are in a group close together.