I’ve been confused as to why my player moves faster than the map scrolls. This is my algorithm.
public void drawMap(Graphics2D g2d) {
int minOffsetX = 0, minOffsetY = 0;
int maxOffsetX = (mapX - Frame.CANVAS_WIDTH);
int maxOffsetY = (mapY - Frame.CANVAS_HEIGHT);
int cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));
int cameraY = (int) (Player.getY() - (Frame.CANVAS_HEIGHT / 2));
if(cameraX < minOffsetX)
cameraX = minOffsetX;
if(cameraY < minOffsetY)
cameraY = minOffsetY;
if(cameraX > maxOffsetX)
cameraX = maxOffsetX;
if(cameraY > maxOffsetY)
cameraY = maxOffsetY;
int tileX = cameraX, tileY = cameraY;
while(tileX % TILE_SIZE != 0) {
tileX--;
}
while(tileY % TILE_SIZE != 0) {
tileY--;
}
for (int y = tileY; y < cameraY + Frame.CANVAS_HEIGHT; y += TILE_SIZE) {
for (int x = tileX; x < cameraX + Frame.CANVAS_WIDTH; x += TILE_SIZE) {
g2d.drawImage(map[y / TILE_SIZE][x / TILE_SIZE], x - cameraX, y - cameraY, null);
}
}
}
It’s pretty much designed to only paint the area of the map being viewed, however for some reason, when I move my player to the right, the player will move at around 1.2x the speed that the map scrolls, and when I move my player to the left, it’ll go back towards the center of the screen.
Can anyone spot the issue? Thanks.