Diagonal Movement on 2D Tilemap

I need ideas on what I should be doing if I’m walking perfectly diagonal into a tile with my logic.

Here’s the entire snippet processing the movement:

	public void updatePlayerPosition() {
		int destX = playerX;
		int destY = playerY;

		// hardcoded for now
		final int speed = 2;

		if (Keyboard.isDown(KeyEvent.VK_W)) {
			destY -= speed;
		} else if (Keyboard.isDown(KeyEvent.VK_S)) {
			destY += speed;
		}

		if (Keyboard.isDown(KeyEvent.VK_A)) {
			destX -= speed;
		} else if (Keyboard.isDown(KeyEvent.VK_D)) {
			destX += speed;
		}

		// movement differences
		int dx = destX - playerX;
		int dy = destY - playerY;

		// we're not moving, so we're gunna stop here.
		if (dx == 0 && dy == 0) {
			return;
		}

		// Tile boundaries our player occupies
		final int minTileX = playerX / TILE_SIZE;
		final int minTileY = playerY / TILE_SIZE;

		final int maxTileX = (playerX + (TILE_SIZE - 1)) / TILE_SIZE;
		final int maxTileY = (playerY + (TILE_SIZE - 1)) / TILE_SIZE;

		// TODO: larger sized players
		// TODO: higher movement speeds?

		int srcX = dx < 0 ? maxTileX : minTileX;
		int srcY = dy < 0 ? maxTileY : minTileY;
		int dstX = srcX + (dx < 0 ? -1 : 1);
		int dstY = srcY + (dy < 0 ? -1 : 1);

		// we're moving diagonally
		if (dx != 0 && dy != 0) {
			// is that next diagonal tile solid?
			if (isSolid(dstX, dstY)) {
				// TODO: what can we do to prevent glitching on corners?
			}
		}

		// we're moving horizontally
		if (dx != 0) {
			boolean blocked = false;

			// check every tile vertically in that direction.
			for (int y = minTileY; y < maxTileY + 1; y++) {
				if (isSolid(dstX, y)) {
					blocked = true;
				}
			}

			if (blocked) {
				// we were blocked from going right, set ourselfs to the left of
				// the blocker.
				if (dx > 0) {
					playerX = (dstX - 1) * TILE_SIZE;
				}
				// vice versa
				else {
					playerX = (dstX + 1) * TILE_SIZE;
				}
			} else {
				playerX = destX;
			}
		}

		// we're moving vertically
		if (dy != 0) {
			boolean blocked = false;

			// check every tile horizontally in that direction
			for (int x = minTileX; x < maxTileX + 1; x++) {
				if (isSolid(x, dstY)) {
					blocked = true;
					break;
				}
			}

			if (blocked) {
				if (dy > 0) {
					playerY = (dstY - 1) * TILE_SIZE;
				} else {
					playerY = (dstY + 1) * TILE_SIZE;
				}
			} else {
				playerY = destY;
			}
		}
	}

This works perfectly both horizontally and vertically, but when you go diagonally then you phase into that tile. Here’s an example of that happening:

http://puu.sh/cRPHF/030413161a.png

This isn’t an incredible problem, as you can’t actually continue moving through the tile you’ve phased into. But it’s not beautiful to see the flickering of your character as it phases in and out diagonally each tick.

This is the position you would be in right before this happens.

http://puu.sh/cRPLK/c1799d301c.png

The orange rectangle is outlining your current tile boundaries. My collision detection only checks if you’re moving either vertically or horizontally, and doesn’t handle when you’re going both. This means that when I’m going diagonal towards the top right, it checks the tile to my right, and the tile above me. Those are both translucent, so the code runs true and sets my position into that diagonal tile.

Are there any solutions you guys could think of? I thought of just making it so I don’t move horizontally when I collide diagonally, which fixes it. But then that breaks movement for running against a wall while trying to move towards it parallel.