This is very simple, but I have spent nearly 6 months messing with this and it is always messed up.
This is a tile platformer, everything is "normal", x and y correspond to tile. Tiles are 32 x 32 pixels.
// Get all tile numbers, top left, top right, bottom left, bottom right
int X = 63; // and in this, say the player is moving right
int Y = 50;
int Width = 33; // This is the problem, if it is < 32, the collisions are all over the place, in the open, where there shouldn't be.
int Height = 50;
int tile_x = floor( (double) X/32);
int tile_y = floor( (double) Y/32);
int tile_x2 = floor( (double) (X + Width)/ 32);
int tile_y2 = floor( (double) (Y + Height)/ 32);
bool open = true;
// This is meant to catch cases where width < 32
if (tile_x == tile_x2) {
if (IsOpenPt(tile_x, tile_y)) { return true; }
}
// loop through tiles
for (int y = tile_y; y < tile_y2; y++) {
for (int x = tile_x; x < tile_x2; x++) {
// this spot is not open
if (!IsOpenPt(x,y)) { open = false; }
}
}
return open;
This works if the width is greater than 32, but if it anything less, everything becomes random open point/ not.
See anything wrong with my formula? I have exhausted searches, nothing. Once again I emphasize, the entire project, I have been trying to get this part to work. I have literally a 100 page notebook of attempts at visualizing and grids to put it on paper.
tile_x == tile_x2
check break anyways if height > 32? But yes, please add more information as mentioned above. \$\endgroup\$