Howdy folks,
So my game progresses at an astoundingly slow rate because I have no real idea on mathematics or processes to do things, so I’ve turned my attention to making it look pretty instead.
I’ve come across a little problem though…
I have this weird grid showing over my map. To start with I thought I’d written a little routine to show a grid but when I couldn’t find one I thought maybe I have a rounding error.
The problem is I have no idea how to sort it out!
This is my level drawing code:
public void draw(Graphics2D gfx, double scale, double scrollX, double scrollY) {
// draw the game tiles
int x;
int y;
for (y = (int)scrollY; y <= (int)scrollY+game.tilesOnScreen(); y++) {
for (x = (int)scrollX; x <= (int)scrollX+game.tilesOnScreen(); x++) {
if (x >= width || y >= height) {
continue;
}
getTileAtLocation(x, y).draw(gfx, (x-scrollX)*tileSize/scale, (y-scrollY)*tileSize/scale, tileSize/scale, tileSize/scale);
}
}
}
scale is 0.48 in the image above,
scrollX and scrollY are both 0.0 in the image above,
tileSize is integer 16.
getTileAtLocation returns a tile object and it’s draw method is this:
public void draw(Graphics2D gfx, double x, double y, double width, double height) {
gfx.drawImage(sprite, (int)Math.round(x), (int)Math.round(y), (int)Math.round(width), (int)Math.round(height), null);
}
I was originally just casting the values to (int) but just in case the error was because they were all rounding down, I changed to Math.round(). This hasn’t made any difference though
Running through it with a calculator, I get the following:
tileSize / scale = 33.333333333
Tile 0,0 - draw at 0, 0
Tile 0,1 - draw at 0, 33.333333333
Tile 0,2 - draw at 0, 66.666666667
Tile 0,3 - draw at 0, 100
I can’t get my head round why tile 3 isn’t at 99.999999 (thus rounding down to a flat 99).
Any help here would be greatly appreciated. I really hope I’m just missing a very simple idea!
P.S. I have tried it at different scales but I always get these lines appearing at different places (evidently because of these darn rounding errors).