Player moving faster than game scrolls

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.

How do you calculate where to render the player?

Are you using delta time to move the player? I think that might be the issue.

Delta time just smooths the animation between frames, it shouldn’t affect scrolling like that.
@OP Your player rendering code is needed to even begin to see how to fix this.

This is my algorithm updating the player position. It’s pretty simple. So yeah, there isn’t a problem with my player movement.

public void move(float deltaTime) {
	if(left && !right)
		xPos -= 100*(deltaTime/1000);
	if(right && !left)
		xPos += 100*(deltaTime/1000);
}

In the game loop (if you must see this):

long currentTime = System.nanoTime();
deltaTime = (currentTime - previousTick) / 1000000F;
previousTick = currentTime;

I repeat my question, how do you calculate where to render the player?

Oh stupid me I misread the question. Here is my code for rendering the player.


			p.animate(System.nanoTime());
			player = p.getSprite();

			AffineTransform t = new AffineTransform();
			t.translate(Player.getX(), Player.getY());
			g2d.drawImage(player, t, null);

I might be missing something, but shouldn’t you compensate for the camera position when you render the player too?

E.g,


t.translate(Player.getX() - cameraX, Player.getY() - cameraY);

Thanks it worked after a little playing around, but could you explain why I had to do that? I thought that drawing the camera like this:


		cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));

would draw the camera like so

Why would the player have to take into account the cameras position if the camera already does so for the player?

I’ll try to explain…

Basically, what you want to do is to render the whole game relative to the screen’s coordinates, and those coordinates never change. The upper left corner is always [0, 0]. That means that if you want to render something from the camera’s perspective, you will have to recalculate the position from its world coordinates to the screen’s coordinate system.

This code,

cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));

doesn’t move any coordinates. All it does is to calculate an integer, which tells you where the camera is located in the world. Lets say it’s calculated to be “100”. Since you want this to be the upper left corner on screen, everything has to be moved relative to the camera.

For example, if your player is located at “250”, it should be rendered at screen coordinate “150”.

Another way to think about it is, what if the player is standing still (for example, at world coordinates [500, 500]), and instead of moving the player you are moving the camera? How would you render the tiles and the player?

Hope this helps.

I see. Thanks for the very clear and easy to understand definition. :slight_smile: