Can't Smoothly Resolve Rectangle Collision

I’m trying to move a sprite around a tiled map where stone and water are solid. When my player sprite collides with a tile he stops moving and won’t move as long as he is colliding. How do I fix this code to allow horizontal movement during vertical collisions and allow vertical movement during horizontal collisions? Also, thanks for helping out in advance :slight_smile:


        // move player based on directions
	// methods for controlling movement
	public void move(){
		playerRect.x += xDir;
		playerRect.y += yDir;
	}
	
	public void checkCollision(){
		for(int i = 0; i < world.mapSize; i ++){
			if(playerRect.intersects(world.blocks[i]) && world.isSolid[i] ){
				intersection = (Rectangle) playerRect.createIntersection(world.blocks[i]);
				resolveCollision();
			}
		}
	}
	
	public void shoot(){
		// figure this out later
	}
	
	private void resolveCollision(){
		if(xDir > 0){
			playerRect.x -= xDir;
		}
		if(xDir < 0){
			playerRect.x -= xDir;
		}
		if(yDir > 0){
			playerRect.y -= yDir;
		}
		if(yDir < 0){
			playerRect.y -= yDir;
		}
	}

Handle horizontal and vertical collisions separately.
Do not use the whole sprite rectangle but certain checkpoints at the border of the sprite. For example, when moving to the right, only the checkpoints at the right sprite border collide with a stone on the right, whereas the top checkpoints stay free und signalize: vertical is free.
That works for me.

Have a look at this topic

Haha, this is so funny. I just came from that topic to read the response here. It’s already in my bookmarks and is a HUGE help. Thanks for the link.